ml-finance-python

python scripts for finance machine learning

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

misc.py

(3773B)


      1 import progressbar
      2 from mpl_toolkits.mplot3d import Axes3D
      3 import matplotlib.pyplot as plt
      4 import matplotlib.cm as cmx
      5 import matplotlib.colors as colors
      6 import numpy as np
      7 
      8 from mlfromscratch.utils.data_operation import calculate_covariance_matrix
      9 from mlfromscratch.utils.data_operation import calculate_correlation_matrix
     10 from mlfromscratch.utils.data_manipulation import standardize
     11 
     12 bar_widgets = [
     13     'Training: ', progressbar.Percentage(), ' ', progressbar.Bar(marker="-", left="[", right="]"),
     14     ' ', progressbar.ETA()
     15 ]
     16 
     17 class Plot():
     18     def __init__(self): 
     19         self.cmap = plt.get_cmap('viridis')
     20 
     21     def _transform(self, X, dim):
     22         covariance = calculate_covariance_matrix(X)
     23         eigenvalues, eigenvectors = np.linalg.eig(covariance)
     24         # Sort eigenvalues and eigenvector by largest eigenvalues
     25         idx = eigenvalues.argsort()[::-1]
     26         eigenvalues = eigenvalues[idx][:dim]
     27         eigenvectors = np.atleast_1d(eigenvectors[:, idx])[:, :dim]
     28         # Project the data onto principal components
     29         X_transformed = X.dot(eigenvectors)
     30 
     31         return X_transformed
     32 
     33 
     34     def plot_regression(self, lines, title, axis_labels=None, mse=None, scatter=None, legend={"type": "lines", "loc": "lower right"}):
     35         
     36         if scatter:
     37             scatter_plots = scatter_labels = []
     38             for s in scatter:
     39                 scatter_plots += [plt.scatter(s["x"], s["y"], color=s["color"], s=s["size"])]
     40                 scatter_labels += [s["label"]]
     41             scatter_plots = tuple(scatter_plots)
     42             scatter_labels = tuple(scatter_labels)
     43 
     44         for l in lines:
     45             li = plt.plot(l["x"], l["y"], color=s["color"], linewidth=l["width"], label=l["label"])
     46 
     47         if mse:
     48             plt.suptitle(title)
     49             plt.title("MSE: %.2f" % mse, fontsize=10)
     50         else:
     51             plt.title(title)
     52 
     53         if axis_labels:
     54             plt.xlabel(axis_labels["x"])
     55             plt.ylabel(axis_labels["y"])
     56 
     57         if legend["type"] == "lines":
     58             plt.legend(loc="lower_left")
     59         elif legend["type"] == "scatter" and scatter:
     60             plt.legend(scatter_plots, scatter_labels, loc=legend["loc"])
     61 
     62         plt.show()
     63 
     64 
     65 
     66     # Plot the dataset X and the corresponding labels y in 2D using PCA.
     67     def plot_in_2d(self, X, y=None, title=None, accuracy=None, legend_labels=None):
     68         X_transformed = self._transform(X, dim=2)
     69         x1 = X_transformed[:, 0]
     70         x2 = X_transformed[:, 1]
     71         class_distr = []
     72 
     73         y = np.array(y).astype(int)
     74 
     75         colors = [self.cmap(i) for i in np.linspace(0, 1, len(np.unique(y)))]
     76 
     77         # Plot the different class distributions
     78         for i, l in enumerate(np.unique(y)):
     79             _x1 = x1[y == l]
     80             _x2 = x2[y == l]
     81             _y = y[y == l]
     82             class_distr.append(plt.scatter(_x1, _x2, color=colors[i]))
     83 
     84         # Plot legend
     85         if not legend_labels is None: 
     86             plt.legend(class_distr, legend_labels, loc=1)
     87 
     88         # Plot title
     89         if title:
     90             if accuracy:
     91                 perc = 100 * accuracy
     92                 plt.suptitle(title)
     93                 plt.title("Accuracy: %.1f%%" % perc, fontsize=10)
     94             else:
     95                 plt.title(title)
     96 
     97         # Axis labels
     98         plt.xlabel('Principal Component 1')
     99         plt.ylabel('Principal Component 2')
    100 
    101         plt.show()
    102 
    103     # Plot the dataset X and the corresponding labels y in 3D using PCA.
    104     def plot_in_3d(self, X, y=None):
    105         X_transformed = self._transform(X, dim=3)
    106         x1 = X_transformed[:, 0]
    107         x2 = X_transformed[:, 1]
    108         x3 = X_transformed[:, 2]
    109         fig = plt.figure()
    110         ax = fig.add_subplot(111, projection='3d')
    111         ax.scatter(x1, x2, x3, c=y)
    112         plt.show()
    113 
    114