L1 基础知识
WHAT: Making decisions from data
WHY: To apply, understand,evaluate
what do we have?
特征向量:
标签:
数据集:
what do we want?
A good way to label new points
x -> h -> y (函数名h:hypothesis)
Example h:For any x,h(x) = +1
Linear classifiers 线性分类器
直线的重新定义:
一个线性分类器:
H : 一系列这样的函数h
How good is a classfier?
可以很好的预测未知的数据
我们有一个损失函数Loss g:guess a:actual
e.g. 0-1 loss
e.g. asymmetric loss 非对称损失
100:假正例(FP:false positive)判假为真
1:假反例(FN:false negative)判真为假
对个未知数据,定义测试误差函数Test error:
定义训练误差函数:
我们希望找到一个学习算法:使
思考:h 是否依赖于D?如何确定参数?
L2 感知器 perceptron
Perceptron algorithm
伪代码如下:
Perceptron()
Initialize
Initialize
for t = 1 to
change = False
for i = 1 to n
an updata
change = True
if not change
break
return
python代码:
from matplotlib import pyplot as plt
import numpy as np
import math
def perceptron(D, T,theta,theta0):
for _ in range(T):
changed = False
for i in range(len(D)):
temp = np.matmul(theta,D[i])+theta0
if y[i] * temp <= 0:
theta = np.add(theta,np.multiply(y[i],D[i]))
theta0 = theta0 + y[i]
changed = True
if not changed:
break
return theta,theta0
D = [[10, 70], [20, 30], [30, 90], [60, 75], [40, 70], [40, 15], [55, 55], [30, 35], [50, 15], [90, 40]]
y = [-1,-1,1,1,1,-1,1,-1,-1,1]
theta = np.transpose([0, 0])
theta0 = 0
T = 1
t,t0 = perceptron(D, T,theta,theta0)
positive_x = []
positive_y = []
negative_x = []
negative_y = []
i = 0
for i in range(len(D)):
if y[i]<0:
negative_x.append(D[i][0])
negative_y.append(D[i][1])
else:
positive_x.append(D[i][0])
positive_y.append(D[i][1])
x = np.linspace(0,100,100,endpoint=False)
mod_t = math.sqrt(t[0]**2+t[1]**2)
y = mod_t*(-t0)- t[0]/t[1] * x
plt.title('Perceptron')
plt.plot(positive_x,positive_y,'+')
plt.plot(negative_x,negative_y,'_')
plt.plot(x,y,'b-')
plt.show()
结果:
*式做了什么?
尝试转向更正确的分类:
让原来的表达式添加一个正数,使guess 与actual 的符号相同
Classifier Quality 分类器性能分析
定义signed distance 符号距离:
定义margin 边距:
定义整个训练集
的边距:
Perceptron Performance 感知器表现
Assumptions:
A : 通过原点(i.e. )
B : 存在一个和
使边距
C : 特征向量在一个半径R内
Conclusion:
迭代次数至多为
, 使
如何让classifiers通过原点?
添加一个维度:
思考:是否是可以线性分类的?怎么获取
?