Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapper feature selection method using MLP models #1

Open
SasmitaManjariNayak opened this issue Feb 21, 2022 · 3 comments
Open

Wrapper feature selection method using MLP models #1

SasmitaManjariNayak opened this issue Feb 21, 2022 · 3 comments

Comments

@SasmitaManjariNayak
Copy link

No description provided.

@SasmitaManjariNayak
Copy link
Author

I am using ANN, LSTM, CNN models for classification. Now I want to apply the wrapper method for feature selection. I need python code for that. Can anyone help me, please?

@SasmitaManjariNayak
Copy link
Author

#importing the necessary libraries
from mlxtend.feature_selection import SequentialFeatureSelector as SFS
from sklearn.linear_model import LinearRegression

Sequential Forward Selection(sfs)

sfs = SFS(LinearRegression(),
k_features=20,
forward=True,
floating=False,
scoring = 'r2',
cv = 0)

sfs.fit(x, y)
sfs.k_feature_names_ # to get the final set of features

I got this code for using the wrapper method in LR model. How I input my ANN or LSTM or CNN model for wrapper feature selection I can't understand. Can anyone help me, please?

@MathurUtkarsh
Copy link

There are several wrapper methods for feature selection in Python, such as Recursive Feature Elimination (RFE), Sequential Backward Selection (SBS), and Genetic Algorithm (GA). Here is an example of how to use RFE with a LSTM model for classification in Python:

from sklearn.feature_selection import RFE
from sklearn.ensemble import RandomForestClassifier
from keras.wrappers.scikit_learn import KerasClassifier
from keras.models import Sequential
from keras.layers import LSTM, Dense

# define the LSTM model
def create_model(input_shape):
    model = Sequential()
    model.add(LSTM(32, input_shape=input_shape))
    model.add(Dense(1, activation='sigmoid'))
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

# create the RFE model and select attributes
estimator = KerasClassifier(build_fn=create_model, input_shape=(X.shape[1], 1), epochs=10, batch_size=32, verbose=0)
rfe = RFE(estimator, n_features_to_select=10)
rfe = rfe.fit(X, y)

This code will train an LSTM model using the RFE wrapper method, and select the top 10 features based on the model's performance. You can adjust the n_features_to_select parameter to select a different number of features, and the input_shape parameter to match the shape of your input data.

You can also apply the wrapper method for feature selection for other models like ANN and CNN, you just need to change the architecture of the model in the create_model function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants