1.numpy import and view version
>>> import numpy as np
>>> print(np.__version__)
1.15.4
2. Create a one-dimensional (or multi-dimensional) arrays
# Created by the initialization list
>>> np.array([1,2,3,4,5])
array([1, 2, 3, 4, 5])
Produced by method # arange
>>> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# Create the specified range and step size
>>> np.arange(3,10,2)
array([3, 5, 7, 9])
#-D (3,3)
>>> np.array([[1,2,3],[4,5,6],[7,8,9]])
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Three-dimensional (2,2,2)
>>> np.array([[[1,3],[2,4]],[[3,6],[4,8]]])
array([[[1, 3],
[2, 4]],
[[3, 6],
[4, 8]]])
3. Create a boolean array
# One-dimensional
>>> np.full(3, True, dtype=bool)
array([ True, True, True])
#-D
>>> np.full((3, 3), True, dtype=bool)
array([[ True, True, True],
[ True, True, True],
[ True, True, True]])
4. Extract (or replacement) from the array element specified conditions are met
# Extract all the odd array
>>> arr = np.arange(10)
>>> arr[arr % 2 == 1]
array([1, 3, 5, 7, 9])
Greater than the number of extraction # 4
>>> arr[arr > 4]
array([5, 6, 7, 8, 9])
Replace all odd # -1
>>> arr[arr % 2 == 1] = -1
>>> arr
array([ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1])
The change in shape of the array
>>> arr = np.arange(12)
>>> arr.reshape(2,6)
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]])
# Set to -1 dimension will automatically match
>>> arr.reshape(3,-1)
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> arr.reshape(2,2,3)
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]]])
6. Type Conversion
>>> a = np.arange(10)
>>> a.dtype
dtype('int32')
# Converted to the type str
>>> a.astype(str)
array(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], dtype='
# Converted to float
>>> a.astype(float)
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
7. Vertical merge array
>>> a = np.arange(8).reshape(2,4)
>>> b = np.arange(8,12)
# method 1
>>> np.vstack((a, b))
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
Method # 2
>>> np.row_stack((a,b))
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
The method of # 3, note that the number of two-dimensional arrays to concatenate same vertical merged
>>> np.concatenate([a, b], axis=0)
Traceback (most recent call last):
File "", line 1, in
np.concatenate([a, b], axis=0)
ValueError: all the input arrays must have same number of dimensions
>>> np.concatenate([a, [b,b]], axis=0)
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[ 8, 9, 10, 11]])
Method # 4, merged two dimensions of the array to be consistent
>>> np.r_[a,[b,b]]
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[ 8, 9, 10, 11]])
8. merge array level
>>> a = np.arange(8).reshape(2,4)
>>> b = np.arange(8,12).reshape(2,2)
# method 1
>>> np.hstack((a,b))
array([[ 0, 1, 2, 3, 8, 9],
[ 4, 5, 6, 7, 10, 11]])
Method # 2
>>> np.column_stack((a,b))
array([[ 0, 1, 2, 3, 8, 9],
[ 4, 5, 6, 7, 10, 11]])
Method # 3
>>> np.concatenate([a, b], axis=1)
array([[ 0, 1, 2, 3, 8, 9],
[ 4, 5, 6, 7, 10, 11]])
Method # 4
>>> np.c_[a,b]
array([[ 0, 1, 2, 3, 8, 9],
[ 4, 5, 6, 7, 10, 11]])
9. Save and read TXT (or csv)
>>> filename = 'data.txt' # or filename = 'data.csv'
>>> a = np.arange(12).reshape(3,4)
# Save: fmt specifies the type of data stored, delimiter specified delimiter
>>> np.savetxt(filename, a, fmt='%d', delimiter=',')
# Read: the type specified dtype read, delimiter specified delimiter
>>> np.loadtxt(filename, dtype=float, delimiter=',')
array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])
10. The array of special operations
>>> a = np.arange(10)
>>> a.sum () # summation
45
>>> a.max () # maximum demand
9
>>> a.min () # for the minimum
0
>>> a.mean () # averaged
4.5
>>> a.ptp () # differencing elements in the array the maximum and minimum
9
The median >>> np.median (a) # seek array
4.5
>>> np.std (a) # find standard array of differential
2.8722813232690143
>>> np.var (a) # seek variance array
8.25
Each square of >>> a ** 2 # a in
array([ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81], dtype=int32)
>>> a.dot (a) # dot product, summing the elements corresponding to multiplied, returns a scalar
285
>>> a.dot (aT) # aT is a transposition can also be used A.transpose ()
285
11. Create a zero matrix, a matrix, a unit matrix
>>> np.zeros ((2, 3)) # zero matrix
array([[0., 0., 0.],
[0., 0., 0.]])
>>> np.ones ((4, 3)) # 1 matrix
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
>>> np.identity (3) # unit matrix, can also be used np.eye (3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
12. The operation of the special matrix
(1) array inner product operation np.dot ()
# One-dimensional inner product, summing the elements of the corresponding multiplied
>>> A=np.array([1, 2, 3])
>>> B=np.array([4, 5, 6])
>>> A.dot(B) # or A.dot(B.T)
32 Wuxi ××× hospital https://yyk.familydoctor.com.cn/20612/
# Two-dimensional product, the matrix A [m, n] is the number of columns is equal to the number of rows B [n, p] of the matrix, with the same linear algebra matrix multiplication (C [i, j] = sum (A [i, k ] * B [k, i]), k in [i, n])
# Assuming C = A · B, the C [1,1] = A [1,1] * B [1,1] + A [1,2] * B [2,1] + A [1,3] * B [3,1] = 1 * 2 + 2 * 2 + 3 * 2 = 12
>>> A=np.array([[1, 2, 3], [4, 5, 6]]) # (2, 3)
>>> B=np.array([[2, 3], [2, 3], [2, 3]]) # (3, 2)
>>> A.dot(B) # or np.dot(A, B)
array([[12, 18],
[30, 45]])
(2) array element multiplication np.multiply ()
# Multiplication element is the multiplication means corresponding to the matrix elements, can be used np.multiply (), can also write directly "*" operator
The dimension of the elements required # multiplication of two matrices must be consistent
# One-dimensional array
>>> A=np.array([1, 2, 3])
>>> B=np.array([4, 5, 6])
>>> A * B
array([ 4, 10, 18])
>>> np.multiply(A, B)
array([ 4, 10, 18])
# Two-dimensional array
>>> A = np.arange(8)
>>> A = A.reshape(2,4)
>>> A
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
>>> A * A
array([[ 0, 1, 4, 9],
[16, 25, 36, 49]])
>>> np.multiply(A, A)
array([[ 0, 1, 4, 9],
[16, 25, 36, 49]])
(3) matrix multiplication
# Matrix multiplication may be used np.matmul (), can also use the "*" operator
Product of the same two-dimensional array of multiplication and # matrix, it can also be used np.dot ()
>>> MA = np.matrix([[1, 2, 3], [4, 5, 6]])
>>> MB = np.matrix([[2, 3], [2, 3], [2, 3]])
>>> MA
matrix([[1, 2, 3],
[4, 5, 6]])
>>> MB
matrix([[2, 3],
[2, 3],
[2, 3]])
>>> MA * MB
matrix([[12, 18],
[30, 45]])
>>> np.matmul (MA, MB) # np.dot (MA, MB), MA.dot (MB)
matrix([[12, 18],
[30, 45]])
(4) calculating Cartesian product
# Cartesian product, also known as direct product, the mapping relationship is actually a collection, you can use itertools.product () to achieve
Example # A = {a, b}, B = {1,2,3}, the Cartesian product of A and B is {(a, 1), (a, 2), (a, 3), (b , 1), (b, 2), (b, 3)}
>>> import itertools
>>> A = np.array(['a', 'b'])
>>> B = np.array([1, 2, 3])
>>> D = itertools.product(A, B)
>>> list (D) # transferred directly list, list elements each tuple type
[('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3)]
# You can also traverse through the loop D
>>> for d in D:
print(d)
('a', 1)
('a', 2)
('a', 3)
('b', 1)
('b', 2)
('b', 3)
pending upgrade:
python data analysis series (two) - Matplotlib use
python data analysis series (three) - Scipy use