2016-11-12 45 views
2

我想用Python的Numpy来计算自动bin大小的直方图。我的documentation读说我应该能够通过bins="auto",但是当我这样做,我得到一个错误:“列表分配索引超出范围”与numpy.histogram

import sys 
import numpy as np 

print(sys.version) 
# 2.7.10 (default, Oct 23 2015, 19:19:21) 
# [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] 

print(np.version.version) 
# 1.8.0rc1 

print(np.histogram([1, 2, 3, 4], bins='auto')) 
# Traceback (most recent call last): 
# File "/Users/phrogz/Code/histopy/histo.py", line 11, in <module> 
#  print(np.histogram([1, 2, 3, 4], bins='auto')) 
# File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/function_base.py", line 183, in histogram 
#  if (np.diff(bins) < 0).any(): 
# File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/function_base.py", line 991, in diff 
#  slice1[axis] = slice(1, None) 
# IndexError: list assignment index out of range# 
# 
# Process finished with exit code 1 

我得到相同的结果与bins任何字符串参数,而它按预期工作如果我提供参数的任何整数。我做错了什么,以及如何获得自动纸盒尺寸计算?

+1

既然你得到了回溯,你可以调试。看起来像自动计算的“箱子”碰巧超出范围,但我不知道为什么。 –

回答

0

问题是PyCharm使用Python 2.7(如sys.version所示,在我将其添加到问题的详细信息中之前我没有注意到)。当我打开PyCharm使用3.5时,它按预期工作。

import sys 
import numpy as np 

print(sys.version) 
# 3.5.2 (default, Oct 11 2016, 05:05:28) 
# [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] 

print(np.version.version) 
# 1.11.2 

print(np.histogram([1, 2, 3, 4], bins='auto')) 
# (array([1, 1, 2]), array([ 1., 2., 3., 4.])) 
+0

这并不能真正显示错误的根本原因。是从3.5安装使用'numpy'还是什么? –

+0

@ivan_pozdeev不知道;我对Python很陌生(还有PyCharm和pip3)。我编辑了答案,表明当我切换解释器时,我得到了不同版本的numpy;也许这是一个古老的numpy? – Phrogz

+2

根据https://docs.scipy.org/doc/numpy-1.8.0/reference/generated/numpy.histogram.html#numpy.histogram,似乎在'1.8.0'中,'直方图'具有' bins = 10',不接受'“auto”'。我认为它会引发一个例外,但显然它并没有。也许这是因为它是'rc'。如果你在调试器下运行,你应该在故障线路上看到'10'。或者,''auto“'可能被误解为”标量序列“。 –

0

问题是您正在使用的numpy版本。自动功能是在numpy版本1.11.0中引入的。 也许版本标签混淆为1.8.0 < 1.11.0,因为11必须被读作精灵而不是一点。