使用Python NumPy计算矩阵列的和:
import numpy
from StringIO import StringIO
fileName = 'test2.csv'
myFile = open(fileName,'r')
print "Reading data from '%s' ..." % fileName
data = myFile.read()
myFile.close()
data = numpy.genfromtxt(StringIO(data), delimiter=',', usecols=(0,1,2))
print "Calculating ..."
print data[:,2]
sumA1 = data[:,2].sum
print "shape =", data.shape
print "sumA1 =", str(sumA1)
print "ok"
则test2.csv
的内容:
我得到这个输出
Reading data from 'test2.csv' ...
Calculating ...
[ 14. 3. 14. 3. 14. 3. 14. 3. 14.]
shape = (9, 3)
sumA1 = <built-in method sum of numpy.ndarray object at 0x00FD8EF8>
ok
为什么我不能用“sumA1=82”代替那个奇怪的信息?我做错什么了?在
事先非常感谢。在
您必须调用
sum
函数。只需替换这段代码:有了这个:
^{pr2}$相关问题 更多 >
编程相关推荐