我有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]]))
回答:
你是在regr
上进行拟合,但却在clf
上调用特征重要性。试试改成这样:
importances = regr.feature_importances_