ml-finance-python

python scripts for finance machine learning

git clone https://9o.is/git/ml-finance-python.git

principal_component_analysis.py

(1201B)


      1 from sklearn import datasets
      2 import matplotlib.pyplot as plt
      3 import matplotlib.cm as cmx
      4 import matplotlib.colors as colors
      5 import numpy as np
      6 from mlfromscratch.unsupervised_learning import PCA
      7 
      8 def main():
      9 
     10     # Demo of how to reduce the dimensionality of the data to two dimension
     11     # and plot the results. 
     12 
     13     # Load the dataset
     14     data = datasets.load_digits()
     15     X = data.data
     16     y = data.target
     17 
     18     # Project the data onto the 2 primary principal components
     19     X_trans = PCA().transform(X, 2)
     20 
     21     x1 = X_trans[:, 0]
     22     x2 = X_trans[:, 1]
     23 
     24     cmap = plt.get_cmap('viridis')
     25     colors = [cmap(i) for i in np.linspace(0, 1, len(np.unique(y)))]
     26 
     27     class_distr = []
     28     # Plot the different class distributions
     29     for i, l in enumerate(np.unique(y)):
     30         _x1 = x1[y == l]
     31         _x2 = x2[y == l]
     32         _y = y[y == l]
     33         class_distr.append(plt.scatter(_x1, _x2, color=colors[i]))
     34 
     35     # Add a legend
     36     plt.legend(class_distr, y, loc=1)
     37 
     38     # Axis labels
     39     plt.suptitle("PCA Dimensionality Reduction")
     40     plt.title("Digit Dataset")
     41     plt.xlabel('Principal Component 1')
     42     plt.ylabel('Principal Component 2')
     43     plt.show()
     44 
     45 
     46 if __name__ == "__main__":
     47     main()