added pratice and feature selection scripts for LR and hyperparam for all classification models as separate scripts in uq_ml_models
This commit is contained in:
parent
fa0f5e5b39
commit
8b0f69bbd9
17 changed files with 2604 additions and 0 deletions
135
uq_ml_models/UQ_SVC.py
Normal file
135
uq_ml_models/UQ_SVC.py
Normal file
|
@ -0,0 +1,135 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Wed May 18 06:03:24 2022
|
||||
|
||||
@author: tanu
|
||||
"""
|
||||
#%% RandomForest + hyperparam: BaseEstimator: ClfSwitcher()
|
||||
class ClfSwitcher(BaseEstimator):
|
||||
def __init__(
|
||||
self,
|
||||
estimator = SGDClassifier(),
|
||||
):
|
||||
"""
|
||||
A Custom BaseEstimator that can switch between classifiers.
|
||||
:param estimator: sklearn object - The classifier
|
||||
"""
|
||||
self.estimator = estimator
|
||||
|
||||
def fit(self, X, y=None, **kwargs):
|
||||
self.estimator.fit(X, y)
|
||||
return self
|
||||
|
||||
def predict(self, X, y=None):
|
||||
return self.estimator.predict(X)
|
||||
|
||||
def predict_proba(self, X):
|
||||
return self.estimator.predict_proba(X)
|
||||
|
||||
def score(self, X, y):
|
||||
return self.estimator.score(X, y)
|
||||
|
||||
parameters = [
|
||||
{
|
||||
'clf__estimator': [SVC(**rs
|
||||
, **njobs)],
|
||||
, 'clf__estimator__kernel': ['linear', 'poly', 'rbf', 'sigmoid', 'precomputed'}
|
||||
, 'clf__estimator__C' : [50, 10, 1.0, 0.1, 0.01]
|
||||
, 'clf__estimator__gamma': ['scale', 'auto']
|
||||
|
||||
}
|
||||
]
|
||||
|
||||
# Create pipeline
|
||||
pipeline = Pipeline([
|
||||
('pre', MinMaxScaler()),
|
||||
('clf', ClfSwitcher()),
|
||||
])
|
||||
|
||||
# Grid search i.e hyperparameter tuning and refitting on mcc
|
||||
gscv_svc = 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_svc_fit = gscv_svc.fit(X, y)
|
||||
|
||||
gscv_svc_fit_be_mod = gscv_svc_fit.best_params_
|
||||
gscv_svc_fit_be_res = gscv_svc_fit.cv_results_
|
||||
|
||||
print('Best model:\n', gscv_svc_fit_be_mod)
|
||||
print('Best models score:\n', gscv_svc_fit.best_score_, ':' , round(gscv_svc_fit.best_score_, 2))
|
||||
|
||||
print('\nMean test score from fit results:', round(mean(gscv_svc_fit_be_re['mean_test_mcc']),2))
|
||||
print('\nMean test score from fit results:', round(np.nanmean(gscv_svc_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_svc_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
|
||||
svc_bts_dict = {#'best_model': list(gscv_svc_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 }
|
||||
svc_bts_dict
|
||||
svc_bts_dict['bts_fscore'] = round(f1_score(y_bts, test_predict),2)
|
||||
svc_bts_dict['bts_mcc'] = round(matthews_corrcoef(y_bts, test_predict),2)
|
||||
svc_bts_dict['bts_precision'] = round(precision_score(y_bts, test_predict),2)
|
||||
svc_bts_dict['bts_recall'] = round(recall_score(y_bts, test_predict),2)
|
||||
svc_bts_dict['bts_accuracy'] = round(accuracy_score(y_bts, test_predict),2)
|
||||
svc_bts_dict['bts_roc_auc'] = round(roc_auc_score(y_bts, test_predict),2)
|
||||
svc_bts_dict['bts_jaccard'] = round(jaccard_score(y_bts, test_predict),2)
|
||||
svc_bts_dict
|
||||
|
||||
# Create a df from dict with all scores
|
||||
pd.DataFrame.from_dict(svc_bts_dict, orient = 'index', columns = 'best_model')
|
||||
|
||||
svc_bts_df = pd.DataFrame.from_dict(svc_bts_dict,orient = 'index')
|
||||
svc_bts_df.columns = ['Logistic_Regression']
|
||||
print(svc_bts_df)
|
||||
|
||||
# Create df with best model params
|
||||
model_params = pd.Series(['best_model_params', list(gscv_svc_fit_be_mod.items() )])
|
||||
model_params_df = model_params.to_frame()
|
||||
model_params_df
|
||||
model_params_df.columns = ['Logistic_Regression']
|
||||
model_params_df.columns
|
||||
|
||||
# Combine the df of scores and the best model params
|
||||
svc_bts_df.columns
|
||||
svc_output = pd.concat([model_params_df, svc_bts_df], axis = 0)
|
||||
svc_output
|
||||
|
||||
# Format the combined df
|
||||
# Drop the best_model_params row from svc_output
|
||||
svc_df = svc_output.drop([0], axis = 0)
|
||||
svc_df
|
||||
|
||||
#FIXME: tidy the index of the formatted df
|
||||
|
||||
###############################################################################
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue