clear all;
wine_data = xlsread('wine.xlsx');
method = 'BP';%PK:PCA & Kmeans PL:PCA & LVQ BP:BP Neural Network'
rate = 0.7;%训练集70%,测试集30%
N = 3;
total_cnt = size(wine_data,1);
train_cnt = round(total_cnt*rate);
test_cnt = total_cnt - train_cnt;
rand_idx = randperm(total_cnt);
train_idx = rand_idx(1:train_cnt);
test_idx = rand_idx(train_cnt+1:total_cnt);
train_data = wine_data(train_idx,2:size(wine_data,2));
train_class = wine_data(train_idx,1);
test_data = wine_data(test_idx,2:size(wine_data,2));
test_class = wine_data(test_idx,1);
dim = size(wine_data,2)-1;
%矩阵z-score标准化
train_SM = zeros(train_cnt,dim);
data_mean = mean(train_data);
data_std = std(train_data);
test_SM = zeros(test_cnt,dim);
for j = 1:dim
train_SM(:,j) = (train_data(:,j) - data_mean(j)) / data_std(j);
test_SM(:,j) = (test_data(:,j) - data_mean(j)) / data_std(j);
end
%三种方法
switch method
case 'PK'
[DS, com_num, PV, score] = PCA(train_SM, 0.66,'ShowFigure');
%初始化聚类中心
center_init = zeros(3,com_num);
for i = 1:N
center_init(i,:) = mean(score(train_class==i,:));
end
[center, train_flag, ~, ~] = Kmeans(score, N,center_init,'ShowFigure');%k-means聚类
test_score = test_SM * PV; %测试集的评分
test_flag = findMinIdx(test_score, center);
case 'PL'
[DS, com_num, PV, score] = PCA(train_SM, 0.66,'ShowFigure');
[pv,train_flag] = LVQ(score, train_class, [], (1:N)', 0.1, 1000,'ShowFigure');%LVQ
test_flag = findMinIdx(test_SM*PV, pv);
case 'BP'
target = (train_class - 1) / 2;
net = newff(train_SM',target',10,{'tansig','purelin'},'traingdx');
net.divideFcn = '';
net.trainParam.show = 50;
net.trainParam.epochs = 1000;
net.trainParam.goal = 0.001;
net.trainParam.lr = 0.01;
net = train(net,train_SM',target');
out = sim(net,train_SM');
train_flag = round(out*2+1)';
out = sim(net,test_SM');
test_flag = round(out*2+1)';
otherwise
fprintf('check method\n');
return;
end
train_correct_cnt = sum(train_flag(:,1) == train_class(:,1));
train_accuracy = train_correct_cnt / train_cnt %训练集准确率
test_correct_cnt = sum(test_flag(:,1) == test_class(:,1));
test_accuracy = test_correct_cnt / test_cnt %训练集准确率
figure;
plot(1:test_cnt,test_class(:,1),'bo');
hold on;
plot(1:test_cnt,test_flag(:,1),'b*');
legend('实际测试集分类','预测测试集分类');
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
机器学习:基于UCI葡萄酒数据集进行葡萄酒分类及产地预测(含源码+数据集文件) 源码是matlab的、源码是matlab的、源码是matlab的。重要事情说三遍!!! 共包含178组样本数据,来源于三个葡萄酒产地,每组数据包含产地标签及13种化学元素含量,即已知类别标签。 把样本集随机分为训练集和测试集,根据已有数据集训练一个能进行葡萄酒产地预测的模型,以正确区分三个产地所产出的葡萄酒, 分别采用PCA+Kmeans、PCA+LVQ、BP神经网络等方法进行模型的训练与测试,准确率都能达到95%左右。 机器学习:基于UCI葡萄酒数据集进行葡萄酒分类及产地预测 共包含178组样本数据,来源于三个葡萄酒产地,每组数据包含产地标签及13种化学元素含量,即已知类别标签。 把样本集随机分为训练集和测试集,根据已有数据集训练一个PCA+Kmeans、PCA+LVQ、BP神经网络等方法进行模型的训练与测试,准确率都能达到95%左右。 机器学习:基于UCI葡萄酒数据集进行葡萄酒分类及产地预测 共包含178组样本数据,来源于三个葡萄酒产地,每组数据包含产地标签及13种化学元素含量,即已知类别标签。
资源推荐
资源详情
资源评论
收起资源包目录
wine_classification-master.zip (6个子文件)
wine_classification-master
findMinIdx.m 405B
LVQ.m 2KB
wineAssess.m 2KB
wine.xlsx 24KB
Kmeans.m 2KB
PCA.m 1KB
共 6 条
- 1
资源评论
- 2301_808792932024-04-22资源有很好的参考价值,总算找到了自己需要的资源啦。
- 2301_774853122023-11-06资源内容总结地很全面,值得借鉴,对我来说很有用,解决了我的燃眉之急。
白话机器学习
- 粉丝: 8215
- 资源: 7687
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功