当我试图绘制一个pandas dataframe
时,我得到了以下错误
ValueError: num必须是1 <= num <= 0,而不是1
代码:
import matplotlib.pyplot as plt
names = ['buying', 'maint', 'doors', 'persons', 'lug_boot', 'safety']
custom = pd.DataFrame(x_train) //only a portion of the csv
custom.columns = names
custom.hist()
plt.show()
我已经尝试从csv
再次读取文件,并且得到了完全相同的错误。
编辑:
print x_train
输出:
[0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 3.0 3.0 3.0]
Edit2:
错误的完整列表(回溯):
回溯(最近一次调用): 文件"temp.py",第104行,custom.dropna().hist() 文件"/home/kostas/anaconda2/lib/python2.7/site-packages/pandas/tools/plotting.py",第2893行,在hist_frame layout=layout中) 文件"/home/kostas/anaconda2/lib/python2.7/site-packages/pandas/tools/plotting.py",第3380行,在_subplots ax0 =fig.add_subplot(nrow,ncols,1,**subplot_kw)中 文件"/home/kostas/anaconda2/lib/python2.7/site-packages/matplotlib/figure.py",第1005行,在add_subplot a= subplot_class_factory(projection_class)(self,*args,**kwargs)中 文件"/home/kostas/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_subplots.py",第64行,在init maxn=rows*cols,num=num中)
发布于 2016-08-27 13:01:30
所以,我很肯定您的问题与数组train_x的格式有关,我尝试了一个10,000行和6 cols数组的程序,它工作得很好,所以问题不是大小问题。由于某种原因,len(x_train)
或len(x_train[0])
中的一个是0。我之所以这么想是因为:
您所获得的ValueError来自matplotlib.axes._subplot模块,该模块处理在一个大图中绘制许多小的子图(因此每个小直方图)。该模块的代码如下:
"""
*rows*, *cols*, *num* are arguments where
the array of subplots in the figure has dimensions *rows*,
*cols*, and where *num* is the number of the subplot
being created. *num* starts at 1 in the upper left
corner and increases to the right.
"""
rows, cols, num = args
rows = int(rows)
cols = int(cols)
if isinstance(num, tuple) and len(num) == 2:
num = [int(n) for n in num]
self._subplotspec = GridSpec(rows, cols)[num[0] - 1:num[1]]
else:
if num < 1 or num > rows*cols:
raise ValueError(
"num must be 1 <= num <= {maxn}, not {num}".format(
maxn=rows*cols, num=num))
您的问题在本部分(请参阅代码中的注释中的说明):
if num < 1 or num > rows*cols:
# maxN is the number of rows*cols and since this is showing 0 for you (in your error stacktrace),
# it means the number of cols being passed into your histogram is 0. Don't know why though :P
raise ValueError(
"num must be 1 <= num <= {maxn}, not {num}".format(
maxn=rows*cols, num=num))
我不知道你是如何阅读你的输入格式,但我很确定问题是相关的。如果将x_train设置为此,则工作正常:
x_train = [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[1.0, 1.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.3333333333333333, 0.3333333333333333, 2.0, 2.0, 2.0, 2.0],
[0.0, 0.0, 3.0, 3.0, 3.0, 3.0]]
在调用问题中的代码之前,尝试这样做,看看这是否有效:
x_train = list([list(x) for x in x_train])
发布于 2017-01-31 15:38:26
我也遇到了同样的问题,我发现这是因为NumPy数组是一个对象数组,而不是一个浮点数组。
试试这个:
x_train = x_train.astype(np.float)
https://stackoverflow.com/questions/39180873
复制相似问题