65 lines
2.1 KiB
Python
65 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
Created on Sat May 21 02:52:36 2022
|
|
|
|
@author: tanu
|
|
"""
|
|
# https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html
|
|
import pandas as pd
|
|
from sklearn.pipeline import Pipeline
|
|
from sklearn.datasets import make_classification
|
|
from sklearn.preprocessing import StandardScaler
|
|
from sklearn.model_selection import GridSearchCV
|
|
from sklearn.neighbors import KNeighborsClassifier
|
|
from sklearn.linear_model import LogisticRegression
|
|
from sklearn.ensemble import RandomForestClassifier
|
|
from sklearn.feature_selection import SelectKBest, mutual_info_classif
|
|
#pd.options.plotting.backend = "plotly"
|
|
X_eg, y_eg = make_classification(n_samples=1000,
|
|
n_features=30,
|
|
n_informative=5,
|
|
n_redundant=5,
|
|
n_classes=2,
|
|
random_state=123)
|
|
|
|
pipe = Pipeline([('scaler', StandardScaler()),
|
|
('selector', SelectKBest(mutual_info_classif, k=9)),
|
|
|
|
('classifier', LogisticRegression())])
|
|
|
|
search_space = [{'selector__k': [5, 6, 7, 10]},
|
|
{'classifier': [LogisticRegression()],
|
|
'classifier__C': [0.01,1.0],
|
|
'classifier__solver': ['saga', 'lbfgs']},
|
|
{'classifier': [RandomForestClassifier(n_estimators=100)],
|
|
'classifier__max_depth': [5, 10, None]},
|
|
{'classifier': [KNeighborsClassifier()],
|
|
'classifier__n_neighbors': [3, 7, 11],
|
|
'classifier__weights': ['uniform', 'distance']}]
|
|
|
|
|
|
|
|
clf = GridSearchCV(pipe, search_space, cv=10, verbose=0)
|
|
|
|
clf2 = clf.fit(X_eg, y_eg)
|
|
clf2._check_feature_names
|
|
clf2.best_estimator_.named_steps['selector'].n_features_in_
|
|
|
|
clf2.best_estimator_ #n of best features
|
|
clf2.best_params_
|
|
clf2.best_estimator_.get_params
|
|
clf2.get_feature_names()
|
|
|
|
|
|
|
|
clf3 = clf2.best_estimator_ #
|
|
clf3._final_estimator
|
|
clf3._final_estimator.C
|
|
clf3._final_estimator.solver
|
|
|
|
|
|
fs_bmod = clf2.best_estimator_
|
|
print('\nbest model with feature selection:', fs_bmod)
|
|
|
|
|