尝试绘制图时返回索引器:元组索引超出范围

2024-04-19 08:15:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着做一些图来了解算法返回坏值的地方,所以我试着用一个箱子过滤这些值,得到某种平均值,然后得到平均值,用平均值做一个图。你知道吗

x(以及所有形式的)是一个数据帧,四舍五入是为了得到更好看的bins(不会对数据产生太大的影响,因为binned的值非常大)

x_tr_g = x_stat_train_good.copy()
x_te_g = x_stat_test_good.copy()
x_tr_b = x_stat_train_bad.copy()
x_te_b = x_stat_test_bad.copy()
x_tr = x_stat_train.copy()
x_te = x_stat_test.copy()
labels = ('train actual',     'test_actual','test_good','test_bad','train_good','train_bad')
train_labels = ('train actual','train_good','train_bad')
test_labels = ('test_actual','test_good','test_bad')
x_trains = (x_tr,x_tr_g,x_tr_b)
x_tests = (x_te,x_te_g,x_te_b)
def roundup(x):
    return int(math.ceil(x / 10.0)) * 10

all_x = (x_tr, x_te, x_tr_g, x_te_g, x_tr_b, x_te_b)
for x in all_x:
    bins =np.linspace(x['price'].min(), x['price'].max(), 50)
    for i in range(0, len(bins)):
        bins[i] = roundup(bins[i])
    x['bin'] = pd.cut(x['price'], bins=bins, labels=bins[1:], precision=0)

for strname in numericals:
    temp = x[x[strname].notnull()]
    x[strname] = temp[strname].astype(int)

for strname in numericals:
    for x,label in zip(x_tests, test_labels):
        df = x[['bin',strname]].groupby('bin').mean()
        df = df.dropna()
        plt.plot(test) #Error occurs here
    plt.xlabel(strname)
    plt.ylabel('price')
    plt.legend(loc=2)
    plt.show()

以及全部错误:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-6-9d00df0dee51> in <module>
      3         df = x[['bin',strname]].groupby('bin').mean()
      4         df = df.dropna()
----> 5         plt.plot(df)
      6     plt.xlabel(strname)
      7     plt.ylabel('price')

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/pyplot.py in plot(scalex, scaley, data, *args, **kwargs)
   2793     return gca().plot(
   2794         *args, scalex=scalex, scaley=scaley, **({"data": data} if data
-> 2795         is not None else {}), **kwargs)
   2796 
   2797 

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/axes/_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
   1664         """
   1665         kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D._alias_map)
-> 1666         lines = [*self._get_lines(*args, data=data, **kwargs)]
   1667         for line in lines:
   1668             self.add_line(line)

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/axes/_base.py in __call__(self, *args, **kwargs)
    223                 this += args[0],
    224                 args = args[1:]
--> 225             yield from self._plot_args(this, kwargs)
    226 
    227     def get_next_color(self):

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs)
    397             func = self._makefill
    398 
--> 399         ncx, ncy = x.shape[1], y.shape[1]
    400         if ncx > 1 and ncy > 1 and ncx != ncy:
    401             cbook.warn_deprecated(

索引器错误:元组索引超出范围

编辑:添加了错误,希望有助于得到答案


Tags: intestselfdfdataplotargstrain