#!/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) ######################################################### # my data pipe = Pipeline([ ('pre', MinMaxScaler()) ('selector', RFECV(LogisticRegression(**rs), cv = skf_cv, scoring = 'matthews_corrcoef')) , ('classifier', LogisticRegression(**rs))]) search_space = [{'selector__min_features_to_select': [1,2]}, {'classifier': [LogisticRegression()], #'classifier__C': np.logspace(0, 4, 10), 'classifier__C': [2, 2.8], 'classifier__max_iter': [100], 'classifier__penalty': ['l1', 'l2'], 'classifier__solver': ['saga'] }] #, #{'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=skf_cv, scoring = mcc_score_fn, refit = 'mcc', verbose=0) clf.fit(X, y) clf.best_params_ clf.best_score_ tp = clf.predict(X_bts) print('\nMCC on Blind test:' , round(matthews_corrcoef(y_bts, tp),2)) print('\nAccuracy on Blind test:', round(accuracy_score(y_bts, tp),2))