python numpy数据类型错误,pyplot的使用效率极低:(

python numpy数据类型错误,pyplot的使用效率极低:(,python,performance,numpy,matplotlib,Python,Performance,Numpy,Matplotlib,[在最新模块中使用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="gre

[在最新模块中使用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'
所以…我是python新手,用它来记录盖革计数器中的数据。你知道如何使第一个函数更有效或解决第二个函数中的错误吗?我在这里不知所措

我在这里找到了脚本:(我在这个网站上找到了Scotts的其他三角形平滑函数,但我也无法让它工作。它更复杂)

请注意,数据点的数量取决于测量的长度(以秒为单位),该长度很可能是几天。我猜一百万个或更多数据点并不罕见


谢谢!

我刚刚得到了某种启示。我所要做的就是在卷积之前将“myarray”转换为float

<>我必须做很多转换才能使整个代码工作正常,这是荒谬的!我认为这在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数据点之间的速度差异了。酷

我刚刚得到了某种启示。我所要做的就是在卷积之前将“myarray”转换为float

<>我必须做很多转换才能使整个代码工作正常,这是荒谬的!我认为这在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数据点之间的速度差异了。酷

首先,我不能重现无效的文字错误,
weight。astype(int)
对我来说没有问题(虽然我使用的是Python2.7,但我怀疑这是原因)第二,如果你把
weight
转换成整数,你只能得到0,因为你所有的数字都小于1。这真的是你想要的吗?如果你不能复制它,我想你的myarray格式是错误的。(它是:33,34,30,…55,49,48)我只是拼命地想让它工作。因为我使用的是Windows和python 3.5,而原始脚本是unix和python 2.7的,所以我一直在与数据类型作斗争。它可能是特定于版本的。
myarray
weight
定义和操作无关……那么卷积还有其他选择吗要知道,手动调用而不是调用函数更快,但这是C++编译。HMPF……如果我至少能理解它为什么这么慢。我需要一些输入:(首先,我不能复制无效的文字错误,<代码>权重。Asthype(int)< /C>对我来说没有问题(虽然我在Python 2.7上,但我怀疑这是原因)。第二,如果你把
weight
转换成整数,你只能得到0,因为你所有的数字都小于1。这真的是你想要的吗?如果你不能复制它,我想你的myarray格式是错误的。(它是:33,34,30,…55,49,48)我只是拼命地想让它工作。因为我使用的是Windows和python 3.5,而原始脚本是unix和python 2.7的,所以我一直在与数据类型作斗争。它可能是特定于版本的。
myarray
weight
定义和操作无关……那么卷积还有其他选择吗要知道,手动地调用它而不是调用函数更快,但那是C++编译。HMPF……如果我至少能理解它为什么这么慢。我需要一些输入:(我在这些程序中遇到的所有问题都很容易解决。我应该在问之前多想想。:DAll我在这些程序中遇到的问题很容易解决。我应该在问之前多想想。:D