numpy基础运算
import numpy as np
array=np.array([[1,2,3],[2,3,4]])
print(array)
[[1 2 3]
[2 3 4]]
array.ndim
2
array.shape
(2, 3)
array.size
6
a = np.array([2,23,4], dtype=np.int64)
a.dtype
dtype('int64')
a=np.array([[1,2,3],[2,3,4]])
a
array([[1, 2, 3],
[2, 3, 4]])
a1 = np.zeros((3,4))
a1
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
a2 = np.ones((3,4),dtype=np.int32)
a2
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]])
a3 = np.empty((3,4),dtype=np.int32)
a3
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]])
a4=np.arange(12)
a4
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
a4=np.arange(12).reshape(3,4)
a4
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
a5 = np.linspace(1,10,6)
a5
array([ 1. , 2.8, 4.6, 6.4, 8.2, 10. ])
a5 = np.linspace(1,10,6).reshape(2,3)
a5
array([[ 1. , 2.8, 4.6],
[ 6.4, 8.2, 10. ]])
a = np.array([10,20,30,40])
b = np.arange(4)
a,b
(array([10, 20, 30, 40]), array([0, 1, 2, 3]))
c = a-b
c
array([10, 19, 28, 37])
c=b**2
c
array([0, 1, 4, 9])
c=10*np.sin(a)
c
array([-5.44021111, 9.12945251, -9.88031624, 7.4511316 ])
d1=b<3
d2=b==3
d1,d2
(array([ True, True, True, False]), array([False, False, False, True]))
a=np.array([[1,1],[2,3]])
b=np.arange(4).reshape((2,2))
a,b
(array([[1, 1],
[2, 3]]),
array([[0, 1],
[2, 3]]))
c=a*b
c
array([[0, 1],
[4, 9]])
c2 = np.dot(a,b)
print(c2)
[[ 2 4]
[ 6 11]]
c2 = a.dot(b)
c2
array([[ 2, 4],
[ 6, 11]])
a = np.random.random((2,4))
a
array([[0.43285409, 0.59119159, 0.13123502, 0.26843777],
[0.01975319, 0.89322356, 0.96109008, 0.20043348]])
np.sum(a)
3.498218778049299
np.max(a)
0.9610900820034279
np.sum(a,axis=1)
array([1.42371846, 2.07450032])
np.sum(a,axis=0)
array([0.45260728, 1.48441515, 1.0923251 , 0.46887125])
axis
- axis的取值取决于数据的维度,如果数据是一维数组那么axis只有0,如果数据是二维的,那么axis可以取0和1,如果数据是三维的,那么axis就可以取0、1和2
- axis=0表示沿着列的方向,做逐行的操作
- axis=1表示沿着行的方向,做逐列的操作
- axis=0表示行就是删除行,axis=1表示列就是删除列
A = np.arange(2,14).reshape((3,4))
A
array([[ 2, 3, 4, 5],
[ 6, 7, 8, 9],
[10, 11, 12, 13]])
np.argmin(A)
0
np.argmax(A)
11
a1=np.mean(A)
a2=A.mean()
a1
a2
7.5
a3 = np.mean(A,axis=0)
a3
array([6., 7., 8., 9.])
b = np.median(A)
b
7.5
c = np.cumsum(A)
c
array([ 2, 5, 9, 14, 20, 27, 35, 44, 54, 65, 77, 90])
d = np.diff(A)
d
array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
e = np.nonzero(A)
e
(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64),
array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))
A = np.arange(14,2,-1).reshape((3,4))
A
array([[14, 13, 12, 11],
[10, 9, 8, 7],
[ 6, 5, 4, 3]])
np.sort(A)
array([[11, 12, 13, 14],
[ 7, 8, 9, 10],
[ 3, 4, 5, 6]])
np.transpose(A)
array([[14, 10, 6],
[13, 9, 5],
[12, 8, 4],
[11, 7, 3]])
A.T
array([[14, 10, 6],
[13, 9, 5],
[12, 8, 4],
[11, 7, 3]])
(A.T).dot(A)
array([[332, 302, 272, 242],
[302, 275, 248, 221],
[272, 248, 224, 200],
[242, 221, 200, 179]])
np.clip(A,5,9)
array([[9, 9, 9, 9],
[9, 9, 8, 7],
[6, 5, 5, 5]])
numpy的索引
import numpy as np
A = np.arange(3,15)
A
array([ 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
A[3]
6
A = np.arange(3,15).reshape(3,4)
A
array([[ 3, 4, 5, 6],
[ 7, 8, 9, 10],
[11, 12, 13, 14]])
A[2]
array([11, 12, 13, 14])
A[1,1]
8
A[1][1]
8
A[2,:]
array([11, 12, 13, 14])
A[:,1]
array([ 4, 8, 12])
A[1,1:2]
array([8])
A[1,1:3]
array([8, 9])
for row in A:
print(row)
[3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]
for col in A.T:
print(col)
[ 3 7 11]
[ 4 8 12]
[ 5 9 13]
[ 6 10 14]
for item in A.reshape(1,12):
print(item)
[ 3 4 5 6 7 8 9 10 11 12 13 14]
A.flatten()
array([ 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
A = np.arange(3,15).reshape(3,4)
A
array([[ 3, 4, 5, 6],
[ 7, 8, 9, 10],
[11, 12, 13, 14]])
for item in A.flat:
print(item)
3
4
5
6
7
8
9
10
11
12
13
14
numpy array合并
A = np.array([1,1,1])
B = np.array([2,2,2])
A,B
(array([1, 1, 1]), array([2, 2, 2]))
C = np.vstack((A,B))
C
array([[1, 1, 1],
[2, 2, 2]])
C.shape
(2, 3)
D=np.hstack((A,B))
D
array([1, 1, 1, 2, 2, 2])
D.shape
(6,)
A[np.newaxis,:]
array([[1, 1, 1]])
A[np.newaxis,:].shape
(1, 3)
A[:,np.newaxis]
array([[1],
[1],
[1]])
A[:,np.newaxis].shape
(3, 1)
A = np.array([1,1,1])[:,np.newaxis]
B = np.array([2,2,2])[:,np.newaxis]
A,B
(array([[1],
[1],
[1]]),
array([[2],
[2],
[2]]))
C = np.concatenate((A,B,B,A),axis=0)
C
array([[1],
[1],
[1],
[2],
[2],
[2],
[2],
[2],
[2],
[1],
[1],
[1]])
C = np.concatenate((A,B,B,A),axis=1)
C
array([[1, 2, 2, 1],
[1, 2, 2, 1],
[1, 2, 2, 1]])
numpy分割
A = np.arange(12).reshape(3,4)
A
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
np.split(A,2,axis=1)
[array([[0, 1],
[4, 5],
[8, 9]]),
array([[ 2, 3],
[ 6, 7],
[10, 11]])]
np.split(A,3,axis=0)
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
np.array_split(A,3,axis=1)
[array([[0, 1],
[4, 5],
[8, 9]]),
array([[ 2],
[ 6],
[10]]),
array([[ 3],
[ 7],
[11]])]
np.vsplit(A,3)
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
A
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
np.hsplit(A,2)
[array([[0, 1],
[4, 5],
[8, 9]]),
array([[ 2, 3],
[ 6, 7],
[10, 11]])]
numpy copy & deep copy
a = np.arange(4)
a
array([0, 1, 2, 3])
b=a
c=a
d=b
a[0]=11
a
array([11, 1, 2, 3])
b
array([11, 1, 2, 3])
c
array([11, 1, 2, 3])
d[1:3]=[22,33]
d
array([11, 22, 33, 3])
a
array([11, 22, 33, 3])
b = a.copy()
b
array([11, 22, 33, 3])
a[3]=44
b
array([11, 22, 33, 3])