<jupyter_start><jupyter_text>$${ x }_{ new }=\frac { x-\mu  }{ \sigma  } $$<jupyter_code>X_std = (X - np.mean(X, axis=0)) / np.std(X, axis=0)<jupyter_output><empty_output><jupyter_text>이번에는 표준화(standardization)을 적용한 데이터와 적용하지 않은 데이터의 covariance matrix를 비교해보자. $$ { \sigma  }_{ jk }=\frac { 1 }{ n-1 } \sum _{ i=1 }^{ n }{ ({ x }_{ ij }-\bar { x } _{ j })({ x }_{ ik }-{ \bar { x }  }_{ k }) } $$<jupyter_code>mean_vec = np.mean(X, axis=0)
cov_mat2 = (X - mean_vec).T.dot((X - mean_vec)) / (X.shape[0]-1)
print("표준화 적용 X \n", cov_mat2)

mean_vec = np.mean(X_std, axis=0)
cov_mat = (X_std - mean_vec).T.dot((X_std - mean_vec)) / (X_std.shape[0]-1)
print("표준화 적용 \n", cov_mat)

np.cov(X.T)
eig_vals, eig_vecs = np.linalg.eig(cov_mat)

print("eig_values \n{}".format(eig_vals))
print("eig_vectors \n{}".format(eig_vecs))<jupyter_output>eig_values 
[ 2.93035378  0.92740362  0.14834223  0.02074601]
eig_vectors 
[[ 0.52237162 -0.37231836 -0.72101681  0.26199559]
 [-0.26335492 -0.92555649  0.24203288 -0.12413481]
 [ 0.58125401 -0.02109478  0.14089226 -0.80115427]
 [ 0.56561105 -0.06541577  0.6338014   0.52354627]]<jupyter_text>표준화(standardization)을 한 경우, correlation matrix와 같은 결과값이 나온다.<jupyter_code>cor_mat = np.corrcoef(X.T)

eig_vals, eig_vecs = np.linalg.eigh(cor_mat)

print("eig_values \n{}".format(eig_vals))
print("eig_vectors \n{}".format(eig_vecs))<jupyter_output>eig_values 
[ 2.91081808  0.92122093  0.14735328  0.02060771]
eig_vectors 
[[ 0.52237162 -0.37231836 -0.72101681  0.26199559]
 [-0.26335492 -0.92555649  0.24203288 -0.12413481]
 [ 0.58125401 -0.02109478  0.14089226 -0.80115427]
 [ 0.56561105 -0.06541577  0.6338014   0.52354627]]<jupyter_text>Singular Vector Decomposition을 돌려보아도 u와 같은 결과가 나온다.<jupyter_code>u, s, v = np.linalg.svd(X_std.T)
u<jupyter_output><empty_output><jupyter_text>PC 선택하기<jupyter_code>eig_pairs = [(np.abs(eig_vals[i]), eig_vecs[:,i]) for i in range(len(eig_vals))]
eig_pairs.sort(key = lambda x: x[0], reverse=True)
eig_pairs
eig_pairs[0][1]
eig_vecs[0]
print("eigen value 내림차순으로 정렬")
for i in eig_pairs:
    print(i[0])
total = sum(eig_vals)
%matplotlib inline

import matplotlib.pyplot as plt
explained_var = [(i/total) * 100 for i in sorted(eig_vals, reverse=True)]
cum_var = np.cumsum(explained_var)
print(explained_var)
print(cum_var)

with plt.style.context('seaborn-darkgrid'):
    plt.bar(range(len(explained_var)), explained_var, align='center',
            label='individual explained variance')
    plt.step(range(len(explained_var)), cum_var, where='mid',
             label='cumulative explained variance', color="red")
    plt.ylabel("Cumulative variance")
    plt.xlabel("Principal components")
    plt.tight_layout()
    plt.legend(loc='best')
    plt.show()<jupyter_output>[72.770452093801353, 23.030523267680632, 3.6838319576273935, 0.51519268089062353]
