1.数组与数字计算
1-1对于一个二维数组,我们进行加2操作,结果会是什么呢
import numpy as npt1=np.arange(24)t2=t1.reshape((4,6))print(t2)print("----------")t3=t2+2print(t3)
运行结果:我们可以发现数组里的每个数都进行了加2操作
D:pythonpython.exe F:/data_analysis/numpy_calculation.py[[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]]----------[[ 2 3 4 5 6 7] [ 8 9 10 11 12 13] [14 15 16 17 18 19] [20 21 22 23 24 25]]进程已结束,退出代码 0
1-2同理对于乘除减法原理也都是一样的
1-3对数组除以0的情况
import numpy as npt1=np.arange(24)t2=t1.reshape((4,6))print(t2)print("----------")t3=t2/0print(t3)
运行结果并没有报错,只是给了警告。0/0的结果是nan,nan的意思是not a number,其它非0常数除以0的结果是inf(infinite即无穷的意思)
D:pythonpython.exe F:/data_analysis/numpy_calculation.pyF:/data_analysis/numpy_calculation.py:7: RuntimeWarning: divide by zero encountered in true_divide t3=t2/0F:/data_analysis/numpy_calculation.py:7: RuntimeWarning: invalid value encountered in true_divide t3=t2/0[[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]]----------[[nan inf inf inf inf inf] [inf inf inf inf inf inf] [inf inf inf inf inf inf] [inf inf inf inf inf inf]]进程已结束,退出代码 0
2.数组与数组进行计算时,是广播机制,数组的每个值都进行计算
2-0当数组的维度不一样时,就会报错
![502780410db403640ed8b1fca7f4055e.png](https://i-blog.csdnimg.cn/blog_migrate/d40c526b29e7e3780c3744c23eb8bcb4.jpeg)
但是也有特殊情况:
2-1当两个数组形状完全一样时,对应位置的数字相加。
import numpy as npt1=np.arange(24)t2=t1.reshape((4,6))print(t2)print("----------")t3=np.arange(100,124).reshape(4,6)print(t3+t2)
运行结果:
D:pythonpython.exe F:/data_analysis/numpy_calculation.py[[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]]----------[[100 102 104 106 108 110] [112 114 116 118 120 122] [124 126 128 130 132 134] [136 138 140 142 144 146]]进程已结束,退出代码 0
2-2当两个数组的行数相同时,对应的行进行相应的操作
import numpy as npt1=np.arange(24)t2=t1.reshape((4,6))print(t2)print("----------")t3=np.arange(0,6)print(t3+t2)
运行结果:
D:pythonpython.exe F:/data_analysis/numpy_calculation.py[[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]]----------[[ 0 2 4 6 8 10] [ 6 8 10 12 14 16] [12 14 16 18 20 22] [18 20 22 24 26 28]]进程已结束,退出代码 0
2-3当两个数组的列数相同时,对应的列进行相应的操作
import numpy as npt1=np.arange(24)t2=t1.reshape((4,6))print(t2)print("----------")t3=np.arange(0,4).reshape((4,1))print(t3+t2)
运行结果:
D:pythonpython.exe F:/data_analysis/numpy_calculation.py[[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]]----------[[ 0 1 2 3 4 5] [ 7 8 9 10 11 12] [14 15 16 17 18 19] [21 22 23 24 25 26]]进程已结束,退出代码 0
数组与数组计算总结:
shape为(3,3,3)的不能够和(3,2)的进行计算;
shape为(3,3,2)的能够和(3,2)的进行计算,因为(3,3,2)后维度是3,2,所以可以和shape为(3,2)的进行计算。
![580f35947191d21668c50124e9afc7b0.png](https://i-blog.csdnimg.cn/blog_migrate/c6d9f8057b3f9190a664ffa9a467939f.jpeg)
广播原则下的例子:
![f2dbb7d974e96af1be71748f501f2243.png](https://i-blog.csdnimg.cn/blog_migrate/75fab7f28cc0a1b09c2612e33b7c4d77.jpeg)
求数组的行或列的和
import numpy as npt1=np.arange(24)t2=t1.reshape((4,6))print(t2)print("*"*50)#对数组的行求和print(np.sum(t2,axis=0))print("*"*50)#对数组的列求和print(np.sum(t2,axis=1))
运行结果:
D:pythonpython.exe F:/data_analysis/test.py[[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]]**************************************************[36 40 44 48 52 56]**************************************************[ 15 51 87 123]进程已结束,退出代码 0
numpy的统计函数
![b05978a7da48888e3e09c8751277fa13.png](https://i-blog.csdnimg.cn/blog_migrate/392e30b4c92e9448e2350076916cc4f7.jpeg)
里面的求和函数除了t.num(),还可以用numpy.sum(t)