# numpy操作小结

23 篇文章 1 订阅

## numpy基础

import numpy as np
nparr = np.array([i for i in range(5)])
nparr.dtype
dtype('int32')
nparr[4] = 1.
nparr.dtype
dtype('int32')
nparr2 = np.array([1, 2, 3.0])
nparr2.dtype
dtype('float64')

## numpy 创建

##### base
nparr = np.array([i for i in range(10)])
nparr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
##### zeros
np.zeros(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
np.zeros(10,dtype=int)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
np.zeros(shape=(2,4),dtype=int)
array([[0, 0, 0, 0],
[0, 0, 0, 0]])
##### ones
np.ones(10)
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
np.ones(shape=(2,3))
array([[1., 1., 1.],
[1., 1., 1.]])
##### full
np.full(shape=(3,5),fill_value=666)
array([[666, 666, 666, 666, 666],
[666, 666, 666, 666, 666],
[666, 666, 666, 666, 666]])
##### arange
np.arange(0,4,0.2)
array([0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8, 2. , 2.2, 2.4,
2.6, 2.8, 3. , 3.2, 3.4, 3.6, 3.8])
[i for i in np.arange(0, 1, 0.2)]
[0.0, 0.2, 0.4, 0.6000000000000001, 0.8]
##### linespace
np.linspace(0,20,11)
array([ 0.,  2.,  4.,  6.,  8., 10., 12., 14., 16., 18., 20.])
np.linspace(0, 20, 10)
array([ 0.        ,  2.22222222,  4.44444444,  6.66666667,  8.88888889,
11.11111111, 13.33333333, 15.55555556, 17.77777778, 20.        ])
##### random
# 产生一个随机数
np.random.random()
0.30175138248960554
# 产生一个shaoe的随机数组
np.random.random((3,5))
array([[0.45925724, 0.93660836, 0.77624171, 0.91976887, 0.16110875],
[0.17279011, 0.05058195, 0.46714559, 0.65925252, 0.56058558],
[0.59439629, 0.04804467, 0.14607784, 0.56502283, 0.66715391]])
# 产生一个按照正态分布随机数组
np.random.normal(size=(3,4))
array([[ 0.69446794,  0.96441901,  0.68527894,  2.57151514],
[ 0.43940671, -0.76200213,  0.43444882,  0.89212366],
[ 0.6425957 , -1.99443125,  0.63850483,  0.08472212]])
# 产生一个均值为0方差为1的正态分布
np.random.normal(0,1,size=(3,4))
array([[ 0.89365562,  1.21913638, -0.29682463, -0.39755723],
[-0.80627872,  0.26746836,  0.06353853, -0.10568317],
[-0.03705902, -0.19471723,  0.48376378,  0.87488668]])

# np.random?
help(np.random)

## numpy.array的基本操作

np.random.seed(666)
arr = np.random.normal(0,1,size=15)
arr = arr.reshape(3,5)
arr
array([[ 0.82418808,  0.479966  ,  1.17346801,  0.90904807, -0.57172145],
[-0.10949727,  0.01902826, -0.94376106,  0.64057315, -0.78644317],
[ 0.60886999, -0.93101185,  0.97822225, -0.73691806, -0.29873262]])
arr.ndim # ndim表示数组的维度
2
arr.shape
(3, 5)
arr.size
15

### numpy 数据访问

arr
array([[ 0.82418808,  0.479966  ,  1.17346801,  0.90904807, -0.57172145],
[-0.10949727,  0.01902826, -0.94376106,  0.64057315, -0.78644317],
[ 0.60886999, -0.93101185,  0.97822225, -0.73691806, -0.29873262]])
arr[0]
array([ 0.82418808,  0.479966  ,  1.17346801,  0.90904807, -0.57172145])
arr[-1]
array([ 0.60886999, -0.93101185,  0.97822225, -0.73691806, -0.29873262])

### 如何访问一个元素

arr[0][0]
0.8241880833175302
arr[0,0] # 建议
0.8241880833175302

### 如何访问多个元素

arr[1,:2] # 访问第一行的前两个元素
array([-0.10949727,  0.01902826])
arr[1,:2:1]
array([-0.10949727,  0.01902826])

## numpy的subarray

sub = arr[:2]
sub[0,0]=1
sub
array([[ 1.        ,  0.479966  ,  1.17346801,  0.90904807, -0.57172145],
[-0.10949727,  0.01902826, -0.94376106,  0.64057315, -0.78644317]])
arr
array([[ 1.        ,  0.479966  ,  1.17346801,  0.90904807, -0.57172145],
[-0.10949727,  0.01902826, -0.94376106,  0.64057315, -0.78644317],
[ 0.60886999, -0.93101185,  0.97822225, -0.73691806, -0.29873262]])

arr[0,0] =  0.82418808
sub = arr[:2].copy()
sub[0,0]= 1
sub
array([[ 1.        ,  0.479966  ,  1.17346801,  0.90904807, -0.57172145],
[-0.10949727,  0.01902826, -0.94376106,  0.64057315, -0.78644317]])
arr
array([[ 0.82418808,  0.479966  ,  1.17346801,  0.90904807, -0.57172145],
[-0.10949727,  0.01902826, -0.94376106,  0.64057315, -0.78644317],
[ 0.60886999, -0.93101185,  0.97822225, -0.73691806, -0.29873262]])

## numpy的合并

x = np.array([1,2,3])
y = np.array([3,2,1])
np.concatenate((x,y)) # 默认按照axis=0，按照行的顺序进行扩展
array([1, 2, 3, 3, 2, 1])
z = np.array([666, 666, 666])
np.concatenate([x, y, z],axis=0)
array([  1,   2,   3,   3,   2,   1, 666, 666, 666])
A = np.array([[1, 2, 3],
[4, 5, 6]])
np.concatenate([A, A],axis=1)
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]])
np.sum(A,axis=1) # axis =1 表示按照行从小到大进行
# axis =0 表示按照列从小达到大
A.shape
# 会得到一个元祖第一个元素表从列的方向上从小到大统计行的数目，axis= 0;第二个元素表示从行的方向上对列从小到大进行排序
(2, 3)
np.vstack([A,z]) #表示axis =0
array([[  1,   2,   3],
[  4,   5,   6],
[666, 666, 666]])
np.hstack([x,y])
array([1, 2, 3, 3, 2, 1])

