我是靠谱客的博主 文艺宝贝,这篇文章主要介绍sklearn随机森林模型:ValueError: Unknown label type: ‘unknown‘sklearn随机森林模型:ValueError: Unknown label type: 'unknown',现在分享给大家,希望可以做个参考。

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

目录

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

问题:

解决:

完整错误:


问题:

分类的标签数据被客户错误地标注为了浮点型;

让学生去做出现了问题,有问题的程序如下;

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def random_forest_selection(df): clf = RandomForestClassifier(n_estimators=100, random_state=42, class_weight = 'balanced', min_samples_leaf = 4, min_samples_split = 4, max_depth = 5, n_jobs = -1,) clf_X = df.drop(['label'], axis=1) clf_y = df.label model = clf.fit(clf_X,clf_y) #feat_importances = pd.DataFrame(model.feature_importances_, index=clf_X.columns, columns=["importance"]) feature_importances = pd.DataFrame({'feature': clf_X.columns, 'importance': model.feature_importances_}) feature_importances.sort_values(by='importance', ascending=False, inplace=True) #feat_importances[:10].plot(figsize(12,5),kind='bar',) plot_feature_importances(feature_importances, n = 10, threshold = 0.95) most = min(50,df.shape[1]//5) return feature_importances.feature[0:100].values my_important = random_forest_selection(df_in) my_important

解决:

初始数据类型为float64

转换时,正确的书写格式为Int64

这样转换可以转换过去,但是在进入模型的时候会发生问题,

所以,终极的正确的的处理方式是:

缺失值填充后进行数据格式转换;且使用小写int64

df_in = df_in.fillna(0)
df_in['label'] = df_in['label'].astype('int64')

复制代码
1
2
3
4
5
6
7
df_in = df_origin df_in['label'] = df_in['label'].astype("int") #'Int64' #df_in['label'] = df_in['label'].astype("Int64") df_in['label'].value_counts() # df_in['label'].describe()

完整错误:


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-41-511d3bf43def> in <module>
----> 1 my_important = random_forest_selection(df_in)
      2 my_important

<ipython-input-40-4490edd7b355> in random_forest_selection(df)
     12     clf_y = df.label
     13 
---> 14     model = clf.fit(clf_X,clf_y)
     15     #feat_importances = pd.DataFrame(model.feature_importances_, index=clf_X.columns, columns=["importance"])
     16     feature_importances = pd.DataFrame({'feature': clf_X.columns, 'importance': model.feature_importances_})

D:anacondalibsite-packagessklearnensemble_forest.py in fit(self, X, y, sample_weight)
    329         self.n_outputs_ = y.shape[1]
    330 
--> 331         y, expanded_class_weight = self._validate_y_class_weight(y)
    332 
    333         if getattr(y, "dtype", None) != DOUBLE or not y.flags.contiguous:

D:anacondalibsite-packagessklearnensemble_forest.py in _validate_y_class_weight(self, y)
    557 
    558     def _validate_y_class_weight(self, y):
--> 559         check_classification_targets(y)
    560 
    561         y = np.copy(y)

D:anacondalibsite-packagessklearnutilsmulticlass.py in check_classification_targets(y)
    181     if y_type not in ['binary', 'multiclass', 'multiclass-multioutput',
    182                       'multilabel-indicator', 'multilabel-sequences']:
--> 183         raise ValueError("Unknown label type: %r" % y_type)
    184 
    185 

ValueError: Unknown label type: 'unknown'

最后

以上就是文艺宝贝最近收集整理的关于sklearn随机森林模型:ValueError: Unknown label type: ‘unknown‘sklearn随机森林模型:ValueError: Unknown label type: 'unknown'的全部内容,更多相关sklearn随机森林模型:ValueError:内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(80)

评论列表共有 0 条评论

立即
投稿
返回
顶部