使用Python,PCA对iris数据集降维2维/3维并进行2D,3D散点图绘制(包括有图例&无图例,有标题Label&无标题Label)

44 篇文章 6 订阅
39 篇文章 5 订阅

这篇博客将介绍如何使用Python,PCA对iris数据集降维2,3并进行2D,3D散点图绘制(包括有图例&无图例,有标题Label&无标题Label)。
着重介绍怎么一次性添加多类型的图例到图表,通过显式获取scatter。

# 方法1
scatter = ax.scatter(x_reduced[:, 0], x_reduced[:, 1], x_reduced[:, 2], c=iris.target)
# 方法2
scatter = plt.scatter(x_reduced[:, 0], x_reduced[:, 1], c=iris.target, marker="d")
# 添加图例名称到图标
plt.legend(handles=scatter.legend_elements()[0],
           labels=sp_names,
           title="species", loc="upper right")

1. 效果图

对鸢尾花进行PCA降维成2维后进行绘制,Seaborn效果图如下:
在这里插入图片描述

对鸢尾花进行PCA降维成2维后进行绘制,Seaborn添加标题及散点拟合线 效果图如下:

在这里插入图片描述

对鸢尾花进行PCA降维成3维后进行绘制,Matplotlib3D效果图如下:

在这里插入图片描述
对鸢尾花进行PCA降维成3维简单绘制效果图如下:
在这里插入图片描述

对鸢尾花进行PCA降维成3维后进行绘制,添加中文标题,xyz轴描述及图例,不同类型用不同的样式mark 效果图如下:
在这里插入图片描述

2. 源码

# 使用PCA对鸢尾花特征进行降维2维/3维并绘制 2D,3D散点图(有图例&无图例,有标题Lable&无标题Lable)
# python iris_pca.py
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
from sklearn import datasets
from sklearn.decomposition import PCA

# 支持中文
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

iris = datasets.load_iris()
R = np.array(iris.data)

R_cov = np.cov(R, rowvar=False)

iris_covmat = pd.DataFrame(data=R_cov, columns=iris.feature_names)
iris_covmat.index = iris.feature_names
eig_values, eig_vectors = np.linalg.eig(R_cov)


def plot_2D_Seaborn():
    featureVector = eig_vectors[:, :2]
    featureVector_t = np.transpose(featureVector)
    # R is the original iris dataset
    R_t = np.transpose(R)
    newDataset_t = np.matmul(featureVector_t, R_t)
    newDataset = np.transpose(newDataset_t)

    # 可视化 绘制2D图
    # 鸢尾花数据创建dataframe
    df = pd.DataFrame(data=newDataset, columns=['PC1', 'PC2'])
    y = pd.Series(iris.target)
    y = y.replace(0, 'setosa')
    y = y.replace(1, 'versicolor')
    y = y.replace(2, 'virginica')
    df['Target'] = y
    # 绘制2维数据,fit_reg是否拟合线
    sns.lmplot(x='PC1', y='PC2', data=df, hue='Target', fit_reg=False, legend=True)
    # sns.lmplot(x='PC1', y='PC2', data=df, hue='Target', fit_reg=True, legend=True)
    plt.title("Seaborn鸢尾花降维2维图")  # 会被截取不全
    plt.show()


