- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我错误地识别了错误的来源。这是我的完整功能(抱歉,如果某些行晦涩难懂...)
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
我调查了这个问题并找到了答案,答案发布在下面(不在这篇文章中)。
#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])
产生输出
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 的“空切片的平均值”。警告并烦人地将其打印在我宝贵的输出中!我决定尝试追踪警告的来源,如 here 所示,例如,所以我放置了
import warnings
warnings.simplefilter("error")
在我的代码顶部,然后返回以下片段的 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.
我省略了大约 2/3 的 Traceback,因为它通过了大约 5 个难以解释的函数,这些函数不影响数据的可读性或大小。
所以我决定打印出整个操作,看看 right_avg
是否真的在尝试空切片的 numpy.mean
... em>真的很奇怪。
最佳答案
我无法重现您的错误。您使用的是最新的 numpy 版本吗?但是,您可以通过使用关键字 ignore 来抑制警告(请参阅 https://docs.python.org/2/library/warnings.html#temporarily-suppressing-warnings)
此错误通常意味着向函数传递了一个空列表。
>>> 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'
希望对您有所帮助。
关于python - numpy "Mean of empty slice."警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31814837/
入门教程使用内置的梯度下降优化器非常有意义。但是,k均值不仅可以插入梯度下降中。似乎我不得不编写自己的优化程序,但是鉴于TensorFlow原语,我不确定如何执行此操作。 我应该采取什么方法? 最佳答
我想知道 K-Mean 和 K-Means++ 算法之间的区别。如果有人了解 K-Means++ 算法的流程,您能举例说明一下吗?虽然,我了解 K-Mean 算法,但发现如何实现 K-Means++
我有不同的数据帧均值计算值。通常,我想它们应该是一样的。或者有什么区别: daily1 = daily_above_zero['2011-2'].mean() daily1 Out[181]: P_S
我有关于人们每周上类旅行次数的数据。随着行程的距离,我对两个变量之间的关系感兴趣。 (预计频率会随着距离的增加而下降,本质上是一种负相关。)Cor.test 支持这个假设:-0.08993444,p
我了解 k-means 算法步骤。 但是我不确定该算法是否会始终收敛?或者观察总是可以从一个质心切换到另一个质心? 最佳答案 该算法总是收敛(按定义)但 不一定是全局最优 . 算法可能会从质心切换到质
(添加了可重现的示例。) 我对 rnorm 函数有点困惑。 我期待 mean(rnorm(100,mean=0,sd=1))为0;和 sd(rnorm(100,mean=0,sd=1))为 1。但给出
我想计算一个平均值。这是带有示例数据的代码: # sample data Nr <- c(1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
我有一个像这样的数据框: Id F M R 7 1 286 907 12 1 286 907 17 1 186 1271 21 1 296 905 30 1
如果我们将 K-means 和顺序 K-means 方法应用于具有相同初始设置的相同数据集,我们会得到相同的结果吗?解释你的理由。 个人认为答案是否定的,顺序K-means得到的结果取决于数据点的呈现
我想使用 MEAN JavaScript 堆栈,但我注意到有两个不同的堆栈,它们有自己的网站和安装方法:mean.js 和 mean.io。所以我开始问自己这个问题:“我用哪一个?”。 所以为了回答这
似乎有多种方法可以安装 Mean Stack (mean.io) 的所有模块。但是,在 c9.io 中执行此操作的最佳方法是什么?我一直在尝试很多事情,但我似乎并没有全部掌握。 c9.io 有专门的
在开发过程中,我希望加载原始(未聚合).js 文件。 Mean.io 文档说: All javascript within public is automatically aggregated wit
我正在尝试添加 angular-material到 mean.io应用。 在我的自定义包中,我使用 bower 来安装 angular-material,现在我有一个 .../public/asset
我只运行以下三行: df = pd.read_hdf('data.h5') print(df.mean()) print(df['derived_3'].mean()) 第一个 print 列出了每一
k-means++算法有助于原始k-means算法的以下两点: 原始的 k-means 算法在输入大小的 super 多项式的最坏情况下运行时间,而 k-means++ 声称是 O(log k)。 与
这两个字段有什么区别? : 每个请求的时间(平均) 每个请求的时间(平均,跨所有并发请求) 它们每个是如何计算的? 示例输出: Time per request: 3953.446 [ms
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 7年前关闭。 Improve this qu
我想看看是否可以根据它们所处理的目标函数来比较两者的性能? 最佳答案 顺便说一句,Fuzzy-C-Means (FCM) 聚类算法也称为Soft K-Means。 目标函数实际上是相同的,唯一的区别是
虽然我看到了很多与此相关的问题,但我并没有真正得到答案,可能是因为我是使用 nltk 集群的新手。我确实需要对聚类新手进行基本解释,特别是关于 NLTK K 均值聚类的向量表示以及如何使用它。我有一个
我在学习mean.io来自 this tutorial video ,它显示了示例包(由 mean package mymodule 创建。它也在 docs 的“包”下进行了描述)。我想帮助了解给定的
我是一名优秀的程序员,十分优秀!