我正在尝试使用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,请注意回归行。

关于python - Scikit学习RFECV ValueError:不支持连续,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60239975/

10-12 19:35