在随机森林分类器中遇到未拟合错误?

我有4个特征和一个目标变量。我使用RandomForestRegressor而不是RandomForestClassifer,因为我的目标变量是浮点数。当我试图拟合我的模型并按排序顺序输出以获取重要特征时,我遇到了未拟合错误,该如何修复?

代码:

import numpy as npfrom sklearn.ensemble import RandomForestRegressorfrom sklearn import datasetsfrom sklearn.datasets import make_regressionfrom sklearn.model_selection import train_test_splitfrom sklearn.feature_selection import SelectFromModelfrom sklearn.metrics import accuracy_score# 将数据分割为30%测试集和70%训练集X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)feat_labels = data.columns[:4]regr = RandomForestRegressor(max_depth=2, random_state=0)#clf = RandomForestClassifier(n_estimators=100, random_state=0)# 训练分类器#clf.fit(X_train, y_train)regr.fit(X, y)importances = clf.feature_importances_indices = np.argsort(importances)[::-1]for f in range(X_train.shape[1]):    print("%2d) %-*s %f" % (f + 1, 30, feat_labels[indices[f]], importances[indices[f]]))

enter image description here


回答:

你是在regr上进行拟合,但却在clf上调用特征重要性。试试改成这样:

importances = regr.feature_importances_

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注