BYÖYO 2018
Yapay Öğrenmeye Giriş I
Ali Taylan Cemgil
2 Temmuz 2018
Verilen girdi ve çıktı ikilileri x,y için parametrik bir fonksyon f oturtma problemi.
Parametre w değerlerini öyle bir seçelim ki y≈f(x;w)
x: Girdi (Input)
y: Çıktı (Output)
w: Parametre (Weight, ağırlık)
e: Hata
Örnek 1: e=y−f(x)
Örnek 2: e=yf(x)−1
E, D: Hata fonksyonu (Error function), Iraksay (Divergence)
Oturtulacak f fonksyonun model parametreleri w cinsinden doğrusal olduğu durum (Girdiler x cinsinden doğrusal olması gerekmez).
Bir g fonksyonu doğrusaldır demek, herhangi skalar a ve b içn g(aw1+bw2)=ag(w1)+bg(w2)
i=1…N
x : Girdi
w1: Eğim
w0: Kesişme
fi≡f(xi;w1,w0)
i=1…N
x : Girdi
w2: Karesel terimin katsayısı
w1: Doğrusal terimin katsayısı
w0: Sabit terim katsayısı
fi≡f(xi;w2,w1,w0)
Bir parabol x'in doğrusal fonksyonu değil ama w2,w1,w0 parametrelerinin doğrusal fonksyonu.
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
from __future__ import print_function
from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets
import matplotlib.pylab as plt
from IPython.display import clear_output, display, HTML
x = np.array([8.0 , 6.1 , 11., 7., 9., 12. , 4., 2., 10, 5, 3])
y = np.array([6.04, 4.95, 5.58, 6.81, 6.33, 7.96, 5.24, 2.26, 8.84, 2.82, 3.68])
def plot_fit(w1, w0):
f = w0 + w1*x
plt.figure(figsize=(4,3))
plt.plot(x,y,'sk')
plt.plot(x,f,'o-r')
#plt.axis('equal')
plt.xlim((0,15))
plt.ylim((0,10))
for i in range(len(x)):
plt.plot((x[i],x[i]),(f[i],y[i]),'b')
# plt.show()
# plt.figure(figsize=(4,1))
plt.bar(x,(f-y)**2/2)
plt.title('Toplam kare hata = '+str(np.sum((f-y)**2/2)))
plt.ylim((0,10))
plt.xlim((0,15))
plt.show()
plot_fit(0.0,3.79)
interact(plot_fit, w1=(-2, 2, 0.01), w0=(-5, 5, 0.01));
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
Gerçek veri: Türkiyedeki araç sayıları
%matplotlib inline
import scipy as sc
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pylab as plt
df_arac = pd.read_csv(u'data/arac.csv',sep=';')
df_arac[['Year','Car']]
#df_arac
Year | Car | |
---|---|---|
0 | 1966 | 91469 |
1 | 1967 | 112367 |
2 | 1968 | 125375 |
3 | 1969 | 137345 |
4 | 1970 | 137771 |
5 | 1971 | 153676 |
6 | 1972 | 187272 |
7 | 1973 | 240360 |
8 | 1974 | 313160 |
9 | 1975 | 403546 |
10 | 1976 | 488894 |
11 | 1977 | 560424 |
12 | 1978 | 624438 |
13 | 1979 | 688687 |
14 | 1980 | 742252 |
15 | 1981 | 776432 |
16 | 1982 | 811465 |
17 | 1983 | 856350 |
18 | 1984 | 919577 |
19 | 1985 | 983444 |
20 | 1986 | 1087234 |
21 | 1987 | 1193021 |
22 | 1988 | 1310257 |
23 | 1989 | 1434830 |
24 | 1990 | 1649879 |
25 | 1991 | 1864344 |
26 | 1992 | 2181388 |
27 | 1993 | 2619852 |
28 | 1994 | 2861640 |
29 | 1995 | 3058511 |
30 | 1996 | 3274156 |
31 | 1997 | 3570105 |
32 | 1998 | 3838288 |
33 | 1999 | 4072326 |
34 | 2000 | 4422180 |
35 | 2001 | 4534803 |
36 | 2002 | 4600140 |
37 | 2003 | 4700343 |
38 | 2004 | 5400440 |
39 | 2005 | 5772745 |
40 | 2006 | 6140992 |
41 | 2007 | 6472156 |
42 | 2008 | 6796629 |
43 | 2009 | 7093964 |
44 | 2010 | 7544871 |
45 | 2011 | 8113111 |
46 | 2012 | 8648875 |
47 | 2013 | 9283923 |
48 | 2014 | 9857915 |
49 | 2015 | 10589337 |
50 | 2016 | 11317998 |
51 | 2017 | 12035978 |
BaseYear = 1995
x = np.matrix(df_arac.Year[0:]).T-BaseYear
y = np.matrix(df_arac.Car[0:]).T/1000000.
plt.plot(x+BaseYear, y, 'o-')
plt.xlabel('Yil')
plt.ylabel('Araba (Milyon)')
plt.show()
%matplotlib inline
from __future__ import print_function
from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets
import matplotlib.pylab as plt
from IPython.display import clear_output, display, HTML
w_0 = 0.27150786
w_1 = 0.37332256
BaseYear = 1995
x = np.matrix(df_arac.Year[0:]).T-BaseYear
y = np.matrix(df_arac.Car[0:]).T/1000000.
fig, ax = plt.subplots()
f = w_1*x + w_0
plt.plot(x+BaseYear, y, 'o-')
ln, = plt.plot(x+BaseYear, f, 'r')
plt.xlabel('Years')
plt.ylabel('Number of Cars (Millions)')
ax.set_ylim((-2,13))
plt.close(fig)
def set_line(w_1, w_0):
f = w_1*x + w_0
e = y - f
ln.set_ydata(f)
ax.set_title('Total Error = {} '.format(np.asscalar(e.T*e/2)))
display(fig)
set_line(0.32,3)
interact(set_line, w_1=(-2, 2, 0.01), w_0=(-5, 5, 0.01));
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
w_0 = 0.27150786
w_1 = 0.37332256
w_2 = 0.1
BaseYear = 1995
x = np.array(df_arac.Year[0:]).T-BaseYear
y = np.array(df_arac.Car[0:]).T/1000000.
fig, ax = plt.subplots()
f = w_2*x**2 + w_1*x + w_0
plt.plot(x+BaseYear, y, 'o-')
ln, = plt.plot(x+BaseYear, f, 'r')
plt.xlabel('Yıl')
plt.ylabel('Araba Sayısı (Milyon)')
ax.set_ylim((-2,13))
plt.close(fig)
def set_line(w_2, w_1, w_0):
f = w_2*x**2 + w_1*x + w_0
e = y - f
ln.set_ydata(f)
ax.set_title('Ortalama Kare Hata = {} '.format(np.sum(e*e/len(e))))
display(fig)
set_line(w_2, w_1, w_0)
interact(set_line, w_2=(-0.1,0.1,0.001), w_1=(-2, 2, 0.01), w_0=(-5, 5, 0.01))
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
<function __main__.set_line>
Öğrenmek: parametre kestirimi w=[w0,w1]
Genelde model veriyi hatasız açıklayamayacağı için her veri noktası için bir hata tanımlıyoruz:
Toplam kare hatayı w0 ve w1 parametrelerini değiştirerek azaltmaya çalışabiliriz.
Hata yüzeyi
from itertools import product
BaseYear = 1995
x = np.matrix(df_arac.Year[0:]).T-BaseYear
y = np.matrix(df_arac.Car[0:]).T/1000000.
# Setup the vandermonde matrix
N = len(x)
A = np.hstack((np.ones((N,1)), x))
left = -5
right = 15
bottom = -4
top = 6
step = 0.05
W0 = np.arange(left,right, step)
W1 = np.arange(bottom,top, step)
ErrSurf = np.zeros((len(W1),len(W0)))
for i,j in product(range(len(W1)), range(len(W0))):
e = y - A*np.matrix([W0[j], W1[i]]).T
ErrSurf[i,j] = e.T*e/2
plt.figure(figsize=(7,7))
plt.imshow(ErrSurf, interpolation='nearest',
vmin=0, vmax=1000,origin='lower',
extent=(left,right,bottom,top),cmap='Blues_r')
plt.xlabel('w0')
plt.ylabel('w1')
plt.title('Error Surface')
plt.colorbar(orientation='horizontal')
plt.show()
(Gauss 1795, Legendre 1805)
A=A(x): Model Matrisi
w: Model Parametreleri
y: Gözlemler
Toplam hatanın gradyanı ddwE(w)=ddw(12y⊤y)+ddw(−y⊤Aw)+ddw(12w⊤A⊤Aw)=0−A⊤y+A⊤Aw=−A⊤(y−Aw)=−A⊤e≡∇E(w)
# Solving the Normal Equations
# Setup the Design matrix
N = len(x)
A = np.hstack((np.ones((N,1)), x))
#plt.imshow(A, interpolation='nearest')
# Solve the least squares problem
w_ls,E,rank,sigma = np.linalg.lstsq(A, y)
print('Parametreler: \nw0 = ', w_ls[0],'\nw1 = ', w_ls[1] )
print('Toplam Kare Hata:', E/2)
f = np.asscalar(w_ls[1])*x + np.asscalar(w_ls[0])
plt.plot(x+BaseYear, y, 'o-')
plt.plot(x+BaseYear, f, 'r')
plt.xlabel('Yıl')
plt.ylabel('Araba sayısı (Milyon)')
plt.show()
Parametreler: w0 = [[4.13258253]] w1 = [[0.20987778]] Toplam Kare Hata: [[37.19722385]]
/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:9: FutureWarning: `rcond` parameter will change to the default of machine precision times ``max(M, N)`` where M and N are the input matrix dimensions. To use the future default and silence this warning we advise to pass `rcond=None`, to keep using the old, explicitly pass `rcond=-1`. if __name__ == '__main__':
A=A(x): Model matrisi
w: Model Parametreleri
y: Gözlemler
Polinom oturtmada ortaya çıkan özel yapılı matrislere Vandermonde matrisleri de denmektedir.
x = np.array([10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5])
N = len(x)
x = x.reshape((N,1))
y = np.array([8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68]).reshape((N,1))
#y = np.array([9.14, 8.14, 8.74, 8.77, 9.26, 8.10, 6.13, 3.10, 9.13, 7.26, 4.74]).reshape((N,1))
#y = np.array([7.46, 6.77, 12.74, 7.11, 7.81, 8.84, 6.08, 5.39, 8.15, 6.42, 5.73]).reshape((N,1))
def fit_and_plot_poly(degree):
#A = np.hstack((np.power(x,0), np.power(x,1), np.power(x,2)))
A = np.hstack((np.power(x,i) for i in range(degree+1)))
# Setup the vandermonde matrix
xx = np.matrix(np.linspace(np.asscalar(min(x))-1,np.asscalar(max(x))+1,300)).T
A2 = np.hstack((np.power(xx,i) for i in range(degree+1)))
#plt.imshow(A, interpolation='nearest')
# Solve the least squares problem
w_ls,E,rank,sigma = np.linalg.lstsq(A, y)
f = A2*w_ls
plt.plot(x, y, 'o')
plt.plot(xx, f, 'r')
plt.xlabel('x')
plt.ylabel('y')
plt.gca().set_ylim((0,20))
#plt.gca().set_xlim((1950,2025))
if E:
plt.title('Mertebe = '+str(degree)+' Hata='+str(E[0]))
else:
plt.title('Mertebe = '+str(degree)+' Hata= 0')
plt.show()
fit_and_plot_poly(0)
/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:18: FutureWarning: `rcond` parameter will change to the default of machine precision times ``max(M, N)`` where M and N are the input matrix dimensions. To use the future default and silence this warning we advise to pass `rcond=None`, to keep using the old, explicitly pass `rcond=-1`.
interact(fit_and_plot_poly, degree=(0,10))
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
<function __main__.fit_and_plot_poly>
Overfit: Aşırı uyum