ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • PCA using Python
    Mathematics 2021. 2. 2. 19:28
    import os
    import numpy as np
    from sklearn.decomposition import TruncatedSVD
    from utils.helper_03 import *
    
    # set random-seed
    np.random.seed(1)
    
    # Generate data
    x = np.arange(-0.5, 0.5, 0.01)
    y = x + (np.random.rand(len(x))-0.5)*0.2
    plt.figure(figsize=(5, 5))
    plt.plot(x, y, 'o'); plt.grid();

    target_arr = np.stack((x, y)).T  # (100x2)
    u, s, vh = np.linalg.svd(target_arr, full_matrices=True)

    first_component = v[:, 0]
    second_component = v[:, 1]
    
    axis1 = first_component[1]/first_component[0] * x
    axis2 = second_component[1]/second_component[0] * x
    
    plt.figure(figsize=(5, 5))
    plt.plot(x, y, 'o');
    plt.plot(x, axis1, label="1st component axis"); plt.plot(x, axis2, label="2nd component axis"); plt.legend();​

     

    'Mathematics' 카테고리의 다른 글

    Odds Ratios and Log(Odds Ratios)  (0) 2021.03.23
    Conditional Expectation  (0) 2021.01.27
    Perpendicular Distance Between a Hyperplane and a Point  (0) 2021.01.13

    Comments