练习:绘制常见损失函数
损失函数:Logistics损失(-1,1) SVM Hinge损失 0/1损失
首先我们来复习一下常见的损失函数
Logistics损失函数/sigmoid函数
指数损失函数(Adaboost) {#三、指数损失函数(Adaboost)}
$$exp\left ( -m \right )$$
Hinge损失函数(SVM) {#四、Hinge损失函数(SVM)}
$$max\left ( 0,1-m \right )$$
0-1损失函数
#coding:utf-8
#/usr/bin/python
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
if __name__ == '__main__':
mpl.rcParams['font.sans-serif'] = [u'SimHei']
mpl.rcParams['axes.unicode_minus'] = False # win正常显示中文设置
x = np.linspace(-2, 3, 1001, dtype=float) #生成数据
# y_logistics = (1 + np.exp(-x))
y_logistics = np.log((1 + np.exp(-x))) / np.log(2) #为了图片好看,过(0,1)点
y_boost = np.exp(-x)
y_01 = x < 0
y_hinge = 1.0 - x
y_hinge[y_hinge < 0] = 0
plt.figure(figsize=(5,5),facecolor='w')
plt.plot(x,y_logistics,'r--',label='Logistics Loss',lw =2)
plt.plot(x,y_boost,'k-',label = 'Adaboost Loss',lw = 1)
plt.plot(x,y_01,'y-',label = '0/1 Loss',lw = 1)
plt.plot(x,y_hinge,'b-',label = 'Hinge Loss',lw = 1)
plt.grid()
plt.title('常见损失函数', fontsize=16)
plt.legend('lower upper')
plt.show()
网友评论