본문 바로가기

코딩으로 익히는 Python/모델링

[Python] 6. L1 norm, L2 norm

728x90
반응형
SMALL
import pandas as pd
import numpy as np
from numpy import linalg
import matplotlib.pyplot as plt
import warnings
import math

warnings.simplefilter('ignore')

p = 1  L1 Norm 
p = 2  L2 Norm

출처 : en.wikipedia.org/wiki/Norm_(mathematics)

 

Norm (mathematics) - Wikipedia

Length in a vector space In mathematics, a norm is a function from a real or complex vector space to the nonnegative real numbers that behaves in certain ways like the distance from the origin: it commutes with scaling, obeys a form of the triangle inequal

en.wikipedia.org


L1

 

np.random.seed(20)
data = np.random.randint(-5,6,size=(2,2))
data
df = pd.DataFrame(data)
df

 

 

linalg.norm(df,ord=1,axis=1) # 행단위로 절댓값의 합
[OUT]:

array([7., 9.])

L2

 

np.random.seed(20)
data = np.random.randint(1,10,size=(3,2))
data
df = pd.DataFrame(data)
df

 

linalg.norm(df,ord=2,axis=1) # 행 단위로 제곱의 합에다가 루트 씌운 값
[OUT]:

array([ 6.40312424, 10.63014581,  3.16227766])

 

plt.scatter(df[0],df[1],s=100,c=['r','g','b'])
plt.show()


cost 함수

 

x_data = np.array([1,2,3])
y_data = np.array([1,2,3])
w = 0
hy = w*x_data
loss = np.mean((hy-y_data)**2) # array는 elementwise하게 계산
loss
[OUT]:

4.666666666666667

 

def cost(x,y,w):
    n = len(x)
    hy = w*x
    loss =  np.sum((hy-y_data)**2)/n
    return loss

 

print(cost(x_data,y_data,0))
print(cost(x_data,y_data,1))
print(cost(x_data,y_data,2))
[OUT]:

4.666666666666667
0.0
4.666666666666667

 

array([ 6.40312424, 10.63014581,  3.16227766])plt.figure(figsize=(10,8))
plt.title('cost')
plt.xlabel('w')
plt.ylabel('cost')
for w in np.linspace(-3,5,50):
    c = cost(x_data,y_data,w)
    plt.plot(w,c,'ro')
plt.show()


L2norm 함수 (Ridge)

 

def cost_l2norm(x,y,w,a):
    n = len(x)
    hy = w*x
    loss =  np.sum((hy-y_data)**2)/n + a*(w**2) 
    return loss
print(cost_l2norm(x_data,y_data,w=1,a=0))
print(cost_l2norm(x_data,y_data,w=1,a=5))
print(cost_l2norm(x_data,y_data,w=1,a=10))
print(cost_l2norm(x_data,y_data,w=1,a=15))
[OUT]:

0.0
5.0
10.0
15.0

 

plt.figure(figsize=(10,8))
plt.title('L2norm')
plt.xlabel('w')
plt.ylabel('cost')
for w in np.linspace(-3,5,50):
    c = cost_l2norm(x_data,y_data,w,a=0)
    plt.plot(w,c,'ro')
    c = cost_l2norm(x_data,y_data,w,a=5)
    plt.plot(w,c,'bo')
    c = cost_l2norm(x_data,y_data,w,a=10)
    plt.plot(w,c,'go')
    c = cost_l2norm(x_data,y_data,w,a=15)
    plt.plot(w,c,'yo')
plt.show()

 

결론

  • a를 크게할수록 w의 최저값은 0에 가까워짐
  • 기울기가 0이라는 의미는 해당 특성 데이터를 예측 시 무시해도됨

L1norm 함수 (Lasso)

 

def cost_l1norm(x,y,w,a):
    n = len(x)
    hy = w*x
    loss =  np.sum((hy-y_data)**2)/n + a*math.fabs(w) 
    return loss
print(cost_l1norm(x_data,y_data,w=1,a=0))
print(cost_l1norm(x_data,y_data,w=1,a=5))
print(cost_l1norm(x_data,y_data,w=1,a=10))
print(cost_l1norm(x_data,y_data,w=1,a=15))
[OUT]:

0.0
5.0
10.0
15.0

 

plt.figure(figsize=(10,8))
plt.title('L1norm')
plt.xlabel('w')
plt.ylabel('cost')
for w in np.linspace(-3,5,50):
    c = cost_l1norm(x_data,y_data,w,a=0)
    plt.plot(w,c,'ro')
    c = cost_l1norm(x_data,y_data,w,a=5)
    plt.plot(w,c,'bo')
    c = cost_l1norm(x_data,y_data,w,a=10)
    plt.plot(w,c,'go')
    c = cost_l1norm(x_data,y_data,w,a=15)
    plt.plot(w,c,'yo')
    c = cost_l1norm(x_data,y_data,w,a=20)
    plt.plot(w,c,'mo')
plt.show()

 

결론

  • a를 크게할수록 w의 최저값은 0에 가까워짐
  • 기울기가 0이라는 의미는 해당 특성 데이터를 예측 시 무시해도됨
  • L2와 비슷하지만 좀더 뾰족한 모양일 뿐

review
- L1 norm & L2 norm 원리 이해
728x90
반응형
LIST