首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >熊猫数据: ValueError: num必须是1 <= num <= 0,而不是1

熊猫数据: ValueError: num必须是1 <= num <= 0,而不是1
EN

Stack Overflow用户
提问于 2016-08-27 11:56:01
回答 2查看 22.7K关注 0票数 15

当我试图绘制一个pandas dataframe时,我得到了以下错误

ValueError: num必须是1 <= num <= 0,而不是1

代码:

代码语言:javascript
运行
复制
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中)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-27 13:01:30

所以,我很肯定您的问题与数组train_x的格式有关,我尝试了一个10,000行和6 cols数组的程序,它工作得很好,所以问题不是大小问题。由于某种原因,len(x_train)len(x_train[0])中的一个是0。我之所以这么想是因为:

您所获得的ValueError来自matplotlib.axes._subplot模块,该模块处理在一个大图中绘制许多小的子图(因此每个小直方图)。该模块的代码如下:

代码语言:javascript
运行
复制
""" 
*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))

您的问题在本部分(请参阅代码中的注释中的说明):

代码语言:javascript
运行
复制
    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设置为此,则工作正常:

代码语言:javascript
运行
复制
    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]]

在调用问题中的代码之前,尝试这样做,看看这是否有效:

代码语言:javascript
运行
复制
x_train = list([list(x) for x in x_train])
票数 3
EN

Stack Overflow用户

发布于 2017-01-31 15:38:26

我也遇到了同样的问题,我发现这是因为NumPy数组是一个对象数组,而不是一个浮点数组。

试试这个:

代码语言:javascript
运行
复制
x_train = x_train.astype(np.float)
票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39180873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档