Open SasmitaManjariNayak opened 2 years ago
from mlxtend.feature_selection import SequentialFeatureSelector as SFS from sklearn.linear_model import LinearRegression
sfs = SFS(LinearRegression(), k_features=20, forward=True, floating=False, scoring = 'r2', cv = 0)
sfs.fit(x, y) sfs.k_featurenames # 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?
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.
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?