Python “numpy”;空切片的平均值。”;警告 更新(真实错误)

Python “numpy”;空切片的平均值。”;警告 更新(真实错误),python,numpy,suppress-warnings,Python,Numpy,Suppress Warnings,我认错了错误的来源。这是我的全部功能(如果某些行不清楚且令人困惑,请抱歉…) 产出 left = 1333 right = 1490 20 from right = [ 0.14138737 0.14085886 0.14038289 0.14045525 0.14078836 0.14083192 0.14072289 0.14082283 0.14058594 0.13977806 0.13955595 0.13998236 0.1400764 0.139

我认错了错误的来源。这是我的全部功能(如果某些行不清楚且令人困惑,请抱歉…)

产出

left =  1333
right =  1490
20 from right =
[ 0.14138737  0.14085886  0.14038289  0.14045525  0.14078836  0.14083192
  0.14072289  0.14082283  0.14058594  0.13977806  0.13955595  0.13998236
  0.1400764   0.1399636   0.14025062  0.14074247  0.14094831  0.14078569
  0.14001536  0.13895717]
mean of 20 =  0.140395
Traceback (most recent call last):
...
  File "getRVs.py", line 201, in removeLines
    right_avg = numpy.mean(input[right:right+20])
  File "C:\Users\MyName\Anaconda\lib\site-packages\numpy\core\fromnumeric.py", line 2735, in mean
    out=out, keepdims=keepdims)
  File "C:\Users\MyName\Anaconda\lib\site-packages\numpy\core\_methods.py", line 59, in _mean
    warnings.warn("Mean of empty slice.", RuntimeWarning)
RuntimeWarning: Mean of empty slice.
当我打印它时,
numpy.mean
似乎运行正确,但当我将它指定给一个值时则不同。如有任何反馈,将不胜感激。感谢您抽出时间阅读我的问题


简要说明 简而言之,我正在编写一个代码来处理科学数据,其中一部分代码涉及取大约20个值的平均值

#left  = An index in the data (on the 'left' side)
#right = An index in the data (on the 'right' side)
#input = The data array

#Find the averages on the left and right sides
left_avg = numpy.mean(input[left-20:left])
right_avg = numpy.mean(input[right:right+20])
这段代码返回一个numpy“Mean of empty slice.”警告并恼人地将其打印到我宝贵的输出中!例如,我决定尝试追踪警告的来源,因此

import warnings
warnings.simplefilter("error")
在我的代码顶部,它返回以下截取的回溯:

  File "getRVs.py", line 201, in removeLines
    right_avg = numpy.mean(input[right:right+20])
  File "C:\Users\MyName\Anaconda\lib\site-packages\numpy\core\fromnumeric.py", line 2735, in mean
    out=out, keepdims=keepdims)
  File "C:\Users\MyName\Anaconda\lib\site-packages\numpy\core\_methods.py", line 59, in _mean
    warnings.warn("Mean of empty slice.", RuntimeWarning)
RuntimeWarning: Mean of empty slice.
我省略了大约2/3的回溯,因为它涉及到大约5个难以解释的函数,这些函数不会影响数据的可读性或大小


所以我决定把整个操作打印出来,看看
right\u avg
是否真的在尝试一个空切片的
numpy.mean
。。。这就是事情变得非常奇怪的时候。

我无法重现你的错误。您使用的是最新的numpy版本吗? 但是,您可以通过使用关键字ignore来抑制警告(请参阅)

此错误通常意味着向函数传递了一个空列表

>>> a = []

>>> import numpy
>>> numpy.mean(a)
/shahlab/pipelines/apps_centos6/Python-2.7.10/lib/python2.7/site-packages/numpy/core/_methods.py:59: RuntimeWarning: Mean of empty slice.
  warnings.warn("Mean of empty slice.", RuntimeWarning)
/shahlab/pipelines/apps_centos6/Python-2.7.10/lib/python2.7/site-packages/numpy/core/_methods.py:71: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
nan
>>> print numpy.mean(a)
nan

>>> import warnings
>>> warnings.simplefilter("ignore")
>>> numpy.mean(a)
nan

>>> a=[ 0.14138737, 0.14085886, 0.14038289, 0.14045525, 0.14078836, 0.14083192, 0.14072289, 0.14082283, 0.14058594, 0.13977806, 0.13955595, 0.13998236, 0.1400764,  0.1399636,  0.14025062, 0.14074247, 0.14094831, 0.14078569, 0.14001536, 0.13895717]
>>> numpy.mean(a)
0.140394615
>>> x = numpy.mean(a)
>>> print x
0.140394615
>>> numpy.__version__
'1.9.2'

希望有帮助

你这个混蛋!答案很明显,不是吗??您显然错误识别了错误所在的代码行。您需要做的是为特定情况编写代码,在这种情况下,数据中考虑的中心点周围的窗口(
左侧
右侧
侧)太靠近数据数组的边缘

