0

我有一堆按以下顺序排列的文件(制表符分隔):

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')  
4

2 回答 2

1

我发现了错误,我的文件长度不一样。这导致了我访问空元素的情况。我添加了一个循环,在每个列表的末尾添加零,直到我得到相同的长度。Schuh 指出,最后添加零可能会导致错误的标准。在我的数据中不是这种情况,但应该注意这一点。

于 2012-05-16T06:21:13.347 回答
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]])

现在您可以像这样获得例如本地列的 std:

    >>>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]])
于 2012-05-15T08:24:15.203 回答