## 感知机算法-原始形式 原

二胡艺

(1).选取初始值$w_{0},b_{0}$
(2).在训练数据中选取$(x_{i},y_{i})$
(3).如果$y_{i}(w\cdot x_{i}+b) \leqslant 0$
$w \leftarrow w + \eta y_{i}x_{i}$
$b \leftarrow b + \eta y_{i}$
(4).转到(2),直到训练集中没有误分类点

%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D

import numpy as np

class Perceptron:
def __init__(self):
self.W = []
self.b = 0
self.eta = 0.1
def __decission_func(self,x,y):
sum = 0
for i in range(len(x)):
sum += self.W[i]*x[i]
return sum + self.b
def fit(self,X,Y):
num_samples,feat_dim = X.shape
self.W = np.zeros((feat_dim))
self.b = 0
OK = False
while OK == False:
num_Ok = 0
for n in range(num_samples):
xi = X[n,:]
yi = Y[n]
if self.__decission_func(xi,yi) <= 0:
self.W = self.W + self.eta * yi * xi
self.b = self.b + self.eta * yi
else:
num_Ok += 1
if num_Ok == num_samples:
OK = True
def predict(self,x):
return 1 if self.__clac_Wx_add_b(x) > 0 else -1

def clac_y(x,W,b):
return -b / W[0] - W[1] / W[0] * x

X = np.array([[3,3],[4,3],[1,1]])
Y = np.array([1,1,-1])
clf = Perceptron()
clf.fit(X,Y)

color = [str(item/255.) for item in Y]
plt.scatter(X[:,0],X[:,1],c = color,s=300)
plt.text(3,2.5,s='+1')
plt.text(4,2.5,s='+1')
plt.text(1,0.5,s='-1')

print 'W=',clf.W,' b=',clf.b
x = [1,3]
y = []
for i in range(2):
y.append(clac_y(x[i],clf.W,clf.b))
plt.plot(x,y)

#pridect data
xx=[2,5]
rsp = clf.predict(xx)
colr = [str(item/255.) for item in [rsp]]
plt.scatter(xx[0],xx[1],s=100)
sym = '+1' if rsp == 1 else '-1'
plt.text(xx[0],xx[1] - 0.5,s=sym)

W= [ 0.1  0.1]  b= -0.3


### 二胡艺

1.感知机模型 在机器学习中，感知机（perceptron）是二分类的线性分类模型，属于监督学习算法。输入为实例的特征向量，输出为实例的类别（取+1和-1）。感知机对应于输入空间中将实例划分为两...

JasonhavenD
2017/12/08
0
0

FoolishFlyFox
2017/12/30
0
0

02/17
0
0

2017/10/18
0
0

2018/08/31
0
0

shzwork

6
0
object 类中有哪些方法？

getClass(): 获取运行时类的对象 equals()：判断其他对象是否与此对象相等 hashcode()：返回该对象的哈希码值 toString()：返回该对象的字符串表示 clone()： 创建并返此对象的一个副本 wait...

happywe

6
0
Docker容器实战(七) - 容器中进程视野下的文件系统

JavaEdge

8
0

7
0

MtrS

5
0