## numpy分割

x = np.arange(10)
x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
x1,x2,x3 = np.split(x,[3,7]) # split传入一个列表表示分割的下标
x1,x2,x3
(array([0, 1, 2]), array([3, 4, 5, 6]), array([7, 8, 9]))
A = np.arange(16).reshape((4, 4))
A
array([[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11],
[12, 13, 14, 15]])
A1,A2 = np.split(A,[2],axis=0)  # 等价于 np.vsplit(A, [2])
A1
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
A1,A2 = np.split(A,[2],axis=1)  #等价于 np.hsplit(A, [2])
A1
array([[ 0,  1],
[ 4,  5],
[ 8,  9],
[12, 13]])

## numpy的运算

n = 10
L = np.array([i for i in range(1,10)])

2*L
array([ 2,  4,  6,  8, 10, 12, 14, 16, 18])
L-1
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
L/2
array([0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])
L+2
array([ 3,  4,  5,  6,  7,  8,  9, 10, 11])
L**2
array([ 1,  4,  9, 16, 25, 36, 49, 64, 81], dtype=int32)
L//2
array([0, 1, 1, 2, 2, 3, 3, 4, 4], dtype=int32)
1/L
array([1.        , 0.5       , 0.33333333, 0.25      , 0.2       ,
0.16666667, 0.14285714, 0.125     , 0.11111111])

### np常见的内置函数

np.abs(L)
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
np.sin(L)
array([ 0.84147098,  0.90929743,  0.14112001, -0.7568025 , -0.95892427,
-0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849])
np.cos(L)
array([ 0.54030231, -0.41614684, -0.9899925 , -0.65364362,  0.28366219,
0.96017029,  0.75390225, -0.14550003, -0.91113026])
np.tan(L)
array([ 1.55740772, -2.18503986, -0.14254654,  1.15782128, -3.38051501,
-0.29100619,  0.87144798, -6.79971146, -0.45231566])
np.arctan(L)
array([0.78539816, 1.10714872, 1.24904577, 1.32581766, 1.37340077,
1.40564765, 1.42889927, 1.44644133, 1.46013911])
np.exp(L)
array([2.71828183e+00, 7.38905610e+00, 2.00855369e+01, 5.45981500e+01,
1.48413159e+02, 4.03428793e+02, 1.09663316e+03, 2.98095799e+03,
8.10308393e+03])
np.exp2(L)
array([  2.,   4.,   8.,  16.,  32.,  64., 128., 256., 512.])
np.log(L)
array([0.        , 0.69314718, 1.09861229, 1.38629436, 1.60943791,
1.79175947, 1.94591015, 2.07944154, 2.19722458])
np.log2(L)
array([0.        , 1.        , 1.5849625 , 2.        , 2.32192809,
2.5849625 , 2.80735492, 3.        , 3.169925  ])
np.log10(L)
array([0.        , 0.30103   , 0.47712125, 0.60205999, 0.69897   ,
0.77815125, 0.84509804, 0.90308999, 0.95424251])

