我试图建立一个简单的线性分类器。我有两个A类和B类,每个类都有两个特性X,y,因此有一个2d数据集。现在,我需要找到分隔这两个类的直线的方程。
Y= \vec w \vec x - b
我很清楚\vec w是什么。\vec w是垂直于分隔两个类的直线的向量,b是沿\vec w移动直线的偏差。当b为0时,直线正好位于原点。
我创建的算法与这个链接中提到的完全一样。https://en.wikipedia.org/wiki/Perceptron#Learning_算法
但是,在某些情况下,它不能正常工作。我直觉地理解了算法的作用。在每一步,它采取一个错误的点,并试图作出正确的分类,通过旋转当前的线段,并移动它,使点更接近正确的类别。
我需要一个算法来帮助我找到\vec w 和b 。
这是我在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,这是我们的输入向量。然后,我们所要做的就是找到\vec w ,因为偏置是自动调整的。对于每一个错误分类,我都会将训练点向量添加到\vec w 中,从而使分类更接近目标。
我的训练是:
[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]发布于 2019-09-04 13:26:08
事实证明,我的实现是正确的,但是由于输入值的范围很大,我花了很长时间才找到解决方案。因此,经过归一化后的距离归一化,感知器算法收敛很快。
发布于 2019-05-14 08:55:10
但是,在某些情况下,它不能正常工作。
由于你没有提供你的数据,我只能大胆地假设。感知器只能对线性分离的数据进行分类,不能对不可线性分离的数据进行聚类.因此,这可能是意外行为的一个原因。
干杯!
https://datascience.stackexchange.com/questions/51925
复制相似问题