修改下列代码使得在命令框中可以得到分界面的方程data=[1 0 1;1 1 1;0 2 1;2 1 -1;2 2 -1;1 3 -1]; X = data(:, [1, 2]); y = data(:, 3); m = size(X, 1); plotData2(X, y); X = [ones(m, 1) X]; error = 1; W = [1;1;1]; first = 1; tim = 0; alph = 0.1; hold on axis([-3 3 -3 3]); x1 = -2:0.2:2; while error > 0 error = 0; for i = 1:m if ((W'*(X(i,:)'))*y(i) <= 0) || (first == 1) first = 0; error = error + 1; tim = tim + 1; W = W + alph*(X(i,:)')*y(i); fprintf('%dn', tim); if W(3) ~= 0 && W(2) ~= 0 y1 = -(W(1)/W(3) + W(2)*x1/W(3)); plot(x1, y1, '-b'); end end end end if W(3) ~= 0 && W(2) ~= 0 y1 = -(W(1)/W(3) + W(2)*x1/W(3)); plot(x1, y1, 'r'); end hold off
时间: 2023-12-13 13:04:45 浏览: 102
下面是修改后的代码:
```matlab
data=[1 0 1;1 1 1;0 2 1;2 1 -1;2 2 -1;1 3 -1];
X = data(:, [1, 2]);
y = data(:, 3);
m = size(X, 1);
X = [ones(m, 1) X];
error = 1;
W = [1;1;1];
first = 1;
tim = 0;
alph = 0.1;
while error > 0
error = 0;
for i = 1:m
if ((W'*(X(i,:)'))*y(i) <= 0) || (first == 1)
first = 0;
error = error + 1;
tim = tim + 1;
W = W + alph*(X(i,:)')*y(i);
fprintf('%dn', tim);
end
end
end
if W(3) ~= 0 && W(2) ~= 0
fprintf('分界面方程为: y = %fx + %f\n', -W(2)/W(3), -W(1)/W(3));
end
```
这段代码同样实现了感知机算法,找到了数据集中的分割直线,并在命令窗口输出了分界面的方程。其中 `W` 是分割直线的参数,`error` 表示分类错误的点的数量,`alph` 是学习率,`tim` 是迭代次数。
相关问题
修改下列代码使得能得到输出分界面图像的同时在命令框中得到分界面方程data=[1 0 1;1 1 1 ;0 2 1; 2 1 -1 ; 2 2 -1;1 3 -1 ]; X = data(:, [1, 2]); y = data(:, 3); m = size(X,1); plotData2(X,y); X = [ones(m,1) X]; error = 1; W=[1;1;1]; first = 1; tim=0; alph=0.1; hold on axis([-3 3 -3 3]); x1=-2:0.2:2; while error>0 error=0; for i = 1:m if ( ((W'*(X(i,:)'))*y(i) <= 0 )|| (first == 1) ) first=0; error=error+1; tim=tim+1; W = W + alph*(X(i,:))'*y(i); fprintf('%dn',tim); if W(3)~=0 && W(2)~=0 y1=-(W(1)/W(3)+W(2)*x1/W(3)); plot(x1,y1,'-b'); end; end; end; end; if W(3)~=0 && W(2)~=0 y1=-(W(1)/W(3)+W(2)*x1/W(3)); plot(x1,y1,'r'); end; hold off
修改后的代码如下:
```
data=[1 0 1;1 1 1;0 2 1;2 1 -1;2 2 -1;1 3 -1];
X = data(:, [1, 2]);
y = data(:, 3);
m = size(X,1);
plotData2(X,y);
X = [ones(m,1) X];
error = 1;
W = [1;1;1];
first = 1;
tim = 0;
alph = 0.1;
hold on
axis([-3 3 -3 3]);
x1 = -2:0.2:2;
while error > 0
error = 0;
for i = 1:m
if ( ((W'*(X(i,:)'))*y(i) <= 0 )|| (first == 1) )
first = 0;
error = error + 1;
tim = tim + 1;
W = W + alph*(X(i,:))'*y(i);
fprintf('%dn',tim);
end;
end;
if W(3) ~= 0 && W(2) ~= 0
y1 = -(W(1)/W(3)+W(2)*x1/W(3));
plot(x1,y1,'-b');
fprintf('分界面方程:%dx1 + %dx2 + %d = 0\n', W(2), W(3), W(1));
end;
end;
if W(3) ~= 0 && W(2) ~= 0
y1 = -(W(1)/W(3)+W(2)*x1/W(3));
plot(x1,y1,'r');
end;
hold off
```
修改的部分:
1. 在分界面绘制的if语句中加入了输出分界面方程的语句。
2. 在while循环中,每次更新分界面权重W后,都会判断分界面方程是否需要输出。
修改下列代码使得能得到输出分界面图像的同时在命令框中得到分界面方程 data=[1 0 1;1 1 1 ;0 2 1; 2 1 -1 ; 2 2 -1;1 3 -1 ]; X = data(:, [1, 2]); y = data(:, 3); m = size(X,1); plotData2(X,y); X = [ones(m,1) X]; error = 1; W=[1;1;1]; first = 1; tim=0; alph=0.1; hold on axis([-3 3 -3 3]); x1=-2:0.2:2; while error>0 error=0; for i = 1:m if ( ((W'*(X(i,:)'))*y(i) <= 0 )|| (first == 1) ) first=0; error=error+1; tim=tim+1; W = W + alph*(X(i,:))'*y(i); fprintf('%dn',tim); if W(3)~=0 && W(2)~=0 y1=-(W(1)/W(3)+W(2)*x1/W(3)); plot(x1,y1,'-b'); end; end; end; end; if W(3)~=0 && W(2)~=0 y1=-(W(1)/W(3)+W(2)*x1/W(3)); plot(x1,y1,'r'); end; hold off
下面是修改后的代码:
```
data=[1 0 1;1 1 1 ;0 2 1; 2 1 -1 ; 2 2 -1;1 3 -1 ];
X = data(:, [1, 2]);
y = data(:, 3);
m = size(X,1);
plotData2(X,y);
X = [ones(m,1) X];
error = 1;
W=[1;1;1];
first = 1;
tim=0;
alph=0.1;
hold on
axis([-3 3 -3 3]);
x1=-2:0.2:2;
while error>0
error=0;
for i = 1:m
if ( ((W'*(X(i,:)'))*y(i) <= 0 )|| (first == 1) )
first=0;
error=error+1;
tim=tim+1;
W = W + alph*(X(i,:))'*y(i);
fprintf('%dn',tim);
end;
end;
end;
if W(3)~=0 && W(2)~=0
y1=-(W(1)/W(3)+W(2)*x1/W(3));
plot(x1,y1,'r');
disp(['Final decision boundary: ', num2str(W(1)), ' + ', num2str(W(2)), 'x1 + ', num2str(W(3)), 'x2 = 0']);
end;
hold off
```
修改后的代码将绘制决策边界的代码移到了 while 循环外面,在 while 循环内只进行权重的更新,直到满足停止条件为止。在程序结束后,在命令框中输出最终的决策边界方程,并在图像中绘制出来。
阅读全文