Pandas 数据框:ValueError: num must be 1 <= num <= 0, not 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39180873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas dataframe: ValueError: num must be 1 <= num <= 0, not 1
提问by KostasRim
I am getting the following error while I am trying to plot a pandas dataframe
:
尝试绘制 a 时出现以下错误pandas dataframe
:
ValueError: num must be 1 <= num <= 0, not 1
ValueError:num 必须是 1 <= num <= 0,而不是 1
Code:
代码:
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()
I have tried to read the file again from the csv
and I am getting the exact same error.
我试图从 再次读取文件,但csv
遇到了完全相同的错误。
Edit:
编辑:
print x_train
output:
print 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]]
[[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]]
Edit2:
编辑2:
Complete list of errors(Traceback):
完整的错误列表(回溯):
Traceback (most recent call last):
File "temp.py", line 104, in custom.dropna().hist()
File "/home/kostas/anaconda2/lib/python2.7/site-packages/pandas/tools/plotting.py", line 2893, in hist_frame layout=layout)
File "/home/kostas/anaconda2/lib/python2.7/site-packages/pandas/tools/plotting.py", line 3380, in _subplots ax0 = fig.add_subplot(nrows, ncols, 1, **subplot_kw)
File "/home/kostas/anaconda2/lib/python2.7/site-packages/matplotlib/figure.py", line 1005, in add_subplot a = subplot_class_factory(projection_class)(self, *args, **kwargs)
File "/home/kostas/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_subplots.py", line 64, in initmaxn=rows*cols, num=num))
回溯(最近一次调用最后一次):
文件“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(nrows, 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行,在initmaxn=rows*cols, num=num))
采纳答案by gowrath
So I'm pretty sure your issue is something to do with the format of the array train_x. I tried your program with an array of 10,000 rows and 6 cols and it worked fine so the issue is not size. For some reason, one of len(x_train)
or len(x_train[0])
is 0. What makes me think this is thus:
所以我很确定你的问题与数组 train_x 的格式有关。我用 10,000 行和 6 列的数组尝试了你的程序,它运行良好,所以问题不是大小。出于某种原因,其中之一len(x_train)
或len(x_train[0])
为 0。是什么让我认为这是这样的:
The ValueError you are getting is from the matplotlib.axes._subplot module which deals with drawing many small subplots within a big plot (so each small histogram). The code of the module is this:
你得到的 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))
Your issue is in this part (see explanation in comments in code):
您的问题在这一部分(请参阅代码注释中的解释):
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))
I don't know how you are reading your input format, but I'm pretty sure the problem is related to it. If you set x_train to this it works fine:
我不知道您是如何阅读输入格式的,但我很确定问题与此有关。如果您将 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]]
Try doing this before calling the code in your question and see if that works:
在调用问题中的代码之前尝试这样做,看看是否有效:
x_train = list([list(x) for x in x_train])
回答by MiniQuark
I had the same problem, and I found that this was due to the fact that the NumPy array was an object array rather than a float array.
我遇到了同样的问题,我发现这是因为 NumPy 数组是一个对象数组而不是一个浮点数组。
Try this:
尝试这个:
x_train = x_train.astype(np.float)