如何在Python上可视化PCA?

时间:2019-07-18 14:07:38

标签: python pca

在整个机器学习世界中,我还很陌生。首先,我想可视化我的PCA组件,但是我认为我在for扣中犯了一个错误:有人看到任何错误吗? 谢谢!

import matplotlib.pyplot as plt

file = pd.read_excel('C:/Users/KER1NU/Desktop/397bar.xlsx','Tabelle1',index_col=None, na_values=['NA']) 

X=pd.read_excel('C:/Users/KER1NU/Desktop/397bar.xlsx','Tabelle1',index_col=None, na_values=['NA'])
del X['Status']#Alle Spalten außer die Ergebnisspalte "Status"


y = file.Status
print(y.shape)
print(y.head)

from sklearn.preprocessing import StandardScaler 

X_Standarized = StandardScaler().fit_transform(X)

print(X_Standarized)

from sklearn.decomposition import PCA

pca = PCA(n_components=2)

principalComponents = pca.fit_transform(X_Standarized)

principalDf = pd.DataFrame(data = principalComponents
 , columns = ['principal component 1', 'principal component 2'])

finalDf = pd.concat([principalDf, file[['Status']]], axis = 1)

print(finalDf)

fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(1,1,1) 
ax.set_xlabel('Principal Component 1', fontsize = 15)
ax.set_ylabel('Principal Component 2', fontsize = 15)
ax.set_title('2 component PCA', fontsize = 20)
targets = ['Iris-setosa', 'Iris-versicolor']
colors = ['r', 'g', 'b']
for target, color in zip(targets,colors):
    indicesToKeep = finalDf['Status'] == target
    ax.scatter(finalDf.loc[indicesToKeep, 'principal component 1']
               , finalDf.loc[indicesToKeep, 'principal component 2']
               , c = color
               , s = 50)
ax.legend(targets)
ax.grid() ```

0 个答案:

没有答案