美文网首页
Numpy - Python for Data Science

Numpy - Python for Data Science

作者: TJH_KYC | 来源:发表于2018-09-15 16:07 被阅读0次

Python for Data Science & Machine Learning Bootcamp
notes by Lucas, Sep 15, 2018


Numpy

1. Introduction

  • The core of Numpy library is Linear Algebra. So Numpy is very fast, and many scientific libraries are based on it;
  • Importing Numpy: import numpy as np;

2. Numpy Arrays

  • The dimension of nd.array can be seen from the numbers of [...] in array(); e.g. array([...]) indicates 1 dim, array([[...]...]) indicates 2 dim, etc.;
  • Note the difference between np.arange() (NOT np.range) and np.linspace();
  • np.eye() creates an Identity Matrix;
  • np.random.rand() generates the random numbers from 0 to 1;
  • np.random.randn() generates the random numbers which obey the normal distribution (mean = 0);
  • np.random.randint() generates ONE random number in [start, end), add a third paramter if wanna generates a certain mount of random numbers;
  • arr.reshape() creates a specific shape of n dimentional array;
  • Note the difference between arr.max() and arr.argmax(), for the real value and for the position (index);
  • arr.shape checks the dim of an array;
  • arr.dtype checks the data type in an array;
  • For np.random.randint(), you can just input from numpy.random import randint, and then just input randint();

3. Numpy Array Indexing & Selection

  • Slice: arr[0:5] selects the group of elements whose index are from 0 to 4;
  • Broadcast: arr[0:3] = 520 makes the first three elements become a bunch of numbers of 520;
  • Copy: arr.copy() makes a copy of the array;
  • arr_2d[0][0] equals arr_2d[0,0], but the later notation is recommended;
  • Conditional selection: arr[arr>5];
  • Combination of functions: arr_2d = np.arrange(0:50).reshape(5,10);

4. Numpy Operations

  • arr + arr means every elements of arr has been doubled, and so were the subtraction and multiplication;
  • The operation of a numpy array with a scalar number is easy to understand;
  • Universal functions: np.sqrt(), np.exp(), np.max(arr) equals arr.max(), np.sin(), np.log();

相关文章

网友评论

      本文标题:Numpy - Python for Data Science

      本文链接:https://www.haomeiwen.com/subject/akctnftx.html