Search code examples
pythonpandasfeature-extractionfeature-selection

Forward Selection using KNN Algorithm in Pandas


Could anyone help me on how to do a forward selection from a dataset using KNN Algorithm in Pandas ?

https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.RFE.html

I followed this website but it doesn't look like a forward selection and it is not using KNN Algorithm.

  • The input is the dataset and Target Variable.
  • The output should be a list of forward selected features.

Is it possible ? If yes, how ?

Thanks in advance.


Solution

  • I tried this and the code is working:

    from sklearn.feature_selection import SelectFromModel
    from sklearn.linear_model import Lasso
    from sklearn import preprocessing
    dataset = pd.read_csv('Data.csv')
    label_encoder = preprocessing.LabelEncoder()
    column_list = dataset.columns.tolist()
    for i in column_list:
        s = str(dataset[i].dtype)
        if (s == 'object'):
            dataset[i] = label_encoder.fit_transform(dataset[i])
    dataset = dataset.dropna()
    x = dataset.drop(columns=[Target])
    y = dataset[Target].values
    estimator = Lasso()
    featureselection = SelectFromModel(estimator)
    featureselection.fit(x,y)
    features = featureselection.transform(x)
    x.columns[featureselection.get_support()]
    

    yet, I would like to use KNN Algorithm instead of Lasso().