Python Numpy数据类型误差和PYPLOT的极低使用:(



[使用Windows 10和带有最新模块的Python 3.5]

你好!

我有两个略有不同的问题,因为一个问题是另一个问题的解决方案。此处的第一个功能非常慢,数据点超过75000,并且与150000不起作用。

#I call the functions like this:
plt.plot(logtime[:recmax-(degree*2-1)] - (logtime[0]-degree), smoothListTriangle(cpm, degree), color="green", linewidth=2, label="Smoothed n="+degree)
plt.plot(logtime[:recmax] - logtime[0], smoothListGaussian2(str(cpm), degree), color="lime", linewidth=5, label="")
#And cpm is always:
cpm = cpm.astype(int) #Array of big number of values

def smoothListTriangle(cpm,degree):  #Thank you Scott from swharden.com!
    weight=[]  
    window=degree*2-1
    smoothed=[0.0]*(len(cpm)-window)
    for x in range(1,2*degree):
        weight.append(degree-abs(degree-x))
    w=np.array(weight)
    for i in range(len(smoothed)):  
        smoothed[i]=sum(np.array(cpm[i:i+window])*w)/float(sum(w))
    #Very, VERY slow...
    return smoothed

较高的"程度"是它所需的时间越长。但是,

,它看起来不会很好。

...

这里的第二个功能应该(方式?)更有效,但是我无法解决数据类型错误:

def smoothListGaussian2(myarray, degree):
    myarray = np.pad(myarray, (degree-1,degree-1), mode='edge')
    window = degree*2-1
    weight = np.arange(-degree+1, degree)/window
    weight = np.exp(-(16*weight**2))
    weight /= sum(weight)
    #weight = weight.astype(int)    #Does throw the "invalid literal" error
    smoothed = np.convolve(myarray, weight, mode='valid')
    return smoothed
    #TypeError: Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe'

我拼命尝试使用numpy解决此数据类型错误。这太痛苦了!这似乎是数组的"重量",那就是float64的那个数组,但是将其转换为以下错误,例如:

ValueError: invalid literal for int() with base 10: '[31 31 33 ..., 48 49 51]'

所以...我是新手Python,并使用它来记录我的Geiger计数器的数据。您是否知道如何使第一个功能更有效或在第二个功能中解决错误?我在这里茫然。

我在这里找到了这些脚本:http://www.swharden.com/wp/2008-11-17-linear-data-smoothing-in-python/#comments(我在Scotts上找到了Scotts其他Triangle-smooth-punction这个网站,但我也无法正常工作。它更复杂)

请注意,数据点的数量取决于测量的秒内的长度,并且该长度很可能是几天。我猜一百万个数据点并不罕见。

谢谢!

我只是有某种启示。我要做的就是在卷动之前将" myarray"转换为漂浮。

我必须进行很多转换才能使整个代码正常工作,这荒谬!我认为这在Python中很容易,但是没有.. :(在这种情况下,C 更好。

def smoothListGaussian2(myarray, degree):
    myarray = np.pad(myarray, (degree - 1, degree - 1), mode='edge')
    window = degree * 2 - 1
    weight = np.arange(-degree + 1, degree) / window
    weight = np.exp(-(16 * weight ** 2))
    weight /= sum(weight)
    myarray = myarray.astype(float)
    smoothed = np.convolve(myarray, weight, mode='valid')
    return smoothed

由于这起作用,因此我可以测试速度,并且非常快。我再也看不到40k和150k数据点之间的速度差异了。酷

最新更新