给出三个清单:
A = [1, 2, 3]
B = [4, 5, 6]
C = [7, 8, 9]
如何计算以下(列)的平均值?
不是以下(行),
import numpy as np
A = np.array([1,2,3])
B = np.array([4,5,6])
C = np.array([7,8,9])
我该怎么办?
答案 0 :(得分:1)
import numpy as np
A = np.array([1,2,3])
B = np.array([4,5,6])
C = np.array([7,8,9])
Z = zip(A,B,C)
for z in Z :
print np.mean(np.asarray(z))
答案 1 :(得分:1)
>>> np.array([
... [1,2,3],
... [4,5,6],
... [7,8,9]
... ]).transpose().mean(axis=1)
array([ 4., 5., 6.])
或使用numpy.transpose:
>>> np.transpose([
... [1,2,3],
... [4,5,6],
... [7,8,9]
... ]).mean(axis=1)
array([ 4., 5., 6.])
<强>更新强>
正如Dave Hirschfeld评论的那样,axis=0
的意思更好:
>>> np.array([
... [1,2,3],
... [4,5,6],
... [7,8,9]
... ]).mean(axis=0)
array([ 4., 5., 6.])