Python
2021. 5. 18. 09:46
https://plotly.com/python/pca-visualization/
PCA Visualization
Visualize Principle Component Analysis (PCA) of your high-dimensional data in Python with Plotly.
plotly.com
여기에 잘 나와있다. matplotlib를 이용하는것보다 훨씬 코드가 간단해진다.
2D
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
df = pd.read_csv("")
pca_df = StandardScaler().fit_transform(df)
pca = PCA(n_components=2)
principalComponents = pca.fit_transform(pca_df)
fig = px.scatter(principalComponents, x=0, y=1, color=df['label'])
fig.show()
3D
pca = PCA(n_components=3)
components = pca.fit_transform(df)
total_var = pca.explained_variance_ratio_.sum() * 100
fig = px.scatter_3d(
components, x=0, y=1, z=2, color=df['label'],
title=f'Total Explained Variance: {total_var:.2f}%',
labels={'0': 'PC 1', '1': 'PC 2', '2': 'PC 3'}
)
fig.show()
'Python' 카테고리의 다른 글
dataframe 범위별 구간 만들기 (0) | 2021.06.02 |
---|---|
pandas read csv시 thousand 숫자형으로 읽기 (0) | 2021.05.24 |
dataframe zero value column remove (0) | 2021.05.09 |
dataframe merge - reduce 이용 (0) | 2021.04.22 |
seaborn boxplot 그리기 (0) | 2021.04.13 |