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) andnp.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()
andarr.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 inputfrom numpy.random import randint
, and then just inputrandint()
;
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]
equalsarr_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 ofarr
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)
equalsarr.max()
,np.sin()
,np.log()
;
网友评论