### 举证运算

A = np.arange(4).reshape(2,2)
A
array([[0, 1],
[2, 3]])
B  = np.full((2,2),4)
B
array([[4, 4],
[4, 4]])
A +B
array([[4, 5],
[6, 7]])
A-B
array([[-4, -3],
[-2, -1]])
A*B
array([[ 0,  4],
[ 8, 12]])
A/B
array([[0.  , 0.25],
[0.5 , 0.75]])
A.dot(B) # 点乘
array([[ 4,  4],
[20, 20]])
A.T # 装置矩阵
array([[0, 2],
[1, 3]])

### 向量与矩阵的运算

v=  np.array([1,2])
v
array([1, 2])
A+v
array([[1, 3],
[3, 5]])
np.vstack([v] * A.shape[0])
array([[1, 2],
[1, 2]])

v.dot(A) # 点乘
array([4, 7])
A.dot(v)
array([2, 8])

#### 矩阵的逆

invA = np.linalg.inv(A)
invA
array([[-1.5,  0.5],
[ 1. ,  0. ]])
A.dot(invA)
array([[1., 0.],
[0., 1.]])
invA.dot(A)
array([[1., 0.],
[0., 1.]])

## numpy聚合操作

import numpy as np
L = np.arange(10,20).reshape(2,5)
L
array([[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])

### sum

np.sum(L,axis=0)
array([25, 27, 29, 31, 33])

### min

np.min(L,axis=0)
array([10, 11, 12, 13, 14])

### max

np.max(L,axis=0)
array([15, 16, 17, 18, 19])

### 其他聚合操作

np.prod(L,axis=0)  # 对数组进行累乘
array([150, 176, 204, 234, 266])
np.mean(L,axis=0)  # 求平均数
array([12.5, 13.5, 14.5, 15.5, 16.5])
np.median(L,axis=0) # 中位数
array([12.5, 13.5, 14.5, 15.5, 16.5])
np.percentile(L, q=50) # 求的是百分位 q表示的是quary
14.5
np.std(L)  # 求标准差
2.8722813232690143
np.var(L)  # 求方差
8.25

## arg下标操作

import numpy as np
x = np.random.normal(0, 1, 1000000)
np.argmin(x)  # 求数组中最小值的下标
971877
x[971877],np.min(x)
(-4.769787080001609, -4.769787080001609)
np.argmax(x)
717652
x[4851],np.max(x)
(1.3925838910372959, 4.927801355024724)

### 排序和使用索引

x = np.arange(16)
x
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])
np.random.shuffle(x)  # 对x进乱序
x
array([13,  9,  3, 15,  8, 12,  6,  2, 11,  4, 14,  5,  7, 10,  1,  0])
np.sort(x)
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])

### 使用索引

np.random.shuffle(x)
x
array([ 4, 15,  9,  1,  5,  3, 14,  8,  7, 12,  2, 11, 10,  0, 13,  6])
index_sort = np.argsort(x)
index_sort
array([13,  3, 10,  5,  0,  4, 15,  8,  7,  2, 12, 11,  9, 14,  6,  1],
dtype=int64)
x[index_sort]
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])

## Fancy index

### Fancy Indexing 应用在二维数组

import numpy as np

x = np.arange(16)
x
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])
X = x.reshape(4, -1)
X
array([[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11],
[12, 13, 14, 15]])
row = np.array([0, 1, 2])
col = np.array([1, 2, 3])
X[row, col]
array([ 1,  6, 11])

### bool比较

X <2
array([[ True,  True, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False]])
X == X
array([[ True,  True,  True,  True],
[ True,  True,  True,  True],
[ True,  True,  True,  True],
[ True,  True,  True,  True]])

### bool比较在fancy index中的使用

np.sum(X<2, axis=0)
array([1, 1, 0, 0])
np.sum((X > 3) & (X< 10))
6
np.sum((X % 2 == 0) | (X > 10))
11
X[X< 5]
array([0, 1, 2, 3, 4])

X
array([[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11],
[12, 13, 14, 15]])
• 1
点赞
• 4
收藏
觉得还不错? 一键收藏
• 打赏
• 0
评论
09-06 641
03-28 7225
09-23 1万+
09-23
06-07 178

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

wjszfq

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。