Search code examples
pythonmachine-learningscikit-learncross-validationrfe

Scikit Learn RFECV ValueError: continuous is not supported


I am trying to use scikit learn RFECV for feature selection in a given dataset using the code below:

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()

I have tried a number of different solutions but I continuously get the following error code:

ValueError: continuous is not supported

Any ideas?

Any help would be very much appreciated!


Solution

  • I believe your error is due to these 2 lines:

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

    accuracy is not defined for continuous outputs. Try changing it to something like:

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

    For a full list of regression scoring metrics see here, note the Regression line.