主成分分析法 (PCA) 用于数据可视化实验 -- Matlab版

第一步:下载数据集。

https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass.html#pendigits

 

第二步:改变数据格式。

注:此数据集的各特征值均为像素,即属于同一量纲,故无需归一化步骤。

原格式为:8 1:88 2:92 3:2 4:99 5:16 6:66 7:94 8:37 9:70 12:24 13:42 14:65 15:100 16:100

改为:88 92 2 99 16 66 94 37 70 0 0 24 42 65 100 100 8

Java代码:

 

public class dataformat {
	public static void main(String[] args) throws IOException
	{
		String filename = "pendigits.t";
		String outfilename = "pendigits";
		int demension = 16;
		
		try(Scanner in = new Scanner(new FileInputStream(filename));
		PrintWriter out = new PrintWriter(outfilename))
		{
			String line;
			while( in.hasNextLine())
			{
				line = in.nextLine();
				//按空格将数据切分
				String[] subComponent = line.trim().split(" ");
				
				//data用于记录对应维度下的特征值
				String[] data = new String[demension];
				
				//第0个是类标,所以从1开始,构造样例的全部特征值
				for (int i = 1; i < subComponent.length; ++i)
				{
					String[] kv = subComponent[i].trim().split(":");
					//将对应维度下的特征值赋值。
					data[Integer.parseInt(kv[0])-1] = kv[1];
				}
				
				//sb用来构造一行样例
				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < demension; ++i)
				{
					if (data[i] != null)
						sb.append(data[i]+" ");
					else
						sb.append("0 "); //如果对应维度下的字符串为null,说明为0值。
				}
				sb.append(subComponent[0].trim());//末尾加类标
				out.println(sb.toString());	//写文件
			}
		}
		
	}
}


第三步:Matlab作图。

将处理好的文件pendigits加入MATLAB路径;

新建脚本pcaVisual.m,代码如下:

load pendigits; % 加载数据集,前提是pendigits文件在MATLAB路径下
[m,n]=size(pendigits); % 获得pendigts的行数m,列数n
[pc, score, latent, tsquare]=pca(pendigits(:,1:n-1));

% pca是主成分分析算法,参数是需要降维的矩阵pendigits(:,1:n-1)——除去类标列。
% 返回的结果:
% pc是(n-1)*(n-1)系数矩阵,用它将pendigits(:,1:n-1)转换为score
% score是转换之后的特征值,大小为m*(n-1),按latent降序排列,按需取前k列,此处我们只需取前三列。
% latent:是一维列向量,大小为(n-1)*1,每一个数据是对应score里相应维的贡献率,降序排列

plot3(0,0,0); % plot(0,0)可用于展示2维图形,但用旋转按钮进行拖动的话,可以看到,其实际就是3维。
hold on;

% 根据样例的列表来画图
for i=1:m
    switch pendigits(i,n)
        case 0
            plot3(score(i,1),score(i,2),score(i,3),'y*');
        case 1
            plot3(score(i,1),score(i,2),score(i,3),'m*');
        case 2
            plot3(score(i,1),score(i,2),score(i,3),'c*');
        case 3
            plot3(score(i,1),score(i,2),score(i,3),'r*');
        case 4
            plot3(score(i,1),score(i,2),score(i,3),'g*');
        case 5
            plot3(score(i,1),score(i,2),score(i,3),'b*');
        case 6
            plot3(score(i,1),score(i,2),score(i,3),'w*');
        case 7
            plot3(score(i,1),score(i,2),score(i,3),'k*');
        case 8
            plot3(score(i,1),score(i,2),score(i,3),'kd');
        otherwise
            plot3(score(i,1),score(i,2),score(i,3),'kv');
    end
end

 

三维立体图:

 

点击箭头所指按钮可以三维旋转来观察其结构。

 

二维平面图:

 

 

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值