Images (Joint distribution)

ML
Author

Nipun Batra

Published

March 18, 2025

import matplotlib.pyplot as plt
import numpy as np
print(np.__version__)
import torch 
import torch.nn as nn

import pandas as pd
# Retina mode
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
2.2.4
from sklearn.datasets import load_digits
X, y = load_digits(return_X_y=True)
X = X.astype(np.float32)
y = y.astype(np.int64)
print(X.shape, y.shape)
(1797, 64) (1797,)
X[0], y[0]
(array([ 0.,  0.,  5., 13.,  9.,  1.,  0.,  0.,  0.,  0., 13., 15., 10.,
        15.,  5.,  0.,  0.,  3., 15.,  2.,  0., 11.,  8.,  0.,  0.,  4.,
        12.,  0.,  0.,  8.,  8.,  0.,  0.,  5.,  8.,  0.,  0.,  9.,  8.,
         0.,  0.,  4., 11.,  0.,  1., 12.,  7.,  0.,  0.,  2., 14.,  5.,
        10., 12.,  0.,  0.,  0.,  0.,  6., 13., 10.,  0.,  0.,  0.],
       dtype=float32),
 np.int64(0))
dig = 10
plt.imshow(X[dig].reshape(8, 8), cmap='gray')
plt.title(f'Target: {y[dig]}')
Text(0.5, 1.0, 'Target: 0')

# Using PCA to reduce the dimensionality of the data to 2d

from sklearn.decomposition import PCA

pca = PCA(n_components=2)
X_reduced = pca.fit_transform(X)
print(X_reduced.shape)
(1797, 2)
# Plotting the reduced data
plt.scatter(X_reduced[:, 0], X_reduced[:, 1], c=y, cmap='tab10')
plt.colorbar()