matlab中的最小二乘线性分类器

use*_*583 6 matlab classification machine-learning least-squares

我很难理解如何在matlab中为我的数据实现最小二乘线性分类器.我的数据有N行,每行10列宽.每行代表一个具有10个特征的数据点.只有两个类,我的测试数据的前N/2行是Class 1,其余的是Class 2.

关于最小二乘在线的所有解释都有意义,但我无法使它们适应我的数据,我只需要一些与我的数据和最小二乘法有关的概念性解释.

3le*_*gos 8

使用最小二乘法进行线性分类

使用最小二乘法来创建线性分类器的想法是定义线性函数

f(x) = wTx
Run Code Online (Sandbox Code Playgroud) 并调整w以使其f(x)接近于1一个类的数据点并接近另一个类的数据点-1.的调整w是通过最小化用于每个数据进行点之间的平方距离f(x)和任一1-1,这取决于它的类.

二维Matlab示例

% Create a two-cluster data set with 100 points in each cluster
N = 100;
X1 = 0.3*bsxfun(@plus, randn(N, 2), [6 6]);
X2 = 0.6*bsxfun(@plus, randn(N, 2), [-2 -1]);

% Create a 200 by 3 data matrix similar to the one you have
% (see note below why 200 by 3 and not 200 by 2)
X = [[X1; X2] ones(2*N, 1)];

% Create 200 by 1 vector containing 1 for each point in the first cluster
% and -1 for each point in the second cluster
b = [ones(N, 1); -ones(N, 1)]

% Solve least squares problem
z = lsqlin(X, b);

% Plot data points and linear separator found above
y = -z(3)/z(2) - (z(1)/z(2))*x;
hold on;
plot(X(:, 1), X(:, 2), 'bx'); xlim([-3 3]); ylim([-3 3]);
plot(x, y, 'r');
Run Code Online (Sandbox Code Playgroud)

线性最小二乘法找到线性分隔符

关于数据矩阵中额外列的注释

我在数据矩阵中添加了一列1,以便允许分离器的移位,从而使其更加通用.如果不这样做,则强制分隔符通过原点,这通常会导致分类结果更糟.