我有一堆按以下顺序排列的文件(标签分隔):
h local average
1 4654 4654
2 5564 5564
3 6846 6846
... ... ...
我在循环中读取文件(附在下面)并将它们存储在二维列表中。然后我将列表转换为数组并将std应用于它。结果如下:
Traceback (most recent call last):
File "plot2.py", line 56, in <module>
e0028 = np.std(ar, axis=0)
File "/usr/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2467, in std
return std(axis, dtype, out, ddof)
TypeError: unsupported operand type(s) for /: 'list' and 'float'
让我感到困惑。我试图在数组中找到一个不浮动但没有弹出的元素。
import numpy as np
import matplotlib.pyplot as plt
from math import fabs, sqrt, pow, pi
h0028 = []
p0028 = []
headLines = 2
fig=plt.figure()
ax1 = fig.add_subplot(1,1,1)
for i in range (0,24):
n = 0
j = i + 560
p = []
f = open('0028/'+str(j)+'.0028/ionsDist.dat')
for line in f:
if n < headLines:
n += 1
continue
words = line.split()
p.append (float(words[1]))
if i == 0:
h0028.append (fabs(int(words[0])))
n += 1
print (n)
p0028.append(p)
f.close()
ar = np.array(p0028)
for a in ar:
for b in a:
if not isinstance(b,float):
print type(a)
e0028 = np.std(ar, axis=0)
p0028 = np.mean(ar, axis=0)
h0028 = np.array(h0028)/10 -2.6
p0028 /= max(p0028)
e0028 /= (sum(p0028)*sqrt(23))
ax1.errorbar(h0028 , p0028, yerr=e0028, color = 'red')
ax1.set_xlim(-0.1,10)
plt.show()
plt.savefig('plot2.png', format='png')
答案 0 :(得分:1)
我无法弄清楚,为什么你的代码不起作用,但也许这会对你有帮助。 您可以像这样阅读文件:
>>>a = np.loadtxt("p0028.csv",dtype="float",skiprows = 1)
>>> a
array([[ 1.00000000e+00, 4.65400000e+03, 4.65400000e+03],
[ 2.00000000e+00, 5.56400000e+03, 5.56400000e+03],
[ 3.00000000e+00, 6.84600000e+03, 6.84600000e+03]])
现在你可以获得例如像这样的本地列:
>>>a_std = np.std(a[:1])
2193.4452352406706
当您循环遍历多个文件时,可以使用vstack方法一起收集数据,这样您就不依赖于文件中的行数:
>>>a = np.loadtxt("p0028.csv",dtype="float",skiprows = 1)
>>> a
array([[ 1.00000000e+00, 4.65400000e+03, 4.65400000e+03],
[ 2.00000000e+00, 5.56400000e+03, 5.56400000e+03],
[ 3.00000000e+00, 6.84600000e+03, 6.84600000e+03]])
>>>b = np.loadtxt("p0028.csv",dtype="float",skiprows = 1)
>>> np.vstack((a,b))
array([[ 1, 4654, 4654],
[ 2, 5564, 5564],
[ 3, 6846, 6846],
[ 1, 4654, 4654],
[ 2, 5564, 5564],
[ 3, 6846, 6846]])
答案 1 :(得分:1)
我发现了错误,我的文件长度不一样。这导致我访问空元素的情况。我添加了一个循环,在每个列表的末尾添加零,直到我得到相同的长度。 Schuh指出,最后加零可能会导致错误标准。在我的数据中并非如此,但应注意这一点。