L1和L2拟合

对照阅读

l1和l2正则化

L2拟合出现异常值则会由于误差平方放大的问题,而L1拟合更好的代表数据。

import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize as spopt

x = np.sort(np.random.uniform(0, 10, 15))
y = 3 + 0.2 * x + 0.1 * np.random.randn(len(x))

# find L1 line fit
l1_fit = lambda x0, x, y: np.sum(np.abs(x0[0] * x + x0[1] - y))
xopt1 = spopt.fmin(func=l1_fit, x0=[1, 1], args=(x, y))

# find L2 line fit
l2_fit = lambda x0, x, y: np.sum(np.power(x0[0] * x + x0[1] - y, 2))
xopt2 = spopt.fmin(func=l2_fit, x0=[1, 1], args=(x, y))

xx = np.arange(0,15)
l1_y = xopt1[0] * xx + xopt1[1]
l2_y = xopt2[0] * xx + xopt2[1]

plt.figure()
plt.plot(x,y,'*')
plt.plot(xx,l1_y)
plt.plot(xx,l2_y)

# adjust data by adding outliers
y2 = y.copy()
y2[3] += 4
y2[13] -= 3

# refit the lines
xopt12 = spopt.fmin(func=l1_fit, x0=[1, 1], args=(x, y2))
xopt22 = spopt.fmin(func=l2_fit, x0=[1, 1], args=(x, y2))

l1_y2 = xopt12[0] * xx + xopt12[1]
l2_y2 = xopt22[0] * xx + xopt22[1]

plt.figure()
plt.plot(x,y2,'*')
l1,=plt.plot(xx,l1_y2)
l2,=plt.plot(xx,l2_y2)
plt.legend([l1, l2], ['l1', 'l2'], loc='upper right')

# do it again
y3 = y.copy()
y3[3] += 4
y3[13] += 3

# refit the lines
xopt13 = spopt.fmin(func=l1_fit, x0=[1, 1], args=(x, y3))
xopt23 = spopt.fmin(func=l2_fit, x0=[1, 1], args=(x, y3))

l1_y3 = xopt13[0] * xx + xopt13[1]
l2_y3 = xopt23[0] * xx + xopt23[1]

plt.figure()
plt.plot(x,y3,'*')
l1,=plt.plot(xx,l1_y3)
l2,=plt.plot(xx,l2_y3)
plt.legend([l1, l2], ['l1', 'l2'], loc='upper right')

合集文章请来公众号:未名方略

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞行codes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值