clc clear [a,b,c,d,e] = textread('H:\Example\Iris\iris.data','%f%f%f%f%s','delimiter',','); setosa = [a(1:50),b(1:50),c(1:50),d(1:50)]; versicolor = [a(51:100),b(51:100),c(51:100),d(51:100)]; clear a b c d %% �����������ֵ��Э������� p = 0.3; [m_c1,cov_c1 ] = bayes_train(setosa); [m_c2,cov_c2 ] = bayes_train(versicolor); %% ���� test_x = [versicolor(1:41,:);setosa(1:30,:)]; %% ������Լ��� P1 = post_prob(test_x,p,m_c1,cov_c1); P2 = post_prob(test_x,p,m_c2,cov_c2); threshold = P1 - P2; t1 = 0; t2 = 0; for k = 1:length(threshold) if threshold(k) > 0 t2 = t2 + 1; else t1 = t1 + 1; end end display(['��',num2str(t2),'��setosa����']) display(['��',num2str(t1),'��versicolor����']) 复原matlab代码
时间: 2023-09-10 14:05:53 浏览: 57
clc;
clear;
[a,b,c,d,e] = textread('H:\Example\Iris\iris.data','%f%f%f%f%s','delimiter',',');
setosa = [a(1:50),b(1:50),c(1:50),d(1:50)];
versicolor = [a(51:100),b(51:100),c(51:100),d(51:100)];
clear a b c d;
p = 0.3;
[m_c1,cov_c1] = bayes_train(setosa);
[m_c2,cov_c2] = bayes_train(versicolor);
test_x = [versicolor(1:41,:);setosa(1:30,:)];
P1 = post_prob(test_x,p,m_c1,cov_c1);
P2 = post_prob(test_x,p,m_c2,cov_c2);
threshold = P1 - P2;
t1 = 0;
t2 = 0;
for k = 1:length(threshold)
if threshold(k) > 0
t2 = t2 + 1;
else
t1 = t1 + 1;
end
end
display(['有',num2str(t2),'个setosa类']);
display(['有',num2str(t1),'个versicolor类']);
相关问题
%数据加载 clear clc load iris.dat; %importdata('iris.dat') %数据预处理 A1=randsample(150,118,false); trainData1=iris(A1,:) A2=randsample(150,32,false); testData1=iris(A2,:) save('trainData1.txt','trainData1','-ascii'), save('testData1.txt','testData1','-ascii'), [f1,f2,f3,f4,class]=textread('trainData1.txt','%f%f%f%f%f',150); [imput,minI,maxI]=premnmx([f1,f2,f3,f4]'); %创建神经网络 s=length(class); output=zeros(s,3); for i=1:s output(i,class(i))=1; end %设置训练参数 net=newff(minmax(input),[10 3],{'logsig','logsig'},'traingdx'); net.trainparam.show=50;%显示中间结果的周期 net.trainparam.epochs=500;%最大迭代数(学习次数) net.trainparam.goal=0.01;%学习速率 net.trainparam.lr=0.01;%学习效率 net=train(net,input,output); %开始测试 [t1,t2,t3,t4,c]=textread('testData1.txt','%f%f%f%f%f',150); [textInput,minI,maxI]=premnmx([t1,t2,t3,t4]'); Y=sim(net,textInput); [s1,s2]=size(Y); hiNum=0; for i=1:s2; [m,Index]=max(Y(:,i)); if(Index==c(i)) hiNum=hiNum+1; end end %统计识别率
这段代码涉及到神经网络的训练和测试过程,其主要步骤如下:
1. 加载数据:从 iris.dat 文件中读取数据,然后将数据随机分为训练集和测试集。
2. 数据预处理:使用 MATLAB 自带的数据预处理函数 `premnmx` 对输入数据进行归一化。
3. 创建神经网络:使用 `newff` 函数创建一个 2 层的神经网络,第 1 层有 10 个神经元,第 2 层有 3 个神经元,激活函数都是 `logsig`。
4. 设置训练参数:设置神经网络的训练参数,包括最大迭代次数、学习速率、学习效率等。
5. 训练神经网络:使用 `train` 函数对神经网络进行训练。
6. 测试神经网络:使用 `sim` 函数对测试集进行预测,然后统计预测正确的数量,计算识别率。
需要注意的是,这段代码中存在一些问题:
1. `input` 变量没有定义,应该改为 `imput`。
2. 训练参数中的学习率 `lr` 和目标误差 `goal` 的值可能需要根据具体情况进行调整。
3. 统计识别率时,应该将 `s1` 改为 150,因为测试集有 150 个样本。
4. 在 `for` 循环中,应该将 `Index` 改为 `index`,因为 MATLAB 是区分大小写的。
以下是修改后的代码:
```
% 数据加载
clear clc
load iris.dat;
% 随机分割训练集和测试集
A1 = randsample(150, 118, false);
trainData1 = iris(A1, :);
A2 = setdiff(1:150, A1);
testData1 = iris(A2, :);
save('trainData1.txt', 'trainData1', '-ascii');
save('testData1.txt', 'testData1', '-ascii');
% 数据预处理
[f1, f2, f3, f4, class] = textread('trainData1.txt', '%f%f%f%f%f', 150);
[input, minI, maxI] = premnmx([f1, f2, f3, f4]');
s = length(class);
output = zeros(s, 3);
for i = 1:s
output(i, class(i)) = 1;
end
% 创建神经网络
net = newff(minmax(input), [10 3], {'logsig', 'logsig'}, 'traingdx');
% 设置训练参数
net.trainparam.show = 50; % 显示中间结果的周期
net.trainparam.epochs = 500; % 最大迭代数(学习次数)
net.trainparam.goal = 0.01; % 目标误差
net.trainparam.lr = 0.01; % 学习率
% 训练神经网络
net = train(net, input, output);
% 测试神经网络
[t1, t2, t3, t4, c] = textread('testData1.txt', '%f%f%f%f%f', 150);
[textInput, minI, maxI] = premnmx([t1, t2, t3, t4]');
Y = sim(net, textInput);
[s1, s2] = size(Y);
hiNum = 0;
for i = 1:s2
[m, index] = max(Y(:, i));
if index == c(i)
hiNum = hiNum + 1;
end
end
% 统计识别率
accuracy = hiNum / 150;
disp(['识别率为:', num2str(accuracy)]);
```
解决以下代码对于此运算,数组的大小不兼容的问题:clear all; clc; % 初始值 h = 8; R = 3; r1 = 0:0.01:R; r2 = -h:0.01:0; phai = 0:0.01:2*pi; % 球部分 x1 = sqrt(R*R-r1.*r1).*cos(phai); y1 = sqrt(R*R-r1.*r1).*sin(phai); z1 = r1+h; % 圆锥部分 x2 = (R/h)*(r2+h).*cos(phai); y2 = (R/h)*(r2+h).*sin(phai); z2 = r2+h; figure plot3(x1,y1,z1,'LineWidth',2); hold on; plot3(x2,y2,z2,'LineWidth',2); xlabel('x'); ylabel('y'); zlabel('z'); title('降落伞');
将圆锥部分的 `r2` 数组改为 `0:0.01:R*h/h` 即可解决数组大小不兼容的问题。因为圆锥的底面半径为 `R`,高度为 `h`,所以底面周长为 `2*pi*R`,而在这段代码中,`r2` 的范围却是 `-h:0.01:0`,长度不足以表示一个完整的底面,需要将其修改为 `0:0.01:R*h/h`,即从 `0` 开始到底面周长的一半结束。修改后的代码如下:
```
clear all;
clc;
% 初始值
h = 8;
R = 3;
r1 = 0:0.01:R;
r2 = 0:0.01:R*h/h; % 修改此处
phai = 0:0.01:2*pi;
% 球部分
x1 = sqrt(R*R-r1.*r1).*cos(phai);
y1 = sqrt(R*R-r1.*r1).*sin(phai);
z1 = r1+h;
% 圆锥部分
x2 = (R/h)*(r2+h).*cos(phai);
y2 = (R/h)*(r2+h).*sin(phai);
z2 = r2+h;
figure
plot3(x1,y1,z1,'LineWidth',2);
hold on;
plot3(x2,y2,z2,'LineWidth',2);
xlabel('x');
ylabel('y');
zlabel('z');
title('降落伞');
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)