47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
from sklearn import datasets
|
|
import matplotlib.pyplot as plt
|
|
import matplotlib.cm as cmx
|
|
import matplotlib.colors as colors
|
|
import numpy as np
|
|
from mlfromscratch.unsupervised_learning import PCA
|
|
|
|
def main():
|
|
|
|
# Demo of how to reduce the dimensionality of the data to two dimension
|
|
# and plot the results.
|
|
|
|
# Load the dataset
|
|
data = datasets.load_digits()
|
|
X = data.data
|
|
y = data.target
|
|
|
|
# Project the data onto the 2 primary principal components
|
|
X_trans = PCA().transform(X, 2)
|
|
|
|
x1 = X_trans[:, 0]
|
|
x2 = X_trans[:, 1]
|
|
|
|
cmap = plt.get_cmap('viridis')
|
|
colors = [cmap(i) for i in np.linspace(0, 1, len(np.unique(y)))]
|
|
|
|
class_distr = []
|
|
# Plot the different class distributions
|
|
for i, l in enumerate(np.unique(y)):
|
|
_x1 = x1[y == l]
|
|
_x2 = x2[y == l]
|
|
_y = y[y == l]
|
|
class_distr.append(plt.scatter(_x1, _x2, color=colors[i]))
|
|
|
|
# Add a legend
|
|
plt.legend(class_distr, y, loc=1)
|
|
|
|
# Axis labels
|
|
plt.suptitle("PCA Dimensionality Reduction")
|
|
plt.title("Digit Dataset")
|
|
plt.xlabel('Principal Component 1')
|
|
plt.ylabel('Principal Component 2')
|
|
plt.show()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |