1.数组拆分
垂直拆分
import numpy as np
n = np.arange(1,13).reshape(6,2)
p = np.vsplit(n,3)
print(n,p)
运行结果:[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]
[11 12]] [array([[1, 2],
[3, 4]]), array([[5, 6],
[7, 8]]), array([[ 9, 10],
[11, 12]])]
水平拆分
import numpy as np
n = np.arange(1,13).reshape(6,2)
i = n.T
p = np.hsplit(i,3)
print(i)
print(p)
运行结果:
[[ 1 3 5 7 9 11]
[ 2 4 6 8 10 12]]
[array([[1, 3],
[2, 4]]), array([[5, 7],
[6, 8]]), array([[ 9, 11],
[10, 12]])]
import numpy as np
n = np.array((1,2,3))
i = np.array((2,3,4))
p = np.dstack((n,i))
f = np.dsplit(p,2)
print(p)
print(f)
运行结果:
[[[1 2]
[2 3]
[3 4]]]
[array([[[1],
[2],
[3]]]), array([[[2],
[3],
[4]]])]
2. numpy基本加减和取行操作
import numpy as np
n = np.array([1,1,1,1])
i = np.array([[1],[1],[1],[1]])
c = np.array([1,1,1,1])
d = np.array([[1,1,1],[2,2,2]])
d[:,1] = np.array([5,5])
print(d)
print(n+i)
print(i-c)
运行结果:
[[1 5 1]
[2 5 2]]
[[2 2 2 2]
[2 2 2 2]
[2 2 2 2]
[2 2 2 2]]
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
3. 矩阵删除、插入、尾部添加操作(delete,insert,append)
删除:
import numpy as np
matrix = [[1,2,3,4],
[5,6,7,8],
[9,10,11,12]]
n = np.delete(matrix, 2, 0)
print(n)
p = np.delete(matrix, 3, 1)
print(p)
f = np.delete(matrix, 2)
print(f)
z = np.delete(matrix, [0,1], 1)
print(z)
运行结果:[[1 2 3 4]
[5 6 7 8]]
[[ 1 2 3]
[ 5 6 7]
[ 9 10 11]]
[ 1 2 4 5 6 7 8 9 10 11 12]
[[ 3 4]
[ 7 8]
[11 12]]
插入:
import numpy as np
matrix = [[1,2,3,4],
[5,6,7,8],
[9,10,11,12]]
q1 = np.insert(matrix, 1, [1,1,1,1], 0) # 第0维度(行)第1行添加[1,1,1,1]
print('>>>>q1>>>>\n',q1)
q2 = np.insert(matrix, 0, [1,1,1], 1) # 第1维度(列)第0列添加1,1,1
print('>>>>q2>>>>\n',q2)
q3 = np.insert(matrix, 3, [1,1,1,1], 0) # 第0维度(行)第3行添加[1,1,1,1]
print('>>>>q3>>>>\n',q3)
运行结果:
>>>>q1>>>>
[[ 1 2 3 4]
[ 1 1 1 1]
[ 5 6 7 8]
[ 9 10 11 12]]
>>>>q2>>>>
[[ 1 1 2 3 4]
[ 1 5 6 7 8]
[ 1 9 10 11 12]]
>>>>q3>>>>
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[ 1 1 1 1]]
尾部添加:
import numpy as np
matrix = [[1,2,3,4],
[5,6,7,8],
[9,10,11,12]]
m1 = np.append(matrix,[[1,1,1,1]],axis=0)
#第0维度(行)尾部添加[[1,1,1,1]],注意两个[],相同维度
print('>>>>m1>>>>\n',m1)
m2 = np.append(matrix,[[1],[1],[1]],axis=1)
#第1维度(列)尾部添加[[1],[1],[1]],注意两个[],相同维度
print('>>>>m2>>>>\n',m2)
m3 = np.append(matrix,[1,1,1,1])
#拉平后再尾部添加[1,1,1,1],这里可以[[1,1,1,1]]和[1,1,1,1]均可
print('>>>>m3>>>>\n',m3)
运行结果:
>>>>m1>>>>
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[ 1 1 1 1]]
>>>>m2>>>>
[[ 1 2 3 4 1]
[ 5 6 7 8 1]
[ 9 10 11 12 1]]
>>>>m3>>>>
[ 1 2 3 4 5 6 7 8 9 10 11 12 1 1 1 1]
4. np.random.choice(a, size, replace, p)
import numpy as np
a1 = np.random.choice(7,5) # 从0~7中随机选择5个数组成一维数组
print(a1)
a2 = np.random.choice([0,1,2,3,4,5,6],5) # 从给定list中随机选择5个数组成一维数组
print(a2)
a3 = np.random.choice(np.array([0,1,2,3,4,5,6]),5) # 将list换成array数组依然可以运行,效果一致
print(a3)
a4 = np.random.choice([0,1,2,3,4,5,6],5,replace=False) # 上述均有重复,将replace设置为False,即可按要求没有重复的选取
print(a4)
a5 = np.random.choice(np.array([0,1,2,3,4,5,6]),5,p=[0.1,0.1,0.1,0.1,0.1,0.1,0.4])
print(a5)
运行结果:
>>>>m1>>>>
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[ 1 1 1 1]]
>>>>m2>>>>
[[ 1 2 3 4 1]
[ 5 6 7 8 1]
[ 9 10 11 12 1]]
>>>>m3>>>>
[ 1 2 3 4 5 6 7 8 9 10 11 12 1 1 1 1]
[4 4 3 4 5]
[5 5 4 1 2]
[1 4 3 5 3]
[6 1 4 2 5]
[0 3 6 2 6]
5.np.argmax(a, axis=None, out=None)
作用是返回轴的最大值的索引值
6.numpy.linspace
numpy.linspace(start, shop, num==50, endpoint=True, retstep=False, dtype=None)
在指定间隔start到stop内返回均匀间隔的数组。
返回num均匀分布的样本,在[start, stop],默认生成50个数据
endpoint, 如果是真,则一定包括stop,如果为False,一定不会有stop
retstep,是否显示步长信息