Skip to content

Instantly share code, notes, and snippets.

@Ayushijain09
Last active July 16, 2020 02:57
Show Gist options
  • Save Ayushijain09/fe6dd8501f8b1cb99eb831d2ecf9f55d to your computer and use it in GitHub Desktop.
Save Ayushijain09/fe6dd8501f8b1cb99eb831d2ecf9f55d to your computer and use it in GitHub Desktop.
Regression with RFE
from sklearn.feature_selection import RFE
lm = LinearRegression()
rfe1 = RFE(lm, 20) # RFE with 20 features
# Fit on train and test data with 20 features
X_train_new = rfe1.fit_transform(X_train, y_train)
X_test_new = rfe1.transform(X_test)
# Print the boolean results
print(rfe1.support_) # Output [False False False False True False False False True False False...]
print(rfe1.ranking_) # Output [36 34 23 26 1 21 12 27 1 13 28 1 18 19 32 25 1 11 9 7 8 10 30 35...]
lm.fit(X_train_new, y_train)
predictions_rfe = lm.predict(X_test_new)
RMSE = np.sqrt(mean_squared_error(y_test, predictions_rfe))
R2 = r2_score(y_test, predictions)
print('R2:',R2,'RMSE:',RMSE) #Output R2: 0.88 RMSE: 0.33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment