ML_AI_training/uq_ml_models_FS/fs_UQ_DT.py

109 lines
3.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed May 18 06:03:24 2022
@author: tanu
"""
parameters = [
{
'clf__estimator': [DecisionTreeClassifier(**rs)]
, 'clf__estimator__max_depth': [None, 2, 4, 6, 8, 10, 12, 16, 20]
, 'clf__estimator__class_weight':['balanced']
, 'clf__estimator__criterion': ['gini', 'entropy', 'log_loss']
, 'clf__estimator__max_features': [None, 'sqrt', 'log2']
, 'clf__estimator__min_samples_leaf': [1, 2, 3, 4, 5, 10]
, 'clf__estimator__min_samples_split': [2, 5, 15, 20]
}
]
# Create pipeline
pipeline = Pipeline([
('pre', MinMaxScaler()),
('clf', ClfSwitcher()),
])
# Grid search i.e hyperparameter tuning and refitting on mcc
gscv_dt = GridSearchCV(pipeline
, parameters
#, scoring = 'f1', refit = 'f1'
, scoring = mcc_score_fn, refit = 'mcc'
, cv = skf_cv
, **njobs
, return_train_score = False
, verbose = 3)
# Fit
gscv_dt_fit = gscv_dt.fit(X, y)
gscv_dt_fit_be_mod = gscv_dt_fit.best_params_
gscv_dt_fit_be_res = gscv_dt_fit.cv_results_
print('Best model:\n', gscv_dt_fit_be_mod)
print('Best models score:\n', gscv_dt_fit.best_score_, ':' , round(gscv_dt_fit.best_score_, 2))
print('\nMean test score from fit results:', round(mean(gscv_dt_fit_be_re['mean_test_mcc']),2))
print('\nMean test score from fit results:', round(np.nanmean(gscv_dt_fit_be_res['mean_test_mcc']),2))
######################################
# Blind test
######################################
# See how it does on the BLIND test
#print('\nBlind test score, mcc:', )
test_predict = gscv_dt_fit.predict(X_bts)
print(test_predict)
print(np.array(y_bts))
y_btsf = np.array(y_bts)
print(accuracy_score(y_btsf, test_predict))
print(matthews_corrcoef(y_btsf, test_predict))
# create a dict with all scores
dt_bts_dict = {#'best_model': list(gscv_dt_fit_be_mod.items())
'bts_fscore' : None
, 'bts_mcc' : None
, 'bts_precision': None
, 'bts_recall' : None
, 'bts_accuracy' : None
, 'bts_roc_auc' : None
, 'bts_jaccard' : None }
dt_bts_dict
dt_bts_dict['bts_fscore'] = round(f1_score(y_bts, test_predict),2)
dt_bts_dict['bts_mcc'] = round(matthews_corrcoef(y_bts, test_predict),2)
dt_bts_dict['bts_precision'] = round(precision_score(y_bts, test_predict),2)
dt_bts_dict['bts_recall'] = round(recall_score(y_bts, test_predict),2)
dt_bts_dict['bts_accuracy'] = round(accuracy_score(y_bts, test_predict),2)
dt_bts_dict['bts_roc_auc'] = round(roc_auc_score(y_bts, test_predict),2)
dt_bts_dict['bts_jaccard'] = round(jaccard_score(y_bts, test_predict),2)
dt_bts_dict
# Create a df from dict with all scores
dt_bts_df = pd.DataFrame.from_dict(dt_bts_dict,orient = 'index')
dt_bts_df.columns = ['DT']
print(dt_bts_df)
# Create df with best model params
model_params = pd.Series(['best_model_params', list(gscv_dt_fit_be_mod.items() )])
model_params_df = model_params.to_frame()
model_params_df
model_params_df.columns = ['DT']
model_params_df.columns
# Combine the df of scores and the best model params
dt_bts_df.columns
dt_output = pd.concat([model_params_df, dt_bts_df], axis = 0)
dt_output
# Format the combined df
# Drop the best_model_params row from dt_output
dt_df = dt_output.drop([0], axis = 0)
dt_df
#FIXME: tidy the index of the formatted df
###############################################################################