Skip to content

Instantly share code, notes, and snippets.

@fuwiak
Created February 25, 2023 12:35
Show Gist options
  • Save fuwiak/2c7c97ce95766dd5f69f6cad8ed91573 to your computer and use it in GitHub Desktop.
Save fuwiak/2c7c97ce95766dd5f69f6cad8ed91573 to your computer and use it in GitHub Desktop.
def compare_algorithms2df(MLA, X_train, X_test, y_train, y_test, sorted_by_measure='accuracy'):
#show grid with compared results - accuracy, recall, ppv, f1-measure, mcc
MLA_columns = []
MLA_compare = pd.DataFrame(columns = MLA_columns)
row_index = 0
for alg in MLA:
predicted = alg.fit(X_train, y_train).predict(X_test)
fp, tp, th = roc_curve(y_test, predicted)
MLA_name = alg.__class__.__name__
MLA_compare.loc[row_index,'MLA Name'] = MLA_name
MLA_compare.loc[row_index, 'MLA Train Accuracy'] = round(alg.score(X_train, y_train), 4)
MLA_compare.loc[row_index, 'MLA Test Accuracy'] = round(alg.score(X_test, y_test), 4)
MLA_compare.loc[row_index, 'MLA Precission'] = precision_score(y_test, predicted)
MLA_compare.loc[row_index, 'MLA Recall'] = recall_score(y_test, predicted)
MLA_compare.loc[row_index, 'MLA AUC'] = auc(fp, tp)
row_index+=1
MLA_compare.sort_values(by = ['MLA AUC'], ascending = False, inplace = True)
return MLA_compare
def roc_curve_MLA(MLA,X_train, X_test, y_train, y_test):
index = 1
for alg in MLA:
predicted = alg.fit(X_train, y_train).predict(X_test)
fp, tp, th = roc_curve(y_test, predicted)
roc_auc_mla = auc(fp, tp)
MLA_name = alg.__class__.__name__
plt.plot(fp, tp, lw=2, alpha=0.3, label='ROC %s (AUC = %0.2f)' % (MLA_name, roc_auc_mla))
index+=1
plt.title('ROC Curve comparison')
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.plot([0,1],[0,1],'r--')
plt.xlim([0,1])
plt.ylim([0,1])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment