%% 清空环境变量 clear clc warning off %% 导入数据 bianliang1 = xlsread('实际名称.xlsx'); %% 参数设置 leibieshumu = 2; Tag = 0; Mshumu = size(bianliang1, 1); %% 划分特征与标签 if (Tag == 1) leibieshumu = length(unique(bianliang1(:, end))); tezheng = bianliang1(: , 1 : end - 1)'; T_train = bianliang1(: , end)'; else tezheng = bianliang1'; % 输入特征 end %% 数据归一化 [pca1, ps_input] = mapminmax(tezheng, 0, 1); %% 建立网络 wangluo = newsom(pca1, leibieshumu); %% 设置训练参数 wangluo.trainParam.epochs = 3000; wangluo.trainParam.goal = 1e-5; wangluo.trainParam.lr = 0.01; %% 训练网络 wangluo = train(wangluo, pca1); %% 仿真预测 yuce = sim(wangluo, pca1); %% 反归一化 jvle = vec2ind(yuce); %% 降维应用主成分分析 pca1 = pca1'; [~, pc_train] = pca(pca1); %% 根据类别的空间预设置 kongjian1 = []; kongjian2 = []; %% 聚类类别 for i = 1: Mshumu % 聚类1 if jvle(i) == 1 kongjian1 = [kongjian1; pc_train(i, 1 : 2)]; end % 聚类2 if jvle(i) == 2 kongjian2 = [kongjian2; pc_train(i, 1 : 2)]; end end %% 绘制聚类类别散点图 figure plot(kongjian1(:, 1), kongjian1(:, 2), '*', 'LineWidth', 1) hold on plot(kongjian2(:, 1), kongjian2(:, 2), '*', 'LineWidth', 1) hold on %% 空间预设置 kongjian1 = []; kongjian2 = []; %% 存在真实类别 if Tag == 1 for i = 1: Mshumu % 类别1 if T_train(i) == 1 kongjian1 = [kongjian1; pc_train(i, 1 : 2)]; end % 类别2 if T_train(i) == 2 kongjian2 = [kongjian2; pc_train(i, 1 : 2)]; end end %% 绘制真实类别散点图 plot(kongjian1(:, 1), kongjian1(:, 2), 'o', 'LineWidth', 1) hold on plot(kongjian2(:, 1), kongjian2(:, 2), 'o', 'LineWidth', 1) hold on end legend('聚类类别A', '聚类类别B') xlabel('降维后第一维度') ylabel('降维后第二维度') keshihua = {'聚类可视化'}; title(keshihua) grid on
标签:123,end,%%,train,kongjian2,kongjian1,pca1 From: https://www.cnblogs.com/1963432477zxl/p/16704130.html