📄 ls.m
字号:
function [test_targets, w] = LS(train_patterns, train_targets, test_patterns, weights)
% Classify using the least-squares algorithm
% Inputs:
% train_patterns - Train patterns
% train_targets - Train targets
% test_patterns - Test patterns
% Weights - Weighted for weighted least squares (Optional)
%
% Outputs
% test_targets - Predicted targets
% w - Decision surface parameters
[Dim, Nf] = size(train_patterns);
Dim = Dim + 1;
train_patterns(Dim,:) = ones(1,Nf);
test_patterns(Dim,:) = ones(1,size(test_patterns,2));
%Weighted LS or not?
switch length(weights),
case Nf + 1,
%Ada boost form
weights = weights(1:Nf);
case Nf,
%Do nothing
otherwise
weights = ones(1, Nf);
end
w = (inv((train_patterns .* (ones(Dim,1)*weights)) * train_patterns') * (train_patterns .* (ones(Dim,1)*weights)) * train_targets')';
test_targets = w * test_patterns;
%If there are only two classes, collapse the targets to classes {1,2}
if (length(unique(train_targets)) == 2)
test_targets = (test_targets > mean(unique(train_targets)));
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -