【问题标题】:Plotting PCA results including original data with scatter plot using Python使用 Python 绘制 PCA 结果,包括带有散点图的原始数据
【发布时间】:2016-02-05 16:53:19
【问题描述】:

作为练习,我对虹膜数据进行了 PCA。这是我的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA # as sklearnPCA
import pandas as pd
#=================
df = pd.read_csv('iris.csv');
# Split the 1st 4 columns comprising values
# and the last column that has species
X = df.ix[:,0:4].values
y = df.ix[:,4].values

X_std = StandardScaler().fit_transform(X);  # standardization of data

# Fit the model with X_std and apply the dimensionality reduction on X_std.
pca = PCA(n_components=2) # 2 PCA components;
Y_pca = pca.fit_transform(X_std)

# How to plot my results???? I am struck here! 

请告知如何绘制我的原始虹膜数据和使用散点图得出的 PCA。

【问题讨论】:

  • 请格式化您的帖子!你都没看过吗?

标签: python numpy matplotlib scikit-learn pca


【解决方案1】:

这是我认为您可以将其可视化的方式。我将把 PC1 放在 X 轴上,将 PC2 放在 Y 轴上,并根据每个点的类别为其着色。代码如下:

#first we need to map colors on labels
dfcolor = pd.DataFrame([['setosa','red'],['versicolor','blue'],['virginica','yellow']],columns=['Species','Color'])
mergeddf = pd.merge(df,dfcolor)

#Then we do the graph
plt.scatter(Y_pca[:,0],Y_pca[:,1],color=mergeddf['Color'])
plt.show()

【讨论】:

    猜你喜欢
    • 2017-11-28
    • 1970-01-01
    • 2014-04-18
    • 2018-06-23
    • 1970-01-01
    • 2020-09-14
    • 2015-10-01
    • 2019-09-26
    • 2018-01-02
    相关资源
    最近更新 更多