def removeLines(input,CRVAL1,CDELT1): #Masks out the Balmer lines from the spectrum

    for i in range(0,len(lineWindows),2):
        left = toIndex(lineWindows[i],CRVAL1,CDELT1)
        right = toIndex(lineWindows[i+1],CRVAL1,CDELT1)

        #Find the averages on the left and right sides
        left_avg = numpy.mean(input[left-20:left])
        right_avg = numpy.mean(input[right:right+20])

        #Find the slope between the averages
        slope = (left_avg - right_avg)/(left - right)

        #Find the y-intercept of the line conjoining the averages
        bval = ((left_avg - slope*left) + (right_avg - slope*right)) / 2

        for j in range(left,right):     #Redefine the data to follow the line conjoining
            input[j] = slope*j + bval   #the sides of the peaks

    left = 0
    left_avg = int(input[0])

    if toIndex(lineWindows[0],CRVAL1,CDELT1) < 0: right = 0
    else: right = toIndex(lineWindows[0],CRVAL1,CDELT1)

    right_avg = numpy.mean(input[right:right+20])
    slope = (left_avg - right_avg)/(left - right)
    bval = ((left_avg - slope*left) + (right_avg - slope*right)) / 2

    for i in range(left, right):
        input[i] = slope*i + bval
    return input
对此

if toIndex(lineWindows[0],CRVAL1,CDELT1) < 0: right = 0    #Index 0, much better!
else: right = toIndex(lineWindows[0],CRVAL1,CDELT1)    #Leave it alone if it isn't a problem.

right_avg = numpy.mean(input[right:right+20])
如果toIndex(lineWindows[0],CRVAL1,CDELT1)<0:right=0#索引0,则更好!
else:right=toIndex(lineWindows[0],CRVAL1,CDELT1)#如果没有问题,就别管它。
右平均值=numpy.平均值(输入[右:右+20])

另外,关于
left=int(输入[0])
,您完全是错误的,所以我为您将其更改为
left=0
。谁知道这个草率、草率的代码还会产生什么简单的错误呢?在发布到堆栈溢出之前,请仔细查看

错误是第二次调用
numpy.mean(input[right:right+20])
input[right:right+20]
为空。第一次和第二次调用之间必须有代码,它正在更改
input
的值。正如您所说,
input
的值正在更改,但在调用之前打印相同的代码时没有错误。你认为我分配左平均值的方式有问题吗?你是对的!稍后在处理
right\u avg
的同一函数中有一部分代码!我已经更新了主线程,以避免浪费人们的时间。多么尴尬啊……检查
right=toIndex的值(lineWindows[0],CRVAL1,CDELT1)
。如果太大,则切片
输入[right:right+20]
可能为空。可能重复感谢您的回答!一开始我确实试着压制这些警告,但后来我的脑子里冒出了一点危险。我在这里处理的是科学数据,这意味着如果我的代码出于未知原因处理一个空列表,那将是一件大事。当我进去查看时,列表并不像numpy告诉我的那样是空的!所以现在我想弄明白numpy为什么要这么做。我也在使用numpy版本1.9.2。这很奇怪。代码不会更改“输入”列表。是否可以发布可以重现此错误的代码(可能使用较小的输入列表)?我将尝试使用一些虚拟数据,看看是否可以得到相同的错误。我刚刚更新了主要帖子。我完全搞错了,把错误的代码行识别为错误。我现在看到的是关于为什么会发生这种警告的更为合理的解释。很抱歉
def removeLines(input,CRVAL1,CDELT1): #Masks out the Balmer lines from the spectrum

    for i in range(0,len(lineWindows),2):
        left = toIndex(lineWindows[i],CRVAL1,CDELT1)
        right = toIndex(lineWindows[i+1],CRVAL1,CDELT1)

        #Find the averages on the left and right sides
        left_avg = numpy.mean(input[left-20:left])
        right_avg = numpy.mean(input[right:right+20])

        #Find the slope between the averages
        slope = (left_avg - right_avg)/(left - right)

        #Find the y-intercept of the line conjoining the averages
        bval = ((left_avg - slope*left) + (right_avg - slope*right)) / 2

        for j in range(left,right):     #Redefine the data to follow the line conjoining
            input[j] = slope*j + bval   #the sides of the peaks

    left = 0
    left_avg = int(input[0])

    if toIndex(lineWindows[0],CRVAL1,CDELT1) < 0: right = 0
    else: right = toIndex(lineWindows[0],CRVAL1,CDELT1)

    right_avg = numpy.mean(input[right:right+20])
    slope = (left_avg - right_avg)/(left - right)
    bval = ((left_avg - slope*left) + (right_avg - slope*right)) / 2

    for i in range(left, right):
        input[i] = slope*i + bval
    return input
right = toIndex(lineWindows[0],CRVAL1,CDELT1)    #Error occurs where right = -10
right_avg = numpy.mean(input[right:right+20])    #Index of -10? Yeah, right.
if toIndex(lineWindows[0],CRVAL1,CDELT1) < 0: right = 0    #Index 0, much better!
else: right = toIndex(lineWindows[0],CRVAL1,CDELT1)    #Leave it alone if it isn't a problem.

right_avg = numpy.mean(input[right:right+20])