1.4-NumPy数组运算
1.4.1 算术运算¶
- add 加
- subtract 减
- multipie 乘
- divide 除
1.4.2 数学运算¶
- 三角函数: sin,cos,tan
- 算术运算
1.4.3 统计运算¶
- mean 均值
- average 均值
- var 方差
- std 标准差
1.4.4 聚合运算¶
- 性能区别: np聚合函数和py内置函数
- sum 累加
- prod 累乘
- max 最大值
- min 最小值
- 统计运算
1.4.5 筛选和排序¶
- where
- sort
import numpy as npIn [2]:
# 算术运算 a = np.random.random(10000)*10 b = np.random.random(10000)*10 a bOut[2]:
array([9.66314537, 9.56406383, 4.43887791, ..., 7.85109353, 6.34951408, 2.53352231])In [3]:
aOut[3]:
array([9.34912059, 1.72634859, 6.30775754, ..., 8.32242753, 8.251723 , 7.08478662])In [4]:
bOut[4]:
array([9.66314537, 9.56406383, 4.43887791, ..., 7.85109353, 6.34951408, 2.53352231])In [5]:
# 既可以使用运算符也可以使用np算术函数 a + bOut[5]:
array([19.01226595, 11.29041242, 10.74663546, ..., 16.17352106, 14.60123708, 9.61830893])In [6]:
np.add(a,b) # 加Out[6]:
array([19.01226595, 11.29041242, 10.74663546, ..., 16.17352106, 14.60123708, 9.61830893])In [7]:
np.subtract(a,b) # 减Out[7]:
array([-0.31402478, -7.83771524, 1.86887963, ..., 0.47133401, 1.90220891, 4.55126431])In [8]:
np.multiply(a,b) # 乘Out[8]:
array([90.34191128, 16.51090807, 27.99936564, ..., 65.34015693, 52.39443141, 17.94946495])In [9]:
np.divide(a,b) # 除Out[9]:
array([0.96750284, 0.18050367, 1.42102524, ..., 1.06003419, 1.29958338, 2.7964177 ])In [10]:
# 数学函数-三角函数 ang = np.array([0,30,45,60,90,120,135,150,180]) rad = np.sin(ang*np.pi/180) radOut[10]:
array([0.00000000e+00, 5.00000000e-01, 7.07106781e-01, 8.66025404e-01, 1.00000000e+00, 8.66025404e-01, 7.07106781e-01, 5.00000000e-01, 1.22464680e-16])In [11]:
ang = np.array([0,30,45,60,90,120,135,150,180]) rad = np.cos(ang*np.pi/180) radOut[11]:
array([ 1.00000000e+00, 8.66025404e-01, 7.07106781e-01, 5.00000000e-01, 6.12323400e-17, -5.00000000e-01, -7.07106781e-01, -8.66025404e-01, -1.00000000e+00])In [12]:
ang = np.array([0,30,45,60,90,120,135,150,180]) rad = np.tan(ang*np.pi/180) radOut[12]:
array([ 0.00000000e+00, 5.77350269e-01, 1.00000000e+00, 1.73205081e+00, 1.63312394e+16, -1.73205081e+00, -1.00000000e+00, -5.77350269e-01, -1.22464680e-16])In [14]:
# 统计 d2 = np.array([1,2,3,4,5]) d2.mean()Out[14]:
3.0In [15]:
np.average(d2)Out[15]:
3.0In [16]:
np.average(d2,weights=np.array([2,2,1,1,1])) # 可以设置权重Out[16]:
2.5714285714285716In [17]:
np.average(d2,weights=np.array([1,1,1,2,3]))Out[17]:
3.625In [18]:
# 中位数 np.median(d2)Out[18]:
3.0In [19]:
d3 = np.array([1,2,3,4,5,6]) np.median(d3)Out[19]:
3.5In [20]:
# 方差 np.var(d3)Out[20]:
2.9166666666666665In [21]:
# 标准差 np.std(d3)Out[21]:
1.707825127659933In [22]:
# 聚合运算 aOut[22]:
array([9.34912059, 1.72634859, 6.30775754, ..., 8.32242753, 8.251723 , 7.08478662])In [23]:
bOut[23]:
array([9.66314537, 9.56406383, 4.43887791, ..., 7.85109353, 6.34951408, 2.53352231])In [24]:
%time sum(a) %time np.sum(a)
CPU times: total: 0 ns Wall time: 0 ns CPU times: total: 0 ns Wall time: 0 nsOut[24]:
49855.5814326455In [25]:
np.prod(d2)Out[25]:
120In [26]:
np.max(d2)Out[26]:
5In [27]:
# 筛选 d3Out[27]:
array([1, 2, 3, 4, 5, 6])In [28]:
np.where(d3>3) # 返回的索引Out[28]:
(array([3, 4, 5], dtype=int64),)In [29]:
np.where(d3<3) # 返回的索引Out[29]:
(array([0, 1], dtype=int64),)In [30]:
np.sort([5,4,2,7,2,1])Out[30]:
array([1, 2, 2, 4, 5, 7])In [ ]: 标签:...,00,01,运算,Numpy,np,array,数据,Out From: https://www.cnblogs.com/mlzxdzl/p/17767809.html