Dimensionality reduction using SVD
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.datasets import load_digits
from sklearn.decomposition import TruncatedSVD
X = load_digits().data
X = X / 255
plt.matshow(X[0].reshape((8, 8)), cmap = 'gray')
plt.show()
svd = TruncatedSVD(n_components=5)
X_svd = svd.fit_transform(X)
image = svd.inverse_transform(X_svd[0].reshape(1, -1))
plt.matshow(image.reshape((8, 8)), cmap = "gray")
plt.show()
print(f"Dimensions of original image: {X[0].shape[0]}")
print(f"Dimensions of reduced image: {X_svd[0].shape[0]}")