我正在尝试使用以下代码使用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
有什么想法吗?
任何帮助将不胜感激!
我相信你的错误是由于这 2 行造成的:
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')
有关回归评分指标的完整列表,请参阅此处,请注意回归行。