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
|
@ -138,94 +138,75 @@ def MultModelsCl(input_df, target, skf_cv
|
||||||
, remainder='passthrough')
|
, remainder='passthrough')
|
||||||
|
|
||||||
# Specify multiple Classification models
|
# Specify multiple Classification models
|
||||||
lr = LogisticRegression(**rs)
|
models = [('Logistic Regression' , LogisticRegression(**rs) )
|
||||||
lrcv = LogisticRegressionCV(**rs)
|
, ('Logistic RegressionCV' , LogisticRegressionCV(**rs) )
|
||||||
gnb = GaussianNB()
|
, ('Gaussian NB' , GaussianNB() )
|
||||||
nb = BernoulliNB()
|
, ('Naive Bayes' , BernoulliNB() )
|
||||||
knn = KNeighborsClassifier()
|
, ('K-Nearest Neighbors' , KNeighborsClassifier() )
|
||||||
svc = SVC(**rs)
|
, ('SVC' , SVC(**rs) )
|
||||||
mlp = MLPClassifier(max_iter = 500, **rs)
|
, ('MLP' , MLPClassifier(max_iter = 500, **rs) )
|
||||||
dt = DecisionTreeClassifier(**rs)
|
, ('Decision Tree' , DecisionTreeClassifier(**rs) )
|
||||||
ets = ExtraTreesClassifier(**rs)
|
, ('Extra Trees' , ExtraTreesClassifier(**rs) )
|
||||||
et = ExtraTreeClassifier(**rs)
|
, ('Extra Tree' , ExtraTreeClassifier(**rs) )
|
||||||
rf = RandomForestClassifier(**rs, n_estimators = 1000 )
|
, ('Random Forest' , RandomForestClassifier(**rs, n_estimators = 1000 ) )
|
||||||
rf2 = RandomForestClassifier(
|
, ('Random Forest2' , RandomForestClassifier(min_samples_leaf = 5
|
||||||
min_samples_leaf = 5
|
|
||||||
, n_estimators = 1000
|
, n_estimators = 1000
|
||||||
, bootstrap = True
|
, bootstrap = True
|
||||||
, oob_score = True
|
, oob_score = True
|
||||||
, **njobs
|
, **njobs
|
||||||
, **rs
|
, **rs
|
||||||
, max_features = 'auto')
|
, max_features = 'auto') )
|
||||||
xgb = XGBClassifier(**rs, verbosity = 0, use_label_encoder =False)
|
, ('XGBoost' , XGBClassifier(**rs, verbosity = 0, use_label_encoder =False) )
|
||||||
|
, ('LDA' , LinearDiscriminantAnalysis() )
|
||||||
lda = LinearDiscriminantAnalysis()
|
, ('Multinomial' , MultinomialNB() )
|
||||||
|
, ('Passive Aggresive' , PassiveAggressiveClassifier(**rs, **njobs) )
|
||||||
mnb = MultinomialNB()
|
, ('Stochastic GDescent' , SGDClassifier(**rs, **njobs) )
|
||||||
|
, ('AdaBoost Classifier' , AdaBoostClassifier(**rs) )
|
||||||
pa = PassiveAggressiveClassifier(**rs, **njobs)
|
, ('Bagging Classifier' , BaggingClassifier(**rs, **njobs, bootstrap = True, oob_score = True) )
|
||||||
|
, ('Gaussian Process' , GaussianProcessClassifier(**rs) )
|
||||||
sgd = SGDClassifier(**rs, **njobs)
|
, ('Gradient Boosting' , GradientBoostingClassifier(**rs) )
|
||||||
|
, ('QDA' , QuadraticDiscriminantAnalysis() )
|
||||||
abc = AdaBoostClassifier(**rs)
|
, ('Ridge Classifier' , RidgeClassifier(**rs) )
|
||||||
bc = BaggingClassifier(**rs, **njobs, bootstrap = True, oob_score = True)
|
, ('Ridge ClassifierCV' , RidgeClassifierCV(cv = 10) )
|
||||||
gpc = GaussianProcessClassifier(**rs)
|
|
||||||
gbc = GradientBoostingClassifier(**rs)
|
|
||||||
qda = QuadraticDiscriminantAnalysis()
|
|
||||||
rc = RidgeClassifier(**rs)
|
|
||||||
rccv = RidgeClassifierCV(cv = 10)
|
|
||||||
|
|
||||||
models = [('Logistic Regression' , lr)
|
|
||||||
, ('Logistic RegressionCV' , lrcv)
|
|
||||||
, ('Gaussian NB' , gnb)
|
|
||||||
, ('Naive Bayes' , nb)
|
|
||||||
, ('K-Nearest Neighbors' , knn)
|
|
||||||
, ('SVC' , svc)
|
|
||||||
, ('MLP' , mlp)
|
|
||||||
, ('Decision Tree' , dt)
|
|
||||||
, ('Extra Trees' , ets)
|
|
||||||
, ('Extra Tree' , et)
|
|
||||||
, ('Random Forest' , rf)
|
|
||||||
, ('Random Forest2' , rf2)
|
|
||||||
, ('XGBoost' , xgb)
|
|
||||||
, ('LDA' , lda)
|
|
||||||
, ('Multinomial' , mnb)
|
|
||||||
, ('Passive Aggresive' , pa)
|
|
||||||
, ('Stochastic GDescent' , sgd)
|
|
||||||
, ('AdaBoost Classifier' , abc)
|
|
||||||
, ('Bagging Classifier' , bc)
|
|
||||||
, ('Gaussian Process' , gpc)
|
|
||||||
, ('Gradient Boosting' , gbc)
|
|
||||||
, ('QDA' , qda)
|
|
||||||
, ('Ridge Classifier' , rc)
|
|
||||||
, ('Ridge ClassifierCV' , rccv)
|
|
||||||
]
|
]
|
||||||
|
|
||||||
mm_skf_scoresD = {}
|
mm_skf_scoresD = {}
|
||||||
|
|
||||||
|
print('\n==============================================================\n'
|
||||||
|
, '\nRunning several classification models (n):', len(models)
|
||||||
|
,'\nList of models:')
|
||||||
|
for m in models:
|
||||||
|
print(m)
|
||||||
|
print('\n================================================================\n')
|
||||||
|
|
||||||
|
index = 1
|
||||||
for model_name, model_fn in models:
|
for model_name, model_fn in models:
|
||||||
print('\nModel_name:', model_name
|
print('\nRunning classifier:', index
|
||||||
, '\nModel func:' , model_fn
|
, '\nModel_name:' , model_name
|
||||||
, '\nList of models:', models)
|
, '\nModel func:' , model_fn)
|
||||||
|
index = index+1
|
||||||
|
|
||||||
model_pipeline = Pipeline([
|
model_pipeline = Pipeline([
|
||||||
('prep' , col_transform)
|
('prep' , col_transform)
|
||||||
, ('model' , model_fn)])
|
, ('model' , model_fn)])
|
||||||
|
|
||||||
print('Running model pipeline:', model_pipeline)
|
print('\nRunning model pipeline:', model_pipeline)
|
||||||
skf_cv_mod = cross_validate(model_pipeline
|
skf_cv_modD = cross_validate(model_pipeline
|
||||||
, input_df
|
, input_df
|
||||||
, target
|
, target
|
||||||
, cv = skf_cv
|
, cv = skf_cv
|
||||||
, scoring = scoring_fn
|
, scoring = scoring_fn
|
||||||
, return_train_score = True)
|
, return_train_score = True)
|
||||||
|
|
||||||
|
#==============================
|
||||||
|
# Extract mean values for CV
|
||||||
|
#==============================
|
||||||
mm_skf_scoresD[model_name] = {}
|
mm_skf_scoresD[model_name] = {}
|
||||||
for key, value in skf_cv_mod.items():
|
|
||||||
|
for key, value in skf_cv_modD.items():
|
||||||
print('\nkey:', key, '\nvalue:', value)
|
print('\nkey:', key, '\nvalue:', value)
|
||||||
print('\nmean value:', mean(value))
|
print('\nmean value:', mean(value))
|
||||||
mm_skf_scoresD[model_name][key] = round(mean(value),2)
|
mm_skf_scoresD[model_name][key] = round(mean(value),2)
|
||||||
#pp.pprint(mm_skf_scoresD)
|
|
||||||
#cvtrain_mcc = mm_skf_scoresD[model_name]['test_mcc']
|
|
||||||
|
|
||||||
#return(mm_skf_scoresD)
|
#return(mm_skf_scoresD)
|
||||||
#%%
|
#%%
|
||||||
|
|
|
@ -101,6 +101,9 @@ jacc_score_fn = {'jcc': make_scorer(jaccard_score)}
|
||||||
def MultModelsCl_dissected(input_df, target, skf_cv
|
def MultModelsCl_dissected(input_df, target, skf_cv
|
||||||
, blind_test_input_df
|
, blind_test_input_df
|
||||||
, blind_test_target
|
, 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']):
|
, var_type = ['numerical', 'categorical','mixed']):
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
@ -201,52 +204,88 @@ def MultModelsCl_dissected(input_df, target, skf_cv
|
||||||
, scoring = scoring_fn
|
, scoring = scoring_fn
|
||||||
, return_train_score = True)
|
, return_train_score = True)
|
||||||
|
|
||||||
#----------
|
#######################################################################
|
||||||
# check 1
|
#======================================================
|
||||||
#----------
|
# Option 1: Add confusion matrix from cross_val_predict
|
||||||
foo_df = pd.DataFrame.from_dict(skf_cv_modD, orient ='index')
|
# Understand and USE with caution
|
||||||
#foo_df = pd.DataFrame.from_dict(skf_cv_modD)
|
|
||||||
|
|
||||||
#===================
|
|
||||||
# Confusion matrix: Not an easy problem to solve! STILL DOING it, 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."
|
# 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
|
# 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)
|
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, y).ravel() # internally
|
||||||
tn, fp, fn, tp = confusion_matrix(y_pred, target).ravel()
|
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)
|
# Build dict
|
||||||
# , 'FP': np.array(fp)
|
|
||||||
# , 'FN': np.array(fn)
|
|
||||||
# , 'TP': np.array(tp)}
|
|
||||||
|
|
||||||
cmD = {'TN' : tn
|
cmD = {'TN' : tn
|
||||||
, 'FP': fp
|
, 'FP': fp
|
||||||
, 'FN': fn
|
, 'FN': fn
|
||||||
, 'TP': tp}
|
, 'TP': tp}
|
||||||
|
#---------------------------------
|
||||||
|
# Update cv dict with cmD and tbtD
|
||||||
|
#----------------------------------
|
||||||
skf_cv_modD.update(cmD)
|
skf_cv_modD.update(cmD)
|
||||||
|
else:
|
||||||
|
skf_cv_modD = skf_cv_modD
|
||||||
|
#######################################################################
|
||||||
|
#=============================================
|
||||||
|
# Option 2: Add targety numbers for data
|
||||||
|
#=============================================
|
||||||
|
if add_yn:
|
||||||
|
|
||||||
#----------
|
#-----------------------------------------------------------
|
||||||
# check 2
|
# Initialise dict of target numbers: training and blind (tbt)
|
||||||
#----------
|
#-----------------------------------------------------------
|
||||||
#foo2_df = pd.DataFrame.from_dict(skf_cv_modD, orient ='index')
|
tbtD = {}
|
||||||
#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] = {}
|
mm_skf_scoresD[model_name] = {}
|
||||||
|
|
||||||
for key, value in skf_cv_modD.items():
|
for key, value in skf_cv_modD.items():
|
||||||
print('\nkey:', key, '\nvalue:', value)
|
print('\nkey:', key, '\nvalue:', value)
|
||||||
print('\nmean value:', np.mean(value))
|
print('\nmean value:', np.mean(value))
|
||||||
mm_skf_scoresD[model_name][key] = round(np.mean(value),2)
|
mm_skf_scoresD[model_name][key] = round(np.mean(value),2)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#return(mm_skf_scoresD)
|
#return(mm_skf_scoresD)
|
||||||
#%%
|
#%%
|
||||||
#=========================
|
#=========================
|
||||||
# Blind test: BTS results
|
# 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)
|
#bts_predict = gscv_fs.predict(blind_test_input_df)
|
||||||
model_pipeline.fit(input_df, target)
|
model_pipeline.fit(input_df, target)
|
||||||
bts_predict = model_pipeline.predict(blind_test_input_df)
|
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('\nMCC on Blind test:' , bts_mcc_score)
|
||||||
print('\nAccuracy on Blind test:', round(accuracy_score(blind_test_target, bts_predict),2))
|
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_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_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)
|
mm_skf_scoresD[model_name]['bts_precision'] = round(precision_score(blind_test_target, bts_predict),2)
|
||||||
|
|
|
@ -104,29 +104,29 @@ else:
|
||||||
|
|
||||||
print('\n#####################################################################\n')
|
print('\n#####################################################################\n')
|
||||||
|
|
||||||
###############################################################################
|
# ###############################################################################
|
||||||
#==================
|
# #==================
|
||||||
# Baseline models
|
# # Baseline models
|
||||||
#==================
|
# #==================
|
||||||
mm_skf_scoresD = MultModelsCl(input_df = X
|
# mm_skf_scoresD = MultModelsCl(input_df = X
|
||||||
, target = y
|
# , target = y
|
||||||
, var_type = 'mixed'
|
# , var_type = 'mixed'
|
||||||
, skf_cv = skf_cv
|
# , skf_cv = skf_cv
|
||||||
, blind_test_input_df = X_bts
|
# , blind_test_input_df = X_bts
|
||||||
, blind_test_target = y_bts)
|
# , blind_test_target = y_bts)
|
||||||
|
|
||||||
baseline_all = pd.DataFrame(mm_skf_scoresD)
|
# baseline_all = pd.DataFrame(mm_skf_scoresD)
|
||||||
baseline_all = baseline_all.T
|
# baseline_all = baseline_all.T
|
||||||
#baseline_train = baseline_all.filter(like='train_', axis=1)
|
# #baseline_train = baseline_all.filter(like='train_', axis=1)
|
||||||
baseline_CT = baseline_all.filter(like='test_', axis=1)
|
# baseline_CT = baseline_all.filter(like='test_', axis=1)
|
||||||
baseline_CT.sort_values(by=['test_mcc'], ascending=False, inplace=True)
|
# baseline_CT.sort_values(by=['test_mcc'], ascending=False, inplace=True)
|
||||||
|
|
||||||
baseline_BT = baseline_all.filter(like='bts_', axis=1)
|
# baseline_BT = baseline_all.filter(like='bts_', axis=1)
|
||||||
baseline_BT.sort_values(by = ['bts_mcc'], ascending = False, inplace = True)
|
# baseline_BT.sort_values(by = ['bts_mcc'], ascending = False, inplace = True)
|
||||||
|
|
||||||
# Write csv
|
# # Write csv
|
||||||
baseline_CT.to_csv(outdir_ml + gene.lower() + '_baseline_CT_allF.csv')
|
# baseline_CT.to_csv(outdir_ml + gene.lower() + '_baseline_CT_allF.csv')
|
||||||
baseline_BT.to_csv(outdir_ml + gene.lower() + '_baseline_BT_allF.csv')
|
# baseline_BT.to_csv(outdir_ml + gene.lower() + '_baseline_BT_allF.csv')
|
||||||
|
|
||||||
|
|
||||||
# #%% SMOTE NC: Oversampling [Numerical + categorical]
|
# #%% SMOTE NC: Oversampling [Numerical + categorical]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue