import numpy as np
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, ConfusionMatrixDisplay, classification_report
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
import seaborn as sns
import torch
from sklearn.utils.multiclass import unique_labels
import torchvision
import torchvision.transforms as transforms
from latexify import latexify
%matplotlib inline
# Retina
%config InlineBackend.figure_format = 'retina'
Notion of Confusion in ML
ML
Tutorial
# Set device (CPU or GPU)
= torch.device("cuda" if torch.cuda.is_available() else "cpu")
device device
device(type='cuda')
# Define transformations
= transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
transform
# Download and load MNIST dataset using torchvision
= torchvision.datasets.MNIST(root='../datasets', train=True, download=True, transform=transform)
train_dataset = torchvision.datasets.MNIST(root='../datasets', train=False, download=True, transform=transform) test_dataset
# Flatten the images for sklearn MLP
= train_dataset.data.numpy().reshape((len(train_dataset), -1))
X_train = train_dataset.targets.numpy()
y_train = test_dataset.data.numpy().reshape((len(test_dataset), -1))
X_test = test_dataset.targets.numpy()
y_test
# Standardize features
= StandardScaler()
scaler = scaler.fit_transform(X_train)
X_train_scaled = scaler.transform(X_test) X_test_scaled
# Plot few images
latexify()= plt.subplots(1, 7, figsize=(8, 10))
fig, axs for i in range(7):
28, 28)), cmap='gray')
axs[i].imshow(X_train[i].reshape((
axs[i].set_title(y_train[i])'off') axs[i].axis(
# Create and train the MLP model
= MLPClassifier(hidden_layer_sizes=(100,), max_iter=20, random_state=42)
mlp_model
mlp_model.fit(X_train_scaled, y_train)
# Predict probabilities on the test set
= mlp_model.predict_proba(X_test_scaled)
y_probabilities
# Predict on the test set
= mlp_model.predict(X_test_scaled)
y_pred
# Evaluate the model
= accuracy_score(y_test, y_pred)
accuracy print(f"Accuracy: {accuracy}")
Accuracy: 0.9735
/home/nipun.batra/miniforge3/lib/python3.9/site-packages/sklearn/neural_network/_multilayer_perceptron.py:691: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (20) reached and the optimization hasn't converged yet.
warnings.warn(
=6)
latexify(fig_width= confusion_matrix(y_test, y_pred)
cm = ConfusionMatrixDisplay(cm).plot(values_format='d', cmap='gray', ax=plt.gca())
cm_display
# Save the figure with a higher resolution and without lossy compression
"../figures/mnist-cm.png", bbox_inches="tight", dpi=400, transparent=True)
plt.savefig(
# Show the plot
# Display classification report
print("Classification Report:")
print(classification_report(y_test, y_pred))
Classification Report:
precision recall f1-score support
0 0.98 0.99 0.98 980
1 0.99 0.99 0.99 1135
2 0.97 0.96 0.97 1032
3 0.96 0.98 0.97 1010
4 0.98 0.97 0.98 982
5 0.98 0.97 0.97 892
6 0.97 0.97 0.97 958
7 0.97 0.98 0.97 1028
8 0.96 0.96 0.96 974
9 0.98 0.96 0.97 1009
accuracy 0.97 10000
macro avg 0.97 0.97 0.97 10000
weighted avg 0.97 0.97 0.97 10000
# Display the first k wrong classified images with highest probabilities
# Find indices of wrongly classified samples
= np.where(y_pred != y_test)[0]
wrong_indices
# Sort wrong predictions by highest class probability
= np.argsort(np.max(y_probabilities[wrong_indices], axis=1))[::-1]
sorted_indices
= 9
k =8)
latexify(fig_widthfor i, idx in enumerate(sorted_indices[:k]):
3, 3, i + 1)
plt.subplot(0].numpy().squeeze(), cmap='gray')
plt.imshow(test_dataset[wrong_indices[idx]][f'True: {y_test[wrong_indices[idx]]}, Pred: {y_pred[wrong_indices[idx]]}\nProb: {np.max(y_probabilities[wrong_indices[idx]]):.1f}')
plt.title(
plt.tight_layout()