</br>
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
X = load_digits().data
y = load_digits().target
print(X.shape)
print(y.shape)
print(y)
(1797, 64) (1797,) [0 1 2 ... 8 9 8]
print(X.max(), X.min())
16.0 0.0
idx = 98
img = np.reshape(X[idx], (8,8))
plt.figure(dpi=20)
plt.imshow(img, cmap='gray')
plt.show()
print(y[idx])
3
# Mean-center the data
Xm = X - (np.mean(X, axis=0))
# Calculate Cov(X)
C = (1 / Xm.shape[0]) * Xm.T @ Xm
# Calculate eigenvectors of Cov(X)
eigvals, eigvecs = np.linalg.eigh(C)
V = eigvecs[:,-3:]
# Project data on to the eigenvectors
Z = Xm @ V
#print(np.unique(X))
print(X[y==3][0:3])
[[ 0. 0. 7. 15. 13. 1. 0. 0. 0. 8. 13. 6. 15. 4. 0. 0. 0. 2. 1. 13. 13. 0. 0. 0. 0. 0. 2. 15. 11. 1. 0. 0. 0. 0. 0. 1. 12. 12. 1. 0. 0. 0. 0. 0. 1. 10. 8. 0. 0. 0. 8. 4. 5. 14. 9. 0. 0. 0. 7. 13. 13. 9. 0. 0.] [ 0. 2. 9. 15. 14. 9. 3. 0. 0. 4. 13. 8. 9. 16. 8. 0. 0. 0. 0. 6. 14. 15. 3. 0. 0. 0. 0. 11. 14. 2. 0. 0. 0. 0. 0. 2. 15. 11. 0. 0. 0. 0. 0. 0. 2. 15. 4. 0. 0. 1. 5. 6. 13. 16. 6. 0. 0. 2. 12. 12. 13. 11. 0. 0.] [ 0. 1. 8. 12. 15. 14. 4. 0. 0. 3. 11. 8. 8. 12. 12. 0. 0. 0. 0. 0. 2. 13. 7. 0. 0. 0. 0. 2. 15. 12. 1. 0. 0. 0. 0. 0. 13. 5. 0. 0. 0. 0. 0. 0. 9. 13. 0. 0. 0. 0. 7. 8. 14. 15. 0. 0. 0. 0. 14. 15. 11. 2. 0. 0.]]
plt.figure(dpi=200)
sc = plt.scatter(Z[:,-1], Z[:,-2], marker='x', c=y)
plt.show()
%matplotlib notebook
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(dpi=150)
ax = fig.add_subplot(111, projection='3d')
for j in range(10):
ax.scatter(Z[y==j,0], Z[y==j,1], Z[y==j,2], marker='x', label=str(j))
ax.legend(bbox_to_anchor=[1.3,1])
plt.show()