added option to add confusion matrix and target numbers in the mult function
This commit is contained in:
parent
905327bf4e
commit
135efcee41
3 changed files with 144 additions and 140 deletions
|
@ -101,6 +101,9 @@ jacc_score_fn = {'jcc': make_scorer(jaccard_score)}
|
|||
def MultModelsCl_dissected(input_df, target, skf_cv
|
||||
, blind_test_input_df
|
||||
, blind_test_target
|
||||
, add_cm = True # adds confusion matrix based on cross_val_predict
|
||||
, add_yn = True # adds target var class numbers
|
||||
, feature_groups = ['']
|
||||
, var_type = ['numerical', 'categorical','mixed']):
|
||||
|
||||
'''
|
||||
|
@ -201,52 +204,88 @@ def MultModelsCl_dissected(input_df, target, skf_cv
|
|||
, scoring = scoring_fn
|
||||
, return_train_score = True)
|
||||
|
||||
#----------
|
||||
# check 1
|
||||
#----------
|
||||
foo_df = pd.DataFrame.from_dict(skf_cv_modD, orient ='index')
|
||||
#foo_df = pd.DataFrame.from_dict(skf_cv_modD)
|
||||
|
||||
#===================
|
||||
# Confusion matrix: Not an easy problem to solve! STILL DOING it, USE with caution
|
||||
#######################################################################
|
||||
#======================================================
|
||||
# Option 1: Add confusion matrix from cross_val_predict
|
||||
# Understand and USE with caution
|
||||
# cross_val_score, cross_val_predict, "Passing these predictions into an evaluation metric may not be a valid way to measure generalization performance. Results can differ from cross_validate and cross_val_score unless all tests sets have equal size and the metric decomposes over samples."
|
||||
# https://stackoverflow.com/questions/65645125/producing-a-confusion-matrix-with-cross-validate
|
||||
#===================
|
||||
y_pred = cross_val_predict(model_pipeline, input_df, target, cv = 10, **njobs)
|
||||
#_tn, _fp, _fn, _tp = confusion_matrix(y_pred, y).ravel() # internally
|
||||
tn, fp, fn, tp = confusion_matrix(y_pred, target).ravel()
|
||||
# create a dict of confusion matrix that can be appended to the one above
|
||||
# cmD = {'TN' : np.array(tn)
|
||||
# , 'FP': np.array(fp)
|
||||
# , 'FN': np.array(fn)
|
||||
# , 'TP': np.array(tp)}
|
||||
#======================================================
|
||||
if add_cm:
|
||||
|
||||
#-----------------------------------------------------------
|
||||
# Initialise dict of Confusion Matrix (cm)
|
||||
#-----------------------------------------------------------
|
||||
cmD = {}
|
||||
|
||||
# Calculate cm
|
||||
y_pred = cross_val_predict(model_pipeline, input_df, target, cv = skf_cv, **njobs)
|
||||
#_tn, _fp, _fn, _tp = confusion_matrix(y_pred, y).ravel() # internally
|
||||
tn, fp, fn, tp = confusion_matrix(y_pred, target).ravel()
|
||||
|
||||
# Build dict
|
||||
|
||||
cmD = {'TN' : tn
|
||||
, 'FP': fp
|
||||
, 'FN': fn
|
||||
, 'TP': tp}
|
||||
#---------------------------------
|
||||
# Update cv dict with cmD and tbtD
|
||||
#----------------------------------
|
||||
skf_cv_modD.update(cmD)
|
||||
else:
|
||||
skf_cv_modD = skf_cv_modD
|
||||
#######################################################################
|
||||
#=============================================
|
||||
# Option 2: Add targety numbers for data
|
||||
#=============================================
|
||||
if add_yn:
|
||||
|
||||
#-----------------------------------------------------------
|
||||
# Initialise dict of target numbers: training and blind (tbt)
|
||||
#-----------------------------------------------------------
|
||||
tbtD = {}
|
||||
|
||||
cmD = {'TN' : tn
|
||||
, 'FP': fp
|
||||
, 'FN': fn
|
||||
, 'TP': tp}
|
||||
skf_cv_modD.update(cmD)
|
||||
|
||||
#----------
|
||||
# check 2
|
||||
#----------
|
||||
#foo2_df = pd.DataFrame.from_dict(skf_cv_modD, orient ='index')
|
||||
#foo_df = pd.DataFrame.from_dict(skf_cv_modD)
|
||||
# training y
|
||||
tyn = Counter(target)
|
||||
tyn_neg = tyn[0]
|
||||
tyn_pos = tyn[1]
|
||||
|
||||
# blind test y
|
||||
btyn = Counter(blind_test_target)
|
||||
btyn_neg = btyn[0]
|
||||
btyn_pos = btyn[1]
|
||||
|
||||
# Build dict
|
||||
tbtD = {'trainingY_neg' : tyn_neg
|
||||
, 'trainingY_pos' : tyn_pos
|
||||
, 'blindY_neg' : btyn_neg
|
||||
, 'blindY_pos' : btyn_pos}
|
||||
|
||||
#---------------------------------
|
||||
# Update cv dict with cmD and tbtD
|
||||
#----------------------------------
|
||||
skf_cv_modD.update(tbtD)
|
||||
else:
|
||||
skf_cv_modD = skf_cv_modD
|
||||
|
||||
#######################################################################
|
||||
#==============================
|
||||
# Extract mean values for CV
|
||||
#==============================
|
||||
mm_skf_scoresD[model_name] = {}
|
||||
|
||||
for key, value in skf_cv_modD.items():
|
||||
print('\nkey:', key, '\nvalue:', value)
|
||||
print('\nmean value:', np.mean(value))
|
||||
mm_skf_scoresD[model_name][key] = round(np.mean(value),2)
|
||||
|
||||
|
||||
|
||||
#return(mm_skf_scoresD)
|
||||
#%%
|
||||
#=========================
|
||||
# Blind test: BTS results
|
||||
#=========================
|
||||
# Build the final results with all scores for a feature selected model
|
||||
# Build the final results with all scores for the model
|
||||
#bts_predict = gscv_fs.predict(blind_test_input_df)
|
||||
model_pipeline.fit(input_df, target)
|
||||
bts_predict = model_pipeline.predict(blind_test_input_df)
|
||||
|
@ -255,22 +294,6 @@ def MultModelsCl_dissected(input_df, target, skf_cv
|
|||
print('\nMCC on Blind test:' , bts_mcc_score)
|
||||
print('\nAccuracy on Blind test:', round(accuracy_score(blind_test_target, bts_predict),2))
|
||||
|
||||
# Diff b/w train and bts test scores
|
||||
#train_test_diff_MCC = cvtrain_mcc - bts_mcc_score
|
||||
# print('\nDiff b/w train and blind test score (MCC):', train_test_diff)
|
||||
|
||||
|
||||
# # create a dict with all scores
|
||||
# lr_btsD = { 'model_name': model_name
|
||||
# , 'bts_mcc':None
|
||||
# , 'bts_fscore':None
|
||||
# , 'bts_precision':None
|
||||
# , 'bts_recall':None
|
||||
# , 'bts_accuracy':None
|
||||
# , 'bts_roc_auc':None
|
||||
# , 'bts_jaccard':None}
|
||||
|
||||
|
||||
mm_skf_scoresD[model_name]['bts_mcc'] = bts_mcc_score
|
||||
mm_skf_scoresD[model_name]['bts_fscore'] = round(f1_score(blind_test_target, bts_predict),2)
|
||||
mm_skf_scoresD[model_name]['bts_precision'] = round(precision_score(blind_test_target, bts_predict),2)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue