Machine Learning - Preliminary Perceptron

What is a perceptron

To understand what a perceptron is, first understand the structure of biological neurons (BN). The structure of biological neurons is shown in the figure below:
Insert image description here

It can be seen from the figure that each biological neuron is mainly composed of four parts: cell body, dendrites, axons and synapses. People with a certain biological foundation can understand that the input end of a neuron has multiple dendrites, which are mainly used to receive input information. The input information is processed through synapses and the input information is accumulated. When the processed input information is greater than a certain threshold, the information will be propagated through the axon. At this time, the neuron is said to be activated. On the contrary, when the processed input information is When it is less than the threshold, the neuron is in an inhibitory state, and it does not transmit data to other neurons or transmits very little information data.

Inspired by biological neurons, psychologist McCulloch and mathematician Pitts proposed the artificial neuron model (Artificial Neuron, AN) in 1943. As shown in the figure, people call it the MP model or "perceptron". Model.

Insert image description here

Data introduction

我们通过判断西瓜是否成熟例子来介绍感知机的算法流程,数据介绍:

The data set is constructed with a total of 30 features to help predict: color, base, knocking sound, etc. The categories are good melons and not good melons. Some of the data is as follows:
Insert image description here
Since our model can only calculate numbers. Therefore, we use x1 to represent the color, x2 to represent the base, and x3 to represent the knocking sound. y represents the category. Among them, x1= 0 means green , x2= 2 means slightly curled up , y=-1 means not a good melon . The details are as follows:
Insert image description here

Perceptron algorithm process

Applying the perceptron model to classify whether watermelons are ripe or not, it can be understood as the following figure by determining the weight and bias of each feature to obtain the basis for judgment:
Establish a mathematical model:
Insert image description here

After establishing the mathematical model, we introduce the algorithm flow of the perceptron as shown in the figure:
Insert image description here

Text explanation:
Insert image description here

However, the above update only applies to misclassified points in the training set. As for why, it can be understood that for misclassified points, the hyperplane must be adjusted so that the misclassified points become correct classification points, and for the correct classification points, There is no need to make adjustments, and the overall program operation time can be shortened.

python code implementation of perceptron

根据上面的算法流程,用python代码实现,这是代码:
#encoding=utf8
import numpy as np
#构建感知机算法

class Perceptron(object):
    def __init__(self, learning_rate = 0.01, max_iter = 200):
        self.lr = learning_rate
        self.max_iter = max_iter
    def fit(self, data, label):
        '''
        input:data(ndarray):训练数据特征
              label(ndarray):训练数据标签
        output:w(ndarray):训练好的权重
               b(ndarry):训练好的偏置
        '''
        #编写感知机训练方法,w为权重,b为偏置
        self.w = np.array([1.]*data.shape[1])
        self.b = np.array([1.])
        
        for i in range(len(label)):
            while label[i]*(np.matmul(self.w,data[i])+self.b) <= 0:
                self.w = self.w + self.lr * (label[i]*data[i])
                self.b = self.b + self.lr * label[i]
       
    def predict(self, data):
        '''
        input:data(ndarray):测试数据特征
        output:predict(ndarray):预测标签
        '''
        yc = np.matmul(data,self.w) + self.b
        for i in range(len(yc)):
            if yc[i] >= 0:
                yc[i] = 1
            else:
                yc[i] = -1
        predict = yc
        return predict
代码在EduCoder平台测试得到通过,因为正确率超过0.8就可以,所以我并没有确切的知道正确率。

Screenshot of the code passed:
Insert image description here

内容参考了周志华的机器学习和黄安埠的深入浅出深度学习等,感兴趣的同学也可以进入EduCoder平台的机器学习修炼指南一起学习机器学习。

Guess you like

Origin blog.csdn.net/qq_44725872/article/details/108673188