Scikit Learn RFECV ValueError: continuous is not supported

Scikit Learn RFECV ValueError: continuous is not supported

我正在尝试使用 scikit learn RFECV 使用以下代码在给定数据集中进行特征选择:

import pandas as pd
from sklearn.ensemble import RandomForestRegressor
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.feature_selection import RFECV

# Data Processing
df = pd.read_csv('Combined_Data_final_2019H2_10min.csv')
X, y = (df.drop(['TimeStamp','Power_kW'], axis=1)), df['Power_kW']
SEED = 10
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=SEED)

# The "accuracy" scoring is proportional to the number of correct classifications
clf_rf_4 = RandomForestRegressor()
rfecv = RFECV(estimator=clf_rf_4, step=1, cv=4,scoring='accuracy')   #4-fold cross-validation (cv=4)

rfecv = rfecv.fit(X_train, y_train)

print('Optimal number of features :', rfecv.n_features_)
print('Best features :', X.columns[rfecv.support_])

# Plot number of features VS. cross-validation scores
plt.figure()
plt.xlabel("Number of features selected")
plt.ylabel("Cross validation score of number of selected features")
plt.plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_)
plt.show()

我尝试了多种不同的解决方案,但我不断收到以下错误代码:

ValueError: continuous is not supported

有什么想法吗?

非常感谢任何帮助!

我认为您的错误是由于这两行造成的:

clf_rf_4 = RandomForestRegressor()
rfecv = RFECV(estimator=clf_rf_4, step=1, cv=4,scoring='accuracy')

accuracy 没有为连续输出定义。尝试将其更改为:

rfecv = RFECV(estimator=clf_rf_4, step=1, cv=4,scoring='r2')

有关回归评分指标的完整列表,请参阅 here,请注意 Regression 行。