I'm trying to use Numpy with Python to compute a histogram with automatic bin size. My reading of the documentation says that I should be able to pass bins="auto"
, but when I do so, I get an error:
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
I get the same result with any string parameter for bins
, while it works as expected if I supply any integer for the parameter. What am I doing wrong, and how do I get automatic bin size calculation?
答案 0 :(得分:0)
The problem was PyCharm using Python 2.7 (as seen by sys.version
, which I hadn't noticed until I added it to the details for the question). When I switched PyCharm to use 3.5, it works as expected.
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.]))
答案 1 :(得分:0)
问题是你正在使用的numpy版本。自动功能是在numpy版本1.11.0中引入的。 也许版本标签混淆为1.8.0&lt; 1.11.0因为11必须被读为elven而不是一分。