# Path: mlfromscratch/unsupervised_learning/principal_component_analysis.py
# class PCA():
#     """A method for doing dimensionality reduction by transforming the feature
#     space to a lower dimensionality, removing correlation between features and
#     maximizing the variance along each feature axis. This class is also used throughout
#     the project to plot data.
#     """
#     def transform(self, X, n_components):
#         """ Fit the dataset to the number of principal components specified in the
#         constructor and return the transformed dataset """
#         covariance_matrix = calculate_covariance_matrix(X)
# 
#         # Where (eigenvector[:,0] corresponds to eigenvalue[0])
#         eigenvalues, eigenvectors = np.linalg.eig(covariance_matrix)
# 
#         # Sort the eigenvalues and corresponding eigenvectors from largest
#         # to smallest eigenvalue and select the first n_components
#         idx = eigenvalues.argsort()[::-1]
#         eigenvalues = eigenvalues[idx][:n_components]
#         eigenvectors = np.atleast_1d(eigenvectors[:, idx])[:, :n_components]
# 
#         # Project the data onto principal components
#         X_transformed = X.dot(eigenvectors)
# 
#         return X_transformed


from sklearn import datasets
from mlfromscratch.unsupervised_learning import PCA
import matplotlib.pyplot as plt
import matplotlib.cm as cmx
import matplotlib.colors as colors
import numpy as np

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

# Path: mlfromscratch/unsupervised_learning/principal_component_analysis.py
# class PCA():
#     """A method for doing dimensionality reduction by transforming the feature
#     space to a lower dimensionality, removing correlation between features and
#     maximizing the variance along each feature axis. This class is also used throughout
#     the project to plot data.
#     """
#     def transform(self, X, n_components):
#         """ Fit the dataset to the number of principal components specified in the
#         constructor and return the transformed dataset """
#         covariance_matrix = calculate_covariance_matrix(X)
# 
#         # Where (eigenvector[:,0] corresponds to eigenvalue[0])
#         eigenvalues, eigenvectors = np.linalg.eig(covariance_matrix)
# 
#         # Sort the eigenvalues and corresponding eigenvectors from largest
#         # to smallest eigenvalue and select the first n_components
#         idx = eigenvalues.argsort()[::-1]
#         eigenvalues = eigenvalues[idx][:n_components]
#         eigenvectors = np.atleast_1d(eigenvectors[:, idx])[:, :n_components]
# 
#         # Project the data onto principal components
#         X_transformed = X.dot(eigenvectors)
# 
#         return X_transformed


# Path: mlfromscratch/examples/principal_component_analysis.py
from sklearn import datasets
from mlfromscratch.unsupervised_learning import PCA
import matplotlib.pyplot as plt
import matplotlib.cm as cmx
import matplotlib.colors as colors
import numpy as np


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)