– 生成0-9数组
a=np.arange(10)
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
– numpy 去重 去重的下标
a=np.array([11,11,12,13,14,15,16,17,12,13,14,15,18,19,20])
a
array([11, 11, 12, 13, 14, 15, 16, 17, 12, 13, 14, 15, 18, 19, 20])unique_value=np.unique(a)
unique_value
array([11, 12, 13, 14, 15, 16, 17, 18, 19, 20])unique_value,indices_list=np.unique(a,return_index=True)
unique_value
array([11, 12, 13, 14, 15, 16, 17, 18, 19, 20])indices_list
array([ 0, 2, 3, 4, 5, 6, 7, 12, 13, 14], dtype=int32)
–翻转数组
np.flip(a)
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
– 统计数量
unique_values,occurrence_count=np.unique(a,return_counts=True)
occurrence_count
array([2, 2, 2, 2, 2, 1, 1, 1, 1, 1])a
array([11, 11, 12, 13, 14, 15, 16, 17, 12, 13, 14, 15, 18, 19, 20])
– bool索引
a>15
array([False, False, False, False, False, False, True, True, False,
False, False, False, True, True, True])a[a>15]
array([16, 17, 18, 19, 20])
–更改维度 查看大小和维度属性
b=a.reshape(5,3)
a.size
15b
array([[11, 11, 12],
[13, 14, 15],
[16, 17, 12],
[13, 14, 15],
[18, 19, 20]])np.flip(b)
array([[20, 19, 18],
[15, 14, 13],
[12, 17, 16],
[15, 14, 13],
[12, 11, 11]])np.flip(b,axis=0)
array([[18, 19, 20],
[13, 14, 15],
[16, 17, 12],
[13, 14, 15],
[11, 11, 12]])np.flip(b,axis=1)
array([[12, 11, 11],
[15, 14, 13],
[12, 17, 16],
[15, 14, 13],
[20, 19, 18]])b[1] = np.flip(b[2])
b
array([[11, 11, 12],
[12, 17, 16],
[16, 17, 12],
[13, 14, 15],
[18, 19, 20]])b[:,1] = np.flip(b[:,1])
b
array([[11, 19, 12],
[12, 14, 16],
[16, 17, 12],
[13, 17, 15],
[18, 11, 20]])b=a.reshape(5,3)
a.size
15a.shape
(15,)b.shape
(5, 3)b.dtype
dtype(‘int32’)b.ndim
2b.transpose()
array([[11, 13, 16, 13, 18],
[11, 14, 17, 14, 19],
[12, 15, 12, 15, 20]])b.T
array([[11, 13, 16, 13, 18],
[11, 14, 17, 14, 19],
[12, 15, 12, 15, 20]])
– 切片
b[np.array([0,2,4]),1:3]
array([[11, 12],
[17, 12],
[19, 20]])
– np.newaxis增加维度
b[:,np.newaxis,:].shape
(5, 1, 3)b[:,np.newaxis,:]
array([[[11, 11, 12]],
[[12, 17, 16]],
[[16, 17, 12]],
[[13, 14, 15]],
[[18, 19, 20]]])
b
array([[11, 11, 12],
[12, 17, 16],
[16, 17, 12],
[13, 14, 15],
[18, 19, 20]])x=np.arange(5)
x
array([0, 1, 2, 3, 4])x[:,np.newaxis] +x[np.newaxis,:]
array([[0, 1, 2, 3, 4],
[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7],
[4, 5, 6, 7, 8]])
– … 选择其余未定义的维度
z = np.arange(81).reshape(3,3,3,3)
z[1,…,2]
array([[29, 32, 35],
[38, 41, 44],
[47, 50, 53]])
indices=(1,1,1,slice(0,2)) #same as (1,1,1,0:2)
z[indices]
array([39, 40])indices=(1,Ellipsis,1) #same as (1,…,1)
z[indices]
array([[28, 31, 34],
[37, 40, 43],
[46, 49, 52]])
– 转成一维数组 flatten 创建新数组 更改不影响原数据 ravel只建视图
x=np.arange(1,13).reshape((3,4))
x
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])x.flatten()
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])x
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])a1=x.flatten()
a1[0]=99
x
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])a1
array([99, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
a2=x.ravel()
a2[0]=99
a2
array([99, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])x
array([[99, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
–赋值
x=np.arange(10)
x[2:7]=1
x
array([0, 1, 1, 1, 1, 1, 1, 7, 8, 9])x[2:7]=np.arange(5)
x
array([0, 1, 0, 1, 2, 3, 4, 7, 8, 9])x[1]=1.2
x
array([0, 1, 0, 1, 2, 3, 4, 7, 8, 9])x=np.arange(0,50,10)
x
array([ 0, 10, 20, 30, 40])x[np.array([1,2,3,1])]+=1
x
array([ 0, 11, 21, 31, 40])
广播描述了numpy在算术运算期间如何处理具有不同形状的数组。受到某些约束,较小的阵列在较大的阵列上’广播’,以使它们具有兼容的形状。
x
array([1., 1., 1., 1., 1.])y
array([1., 1., 1., 1., 1.])x=np.arange(4)
x+y
Traceback (most recent call last):
File “”, line 1, in
ValueError: operands could not be broadcast together with shapes (4,) (5,)xx
array([[0],
[1],
[2],
[3]])y
array([1., 1., 1., 1., 1.])xx+y
array([[1., 1., 1., 1., 1.],
[2., 2., 2., 2., 2.],
[3., 3., 3., 3., 3.],
[4., 4., 4., 4., 4.]])
– newaxis索引运算符将一个新轴插入a,使其成为二维4x1数组。将4x1数组与b形状为的组合会(3,)生成一个4x3数组
a=np.array([0,10,20,30])
b=np.arange(1,4)
a[:,np.newaxis]+b
array([[ 1, 2, 3],
[11, 12, 13],
[21, 22, 23],
[31, 32, 33]])
https://numpy.org/doc/stable/user/basics.broadcasting.html