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

Kos*_*Rim 13 python histogram pandas

我在尝试绘制一个时遇到以下错误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()
Run Code Online (Sandbox Code Playgroud)

我试图再次从该文件读取文件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(最近一次调用最后一次):

在custom.dropna()文件"temp.py",第104行.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))

Min*_*ark 15

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

试试这个:

x_train = x_train.astype(np.float)
Run Code Online (Sandbox Code Playgroud)


gow*_*ath 3

所以我很确定你的问题与数组 train_x 的格式有关。我用 10,000 行和 6 列的数组尝试了你的程序,它运行良好,所以问题不是大小。由于某种原因, or 之一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))
Run Code Online (Sandbox Code Playgroud)

您的问题在这一部分(请参阅代码注释中的解释):

    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))
Run Code Online (Sandbox Code Playgroud)

我不知道你如何阅读你的输入格式,但我很确定问题与它有关。如果你将 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]]
Run Code Online (Sandbox Code Playgroud)

在调用问题中的代码之前尝试执行此操作,看看是否有效:

x_train = list([list(x) for x in x_train])
Run Code Online (Sandbox Code Playgroud)