首页 文章

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

提问于
浏览
9

我正在尝试绘制 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] [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:

完整的错误列表(Traceback):

Traceback(最近一次调用最后一次):文件“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行,在init maxn = rows * cols ,num = num))

2 回答

  • 8

    所以我很确定你的问题与数组train_x的格式有关 . 我用10,000行和6列的数组尝试了你的程序,它工作正常,所以问题不是大小 . 出于某种原因, 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设置为this它可以正常工作:

    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])
    
  • 1

    我有同样的问题,我发现这是因为NumPy数组是一个对象数组而不是一个浮点数组 .

    试试这个:

    x_train = x_train.astype(np.float)
    

相关问题