gpt4 book ai didi

python - 我在随机森林分类器中收到“未拟合”错误?

转载 作者:行者123 更新时间:2023-11-30 09:25:58 25 4
gpt4 key购买 nike

我有 4 个特征和 1 个目标变量。我使用 RandomForestRegressor 而不是 RandomForestClassifer 因为我的目标变量是 float。当我尝试拟合我的模型,然后按排序顺序输出它们以获得重要功能时,我收到“未拟合”错误,如何修复它?

代码:

import numpy as np
from sklearn.ensemble import RandomForestRegressor
from sklearn import datasets
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.feature_selection import SelectFromModel
from sklearn.metrics import accuracy_score

# Split the data into 30% test and 70% training
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)

# Train the classifier
#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_

关于python - 我在随机森林分类器中收到“未拟合”错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49578174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com