# PCA降2维
# 无图例 & 有图例
def plot_2D_PCA_Legend():
    # 进行PCA降维
    x_reduced = PCA(n_components=2).fit_transform(iris.data)
    y = pd.Series(iris.target)
    y = y.replace(0, 'setosa')
    y = y.replace(1, 'versicolor')
    y = y.replace(2, 'virginica')

    fig = plt.figure()
    ax = fig.add_subplot()
    # 2D散点图无图例
    ax.scatter(x_reduced[:, 0], x_reduced[:, 1], c=iris.target, marker="d")
    plt.show()

    print(np.unique(np.array(y)).tolist())
    print(np.unique(iris.target).tolist())
    # ax.legend(np.unique(iris.target).tolist())
    # ax.legend(np.unique(np.array(y)).tolist(),loc='upper right')

    # 2D散点图有标题,label,图例
    plt.figure(figsize=(8, 6))
    sp_names = np.unique(np.array(y)).tolist()
    scatter = plt.scatter(x_reduced[:, 0], x_reduced[:, 1], c=iris.target, marker="d")
    plt.title('鸢尾花降维2维图')
    plt.xlabel("PC1", size=18)
    plt.ylabel("PC2", size=18)
    # 添加图例名称到图标
    plt.legend(handles=scatter.legend_elements()[0],
               labels=sp_names,
               title="species", loc="upper right")
    plt.show()


# PCA降3维
# 无图例 & 有图例
def plot_3D_PCA_Legend():
    # 进行PCA降维
    x_reduced = PCA(n_components=3).fit_transform(iris.data)
    y = pd.Series(iris.target)
    y = y.replace(0, 'setosa')
    y = y.replace(1, 'versicolor')
    y = y.replace(2, 'virginica')

    fig = plt.figure()
    ax = Axes3D(fig)
    # 3D散点图无图例
    ax.scatter(x_reduced[:, 0], x_reduced[:, 1], x_reduced[:, 2], c=iris.target)
    plt.show()

    sp_names = np.unique(np.array(y)).tolist()
    fig = plt.figure()
    ax = Axes3D(fig)
    # 3D散点图有标题,label,图例
    scatter = ax.scatter(x_reduced[:, 0], x_reduced[:, 1], x_reduced[:, 2], c=iris.target)
    ax.set_title('鸢尾花降维3维图')
    ax.set_xlabel("PC1", size=18)
    ax.set_ylabel("PC2", size=18)
    ax.set_zlabel("PC3", size=18)
    # 添加图例名称到图标
    ax.legend(handles=scatter.legend_elements()[0],
              labels=sp_names,
              title="species", loc="upper right")
    plt.show()


# 不同的PCA降维
def plot_3D():
    featureVector = eig_vectors[:, :3]
    featureVector_t = np.transpose(featureVector)
    # R is the original iris dataset
    R_t = np.transpose(R)
    newDataset_t = np.matmul(featureVector_t, R_t)
    newDataset = np.transpose(newDataset_t)

    # 构建DataFrame
    df = pd.DataFrame(data=newDataset, columns=['PC1', 'PC2', 'PC3'])
    y = pd.Series(iris.target)
    y = y.replace(0, 'setosa')
    y = y.replace(1, 'versicolor')
    y = y.replace(2, 'virginica')
    df['Target'] = y

    # print(df.head(5))
    # 根据其中一列分组
    df = df.groupby("Target")
    # print(df.groups)  # key
    # print(df.get_group('setosa'))  # 某个value

    # matplot支持的点样式及点颜色
    marks = ['.', '*', 's', ',', 'o', 'v', '^', '<', '>', '1', '2', '3', '4', '8', 'p', 'P', 'h', 'H', '+', 'x', 'X',
             'D', 'd', '|', '_']
    colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'w']

    fig = plt.figure()
    ax = fig.add_subplot(projection='3d')
    ax.set_title('鸢尾花降维3维图')
    for i, key in enumerate(df.groups.keys()):
        val = df.get_group(key)
        ax.scatter(val["PC1"], val["PC2"], val["PC3"], c=colors[i % len(colors)], marker=marks[i % len(marks)],
                   label=key)
    ax.set_xlabel('PC1')
    ax.set_ylabel('PC2')
    ax.set_zlabel('PC3')
    ax.legend(loc='upper right')
    plt.show()


plot_2D_Seaborn()
plot_2D_PCA_Legend()
plot_3D_PCA_Legend()
plot_3D()

参考

  • 4
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序媛一枚~

您的鼓励是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值