2016-11-12 25 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必須被讀作精靈而不是一點。

相關問題