Python的向量化

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]
  ))
print(a)

cal=a.sum(axis=0)#每一列求和,竖直方向求和。水平方向则是axis=1
print(cal)
percent=100*a/cal.reshape(1,4)#广播的例子。a是3*4矩阵,cal会自动为1*4矩阵。在这里,reshape可以去掉
print(percent)

结果:[[94.91525424 0. 2.83140283 88.42652796] [ 2.03389831 43.51464435 33.46203346 10.40312094] [ 3.05084746 56.48535565 63.70656371 1.17035111]]

np.random.rand(5)---->a=a.reshape()–>np.random.rand(5,1)

猜你喜欢

转载自blog.csdn.net/weixin_42348105/article/details/87994089