Dimentionality Reduction Implementation
Dimentionality Reduction Implementation
selector.fit_transform(X)
array([[2, 0],
[1, 4],
[1, 1]])
import numpy as np
import pandas as pd
def find_correlation(df,thresh=0.9):
corrMatrix = df.corr()
corrMatrix.loc[:,:] = np.tril(corrMatrix,k=-1)
already_in = set()
result=[]
for col in corrMatrix:
perfect_corr = corrMatrix[col][corrMatrix[col]>thresh].index.tolist()
if perfect_corr and col not in already_in:
already_in.update(set(perfect_corr))
perfect_corr.append(col)
result.append(perfect_corr)
select_nested = [f[1:] for f in result]
select_flat = [i for j in select_nested for i in j]
return select_flat
dat = datasets.load_iris()
dat
https://colab.research.google.com/drive/16bIT6mstLvpgZuj_ZI7ejI961t95syQD?authuser=1#scrollTo=HJSc02WMUMDo&printMode=true 1/8
7/23/2020 Dimentionality Reduction - Colaboratory
https://colab.research.google.com/drive/16bIT6mstLvpgZuj_ZI7ejI961t95syQD?authuser=1#scrollTo=HJSc02WMUMDo&printMode=true 4/8
7/23/2020 Dimentionality Reduction - Colaboratory
#PCA implementation
from sklearn.decomposition import PCA
X = np.array([[-1,-1],[-2,-1,],[-3,-2],[1,1],[2,1],[3,2]])
pca = PCA(n_components=2)
pca.fit(X)
print(pca.explained_variance_ratio_)
[0.99244289 0.00755711]
print(pca.singular_values_)
[6.30061232 0.54980396]
array([[-1, -1],
[-2, -1],
[-3, -2],
[ 1, 1],
[ 2, 1],
[ 3, 2]])
y = np.array([1,1,1,2,2,2])
https://colab.research.google.com/drive/16bIT6mstLvpgZuj_ZI7ejI961t95syQD?authuser=1#scrollTo=HJSc02WMUMDo&printMode=true 7/8
7/23/2020 Dimentionality Reduction - Colaboratory
print(clf.predict([[-0.9,-3.0]]))
[1]
print(clf.predict([[-0.3,2]]))
[2]
https://colab.research.google.com/drive/16bIT6mstLvpgZuj_ZI7ejI961t95syQD?authuser=1#scrollTo=HJSc02WMUMDo&printMode=true 8/8