Python–Numpy
1. 导包
import numpy as np
2. 创建一个数组Array(不同于List
)
array1 = np.array([1 ,2 ,3 ,4 ,5 ])
print(array1)
[1 2 3 4 5]
print(array1.dtype)
int32
array2 = np.array([1. ,2 ,3 ,4 ])
print(array2)
[1. 2. 3. 4.]
array3 = np.array([1 ,2 ,3 ,4 ],dtype = 'float64' )
print(array3.dtype)
float64
np.array([range(i,i+3 ) for i in [2 ,3 ,4 ]])
array([[2, 3, 4],
[3, 4, 5],
[4, 5, 6]])
3. 使用numpy创建一些特殊的数组
np.zeros(10 ,dtype='float32' )
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)
np.zeros([2 ,3 ],dtype='int32' )
array([[0, 0, 0],
[0, 0, 0]])
np.ones([3 ,3 ],dtype='float32' )
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=float32)
np.full((2 ,2 ),3.14 )
array([[3.14, 3.14],
[3.14, 3.14]])
np.arange(1 ,10 ,2 )
array([1, 3, 5, 7, 9])
np.linspace(0 ,1 ,5 )
array([0. , 0.25, 0.5 , 0.75, 1. ])
np.random.random((3 ,3 ))
array([[0.96106772, 0.53449777, 0.99360549],
[0.02162571, 0.90131666, 0.16706767],
[0.27723283, 0.88013812, 0.96710278]])
np.random.normal(0 ,1 ,(3 ,3 ))
array([[-0.00746501, 0.7445069 , -0.9031185 ],
[-0.16900041, 0.69525934, -2.18961853],
[-1.55475733, -0.33409044, 0.75753941]])
np.random.randint(1 ,10 ,(3 ,3 ))
array([[5, 8, 5],
[4, 4, 2],
[3, 9, 4]])
np.eye(4 )
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
np.empty(2 )
array([5.43230922e-312, 7.29112202e-304])
4. Array的元素属性
np.random.seed(0 )
x1 = np.random.randint(1 ,10 ,size=(2 ,3 ,5 ))
print(x1)
[[[6 1 4 4 8]
[4 6 3 5 8]
[7 9 9 2 7]]
[[8 8 9 2 6]
[9 5 4 1 4]
[6 1 3 4 9]]]
print("x1 ndim:" ,x1.ndim)
print("x1 shape" ,x1.shape)
print("x1 size" ,x1.size)
x1 ndim: 3
x1 shape (2, 3, 5)
x1 size 30
5. Array 元素取值
element = np.arange(1 ,10 ,2 )
print(element[0 ])
print(element[-1 ])
1
9
elements = np.random.randint(1 ,10 ,(3 ,3 ))
print(elements)
[[8 4 3]
[8 3 1]
[1 5 6]]
print(elements[1 ][1 ])
3
print(elements[1 ,1 ])
3
elements[1 ,1 ] = 12
print(elements)
[[ 8 4 3]
[ 8 12 1]
[ 1 5 6]]
elements[0 ,0 ] = 3.14
print(elements)
[[ 3 4 3]
[ 8 12 1]
[ 1 5 6]]
6. Array 切片 Slicing
slicing = np.arange(10 )
print(slicing[:3 ])
print(slicing[8 :])
print(slicing[6 :-1 ])
[0 1 2]
[8 9]
[6 7 8]
print(slicing[::2 ])
print(slicing[::-1 ])
[0 2 4 6 8]
[9 8 7 6 5 4 3 2 1 0]
mutl = np.random.randint(1 ,10 ,[3 ,3 ])
print(mutl)
[[8 4 7]
[8 3 1]
[4 6 5]]
print(mutl[:1 ,:1 ])
[[8]]
print(mutl[:1 ,::2 ])
[[8 7]]
print(mutl[0 ,:])
print(mutl[0 ])
[8 4 7]
[8 4 7]
list1 = range(10 )
for i in list1:
print(i,end =" " )
print("" )
tmp = list1[3 ]
tmp = 20
for i in list1:
print(i,end =" " )
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
array1 = np.arange(10 ).reshape(2 ,5 )
print(array1)
tmp = array1[:,1 ]
print(tmp)
tmp[0 ] = 20
print(array1)
[[0 1 2 3 4]
[5 6 7 8 9]]
[1 6]
[[ 0 20 2 3 4]
[ 5 6 7 8 9]]
tmp2 = array1[:,1 ].copy()
print(tmp2)
[20 6]
tmp[0 ] = 1
print(array1)
[[0 1 2 3 4]
[5 6 7 8 9]]
7. Array数组的拼接
x = np.array([1 ,2 ,3 ])
y = np.array([3 ,2 ,1 ])
np.concatenate([x,y])
array([1, 2, 3, 3, 2, 1])
z = np.array([99 ,99 ,99 ])
np.concatenate([x,y,z])
array([ 1, 2, 3, 3, 2, 1, 99, 99, 99])
grid = np.arange(1 ,7 ).reshape(2 ,3 )
print(grid)
[[1 2 3]
[4 5 6]]
np.concatenate([grid,grid])
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3],
[4, 5, 6]])
np.concatenate([grid,grid],axis = 1 )
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]])
np.vstack([grid,grid])
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3],
[4, 5, 6]])
np.hstack([grid,grid])
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]])
轴这个概念是为不止一个维度的数组准备的; axis = 0,第一个维度,垂直向下贯穿所有行数据,数据聚合时,注意贯穿,合并数组时,注意垂直向下; axis = 1,第二个维度,水平贯穿所有列数据,注意的点如上;
x = np.arange(10 )
print(x)
[0 1 2 3 4 5 6 7 8 9]
x1,x2,x3 = np.split(x,[1 ,3 ])
print(x1,x2,x3)
[0] [1 2] [3 4 5 6 7 8 9]
y = np.arange(1 ,13 ).reshape(3 ,4 )
print(y)
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
y1,y2 = np.vsplit(y,[1 ])
print(y1,"\n" ,y2)
[[1 2 3 4]]
[[ 5 6 7 8]
[ 9 10 11 12]]
y3,y4 = np.hsplit(y,[1 ])
print(y3,"\n" ,y4)
[[1]
[5]
[9]]
[[ 2 3 4]
[ 6 7 8]
[10 11 12]]
print(y)
print(y.sum(axis=0 ))
print(y.sum(axis=1 ))
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[15 18 21 24]
[10 26 42]
8. Array数学函数
print(y.min(),y.max())
1 12 3.452052529534663
print(y.var(),y.std())
11.916666666666666 3.452052529534663
9. Rules of Broadcasting
If the two arrays differ in their number of dimensions,the shape of the one with fewer dimensions is padded with ones on its leading (left)side; If the shape of the two arrays does not match in any dimension,the array with shape equal to 1 in that dimension is stretched to match the other shape; If in any dimension the sizes disagree and neither is equal to 1,an error is raised.