试图使用Scikit-learn在Python上绘制PCA,附带空白图

问题描述 投票:0回答:1

我遵循this指南来标准化我的数据并在之后进行绘制。但是,在设置数据并完成之后,它会显示一个空白图表。我还是python的新手,所以我不确定发生了什么问题。

import pandas as pd
import matplotlib as plt
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA

data = {"dogs": ["Labs","Yorkshire","Husky","Retriever"], 
        "treats": [245,227,103,66], 
        "meat": [235,247,252,267], 
        "apples": [685, 802, 750, 586], 
        "fish": [147, 160, 122, 93], 
        "toys": [193, 231, 184, 409], 
        "greens": [151, 125, 147, 339], 
        "rubbers": [750, 874, 566, 1033], 
        "shoes": [251, 211, 671, 113], 
     }

df = pd.DataFrame(data)


df.head()
pca = PCA(n_components=2)
principalComponents = pca.fit_transform(x)
principalDf = pd.DataFrame(data = principalComponents
             , columns = ['principal component 1', 'principal component 2'])
principalDf
finalDf = pd.concat([principalDf, df[['dogs']]], axis = 1)
finalDf
fig = plt.figure(figsize = (8,8))
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 = features
colors = ['r', 'g', 'b','r','b','g','r','b']
for target, color in zip(targets,colors):
    indicesToKeep = finalDf['Country'] == target
    ax.scatter(finalDf.loc[indicesToKeep, 'principal component 1'],  
               finalDf.loc[indicesToKeep, 'principal component 2'],
               c = color, s = 50)
ax.legend(targets)
ax.grid()

Stackoverflow说我还不能放入图像,所以我不能放入结果,但是得到一个空图。出现的所有内容(包括图例),但不显示任何值。

python pandas pca
1个回答
0
投票

当我运行您输入的代码时,我得到了很多错误,而不是空白图形。我已经稍微更新了代码(您可以将其与您的代码进行比较),并且更新后的代码在下面,我将获得此代码的图形。试试看。

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA

data = {"dogs": ["Labs","Yorkshire","Husky","Retriever"], 
        "treats": [245,227,103,66], 
        "meat": [235,247,252,267], 
        "apples": [685, 802, 750, 586], 
        "fish": [147, 160, 122, 93], 
        "toys": [193, 231, 184, 409], 
        "greens": [151, 125, 147, 339], 
        "rubbers": [750, 874, 566, 1033], 
        "shoes": [251, 211, 671, 113], 
     }

df = pd.DataFrame(data)

# Separating out the features
x = df.iloc[:,1:].values# Separating out the target
x = StandardScaler().fit_transform(x)

pca = PCA(n_components=2)
principalComponents = pca.fit_transform(x)
principalDf = pd.DataFrame(data = principalComponents
             , columns = ['principal component 1', 'principal component 2'])

finalDf = pd.concat([principalDf, df[['dogs']]], axis = 1)

fig = plt.figure(figsize=(10, 5))
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 = finalDf.dogs.values
colors = ['r','g','b','y']
for target, color in zip(targets,colors):
    indicesToKeep = finalDf['dogs'] == target
    ax.scatter(finalDf.loc[indicesToKeep, 'principal component 1']
               , finalDf.loc[indicesToKeep, 'principal component 2']
               , c = color
               , s = 50)
ax.legend(targets)
ax.grid()
© www.soinside.com 2019 - 2024. All rights reserved.