Are you sure you want to delete this task? Once this task is deleted, it cannot be recovered.
chenzomi 24c9001cf8 | 1 year ago | |
---|---|---|
.. | ||
images | 1 year ago | |
main.py | 1 year ago | |
readme.md | 1 year ago |
降维是一种对高维度特征数据预处理方法。降维是将高维度的数据保留下最重要的一些特征,去除噪声和不重要的特征,从而实现提升数据处理速度的目的。在实际的生产和应用中,降维在一定的信息损失范围内,可以为我们节省大量的时间和成本。降维也成为应用非常广泛的数据预处理方法。本实验主要介绍使用MindSpore在多维度数据集上进行PCA降维实验。
降维具有如下一些优点:
降维的算法有很多,比如奇异值分解(SVD)、主成分分析(PCA)、因子分析(FA)、独立成分分析(ICA)。本实验使用PCA实现降维。PCA(Principal Component Analysis)是一种使用最广泛的数据降维算法,常用于高维数据的降维,可用于提取数据的主要特征分量。其主要思想是将n维特征映射到k维上,这k维是全新的正交特征也被称为主成分,是在原有n维特征的基础上重新构造出来的k维特征。
PCA的算法步骤:
设有 m 条 n 维数据。
Iris数据集是模式识别最著名的数据集之一。数据集包含3类,每类50个实例,其中每个类都涉及一种鸢尾植物。 本实验使用iris数据集,做一个降维数据集。Iris数据集的官网:Iris Data Set。
每个样本含有4个数值属性和一个类别属性:
概括统计:
Attribute | Min | Max | Mean | SD | Class Correlation |
---|---|---|---|---|---|
sepal length: | 4.3 | 7.9 | 5.84 | 0.83 | 0.7826 |
sepal width: | 2.0 | 4.4 | 3.05 | 0.43 | -0.4194 |
petal length: | 1.0 | 6.9 | 3.76 | 1.76 | 0.9490 (high!) |
petal width: | 0.1 | 2.5 | 1.20 | 0.76 | 0.9565 (high!) |
从课程gitee仓库中下载本实验相关脚本。将脚本和数据集组织为如下形式:
pca
├── main.py
└── iris.data
本实验需要使用华为云OBS存储脚本和数据集,可以参考快速通过OBS控制台上传下载文件了解使用OBS创建桶、上传文件、下载文件的使用方法(下文给出了操作步骤)。
提示: 华为云新用户使用OBS时通常需要创建和配置“访问密钥”,可以在使用OBS时根据提示完成创建和配置。也可以参考获取访问密钥并完成ModelArts全局配置获取并配置访问密钥。
打开OBS控制台,点击右上角的“创建桶”按钮进入桶配置页面,创建OBS桶的参考配置如下:
点击新建的OBS桶名,再打开“对象”标签页,通过“上传对象”、“新建文件夹”等功能,将脚本和数据集上传到OBS桶中。上传文件后,查看页面底部的“任务管理”状态栏(正在运行、已完成、失败),确保文件均上传完成。若失败请:
推荐使用ModelArts训练作业进行实验,适合大规模并发使用。若使用ModelArts Notebook,请参考LeNet5及Checkpoint实验案例,了解Notebook的使用方法和注意事项。
导入MindSpore模块和辅助模块,设置MindSpore上下文,如执行模式、设备等。
import os
import csv
import numpy as np
import seaborn as sns
import mindspore as ms
import matplotlib.pyplot as plt
from mindspore import nn, context
from mindspore.ops import operations as ops
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
读取Iris数据集iris.data
;并检查部分数据。
with open('iris.data') as csv_file:
data = list(csv.reader(csv_file, delimiter=','))
print(data[0:5],data[50:55],data[100:105]); # print some samples
[['5.1', '3.5', '1.4', '0.2', 'Iris-setosa'],
['4.9', '3.0', '1.4', '0.2', 'Iris-setosa'],
['4.7', '3.2', '1.3', '0.2', 'Iris-setosa'],
['4.6', '3.1', '1.5', '0.2', 'Iris-setosa'],
['5.0', '3.6', '1.4', '0.2', 'Iris-setosa']]
[['7.0', '3.2', '4.7', '1.4', 'Iris-versicolor'],
['6.4', '3.2', '4.5', '1.5', 'Iris-versicolor'],
['6.9', '3.1', '4.9', '1.5', 'Iris-versicolor'],
['5.5', '2.3', '4.0', '1.3', 'Iris-versicolor'],
['6.5', '2.8', '4.6', '1.5', 'Iris-versicolor']]
[['6.3', '3.3', '6.0', '2.5', 'Iris-virginica'],
['5.8', '2.7', '5.1', '1.9', 'Iris-virginica'],
['7.1', '3.0', '5.9', '2.1', 'Iris-virginica'],
['6.3', '2.9', '5.6', '1.8', 'Iris-virginica'],
['6.5', '3.0', '5.8', '2.2', 'Iris-virginica']]
数据集有3类样本共150条,将样本的4个属性作为自变量$X$,将样本的3个类别映射为{0, 1, 2},作为因变量$Y$。
label_map = {
'Iris-setosa': 0,
'Iris-versicolor': 1,
'Iris-virginica': 2
}
X = np.array([[float(x) for x in s[:-1]] for s in data[:150]], np.float32)
Y = np.array([label_map[s[-1]] for s in data[:150]], np.int32)
利用MindSpore提供的ReduceMean,Reshape,MatMul,TopK,GatherV2等算子,通过矩阵运算的方式对输入样本数据进行操作,计算出降维到k维后的数据。
class PcaNet(nn.Cell):
def __init__(self):
super(PcaNet, self).__init__()
self.reduce_mean = ops.ReduceMean(keep_dims=True)
self.reshape = ops.Reshape()
self.matmul_a = ops.MatMul(transpose_a=True)
self.matmul_b = ops.MatMul(transpose_b=True)
self.top_k = ops.TopK(sorted=True)
self.gather = ops.GatherV2()
def construct(self, x, dim=2):
'''
x:输入矩阵
dim:降维之后的维度数
'''
m = X.shape[0]
# 计算张量的各个维度上的元素的平均值
mean = self.reduce_mean(x, axis=1)
# 去中心化
x_new = x - self.reshape(mean, (-1, 1))
# 无偏差的协方差矩阵
cov = self.matmul_a(x_new, x_new) / (m - 1)
# 计算特征分解
cov = cov.asnumpy()
e, v = np.linalg.eigh(cov)
# 将特征值从大到小排序,选出前dim个的index
e_index_sort = self.top_k(ms.Tensor(e), dim)[1]
# 提取前排序后dim个特征向量
v_new = self.gather(ms.Tensor(v), e_index_sort, 0)
# 降维操作
pca = self.matmul_b(x_new, v_new)
return pca
将样本数据转换为Tensor格式后进行PCA降维操作;将4维的iris数据通过PCA转化为2维数据,再通过matplotlib
模块进行可视化展示出来。
net = PcaNet()
pca_data = ms.Tensor(np.reshape(X,(X.shape[0],-1)),ms.float32)
pca_data = net(pca_data,dim=2)
# 可视化数据降维后效果图
color_mapping = {0: sns.xkcd_rgb['bright purple'],1: sns.xkcd_rgb['pale red'], 2: sns.xkcd_rgb['green']}
colors = list(map(lambda x: color_mapping[x], Y))
plt.scatter(pca_data[:, 0].asnumpy(), pca_data[:, 1].asnumpy(), c=colors)
plt.show()
创建训练作业时,运行参数会通过脚本传参的方式输入给脚本代码,脚本必须解析传参才能在代码中使用相应参数。如data_url对应数据存储路径(OBS路径),脚本对传参进行解析后赋值到args
变量里,在后续代码里可以使用。
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--data_url', required=True, default=None, help='Location of data.')
args, unknown = parser.parse_known_args()
MindSpore暂时没有提供直接访问OBS数据的接口,需要通过ModelArts自带的moxing框架与OBS交互。将OBS桶中的数据拷贝至执行容器中,供MindSpore使用:
import moxing
# src_url形如's3://OBS/PATH',为OBS桶中数据集的路径,dst_url为执行容器中的路径,两者皆为目录/皆为文件
moxing.file.copy_parallel(src_url=os.path.join(args.data_url, 'iris.data'), dst_url='iris.data')
可以参考使用常用框架训练模型来创建并启动训练作业(下文给出了操作步骤)。当模型引用了依赖包时创建训练作业同时需要在代码目录下放置相应的文件或安装包。
在代码目录中创建一个命名为“pip-requirements.txt”的文件,并且在文件中写明依赖包的包名及其版本号(可选),格式为包名==版本号。例如,代码目录对应的OBS路径下,包含启动文件,同时还存在“pip-requirements.txt”文件。代码目录的结构如下所示:
├── 模型启动文件所在OBS文件夹
├── main.py #启动文件。
├── pip-requirements.txt #定义的配置文件,用于指定依赖包的包名及版本号。
“pip-requirements.txt”文件内容如下所示:
seaborn
打开ModelArts控制台-训练管理-训练作业,点击“创建”按钮进入训练作业配置页面,创建训练作业的参考配置:
main.py
启动并查看训练过程:
本实验利用降维的思想,通过MindSpore实现了PCA降维。在Iris数据集上进行降维后,把多属性的数据转化为少数几个综合属性。PCA作为一个非监督学习的降维方法,它只需要特征值分解,就可以对数据进行压缩和去噪。
MindSpore实验,仅用于教学或培训目的。配合MindSpore官网使用。 MindSpore experiments, for teaching or training purposes only. Use it together with the MindSpore official website.
CSV Jupyter Notebook Text Python Markdown other
Dear OpenI User
Thank you for your continuous support to the Openl Qizhi Community AI Collaboration Platform. In order to protect your usage rights and ensure network security, we updated the Openl Qizhi Community AI Collaboration Platform Usage Agreement in January 2024. The updated agreement specifies that users are prohibited from using intranet penetration tools. After you click "Agree and continue", you can continue to use our services. Thank you for your cooperation and understanding.
For more agreement content, please refer to the《Openl Qizhi Community AI Collaboration Platform Usage Agreement》