n=100,对随机2n个数据点进行支持向量机软间隔训练,写出matlab代码,求b,w使用线性增广拉格朗日法训练模型
时间: 2023-12-24 20:03:04 浏览: 73
支持向量机 Matlab代码和数据.zip
5星 · 资源好评率100%
以下是MATLAB代码实现:
```
% 生成随机数据
n = 100;
rand_data = rand(2*n,2);
X = rand_data(:,1:2);
Y = ones(2*n,1);
Y(n+1:end) = -1;
% 定义参数
C = 1;
tol = 1e-4;
max_passes = 10;
% 初始化 alpha, b, E
alpha = zeros(2*n,1);
b = 0;
E = -Y;
passes = 0;
while(passes < max_passes)
num_changed_alphas = 0;
for i = 1:2*n
% 计算预测值
f_i = E(i);
for j=1:2*n
f_i = f_i + alpha(j)*Y(j)*X(j,:)*X(i,:)';
end
% 计算误差
E_i = f_i - Y(i);
% 判断是否需要进行优化
if((Y(i)*E_i < -tol && alpha(i) < C) || (Y(i)*E_i > tol && alpha(i) > 0))
% 随机选择第二个alpha
j = randi([1 2*n],1);
while(j == i)
j = randi([1 2*n],1);
end
% 计算预测值
f_j = E(j);
for k=1:2*n
f_j = f_j + alpha(k)*Y(k)*X(k,:)*X(j,:)';
end
% 计算误差
E_j = f_j - Y(j);
% 保存旧的alpha
alpha_i_old = alpha(i);
alpha_j_old = alpha(j);
% 计算L和H
if(Y(i) == Y(j))
L = max(0,alpha(j) + alpha(i) - C);
H = min(C,alpha(j) + alpha(i));
else
L = max(0,alpha(j) - alpha(i));
H = min(C,C + alpha(j) - alpha(i));
end
% 判断L和H是否相等
if(L == H)
continue;
end
% 计算eta
eta = 2*X(i,:)*X(j,:)' - X(i,:)*X(i,:)' - X(j,:)*X(j,:)';
if(eta >= 0)
continue;
end
% 更新alpha_j
alpha(j) = alpha(j) - (Y(j)*(E_i - E_j))/eta;
% 修剪alpha_j
if(alpha(j) > H)
alpha(j) = H;
elseif(alpha(j) < L)
alpha(j) = L;
end
% 判断alpha_j是否有足够的变化量
if(abs(alpha(j) - alpha_j_old) < tol)
alpha(j) = alpha_j_old;
continue;
end
% 更新alpha_i
alpha(i) = alpha(i) + Y(i)*Y(j)*(alpha_j_old - alpha(j));
% 更新b
b1 = b - E_i - Y(i)*(alpha(i) - alpha_i_old)*X(i,:)*X(i,:)'...
- Y(j)*(alpha(j) - alpha_j_old)*X(i,:)*X(j,:)';
b2 = b - E_j - Y(i)*(alpha(i) - alpha_i_old)*X(i,:)*X(j,:)'...
- Y(j)*(alpha(j) - alpha_j_old)*X(j,:)*X(j,:)';
if(alpha(i) > 0 && alpha(i) < C)
b = b1;
elseif(alpha(j) > 0 && alpha(j) < C)
b = b2;
else
b = (b1 + b2)/2;
end
% 更新E
E = -Y;
for k=1:2*n
E(k) = E(k) + alpha(k)*Y(k)*X(k,:)*X(i,:)';
E(k) = E(k) + alpha(k)*Y(k)*X(k,:)*X(j,:)';
E(k) = E(k) + b - b1;
end
% 计数
num_changed_alphas = num_changed_alphas + 1;
end
end
% 更新passes
if(num_changed_alphas == 0)
passes = passes + 1;
else
passes = 0;
end
end
% 计算w
w = zeros(1,2);
for i=1:2*n
w = w + alpha(i)*Y(i)*X(i,:);
end
% 输出结果
disp('b:');
disp(b);
disp('w:');
disp(w);
```
阅读全文