ValueError: Bin labels must be one fewer than the number of bin edges

Posted Data+Science+Insight

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ValueError: Bin labels must be one fewer than the number of bin edges相关的知识,希望对你有一定的参考价值。

ValueError: Bin labels must be one fewer than the number of bin edges

目录

ValueError: Bin labels must be one fewer than the number of bin edges

解决:

问题:

完整错误:


解决:

5个分位数可以把数据序列分为4个区间,那么给了5个标签当然有问题了

test = np.array([1, 7, 5, 4, 6, 3,20,10,0,0,4,9,30,28,16,4,5,3,7])

percentiles = np.percentile(test, [0, 25, 50, 75,100])

percentiles = list(percentiles)

percentiles

pd.cut(test,bins = percentiles, labels=["suck", "bad", "medium", "good", "awesome"])

问题:

test = np.array([1, 7, 5, 4, 6, 3,20,10,0,0,4,9,30,28,16,4,5,3,7])

percentiles = np.percentile(test, [0, 25, 50, 75,100])

percentiles = list(percentiles)

percentiles

pd.cut(test,bins = percentiles, labels=["bad", "medium", "good", "awesome"])

 [0.0, 3.5, 5.0, 9.5, 30.0]

['bad', 'good', 'medium', 'medium', 'good', ..., 'awesome', 'medium', 'medium', 'bad', 'good']
Length: 19
Categories (4, object): ['bad' < 'medium' < 'good' < 'awesome']

完整错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-222-d05677cd894a> in <module>
----> 1 pd.cut(test,bins = percentiles, labels=["suck", "bad", "medium", "good", "awesome"])

D:\\anaconda\\lib\\site-packages\\pandas\\core\\reshape\\tile.py in cut(x, bins, right, labels, retbins, precision, include_lowest, duplicates, ordered)
    280         dtype=dtype,
    281         duplicates=duplicates,
--> 282         ordered=ordered,
    283     )
    284 

D:\\anaconda\\lib\\site-packages\\pandas\\core\\reshape\\tile.py in _bins_to_cuts(x, bins, right, labels, precision, include_lowest, dtype, duplicates, ordered)
    431             if len(labels) != len(bins) - 1:
    432                 raise ValueError(
--> 433                     "Bin labels must be one fewer than the number of bin edges"
    434                 )
    435         if not is_categorical_dtype(labels):

ValueError: Bin labels must be one fewer than the number of bin edges

以上是关于ValueError: Bin labels must be one fewer than the number of bin edges的主要内容,如果未能解决你的问题,请参考以下文章

Pandas 返回:ValueError: Unknown label type: 'continuous'

ValueError: Unknown label type: ‘continuous‘

报错与解决 | ValueError: pos_label=‘pos‘ is not a valid label

ValueError: 标签形状必须为 [batch_size, labels_dimension],得到 (128, 2)

ValueError: len(index) != len(labels) for groupby pandas

sklearn随机森林模型:ValueError: Unknown label type: ‘unknown‘