Snippets Collections
import pandas as pd
from sklearn.feature_selection import RFE
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris 

data = load_iris()
X, y = data.data, data.target

# Create a logistic regression model (you can use any other estimator as well).
estimator = LogisticRegression()

# Set the number of features you want to select (you can adjust this value as needed).
num_features_to_select = 2

# Perform RFE to get the best 'num_features_to_select' features.
rfe = RFE(estimator, n_features_to_select=num_features_to_select)
X_rfe = rfe.fit_transform(X, y)

# Get the selected feature indices.
selected_feature_indices = rfe.support_

# Get the selected feature names.
selected_feature_names = [feature_name for feature_name, selected in zip(data.feature_names, selected_feature_indices) if selected]
star

Wed Jul 26 2023 07:19:00 GMT+0000 (Coordinated Universal Time)

#rfe #recursivefeatureelimination

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension