<jupyter_start><jupyter_text>Here in order to learn more about how each of the evaluation metric works I have chosen a simple dataset.   The dataset is Social Network Ads which predicts whether the customer will purchase a product or not given his gender, age and salary. To evaluate it with all the metrics I have choosen a binary classification.<jupyter_code># Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Social_Network_Ads.csv')
X = dataset.iloc[:, [2, 3]].values
y = dataset.iloc[:, 4].values

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Fitting Decision Tree Classification to the Training set
from sklearn.tree import DecisionTreeClassifier
classifier = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
classifier.fit(X_train, y_train)

# Predicting the Test set results
y_pred = classifier.predict(X_test)<jupyter_output><empty_output><jupyter_text>1. Confusion Matrix Evaluation<jupyter_code># Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)<jupyter_output>[[62  6]
 [ 3 29]]<jupyter_text>2. Accuracy Evaluation<jupyter_code>from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_pred)
print(accuracy)<jupyter_output>0.91<jupyter_text>3. Precision Evaluation<jupyter_code>from sklearn.metrics import precision_score
precision = precision_score(y_test, y_pred)
print(precision)<jupyter_output>0.8285714285714286<jupyter_text>4. Recall Evaluation<jupyter_code>from sklearn.metrics import recall_score
recall = recall_score(y_test, y_pred)
print(recall)<jupyter_output>0.90625<jupyter_text>5. F1-score Evaluation<jupyter_code>from sklearn.metrics import f1_score
f1 = f1_score(y_test, y_pred)
print(f1)<jupyter_output>0.8656716417910447<jupyter_text>6. Specificity Evaluation<jupyter_code>specificity = cm[1,1]/(cm[1,1]+cm[0,1])
print(specificity)<jupyter_output>0.8285714285714286<jupyter_text>7. Logarithmic Loss Evaluation<jupyter_code>from math import log

# calculate binary cross entropy
def binary_cross_entropy(actual, predicted):
	sum_score = 0.0
	for i in range(len(actual)):
		sum_score += actual[i] * log(1e-15 + predicted[i])
	mean_sum_score = 1.0 / len(actual) * sum_score
	return -mean_sum_score
 
loss = binary_cross_entropy(y_test, y_pred)
print(loss)<jupyter_output>1.0361632918473205<jupyter_text>8. Area Under Curve (AUC) Evaluation<jupyter_code>from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve
import matplotlib.pyplot as plt

auc = roc_auc_score(y_test, y_pred)
print(auc)

fpr,tpr,threshold=roc_curve(y_test, y_pred)
plt.plot([0,1],[0,1],linestyle="-",label="Generalized Model Curve")
plt.plot(fpr,tpr,marker=".",label="ROC CURVE")
plt.legend()
plt.show()<jupyter_output>0.9090073529411765
{'fraction_non_alphanumeric': 0.09433962264150944, 'fraction_numerical': 0.04339622641509434, 'mean_word_length': 7.505347593582887, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '448053', 'license': 'MIT', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1115, 'n_words': 265, 'path': 'Drishti_ML_ClassificationProblemEvaluation.ipynb', 'repo_name': 'prathyu3122/Open-contributions', 'size': '3180'}