20230324 2315
개요
두 개의 평균이 다른 정규분포에서 10개 씩 추출한 데이터가 있는데, 특정 데이터가 어디 출신인지 알아내고 싶다.
유사한 응용이 많다. 결과를 보고 원인을 찾아내는 문제
- 법정에서 심증은 있으나 물증은 없는 경우
- 내가 산 채권이 실제 신용등급이 A인지 아닌지..
해봅시다
1) 표준편차가 1이고, 평균이 각각 0, 5인 난수를 10개씩 생성
import numpy as np
import matplotlib.pyplot as plt
x1=np.random.normal(0.0,1.0,10)
rnd=np.random.RandomState(123)
d1=rnd.randn(10,2)
d2=rnd.randn(10,2)+5
plt.scatter(d1[:,0],d1[:,1])
plt.scatter(d2[:,0],d2[:,1])
결과
2) 생성 결과로 training
w=np.zeros(2)
b=0
x=np.concatenate((d1,d2),axis=0)
def y(x):
return step(np.dot(w,x)+b)
def step(x):
return 1*(x>0)
def t(i):
if i < 10:
return 0
else:
return 1
while True:
classified = True
for i in range(20):
delta_w = (t(i)-y(x[i]))*x[i]
delta_b = (t(i)-y(x[i]))
w += delta_w
b += delta_b
classified *= all(delta_w == 0) * (delta_b == 0)
if classified:
break
plt.scatter(x[:,0],x[:,1])
x1t = np.arange(2,4,0.01)
x2t = (-b-w[0]*x1t)/w[1]
plt.scatter(x1t,x2t)
결과
(note★) STUDY: classified *= all(delta_w == 0) * (delta_b == 0)
- delta_w가 vector란 말인데..위에 보면 "delta_w = (t(i)-y(x[i]))*x[i]"이다.
- t[i],x[i] 등등이 벡타라는 뜻이다.그럼 delta_b는 벡터가 아닌가? No, it's not vector. It's scalar.
- 정확히는 x[i]가 벡터이다.. 2 by 20 matrix에서 i-th column을 잘라낸 것..
(note) if you want to see how the code finds the solution,...
w=np.zeros(2)
b=0
x=np.concatenate((d1,d2),axis=0)
plt.scatter(d1[:,0],d1[:,1])
plt.scatter(d2[:,0],d2[:,1])
x1t = np.arange(-2,6,0.01)
def y(x):
return step(np.dot(w,x)+b)
def step(x):
return 1*(x>0)
def t(i):
if i < 10:
return 0
else:
return 1
i_th = 1
while True:
classified = True
for i in range(20):
delta_w = (t(i)-y(x[i]))*x[i]
delta_b = (t(i)-y(x[i]))
w += delta_w
b += delta_b
classified *= all(delta_w == 0) * (delta_b == 0)
x2t = (-b-w[0]*x1t)/w[1]
plt.plot(x1t,x2t,marker=',')
plt.text(x1t[0],x2t[0],i_th)
i_th += 1
if classified:
break
result
'[AI]Artificial Inteligence 인공지능' 카테고리의 다른 글
AI understanding Gradient Descent 경사하강법 이해 (0) | 2023.04.11 |
---|---|
AI 이진분류 다중분류 (0) | 2023.03.30 |
AI 단순 퍼셉트론은 선형분류만 가능 (0) | 2023.03.26 |
AI Sigmoid 함수 (0) | 2023.03.25 |
AI Neural Net as 디지털 회로 Digital Gate (0) | 2023.03.25 |