京公网安备 11010802034615号
经营许可证编号:京B2-20210330
感知机(Perceptron)或者叫做感知器,是Frank Rosenblatt在1957年就职于Cornell航空实验室(Cornell Aeronautical Laboratory)时所发明的一种人工神经网络,是机器学习领域最基础的模型,被誉为机器学习的敲门砖。
感知机是生物神经细胞的简单抽象,可以说是形式最简单的一种前馈神经网络,是一种二元线性分类模型。感知机的输入为实例的特征向量,输出为实例的类别取+1和-1.虽然现在看来感知机的分类模型,大多数情况下的泛化能力不是很强,但是感知机是最古老的分类方法之一,是神经网络的雏形,同时也是支持向量机的基础,如果能够将感知机研究透彻,对我们支持向量机、神经网络的学习也有很大帮助。
一、感知机模型
感知机的几何解释:线性方程
二·、感知机算法
1.原始形式
from random import randint import numpy as np import matplotlib.pyplot as plt class TrainDataLoader: def __init__(self): pass def GenerateRandomData(self, count, gradient, offset): x1 = np.linspace(1, 5, count) x2 = gradient*x1 + np.random.randint(-10,10,*x1.shape)+offset dataset = [] y = [] for i in range(*x1.shape): dataset.append([x1[i], x2[i]]) real_value = gradient*x1[i]+offset if real_value > x2[i]: y.append(-1) else: y.append(1) return x1,x2,np.mat(y),np.mat(dataset) class SimplePerceptron: def __init__(self, train_data = [], real_result = [], eta = 1): self.w = np.zeros([1, len(train_data.T)], int) self.b = 0 self.eta = eta self.train_data = train_data self.real_result = real_result def nomalize(self, x): if x > 0 : return 1 else : return -1 def model(self, x): # Here are matrix dot multiply get one value y = np.dot(x, self.w.T) + self.b # Use sign to nomalize the result predict_v = self.nomalize(y) return predict_v, y def update(self, x, y): # w = w + n*y_i*x_i self.w = self.w + self.eta*y*x # b = b + n*y_i self.b = self.b + self.eta*y def loss(slef, fx, y): return fx.astype(int)*y def train(self, count): update_count = 0 while count > 0: # count-- count = count - 1 if len(self.train_data) <= 0: print("exception exit") break # random select one train data index = randint(0,len(self.train_data)-1) x = self.train_data[index] y = self.real_result.T[index] # wx+b predict_v, linear_y_v = self.model(x) # y_i*(wx+b) > 0, the classify is correct, else it's error if self.loss(y, linear_y_v) > 0: continue update_count = update_count + 1 self.update(x, y) print("update count: ", update_count) pass def verify(self, verify_data, verify_result): size = len(verify_data) failed_count = 0 if size <= 0: pass for i in range(size): x = verify_data[i] y = verify_result.T[i] if self.loss(y, self.model(x)[1]) > 0: continue failed_count = failed_count + 1 success_rate = (1.0 - (float(failed_count)/size))*100 print("Success Rate: ", success_rate, "%") print("All input: ", size, " failed_count: ", failed_count) def predict(self, predict_data): size = len(predict_data) result = [] if size <= 0: pass for i in range(size): x = verify_data[i] y = verify_result.T[i] result.append(self.model(x)[0]) return result if __name__ == "__main__": # Init some parameters gradient = 2 offset = 10 point_num = 1000 train_num = 50000 loader = TrainDataLoader() x, y, result, train_data = loader.GenerateRandomData(point_num, gradient, offset) x_t, y_t, test_real_result, test_data = loader.GenerateRandomData(100, gradient, offset) # First training perceptron = SimplePerceptron(train_data, result) perceptron.train(train_num) perceptron.verify(test_data, test_real_result) print("T1: w:", perceptron.w," b:", perceptron.b) # Draw the figure # 1. draw the (x,y) points plt.plot(x, y, "*", color='gray') plt.plot(x_t, y_t, "+") # 2. draw y=gradient*x+offset line plt.plot(x,x.dot(gradient)+offset, color="red") # 3. draw the line w_1*x_1 + w_2*x_2 + b = 0 plt.plot(x, -(x.dot(float(perceptron.w.T[0]))+float(perceptron.b))/float(perceptron.w.T[1]) , color='green') plt.show()2.对偶形式
from random import randint import numpy as np import matplotlib.pyplot as plt class TrainDataLoader: def __init__(self): pass def GenerateRandomData(self, count, gradient, offset): x1 = np.linspace(1, 5, count) x2 = gradient*x1 + np.random.randint(-10,10,*x1.shape)+offset dataset = [] y = [] for i in range(*x1.shape): dataset.append([x1[i], x2[i]]) real_value = gradient*x1[i]+offset if real_value > x2[i]: y.append(-1) else: y.append(1) return x1,x2,np.mat(y),np.mat(dataset) class SimplePerceptron: def __init__(self, train_data = [], real_result = [], eta = 1): self.alpha = np.zeros([train_data.shape[0], 1], int) self.w = np.zeros([1, train_data.shape[1]], int) self.b = 0 self.eta = eta self.train_data = train_data self.real_result = real_result self.gram = np.matmul(train_data[0:train_data.shape[0]], train_data[0:train_data.shape[0]].T) def nomalize(self, x): if x > 0 : return 1 else : return -1 def train_model(self, index): temp = 0 y = self.real_result.T # Here are matrix dot multiply get one value for i in range(len(self.alpha)): alpha = self.alpha[i] if alpha == 0: continue gram_value = self.gram[index].T[i] temp = temp + alpha*y[i]*gram_value y = temp + self.b # Use sign to nomalize the result predict_v = self.nomalize(y) return predict_v, y def verify_model(self, x): # Here are matrix dot multiply get one value y = np.dot(x, self.w.T) + self.b # Use sign to nomalize the result predict_v = self.nomalize(y) return predict_v, y def update(self, index, x, y): # alpha = alpha + 1 self.alpha[index] = self.alpha[index] + 1 # b = b + n*y_i self.b = self.b + self.eta*y def loss(slef, fx, y): return fx.astype(int)*y def train(self, count): update_count = 0 train_data_num = self.train_data.shape[0] print("train_data:", self.train_data) print("Gram:",self.gram) while count > 0: # count-- count = count - 1 if train_data_num <= 0: print("exception exit") break # random select one train data index = randint(0, train_data_num-1) if index >= train_data_num: print("exceptrion get the index") break; x = self.train_data[index] y = self.real_result.T[index] # w = \sum_{i=1}^{N}\alpha_iy_iGram[i] # wx+b predict_v, linear_y_v = self.train_model(index) # y_i*(wx+b) > 0, the classify is correct, else it's error if self.loss(y, linear_y_v) > 0: continue update_count = update_count + 1 self.update(index, x, y) for i in range(len(self.alpha)): x = self.train_data[i] y = self.real_result.T[i] self.w = self.w + float(self.alpha[i])*x*float(y) print("update count: ", update_count) pass def verify(self, verify_data, verify_result): size = len(verify_data) failed_count = 0 if size <= 0: pass for i in range(size-1): x = verify_data[i] y = verify_result.T[i] if self.loss(y, self.verify_model(x)[1]) > 0: continue failed_count = failed_count + 1 success_rate = (1.0 - (float(failed_count)/size))*100 print("Success Rate: ", success_rate, "%") print("All input: ", size, " failed_count: ", failed_count) def predict(self, predict_data): size = len(predict_data) result = [] if size <= 0: pass for i in range(size): x = verify_data[i] y = verify_result.T[i] result.append(self.model(x)[0]) return result if __name__ == "__main__": # Init some parameters gradient = 2 offset = 10 point_num = 1000 train_num = 1000 loader = TrainDataLoader() x, y, result, train_data = loader.GenerateRandomData(point_num, gradient, offset) x_t, y_t, test_real_result, test_data = loader.GenerateRandomData(100, gradient, offset) # train_data = np.mat([[3,3],[4,3],[1,1]]) # First training perceptron = SimplePerceptron(train_data, result) perceptron.train(train_num) perceptron.verify(test_data, test_real_result) print("T1: w:", perceptron.w," b:", perceptron.b) # Draw the figure # 1. draw the (x,y) points plt.plot(x, y, "*", color='gray') plt.plot(x_t, y_t, "+") # 2. draw y=gradient*x+offset line plt.plot(x,x.dot(gradient)+offset, color="red") # 3. draw the line w_1*x_1 + w_2*x_2 + b = 0 plt.plot(x, -(x.dot(float(perceptron.w.T[0]))+float(perceptron.b))/float(perceptron.w.T[1]) , color='green') plt.show()
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
CDA数据分析师 出品 作者:李诗怡 1. VLOOKUP:按行跨区域找数据(最常用) 作用:在指定的表格或数据区域的第一列里找目标值, ...
2026-10-23数据敏感度不是天赋,也不是“看一眼数字就能发现问题”的直觉。对市场专员来说,它更接近一套可练习的工作习惯:看到投放ROI、 ...
2026-10-23市场营销专员转数据分析,不是从零重来。你已有的渠道投放、活动执行、线索跟进经验,本身就是分析工作的原料——缺的不是业务理 ...
2026-10-23先说结论:市场岗位并非必须考数据分析证书。招聘方更常看的是你能否把渠道效果、活动ROI、线索转化这些业务问题讲清楚,证书只 ...
2026-10-23市场分析师的职场知识提升,最怕两件事:一是把“学工具”当成学习本身,Excel、SQL、Python轮着学一遍,回到工作里还是不知道怎 ...
2026-10-23市场分析师的通用技能可以归为五块:业务与市场理解、调研与竞品分析、用户与渠道数据分析、指标与分析表达、工具操作。它们不是 ...
2026-10-23市场分析师的核心技能,可以先用一个框架回答:懂业务与市场、能取数与处理数据、会指标与分析方法、能把洞察讲清楚。这四块是共 ...
2026-10-23市场策划不是“会写文案、能办活动”这么简单。它本质上是一个用创意和资源撬动业务结果的岗位:先理解市场和人群,再形成方案, ...
2026-10-23社群运营不是“拉群、发通知、维护气氛”这么简单。它真正要对的结果是:用户进得来、留得住、愿意互动、最终产生转化或复购。因 ...
2026-10-23营销人的能力提升计划,最容易做成一张课程清单:报名几门课、读完几本书、考一个证,年底回头看,工作方式却没变。问题不在努力 ...
2026-10-23人力资源专员的职场价值,不由“做了多少事”决定,而由“解决什么问题、留下什么成果”决定。同样做招聘、算考勤、办入离职,有 ...
2026-10-23产品经理的职场价值,不主要由会画原型、会写文档决定,而由能否用业务结果证明自己的判断与推动决定。同一岗位的人价值差距大, ...
2026-10-23运营专员的能力提升,最容易走偏成两件事:一是把市面上所有工具都学一遍,二是把沟通、协作这类通用能力当成唯一答案。更有效的 ...
2026-10-23市场总监的沟通与管理,难点通常不在话术,而在三件事能否同时成立:向上用结论和数据对齐目标,平级用共同指标换取协同,向下用 ...
2026-10-23市场分析师的沟通失分,往往不是“嘴笨”,而是三件事没做好:结论没提炼出来、指标口径没对齐、建议没有落到决策上。所以提升沟 ...
2026-10-23社群运营做久了,很容易陷入一种忙碌:每天在群里发内容、回消息、做活动,热闹的时候很热闹,可一到月底复盘,说不清哪些动作带 ...
2026-10-23品牌运营的技能提升,最容易走偏的一步是先报课、先学工具。更有效的顺序是:先沿“品牌定位与策略—内容与传播—渠道与活动执行 ...
2026-10-23营销人员想赢得客户信任,单靠话术和关系维护越来越不够。客户在签约前会追问:你懂我的业务吗?方案里的数字怎么来的?承诺的效 ...
2026-10-23APP运营专员的能力提升,容易陷入两种误区:一是把“会用工具”当成能力全部,学了一堆Excel函数和SQL语法,回到岗位仍说不清一 ...
2026-10-23社群运营的沟通难点,不在于“会不会说话”,而在于同一件事要对三类人说清楚:对用户要安抚情绪、给出确定答复;对产品、活动、 ...
2026-10-23