saving work

This commit is contained in:
Tanushree Tunstall 2022-07-01 20:37:41 +01:00
parent d812835713
commit b5777a17c9
3 changed files with 103 additions and 22 deletions

View file

@ -77,6 +77,7 @@ import re
#####################################
rs = {'random_state': 42}
njobs = {'n_jobs': 10}
scoring_fn = ({ 'mcc' : make_scorer(matthews_corrcoef)
, 'fscore' : make_scorer(f1_score)
@ -87,6 +88,9 @@ scoring_fn = ({ 'mcc' : make_scorer(matthews_corrcoef)
, 'jcc' : make_scorer(jaccard_score)
})
mcc_score_fn = {'mcc': make_scorer(matthews_corrcoef)}
jacc_score_fn = {'jcc': make_scorer(jaccard_score)}
skf_cv = StratifiedKFold(n_splits = 10
#, shuffle = False, random_state= None)
, shuffle = True,**rs)
@ -95,9 +99,6 @@ rskf_cv = RepeatedStratifiedKFold(n_splits = 10
, n_repeats = 3
, **rs)
mcc_score_fn = {'mcc': make_scorer(matthews_corrcoef)}
jacc_score_fn = {'jcc': make_scorer(jaccard_score)}
###############################################################################
def fsgs_rfecv(input_df
, target
@ -109,7 +110,10 @@ def fsgs_rfecv(input_df
, custom_fs = RFECV(DecisionTreeClassifier(**rs) , cv = skf_cv, scoring = 'matthews_corrcoef')
, cv_method = skf_cv
, var_type = ['numerical', 'categorical' , 'mixed']
, resampling_type = 'none'
, verbose = 3
, random_state = 42
, n_jobs = 10
):
'''
returns
@ -120,6 +124,10 @@ def fsgs_rfecv(input_df
optimised/selected based on mcc
'''
rs = {'random_state': random_state}
njobs = {'n_jobs': n_jobs}
###########################################################################
#================================================
# Determine categorical and numerical features
@ -375,6 +383,8 @@ def fsgs_rfecv(input_df
output_modelD['train_score (MCC)'] = train_bscore
output_modelD['bts_mcc'] = bts_mcc_score
output_modelD['train_bts_diff'] = round(train_test_diff,2)
output_modelD['resampling'] = resampling_type
print(output_modelD)
nlen = len(output_modelD)

View file

@ -77,9 +77,6 @@ import re
import itertools
from sklearn.model_selection import LeaveOneGroupOut
#%% GLOBALS
rs = {'random_state': 42}
njobs = {'n_jobs': 10}
scoring_fn = ({ 'mcc' : make_scorer(matthews_corrcoef)
, 'fscore' : make_scorer(f1_score)
, 'precision' : make_scorer(precision_score)
@ -146,7 +143,7 @@ def MultModelsCl_logo_skf(input_df
, blind_test_df = pd.DataFrame()
, blind_test_target = pd.Series(dtype = int)
, tts_split_type = "none"
, group = 'none'
#, group = 'none'
, resampling_type = 'none' # default
, add_cm = True # adds confusion matrix based on cross_val_predict
@ -188,11 +185,11 @@ def MultModelsCl_logo_skf(input_df
, **rs)
logo = LeaveOneGroupOut()
# select CV type:
if group == 'none':
sel_cv = skf_cv
else:
sel_cv = logo
# # select CV type:
# if group == 'none':
# sel_cv = skf_cv
# else:
# sel_cv = logo
#======================================================
# Determine categorical and numerical features
#======================================================
@ -277,7 +274,7 @@ def MultModelsCl_logo_skf(input_df
, input_df
, target
, cv = sel_cv
, groups = group
#, groups = group
, scoring = scoring_fn
, return_train_score = True)
#==============================
@ -306,7 +303,12 @@ def MultModelsCl_logo_skf(input_df
cmD = {}
# Calculate cm
y_pred = cross_val_predict(model_pipeline, input_df, target, cv = sel_cv, groups = group, **njobs)
y_pred = cross_val_predict(model_pipeline
, input_df
, target
, cv = sel_cv
#, groups = group
, **njobs)
#_tn, _fp, _fn, _tp = confusion_matrix(y_pred, y).ravel() # internally
tn, fp, fn, tp = confusion_matrix(y_pred, target).ravel()