我正在尝试构建一个简单的线性分类器。我有两个类 A 和 B,每个类都有两个特征 [x, y],因此是一个 2d 数据集。现在,我需要找到分隔这两个类的线的方程。
y =
我确切地知道什么是。是垂直于分隔两个类的线的向量,b 是沿线移动的偏差. 当 b 为 0 时,直线正好在原点。
我完全按照此链接中的说明创建了算法。 https://en.wikipedia.org/wiki/Perceptron#Learning_algorithm
但是,在某些情况下它不能正常工作。我已经直观地理解了算法的作用。在每一步中,它取一个错误分类的点,并尝试通过旋转当前线段并移动它,使该点更接近正确的类别来进行正确的分类。
我需要一个算法来帮助我找到和.
这是我在 ES6 中的算法。
const learn = (target, learning_rate = .00001) => {
const { featureVector } = target;
// shift towards object
// const nm = math.norm(lineConfig.normal);
// lineConfig.normal = math.divide(lineConfig.normal, nm);
// lineConfig.bias /= nm;
const observedFeatureVector = featureVector.concat(1);
const wc = lineConfig.normal.concat(-lineConfig.bias);
const boundaryValue = math.dot(wc, observedFeatureVector);
const curOutput = boundaryValue >= 0 ? 1 : -1;
// lineConfig.bias += learning_rate * -target.cls;
// [w -b] [x, 1]
const dv = math.multiply(observedFeatureVector, learning_rate * (target.cls - curOutput));
const w = math.add(wc, dv);
lineConfig.bias = -w.pop();
lineConfig.normal = w;
};
我通过添加 1 [x 1] 将我的输入二维数据集转换为 3d 是我们的输入向量。然后,我们需要做的就是找到只是因为偏差是自动调整的。对于每个错误分类,我将训练点向量添加到使分类更接近目标。
我的训练集是这样的:
[200, 200, 1]
[227, 347, 1]
[399, 237, 1]
[358, 344, 1]
[265, 263, 1]
[60, 20, -1]
[26, 100, -1]
[126, 95, -1]
[145, 58, -1]
[51, 141, -1]
[49, 186, -1]