Python numpy “空切片的平均值。” 警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31814837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
numpy "Mean of empty slice." warning
提问by boof
UPDATE (The realerror)
更新(真正的错误)
I misidentified where the error was coming from. Here is my function in its entirety (sorry if some of the lines are obscure and confusing...)
我错误地识别了错误的来源。这是我的全部功能(对不起,如果某些行是晦涩难懂的......)
def removeLines(input,CRVAL1,CDELT1): #Masks out the Balmer lines from the spectrum
#Numbers 4060, 4150, 4300, 4375, 4800, and 4950 obtained from fit_RVs.pro.
#Other numbers obtained from the Balmer absorption series lines
for i in range(0,len(lineWindows),2):
left = toIndex(lineWindows[i],CRVAL1,CDELT1)
right = toIndex(lineWindows[i+1],CRVAL1,CDELT1)
print "left = ", left
print "right = ", right
print "20 from right =\n", input[right:right+20]
print "mean of 20 = ", numpy.mean(input[right:right+20])
#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]) #<--- NOT here
print "right_avg = ", right_avg
#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 = int(input[0])
left_avg = int(input[0])
right = toIndex(lineWindows[0],CRVAL1,CDELT1)
right_avg = numpy.mean(input[right:right+20]) #<---- THIS IS WHERE IT IS!
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
I have investigated the issue and found the answer, which is posted below (not in this post).
我调查了这个问题并找到了答案,答案在下面(不在这篇文章中)。
The error (The silly fakeerror)
错误(愚蠢的假错误)
#left = An index in the data (on the 'left' side)
#right = An index in the data (on the 'right' side)
#input = The data array
print "left = ", left
print "right = ", right
print "20 from right =\n", input[right:right+20]
print "mean of 20 = ", numpy.mean(input[right:right+20])
#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])
produced the output
产生了输出
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.
It would appear that numpy.mean
runs correctly when I print it, but differently when I assign it to a value. Any feedback would be very appreciated. Thank you for taking the time to read my question.
numpy.mean
当我打印它时它看起来运行正确,但当我将它分配给一个值时会有所不同。任何反馈将不胜感激。感谢您花时间阅读我的问题。
Brief explanation
简要说明
In short, I am writing a code to handle scientific data and part of the code involves taking the mean of about 20 values.
简而言之,我正在编写一个代码来处理科学数据,部分代码涉及取大约 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])
This code returns a numpy "Mean of empty slice." warning and annoyingly prints it in my precious output! I decided to try and track down the source of the warning as seen here, for example, so I placed
此代码返回一个 numpy“空切片的平均值”。警告并烦人地将其打印在我宝贵的输出中!例如,我决定尝试追踪此处所见警告的来源,因此我放置了
import warnings
warnings.simplefilter("error")
at the top of my code, which then returned the following snipped Traceback:
在我的代码顶部,然后返回以下截断的回溯:
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.
I omitted about 2/3 of the Traceback because it moves through about 5 difficult-to-explain functions that do not affect the readability or size of the data.
我省略了大约 2/3 的回溯,因为它通过了大约 5 个难以解释的函数,这些函数不会影响数据的可读性或大小。
So I decided to print out the whole operation to see if right_avg
really was attempting a numpy.mean
of an empty slice... And that's when things got really weird.
所以我决定打印出整个操作,看看是否right_avg
真的在尝试一个numpy.mean
空切片......这就是事情变得非常奇怪的时候。
采纳答案by boof
You dingbat! The answer is obvious, is it not?? You clearly misidentified which line of code your error sat on. What you need to do is write in code for the specific case in which the window (left
and right
sides) around the center-point being considered in the data is too close to the edge of the data array.
你个混蛋!答案显而易见,不是吗??您显然错误地识别了您的错误所在的代码行。您需要做的是针对特定情况编写代码,其中数据中考虑的中心点周围的窗口(left
和right
边)太靠近数据数组的边缘。
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
Simply change this
简单地改变这个
right = toIndex(lineWindows[0],CRVAL1,CDELT1) #Error occurs where right = -10
right_avg = numpy.mean(input[right:right+20]) #Index of -10? Yeah, right.
to this
对此
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])
Also, you were totallywrong about left = int(input[0])
, so I changed it to left = 0
for you. Who knowswhat other simple errors this sloppy, sloppy code yields? Look a little harder before posting to Stack Overflow, please!
另外,你完全错了left = int(input[0])
,所以我把它改成left = 0
了你。谁知道这个草率的、草率的代码会产生哪些其他简单的错误?在发布到 Stack Overflow 之前,请仔细看一下,拜托!
回答by Diljot
I couldn't reproduce your error. Are you using the latest numpy version? However you could suppress the warnings by uding the keyword ignore( see https://docs.python.org/2/library/warnings.html#temporarily-suppressing-warnings)
我无法重现您的错误。您使用的是最新的 numpy 版本吗?但是,您可以通过使用关键字 ignore 来抑制警告(请参阅https://docs.python.org/2/library/warnings.html#temporously-suppressing-warnings)
This error normally means that an empty list was passed to the function.
此错误通常意味着向函数传递了一个空列表。
>>> 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'
Hope that helps.
希望有帮助。