1026-徐同学

Numpy

安装

Pycharm直接安装numpy包

Files->Setting->Project workspace->Project:，点击+按钮，搜索numpy包，点击安装即可。

numpy基础运算

import numpy as np
y = np.array([[11,4,2],[2,6,1],[32,6,42]])
print(y)
print('number of dim:',y.ndim)
print('shape:',y.shape)

[[11  4  2]
[ 2  6  1]
[32  6 42]]
number of dim: 2
shape: (3, 3)
import numpy as np
a = np.array([10,20,30,40])
b = np.arange(4)
c = 10*np.sin(a)
print(c)

[-5.44021111  9.12945251 -9.88031624  7.4511316 ]
import numpy as np
a = np.array([10,20,30,40])
b = np.arange(4)
print(b)
print(b==3)

[0 1 2 3]
[False False False  True]
import numpy as np
a = np.array([[1,1],[0,1]])
b = np.arange(4).reshape((2,2))

c = a*b
c_dot = np.dot(a,b)
print(c)
print(c_dot)

[[0 1]
[0 3]]
[[2 4]
[2 3]]
import numpy as np
a = np.random.random((2,4))

print(np.sum(a))
print(np.min(np.sum(a,axis=1)))#每一行
print(np.min(np.min(a,axis=0)))#每一列
print(np.min(np.max(a,axis=1)))

3.443379837630782
3.443379837630782
0.16345946606982198
0.7558721678310337

import numpy as np
A= np.arange(2,14).reshape((3,4))
print(np.argmin(A))
print(np.argmax(A))
print(np.mean(A))
print(np.median(A))
print(np.cumsum(A))
print(np.diff(A))

0
11
7.5
7.5
[ 2  5  9 14 20 27 35 44 54 65 77 90]
[[1 1 1]
[1 1 1]
[1 1 1]]
import numpy as np
A= np.arange(14,2,-1).reshape((3,4))
print(A)
print(np.transpose(A))
print(A.T)

[[14 13 12 11]
[10  9  8  7]
[ 6  5  4  3]]
[[14 10  6]
[13  9  5]
[12  8  4]
[11  7  3]]
[[14 10  6]
[13  9  5]
[12  8  4]
[11  7  3]]

import numpy as np
A= np.arange(14,2,-1).reshape((3,4))
print(A)
print(np.clip(A,5,9))

[[14 13 12 11]
[10  9  8  7]
[ 6  5  4  3]]
[[9 9 9 9]
[9 9 8 7]
[6 5 5 5]]
import numpy as np
A= np.arange(3,15).reshape((3,4))
print(A)
print(A[2][1])
print(A[1,1:2])

[[ 3  4  5  6]
[ 7  8  9 10]
[11 12 13 14]]
12
[8]
import numpy as np
A= np.array([1,1,1])
B= np.array([2,2,2])
C = np.vstack((A,B))
D = np.hstack((A,B))
print(D)
print(A.shape,D.shape)

[1 1 1 2 2 2]
(3,) (6,)
import numpy as np
A = np.arange(12).reshape((3,4))
print(A)
print(np.split(A,2,axis=1))

[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2,  3],
[ 6,  7],
[10, 11]])]

Vieu3.3主题

Q Q 登 录