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

我有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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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