๐ ์ ๋ฒ ์๊ฐ์ PCA์ ๊ฐ๋ ๋ฐ ์ฃผ์ถ์ ์ฐพ๊ธฐ๊น์ง์ ์์ธํ ๊ณผ์ ์ ์ฌ๋ฌ ๋ฐฉ๋ฉด์ผ๋ก ๋ถ์ํด๋ณด๊ณ ์์๋ณด์๋ค.
PCA(concepts)
* dimensionality reduction ๊ธฐ๋ฒ ์ค ๋ํ์ ์ธ ๋ฐฉ๋ฒ์ธ PCA์ ๋ํด์ ์์๋ณด์! concepts> โ ๊ณ ์ฐจ์์ data๋ฅผ ๋ฎ์ ์ฐจ์์ผ๋ก ์ฐจ์ ์ถ์ํ๋ ๊ธฐ๋ฒ (dimensionality reduction) โก ๊ธฐ์ค์ด ๋๋ ์ ๋ณด๋ data์ ๋ถ์ฐ! (๋ถ์ฐ
sh-avid-learner.tistory.com
๐ ์ด์ ๋ ์ง์ code๋ก ์คํํด scree plot์ผ๋ก ์๊ฐํํด๋ณด๊ณ ์ฃผ์ด์ง unsupervised data๋ฅผ ์๋ง๊ฒ clusteringํด ์ค์ data๊ฐ PC ์ถ์ ๋ง๊ฒ ์ ๋ถ๋ฆฌ๊ฐ ๋๋์ง ์ฒดํฌํด๋ณด๋ ๊ณผ์ ๊น์ง ํด ๋ณด๋ ค ํ๋ค!
๐ PCA์ ๋ํด์ ๋ฐฐ์ ๋ ๊ฐ๋ ์ ์๋ ์์ธํ 6๊ฐ์ง์ step์ผ๋ก ๋๋ ์ ์๋ค.
โ standardization
(np.mean๊ณผ np.std๋ก ์ง์ ์ฐ์ฐ์ด ๊ฐ๋ฅ / ๋๋ sklearn์ StandardScaler() ์ฌ์ฉ)
โก constructing a covariance matrix
(np.cov ์ฌ์ฉ / ๋๋ sklearn์ PCA๊ฐ ์์์ internallyํ๊ฒ ์ฐ์ฐํด์ค)
โข performing eigendecomposition of covariance matrix (decompose the matrix into its eigenvectors & eigenvalues)
(np.linalg.eig ์ฌ์ฉ / ๋๋ ์์์ ์ธ๊ธํ sklearn์ PCA ํจ์๊ฐ ์์์ ์ฐ์ฐ)
โฃ selection of most important eigenvectors & eigenvalues
(์ง์ eigenvalue๋ฅผ ๋์ดํด PC๋ฅผ ๊ณ ๋ฅด๊ฑฐ๋ / sklearn์ PCA parameter๋ก ์ ํด๋์ ์๋งํผ PC๋ฅผ ์์์ ๊ณจ๋ผ์ค๋ค)
โค constructing a projection matrix (using selected eigenvectors) - feature matrix๋ผ๊ณ ํ์!
โฅ transformation of training/test dataset
Q. diesel์ ์๋ฃ๋ก ์ฌ์ฉํ ์ฐจ์ ์ฌ์ฉํ์ง ์์ ์ฐจ์ ๋ํ ์ ๋ณด๊ฐ ๋ด๊ฒจ ์๋ ๋ฐ์ดํฐํ๋ ์์ด ์๋ค. PCA๋ฅผ ์ฌ์ฉํ๊ธฐ์ ์ ์ ํ feature columns๋ง ๊ณจ๋ผ feature reduction์ ์งํํด ๋จ ๋ ๊ฐ์ ์ฃผ์ถ PC1๊ณผ PC2๋ง ๋จ๊ฒจ๋ณด์. ๊ทธ๋ฆฌ๊ณ , target์ผ๋ก ์ธ์ ๋ diesel ์ฌ์ฉ์ฌ๋ถ๋ฅผ ๋จ ๋ ๊ฐ์ ์๋ก์ด feature(PC1, PC2)๋ก clustering์ด ์ฝ๊ฒ ๊ฐ๋ฅํ์ง pca plot๊ณผ scree plot ์๊ฐํ๋ฅผ ํตํด clustering problem์ ํด๊ฒฐ ์ฌ๋ถ ์ ๋น์ฑ์ ๋ณด์ฌ๋ผ.
A.
1> dataset ์ค๋น (null column ์ ๊ฑฐ)
#1) load the dataset
path = 'https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DA0101EN-SkillsNetwork/labs/Data%20files/module_5_auto.csv'
car = pd.read_csv(path)
car.to_csv('module_5_auto.csv')
car=car._get_numeric_data()
#1) + check if there is null data in the dataset
nulls_checked = car.isnull().sum()
nulls_col = nulls_checked[nulls_checked != 0].index[0] #deleting 'stroke' col
car = car.loc[:, car.columns!=nulls_col]
car = car.iloc[:, 3:19]
2> feature์ target ๋ถ๋ฆฌํ๊ณ feature standardization
(์ฌ๊ธฐ์ target์ diesel ์ฌ์ฉ ์ฌ๋ถ๋ฅผ ํํ์ผ๋ฉฐ, ์ค์ PCA๋ target ๋ถ๋ฆฌ ์์ด data ๊ทธ ์์ฒด๋ฅผ ์ค์ด๋ ๋ฐ ๋ชฉ์ ์ด ์์ง๋ง, ์ฐ๋ฆฌ๋ PCA์ ํจ๊ณผ๋ฅผ ์ฆ๋ช ํ๊ธฐ ์ํด, ๋จ 2์ฐจ์ ๋ง์ผ๋ก๋ data๊ฐ ์ ๋ถ๋ฆฌ๊ฐ ๋์๋ ์ง ํ์ธํด๋ณด๊ณ ์ target์ ๋ฐ๋ก ๋ถ๋ฆฌํ๊ณ ํ์ ์๊ฐํํ ์์ !)
#2) standardization
from sklearn.preprocessing import StandardScaler
target = 'diesel'
# Separating out the features
x = car.loc[:, car.columns != target].values #15 dimensions
# Separating out the target
y = car.loc[:,[target]].values
# Standardizing the features
x = StandardScaler().fit_transform(x)
print(x)
- ํ์คํ ์ดํ์ dataframe number -
3> PCA ์งํ (n_components๋ 2๋ก ์ค์ !)
โถsklearn.decomposition.PCA docu()โ
https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html
class sklearn.decomposition.PCA(n_components=None, *, copy=True, whiten=False, svd_solver='auto', tol=0.0, iterated_power='auto', n_oversamples=10, power_iteration_normalizer='auto', random_state=None)
#3) perform PCA
from sklearn.decomposition import PCA
pca = PCA(n_components=2) #dimensionality 15 to 2
principalComponents = pca.fit_transform(x) #perform PCA
principalDf = pd.DataFrame(data = principalComponents, columns = ['PC1', 'PC2'])
finalDf = pd.concat([principalDf, car[[target]]], axis = 1)
finalDf
4> pca plot & scree plot ์๊ฐํ (๋ target์ ๊ตฌ๋ถ์ ์ํด ์์ ๋ค๋ฅด๊ฒ ์ ํ)
#4) visualize 2D projection - pca plot
fig = plt.figure(figsize = (8,8))
ax = fig.add_subplot(1,1,1)
ax.set_xlabel('PC1', fontsize = 15)
ax.set_ylabel('PC2', fontsize = 15)
ax.set_title('PCA w/ two components', fontsize = 20)
targets = [0, 1]
colors = ['r', 'b']
for target, color in zip(targets,colors):
indicesToKeep = finalDf['diesel'] == target
ax.scatter(finalDf.loc[indicesToKeep, 'PC1']
, finalDf.loc[indicesToKeep, 'PC2']
, c = color
, s = 50)
ax.legend(['no diesel', 'yes diesel'])
ax.grid()
def scree_plot(pca):
num_components = len(pca.explained_variance_ratio_)
ind = np.arange(num_components)
vals = pca.explained_variance_ratio_
ax = plt.subplot()
cumvals = np.cumsum(vals)
ax.bar(ind, vals, color = ['#00da75', '#f1c40f', '#ff6f15', '#3498db']) # Bar plot
ax.plot(ind, cumvals, color = '#c0392b') # Line plot
for i in range(num_components):
ax.annotate(r"%s" % ((str(vals[i]*100)[:3])), (ind[i], vals[i]), va = "bottom", ha = "center", fontsize = 13)
ax.set_xlabel("PC")
ax.set_ylabel("Variance")
plt.title('Scree plot')
scree_plot(pca)
5> ์๊ฐํ ๊ฒฐ๊ณผ ๋ถ์
๐ ์ด 15๊ฐ์ feature๊ฐ ์กด์ฌํ๋ car data๋ฅผ variance๋ฅผ ๊ฐ์ฅ ์ ์ค๋ช ํ๋ ๋จ ๋ ๊ฐ์ ์ถ, PC1๊ณผ PC2๋ง์ผ๋ก ๋ฝ์๋ด์ด ์๊ฐํํ๋ค. ๊ทธ ๊ฒฐ๊ณผ, PC1๋ง์ผ๋ก๋ diesel ์ฌ์ฉ ์ ๋ฌด๋ฅผ ์ ํํ ํ๋ณํ ์ ์์์ง๋ง, PC2 ์ฃผ์ถ์ด ๋ํด์ง๋ค๋ฉด diesel ์ ๋ฌด๊ฐ ๊ทน๋ช ํ clustering๋ ์ ์์์ pca plot์ ํตํด ์ ์ถํ ์ ์๋ค.
๐ scree plot์ผ๋ก ์๊ฐํํ ๊ฒฐ๊ณผ ์ญ์ PC1๋ง์ผ๋ก๋ ์ ์ฒด variance์ ์ฝ 54%๋ง ์ค๋ช ๋ ์ ์์์ ์ ์ ์๋ค. ์ฌ๊ธฐ์ PC2 17%๊ฐ ๋ํด์ง ํฉ๊ณ ์ฝ 70%์ ์ค๋ช ๋ ฅ์ผ๋ก diesel ์ฌ์ฉ ์ ๋ฌด๊ฐ clustering์ด ๊ฐ๋ฅํจ์ ์ ์ ์๋ค.
๐ dataset ์์ฒด ์์ผ๋ก๋ง ๋ดค์ ๋๋ diesel์ด ์์ ๊ฒฝ์ฐ์ data๊ฐ ์๋์ ์ผ๋ก ๋น์ค์ด ์ ๊ฒ ์ฐจ์งํด, ์ ์ด์ data target ๋ถ๊ท ํ ๋ฌธ์ ๊ฐ ์๊ฒผ๋ค. ์ข ๋ ๊ท ์ผํ target ๋ถํฌ์ data์ ์ถฉ๋ถํ ๊ฐฏ์์ data๊ฐ ์กด์ฌํ๋ค๋ฉด ๋ ์ข์ clustering ๊ฒฐ๊ณผ๊ฐ ๋์ค์ง ์์์ ๊น ์์ธกํด ๋ณธ๋ค.
๐ต๏ธ ์ถํ
→ ์ค์ ๋ก PCA๋ฅผ ํตํด data reduction(extraction) ํจ๊ณผ๋ฅผ pca plot๊ณผ scree plot์ ํตํด ์ฆ๋ช ํด ๋ณด์๋ค! ์ถํ์๋ ์ค์ ML ๋ชจ๋ธ๋ง์ ์ ์ฉํ ๋ PCA ์ด์ ๊ณผ ์ดํ data๋ฅผ model์ ์ง์ด๋ฃ์์ ๊ฒฝ์ฐ์ ๋ฐ๋ฅธ elasped time๊ณผ model accuracy์ ์ฐจ์ด๋ฅผ ์ฆ๋ช ํด๋ณด๊ณ ์ ํ๋ค ๐
* ์ถ์ฒ1) https://vitalflux.com/feature-extraction-pca-python-example/
* ์ถ์ฒ2) https://towardsdatascience.com/pca-using-python-scikit-learn-e653f8989e60
'Machine Learning > Fundamentals' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
All About Evaluation Metrics (2/2) โ MAPE, MPE (0) | 2022.06.11 |
---|---|
Unsupervised Learning (0) | 2022.06.03 |
PCA(concepts) (0) | 2022.05.30 |
Feature Selection vs. Feature Extraction (0) | 2022.05.18 |
feature selection (1) - selectKBest (+jointplot) (0) | 2022.04.20 |
๋๊ธ