numpy_learn
Important features: N-dimensional array of objects, i.e., objects ndarray
ndarray object if the type of data stored in the same
import numpy as np
data = np.arange(12).reshape(3,4)
data
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
# 维度个数
data.ndim
2
# 数组维度
data.shape
(3, 4)
# 数组元素总个数
data.size
12
# 数组元素类型
data.dtype
dtype('int64')
# 数组元素字节大小
data.itemsize
8
Objects created Numpy
Array (), brackets python incoming data types, such as the list, tuple, etc.
import numpy as np
data1 = np.array([1,2,3]) # 传入列表,构建一个1维数组
data1
array([1, 2, 3])
data1.ndim
1
data2 = np.array([[1,2,3],[2,3,4]])
data2
array([[1, 2, 3],
[2, 3, 4]])
# 括号里面传入元组
np.zeros((3,4))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
np.ones((3,4))
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
# 空数组,里面的数据是随机的,默认的数据float64
np.empty((3,4)).dtype
dtype('float64')
# 返回的是一个数组,而不是列表
# 等差数组
np.arange(1,20,5)
array([ 1, 6, 11, 16])
# 可以申明元素数据类型
np.ones((3,4), int)
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]])
ndarray object data type
int,float,bool,unicode,string,complex
data_one = np.array([1,2,3])
data_one.dtype.name
'int64'
Data type conversion
asdtype ()
data_turn1 = data_one.astype('float64')
data_turn1.dtype
dtype('float64')
data_turn2 = data_one.astype(np.float32)
data_turn2.dtype
dtype('float32')
# 字符直接转数字
str_data = np.array(['1','2'])
str_data
array(['1', '2'], dtype='<U1')
str_data1 = str_data.astype(np.int)
str_data1
array([1, 2])
Array Operations
# 对应元素相加、减、乘、除
data1 = np.array([1,2,3])
data2 = np.array([2,3,4])
data1 + data2
array([3, 5, 7])
# 数组广播,数组形状不同时,做运算会自动进行扩展
data3 = np.array([[1],[2],[3],[4]])
data3
array([[1],
[2],
[3],
[4]])
data4 = np.array([1,2,3])
data4
array([1, 2, 3])
data3 + data4
array([[2, 3, 4],
[3, 4, 5],
[4, 5, 6],
[5, 6, 7]])
data3 + 10
array([[11],
[12],
[13],
[14]])
Array indexing and slicing
1-dimensional array
data1 = np.arange(10)
data1
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
data1[0]
0
data1[0:3]
array([0, 1, 2])
data1[:3]
array([0, 1, 2])
data1[:6:1]
array([0, 1, 2, 3, 4, 5])
data1[:6:2]
array([0, 2, 4])
2-dimensional array
data2 = np.array([[1,2,3],[2,3,4],[3,4,5]])
data2
array([[1, 2, 3],
[2, 3, 4],
[3, 4, 5]])
data2[0]
array([1, 2, 3])
data2[0:2] # 后不包
array([[1, 2, 3],
[2, 3, 4]])
data2[0:2,0:2]
array([[1, 2],
[2, 3]])
# 获取某一列时,行哪里要用:
data2[:,0:2]
array([[1, 2],
[2, 3],
[3, 4]])
Fancy index
# 建立空数组
data1 = np.empty((4,4))
data1
array([[ 5.92878775e-323, 0.00000000e+000, 0.00000000e+000,
0.00000000e+000],
[ 0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
0.00000000e+000],
[ 0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
0.00000000e+000],
[ 2.68156159e+154, -1.49458022e-154, 2.68156159e+154,
-4.34384816e-311]])
# 动态为数组添加元素
for i in range(4):
data1[i] = np.arange(i, i+4)
data1
array([[0., 1., 2., 3.],
[1., 2., 3., 4.],
[2., 3., 4., 5.],
[3., 4., 5., 6.]])
# 获取第0、2行数据
data1[[0,2]]
array([[0., 1., 2., 3.],
[2., 3., 4., 5.]])
data1[[0,2], [0,1]]
array([0., 3.])
Boolean Index
data1 = np.arange(12).reshape(3,4)
data1
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
data1 == 1
array([[False, True, False, False],
[False, False, False, False],
[False, False, False, False]])
data1[data1 == 2]
array([2])
Axisymmetric array and transpose
data1 = np.arange(12).reshape(3,4)
data1
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
data1.T
array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])
numpy generic function
- abs fabs absolute value
- square root sqrt
- square square
- exp exponential power
- log、log10、log2、log1p
- sign sign function, a positive, negative -1,0
- the smallest integer greater than or equal ceil
- floor largest integer less than or equal
- After rounding the smallest integer rint
- modf fractional and integer arrays separated in the form of two separate return
- isnan whether an integer
- isfinite isinf infinite and finite
- cosh cos sin tan born fishy
arcos arccosh arcsin
- add addition
- substract subtraction
- multiply multiply
- In addition to divide floor_divide and divisible
- maximum fmax maximum
- minimum fmin minimum
- mod find touch
copysign copy element symbols
data = np.array([4,9,16])
np.sqrt(data)
array([2., 3., 4.])
np.ceil(data)
array([ 4., 9., 16.])
data analysis
Switch logic array operation conditions
data1 = np.array([1,2,3])
data2 = np.array(['a','b','c'])
data3 = np.array([True, False, True])
# 当data3中为True时,从data1中取值,反之取data2
np.where(data3,data1,data2)
array(['1', 'b', '3'], dtype='<U21')
An array of statistical computing
data1 = np.arange(12)
data1
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
data1.sum()
66
data1.mean()
5.5
data1.min()
0
data1.max()
11
# 最小值索引
data1.argmin()
0
data1.argmax()
11
data1.cumsum()
array([ 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66])
# 累计求乘积
data1.cumprod()
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
Sequence
data = np.arange(12).reshape(3,4).T
data
array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])
data.sort()
data
array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])
data.sort(0)
data
array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])
Check the array elements
data = np.array([1,2,3])
data
array([1, 2, 3])
np.any(data>4)
False
np.all(data>0)
True
And a collection of other unique logic
# unique取唯一值,并对数组进行排序
data = np.array([3,3,2,1])
np.unique(data)
array([1, 2, 3])
- np.in1d (a, b) determines whether the array element in a b inside the array, return a Boolean result
- np.intersect1d (a, b) calculating a, b of the common element, and returns the sort results
- np.union1d (a, b) calculating a, b and set, and returns the results in order
- np.setdiff1d (a, b) set of difference
- np.setxor1d (a, b) set symmetric difference, that there is an array, but the array is not present at the same time two elements
Linear algebra module
numpy.linalg module has a standard set of things and the inverse matrix decomposition and the like determinant
x = np.array([[1],[2]])
y = np.array([3,4]).reshape(1,2)
x
array([[1],
[2]])
y
array([[3, 4]])
np.dot(x,y)
array([[3, 4],
[6, 8]])
- np.dot () dot product
- np.diag () returns in the form of a square diagonal dimensional array, one-dimensional array or matrix into
- np.trace () and the diagonal elements calculated
- np.linalg.det () calculated Matrices
- np.linalg.eig () square matrix calculated eigenvalues and eigenvectors
- np.linalg.inv () to calculate the inverse square
- np.linalg.qr () decomposition qr calculated
- np.linalg.svd () calculates singular values
- np.linalg.solve () linear equations Ax = b, where A is a square matrix
- np.linalg.lstsq () Ax = b in calculating the least squares solution
data = np.arange(16).reshape(4,4)
data
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
np.diag(data)
array([ 0, 5, 10, 15])
np.trace(data)
30
np.linalg.det(data)
-2.9582283945787796e-30
np.linalg.eig(data)
(array([ 3.24642492e+01, -2.46424920e+00, 1.92979794e-15, -4.09576009e-16]),
array([[-0.11417645, -0.7327781 , 0.54500164, 0.00135151],
[-0.3300046 , -0.28974835, -0.68602671, 0.40644504],
[-0.54583275, 0.15328139, -0.2629515 , -0.8169446 ],
[-0.76166089, 0.59631113, 0.40397657, 0.40914805]]))
np.linalg.inv(data)
array([[ 9.00719925e+14, -4.50359963e+14, -1.80143985e+15,
1.35107989e+15],
[-2.40191980e+15, 2.70215978e+15, 1.80143985e+15,
-2.10167983e+15],
[ 2.10167983e+15, -4.05323966e+15, 1.80143985e+15,
1.50119988e+14],
[-6.00479950e+14, 1.80143985e+15, -1.80143985e+15,
6.00479950e+14]])
np.linalg.qr(data)
(array([[ 0. , -0.83666003, 0.48308786, 0.25812035],
[-0.26726124, -0.47809144, -0.8365087 , 0.01591196],
[-0.53452248, -0.11952286, 0.22375381, -0.80618499],
[-0.80178373, 0.23904572, 0.12966702, 0.53215267]]),
array([[-1.49666295e+01, -1.65701970e+01, -1.81737645e+01,
-1.97773319e+01],
[ 0.00000000e+00, -1.19522861e+00, -2.39045722e+00,
-3.58568583e+00],
[ 0.00000000e+00, 0.00000000e+00, -1.71941189e-15,
-5.12511879e-15],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
-8.82043219e-16]]))
random module
# 生成数组,每次运行出来的数组都不一样
np.random.rand(3,3)
array([[0.93921666, 0.42365706, 0.84692929],
[0.08043763, 0.19183508, 0.59509369],
[0.10962175, 0.58845541, 0.47584158]])
- np.random.seed () generates a random number seed, if the parameter, i.e., the same value is generated every time, there is no system time is selected, each generated value is not the same
- np.random.rand () sample values uniformly distributed
- np.random.randint () from a given integer randomly selected upper and lower limit
- np.random.normal () Shota distribution
- np.random.uniform () sample values [0,1] is uniformly distributed
np.random.uniform(1,5,20)
array([1.73527813, 4.37708175, 1.23993594, 3.11160917, 4.81709012,
1.37763267, 4.08172748, 3.16229412, 4.83786832, 1.696155 ,
2.17848768, 3.38546968, 3.34576086, 3.303562 , 1.16051986,
3.37224742, 4.87527035, 2.48957213, 3.27498108, 1.46253374])
np.random.uniform(20)
6.760672905250747
np.random.randint(0,2,10)
array([0, 1, 1, 1, 1, 0, 1, 1, 0, 1])
np.random.normal(1,5,10)
array([-3.03222528, 1.00433415, -6.68152162, -7.8533515 , -3.10631387,
4.76162682, 2.9385615 , -6.95958349, -5.98428693, 1.62605193])
Case: alcoholic stroll
Origin initiation, at every step, direction uncertain, after a time t, calculate the distance and position of an alcoholic home position.
Suppose 2000 steps away, 0.5 m each step, to move forward step by +1, -1 backward step 1, calculate the distance from the origin, that is, all of the accumulated number of steps required for bonding
import numpy as np
steps = 2000
draws = np.random.randint(0,2,steps)
# draws=0时,derection=1
derection_steps = np.where(draws>0,1,-1)
# 计算距离
distance = derection_steps.cumsum()
distance
array([ -1, -2, -3, ..., -26, -25, -26])
# 最大前进步数
distance.max()
39
# 第几步最大
distance.argmax()
486
distance.min()
-55
distance.argmin()
1212
# 第几步时,距离原点距离=15米
dst = 15/0.5
(np.abs(distance)>=dst).argmax()
461