PLA算法实现

这篇博客介绍了感知机算法的原理,并分别使用Python和C++实现了该算法。在Python中,通过从头编写代码展示了感知机模型的训练过程;而在C++中,同样构建了感知机模型并进行训练。文章提供了完整的代码示例,帮助读者理解感知机的工作机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原理参考:PLA算法

代码传送门:https://github.com/taifyang/machine-learning

python实现:

import numpy as np

#感知机模型
class Perceptron:
    def __init__(self,x,y):
        self.x=x
        self.y=y
        self.w=np.zeros(self.x.shape[1]) #初始化权重w
        self.b = 0

    def sign(self, w, b, x):
        y = np.dot(x, w)+b
        return int(y)

    def update(self,label_i,data_i):  
        tmp = label_i*data_i
        self.w += tmp
        self.b += label_i

    def train(self):
        isFind=False
        while not isFind:
            count=0
            for i in range(self.x.shape[0]):
                tmp_y=self.sign(self.w, self.b, self.x[i,:])
                if tmp_y*self.y[i]<=0:   #如果误分类
                    count+=1
                    self.update(self.y[i],self.x[i,:])
            if count==0:
                isFind=True
        return self.w, self.b
        

if __name__ == '__main__':
    x = np.array([[3,-3],[4,-3],[1,1],[1,2]])
    y = np.array([-1, -1, 1, 1])
    myperceptron = Perceptron(x, y)
    w, b = myperceptron.train()
    print('最终训练得到的w和b为:', w, b)

python调包:

import numpy as np
from sklearn.linear_model import Perceptron

#感知机模型
def MyPerceptron(x, y):   
    clf = Perceptron(fit_intercept=True, max_iter=50, shuffle=False) #定义感知机  
    clf.fit(x, y)  #训练感知机   
    w = clf.coef_ #得到权重矩阵    
    b = clf.intercept_ #得到截距
    return w, b


if __name__ == '__main__':
    x = np.array([[3, -3], [4, -3], [1, 1], [1, 2]])
    y = np.array([-1, -1, 1, 1])
    w, b = MyPerceptron(x, y)
    print('最终训练得到的w和b为:', w, ',', b)

c++实现:

#include <iostream>
#include <vector>

//感知机模型
class Perceptron
{
public:
	Perceptron(std::vector<std::vector<float>> x, std::vector<float> y)
	{
		m_x = x;
		m_y = y;
		m_w.resize(m_x[0].size(), 0);
		m_b = 0;
	}

	float sign(std::vector<float> w, float b, std::vector<float> x)
	{
		float y = b;
		for (size_t i = 0; i < w.size(); i++)
		{
			y += w[i] * x[i];
		}
		return y;
	}

	void update(float label_i, std::vector<float> data_i)
	{
		for (size_t i = 0; i < m_w.size(); i++)
		{
			m_w[i] += label_i * data_i[i];
		}
		m_b += label_i;
	}

	void train()
	{
		bool isFind = false;
		while (!isFind)
		{
			float count = 0;
			for (size_t i = 0; i < m_x.size(); i++)
			{
				float tmp_y = sign(m_w, m_b, m_x[i]);
				if (tmp_y*m_y[i] <= 0) //如果误分类
				{
					++count;
					update(m_y[i], m_x[i]);
				}
			}
			if (count == 0)
			{
				std::cout << "最终训练得到的w为:";
				for (auto i : m_w)	std::cout << i << " ";
				std::cout << "\n最终训练得到的b为:";
				std::cout << m_b << "\n";
				isFind = true;
			}
		}
	}

private:
	std::vector<std::vector<float>> m_x;
	std::vector<float> m_y;
	std::vector<float> m_w;
	float m_b;
};


int main(int argc, char* argv[])
{
	std::vector<std::vector<float>> x = { { 3, -3 },{ 4, -3 },{ 1, 1 },{ 1, 2 } };
	std::vector<float> y = { -1, -1, 1, 1 };

	Perceptron myperceptron = Perceptron(x, y);
	myperceptron.train();

	system("pause");
	return EXIT_SUCCESS;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

给算法爸爸上香

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值