[  72.77045209   95.80097536   99.48480732  100.        ]<jupyter_text>새로운 피쳐스페이스로 투영하는 projection matrix를 만들어보자<jupyter_code>pc = 2
projection_matrix  = np.hstack((eig_pairs[i][1].reshape(len(eig_vals),1)) for i in range(pc))
projection_matrix
Y = X_std.dot(projection_matrix)
np.shape(Y)
Y
label = df['class'].unique()
label
with plt.style.context("seaborn-darkgrid"):
    for l in zip(label):
        plt.scatter(Y[y==l,0], Y[y==l,1],
                    label=l)
    plt.xlabel("PC 1")
    plt.ylabel("PC 2")
    plt.legend()
    plt.show()<jupyter_output><empty_output><jupyter_text>scikit-learn패키지를 이용하여 구해보기<jupyter_code>from sklearn.decomposition import PCA as sklearnPCA
pca = sklearnPCA(n_components=2)
Y_ = pca.fit_transform(X_std)
label = np.unique(label)
with plt.style.context("seaborn-darkgrid"):  
    for l in label:
        plt.scatter(Y_[y==l,0], Y_[y==l,1],label=l)
    plt.xlabel("PC 1")
    plt.ylabel("PC 2")
    plt.legend()
    plt.show() 
class PCA_jay:
    def __init__(self, input_data, label=0):
        self.data = input_data
        self.label = label
    
    def eigen(self):
        self.X_std = (self.data - np.mean(self.data, axis=0)) / np.std(self.data, axis=0)
        mean_vec = np.mean(self.X_std, axis=0)
        cov_mat = (self.X_std - mean_vec).T.dot((self.X_std - mean_vec)) / (self.X_std.shape[0]-1)
        self.eig_vals, self.eig_vecs = np.linalg.eig(cov_mat)
        return self.eig_vals, self.eig_vecs
    
    def projection(self, n_components=2):
        eig_pairs = [(np.abs(self.eig_vals[i]), self.eig_vecs[:,i]) for i in range(len(self.eig_vals))]
        eig_pairs.sort(key = lambda x: x[0], reverse=True)
        projection_matrix  = np.hstack((eig_pairs[i][1].reshape(len(self.eig_vals),1)) for i in range(n_components))
        self.Y = self.X_std.dot(projection_matrix)
        return self.Y
    
    def scree_plot(self):
        total = sum(self.eig_vals)
        explained_var = [(i/total) * 100 for i in sorted(self.eig_vals, reverse=True)]
        cum_var = np.cumsum(explained_var)

        with plt.style.context('seaborn-darkgrid'):
            
            plt.bar(range(len(explained_var)), explained_var, align='center',
                    label='individual explained variance')
            plt.step(range(len(explained_var)), cum_var, where='mid',
                     label='cumulative explained variance',color = "red")
            plt.ylabel("Cumulative variance")
            plt.xlabel("Principal components")
            plt.tight_layout()
            plt.legend(loc='best')
            plt.show()
            
    def pca_plot(self):
        label = np.unique(self.label)
        with plt.style.context("seaborn-darkgrid"):  
            for l in label:
                plt.scatter(self.Y[y==l,0], self.Y[y==l,1],label=l)
            plt.xlabel("PC 1")
            plt.ylabel("PC 2")
            plt.legend()
            plt.show() 

        
test = PCA_jay(input_data=X, label=y)
eigen_values, eigen_vectors = test.eigen()
print("Eigen values \n {}".format(eigen_values))
print("Eigen vectors \n {}".format(eigen_vectors))
projection = test.projection(n_components=2)
print("Projected values \n {}".format(projection))
test.pca_plot()
test.scree_plot()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15756578947368421, 'fraction_numerical': 0.08404605263157895, 'mean_word_length': 3.5245535714285716, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '645103', 'license': 'MIT', 'n_tokens_mistral': 2859, 'n_tokens_neox': 2641, 'n_words': 409, 'path': '01 Dimensionality Reduction/Tutorial 02 - Unsupervised linear embedding/BA)PCA.ipynb', 'repo_name': 'KateYeon/Business-Anlaytics', 'size': '6080'}