Skip to content

Instantly share code, notes, and snippets.

@fuad021
Last active November 28, 2020 05:45
Show Gist options
  • Save fuad021/5854f3cdd2b6f491b0d8c12fed4bb36f to your computer and use it in GitHub Desktop.
Save fuad021/5854f3cdd2b6f491b0d8c12fed4bb36f to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# =============================================
# Author: Fuad Al Abir
# Date: 31 Oct 2020
# Problem: Performance Evaluation
# Course: CSE 3210
# =============================================
# libraries
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
# data loading
data = load_breast_cancer()
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=21)
# classification using decision tree
dt = DecisionTreeClassifier()
dt.fit(X_train, y_train)
y_pred = dt.predict(X_test)
# creating confusion matrix
def create_confusion_matrix(y_test, y_pred):
tp, tn, fp, fn = 0, 0, 0, 0
for i in range(len(y_test)):
if y_test[i] == y_pred[i] and y_test[i] == 0:
tn += 1
elif y_test[i] == y_pred[i] and y_test[i] == 1:
tp += 1
elif y_test[i] != y_pred[i] and y_test[i] == 0:
fp += 1
elif y_test[i] != y_pred[i] and y_test[i] == 1:
fn += 1
return [[tn, fp], [fn, tp]]
matrix = create_confusion_matrix(y_test, y_pred)
matrix_df = pd.DataFrame(matrix, columns=['Predicted (0)', 'Predicted (1)'], index=['Real (0)', 'Real (1)'])
print(matrix_df)
# confusion matrix heatmap
sns.set(font_scale=2)
plt.figure(figsize=(12, 8))
sns.heatmap(matrix_df, annot=True)
plt.show()
# calcualting performance measurements
def performence_measures(confusion_matrix):
tn, fp, fn, tp = confusion_matrix[0][0], confusion_matrix[0][1], confusion_matrix[1][0], confusion_matrix[1][1]
measures = {}
measures['accuracy'] = (tn+tp)/(tn+fp+fn+tp)
measures['precision'] = (tp)/(tp+fp)
measures['recall'] = (tp)/(tp+fn)
measures['f1-score'] = 2*measures['precision']*measures['recall']/(measures['precision']+measures['recall'])
return measures
print(performence_measures(matrix))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment