Python使用扩展库numpy计算矩阵加权平均值
![作者头像](https://ask.qcloudimg.com/http-save/yehe-1653730/ku4w3oivae.jpeg)
本文介绍Python扩展库numpy的函数average()的用法。
>>> import numpy as np
# 创建二维矩阵 >>> x = np.matrix([[1,2,3], [4,5,6]])
# 设置权重 >>> w1 = [0.3, 0.7]
# 纵向计算加权平均 >>> np.average(x, axis=0, weights=w1) matrix([[ 3.1, 4.1, 5.1]])
>>> w2 = [0.3, 0.3, 0.4]