163 lines
5.6 KiB
Python
163 lines
5.6 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
Created on Fri Mar 4 15:25:33 2022
|
|
|
|
@author: tanu
|
|
"""
|
|
#%%
|
|
|
|
import os, sys
|
|
import pandas as pd
|
|
import numpy as np
|
|
import pprint as pp
|
|
#from copy import deepcopy
|
|
from sklearn import linear_model
|
|
from sklearn.linear_model import LogisticRegression, LinearRegression
|
|
from sklearn.naive_bayes import BernoulliNB
|
|
from sklearn.neighbors import KNeighborsClassifier
|
|
from sklearn.svm import SVC
|
|
from sklearn.tree import DecisionTreeClassifier
|
|
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
|
|
from sklearn.neural_network import MLPClassifier
|
|
from xgboost import XGBClassifier
|
|
from sklearn.preprocessing import StandardScaler, MinMaxScaler, OneHotEncoder
|
|
|
|
from sklearn.compose import ColumnTransformer
|
|
from sklearn.compose import make_column_transformer
|
|
|
|
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score
|
|
from sklearn.metrics import roc_auc_score, roc_curve, f1_score, matthews_corrcoef
|
|
from sklearn.metrics import make_scorer
|
|
from sklearn.metrics import classification_report
|
|
|
|
from sklearn.metrics import average_precision_score
|
|
|
|
from sklearn.model_selection import cross_validate
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.model_selection import StratifiedKFold
|
|
|
|
from sklearn.pipeline import Pipeline
|
|
from sklearn.pipeline import make_pipeline
|
|
|
|
from sklearn.feature_selection import RFE
|
|
from sklearn.feature_selection import RFECV
|
|
import itertools
|
|
import seaborn as sns
|
|
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
print(np.__version__)
|
|
print(pd.__version__)
|
|
from statistics import mean, stdev, median, mode
|
|
|
|
from imblearn.over_sampling import RandomOverSampler
|
|
from imblearn.over_sampling import SMOTE
|
|
from imblearn.pipeline import Pipeline
|
|
#from sklearn.datasets import make_classification
|
|
from sklearn.model_selection import cross_validate
|
|
from sklearn.model_selection import RepeatedStratifiedKFold
|
|
from sklearn.ensemble import AdaBoostClassifier
|
|
from imblearn.combine import SMOTEENN
|
|
from imblearn.under_sampling import EditedNearestNeighbours
|
|
|
|
#%%
|
|
rs = {'random_state': 42}
|
|
# Done: add preprocessing step with one hot encoder
|
|
# Done: get accuracy and other scores through K-fold stratified cv
|
|
|
|
scoring_fn = ({ 'fscore' : make_scorer(f1_score)
|
|
, 'mcc' : make_scorer(matthews_corrcoef)
|
|
, 'precision' : make_scorer(precision_score)
|
|
, 'recall' : make_scorer(recall_score)
|
|
, 'accuracy' : make_scorer(accuracy_score)
|
|
, 'roc_auc' : make_scorer(roc_auc_score)
|
|
#, 'jaccard' : make_scorer(jaccard_score)
|
|
})
|
|
|
|
|
|
# Multiple Classification - Model Pipeline
|
|
def MultClassPipelineCV(X_train, X_test, y_train, y_test, input_df, var_type = ['numerical', 'categorical','mixed']):
|
|
|
|
# determine categorical and numerical features
|
|
numerical_ix = input_df.select_dtypes(include=['int64', 'float64']).columns
|
|
numerical_ix
|
|
categorical_ix = input_df.select_dtypes(include=['object', 'bool']).columns
|
|
categorical_ix
|
|
|
|
# Determine preprocessing steps ~ var_type
|
|
if var_type == 'numerical':
|
|
t = [('num', MinMaxScaler(), numerical_ix)]
|
|
|
|
if var_type == 'categorical':
|
|
t = [('cat', OneHotEncoder(), categorical_ix)]
|
|
|
|
if var_type == 'mixed':
|
|
t = [('cat', OneHotEncoder(), categorical_ix)
|
|
, ('num', MinMaxScaler(), numerical_ix)]
|
|
|
|
col_transform = ColumnTransformer(transformers = t
|
|
, remainder='passthrough')
|
|
|
|
#%%
|
|
log_reg = LogisticRegression(**rs)
|
|
nb = BernoulliNB()
|
|
knn = KNeighborsClassifier()
|
|
svm = SVC(**rs)
|
|
mlp = MLPClassifier(max_iter=500, **rs)
|
|
dt = DecisionTreeClassifier(**rs)
|
|
et = ExtraTreesClassifier(**rs)
|
|
rf = RandomForestClassifier(**rs)
|
|
rf2 = RandomForestClassifier(
|
|
min_samples_leaf=50,
|
|
n_estimators=150,
|
|
bootstrap=True,
|
|
oob_score=True,
|
|
n_jobs=-1,
|
|
random_state=42,
|
|
max_features='auto')
|
|
|
|
xgb = XGBClassifier(**rs, verbosity=0)
|
|
|
|
models = [
|
|
('Logistic Regression', log_reg),
|
|
('Naive Bayes', nb),
|
|
('K-Nearest Neighbors', knn),
|
|
('SVM', svm),
|
|
('MLP', mlp),
|
|
('Decision Tree', dt),
|
|
('Extra Trees', et),
|
|
('Random Forest', rf),
|
|
('Random Forest2', rf2),
|
|
#('XGBoost', xgb)
|
|
]
|
|
|
|
skf_cv_scores = {}
|
|
|
|
for model_name, model_fn in models:
|
|
print('\nModel_name:', model_name
|
|
, '\nModel func:' , model_fn
|
|
, '\nList of models:', models)
|
|
|
|
# model_pipeline = Pipeline([
|
|
# ('pre' , MinMaxScaler())
|
|
# , ('model' , model_fn)])
|
|
|
|
model_pipeline = Pipeline([
|
|
('prep' , col_transform)
|
|
, ('model' , model_fn)])
|
|
|
|
print('Running model pipeline:', model_pipeline)
|
|
skf_cv = cross_validate(model_pipeline
|
|
, X_train
|
|
, y_train
|
|
, cv = 10
|
|
, scoring = scoring_fn
|
|
, return_train_score = True)
|
|
skf_cv_scores[model_name] = {}
|
|
for key, value in skf_cv.items():
|
|
print('\nkey:', key, '\nvalue:', value)
|
|
print('\nmean value:', mean(value))
|
|
skf_cv_scores[model_name][key] = round(mean(value),2)
|
|
#pp.pprint(skf_cv_scores)
|
|
return(skf_cv_scores)
|
|
|