标签:numpy ast pytho adc broadcast axis cas sum cal
python中broadcast机制非常实用,在python中的广播机制其实很简单,我们看两个例子。
#eg.1
import numpy as np A=np.array([[56.0,0.0,4.4,68.0], [1.2,104.0,52.0,8.0], [1.8,135.0,99.0,0.9]]) cal=np.sum(A,axis=0) percentage=100*A/cal.reshape(1,4) #reshape保证行状,开支小
我们可以看到 A的维度是(3,4),cal的维度是(1,4),即python在此处将cal复制了3次,将cal扩展为(3,4),即cal‘=np.array([[59. , 239. , 155.4, 76.9],
[59. , 239. , 155.4, 76.9],
[59. , 239. , 155.4, 76.9]])
#eg.2
B=np.array([1,2,3,4]).reshape(4,1)
c=100
res=B+100
即(4,1)+(1,1),python这里会将(1,1)扩展为(4,1)
标签:numpy ast pytho adc broadcast axis cas sum cal
原文地址:https://www.cnblogs.com/lawrence-ml/p/11404518.html