반응형
Notice
Recent Posts
Recent Comments
Link
관리 메뉴

bro's coding

sklearn.Compare Ridge and Rasso 본문

[AI]/python.sklearn

sklearn.Compare Ridge and Rasso

givemebro 2020. 4. 17. 14:43
반응형

릿지(Ridge)  라쏘(Lasso) 는 오차값에 규제(Regulation) 항 또는 벌점(Penalty) 항을 추가해서, 좀 더 단순화된 모델 또는 일반화된 모델을 제공하는 방법이다.

 

 
 

<릿지가 제곱 라쏘가 절댓값>

import numpy as np
import matplotlib.pyplot as plt

 

# graph size
fig=plt.figure(figsize=[12,6])

# -10부터 10까지 100개로 분할함
rng=np.linspace(-10,10,100)

# mse
mse=(0.5*(rng-3))**2+30

# ridge's alpha = 1
l2=rng**2
# rasso's alpha = 5
l1=5*np.abs(rng)

# ridge
ridge=mse+l2
# lasso
lasso=mse+l1

# visualization
plt.subplot(1,2,1)
plt.plot(rng,mse,label='MSE')
plt.plot(rng,l2,'--',label='L2')
plt.plot(rng,ridge,lw=2,label='Ridge')
plt.xlabel('w')
plt.ylabel('Error')
plt.legend()

plt.subplot(1,2,2)
plt.plot(rng,mse,label='MSE')
plt.plot(rng,l1,'--',label='L1')
plt.plot(rng,lasso,lw=2,label='Lasso')
plt.xlabel('w')
plt.ylabel('Error')
plt.legend()

# 릿지와 라쏘 모두 Error 의 최소값 위치가 w 값이 0쪽으로 치우쳐 진다

# 라쏘의 경우 릿지 보다 최소값 근처에서 기울기가 크다

# 규제의 강도를 의미하는 alpha 값이 커질수록 w 가 0으로 향하는 정도와 속도가 커진다(alpha= 1/C)
반응형
Comments