Sınıflandırma

Kaggle https://www.kaggle.com/datasets

UCI Machine Learning Repository https://archive.ics.uci.edu/ml/datasets.html

Scikit-learn http://scikit-learn.org/stable/datasets/

Türkiye İstatistik Kurumu http://www.tuik.gov.tr

A Handbook of Small Data Sets edited by D.J. Hand, F. Daly, A.D. Lunn, K.J. McConway and E. Ostrowski from Chapman and Hall, ISBN 0 412 39920 2. https://www.stat.ncsu.edu/research/sas/sicl/data/

Iris (Süsen Çiçeği) Veri Kümesi

(Fisher, 1926)

Sınıf 1 Sınıf 2 Sınıf 3 ? ?
Iris Setosa Iris Versicolour Iris Virginica ? ?

Features

  • çanak yaprağı boyu (sepal length) cm
  • çanak yaprağı genişliği (sepal width) cm
  • taç yaprağı boyu (petal length) cm
  • taç yaprağı genişliği (petal width) cm


In [5]:
import numpy as np
import pandas as pd


df_iris = pd.read_csv(u'data/iris.txt',sep=' ')

for i in range(150):
    print(df_iris[i:i+1])


    sl   sw   pl   pw  c
0  5.1  3.5  1.4  0.2  1
    sl   sw   pl   pw  c
1  4.9  3.0  1.4  0.2  1
    sl   sw   pl   pw  c
2  4.7  3.2  1.3  0.2  1
    sl   sw   pl   pw  c
3  4.6  3.1  1.5  0.2  1
    sl   sw   pl   pw  c
4  5.0  3.6  1.4  0.2  1
    sl   sw   pl   pw  c
5  5.4  3.9  1.7  0.4  1
    sl   sw   pl   pw  c
6  4.6  3.4  1.4  0.3  1
    sl   sw   pl   pw  c
7  5.0  3.4  1.5  0.2  1
    sl   sw   pl   pw  c
8  4.4  2.9  1.4  0.2  1
    sl   sw   pl   pw  c
9  4.9  3.1  1.5  0.1  1
     sl   sw   pl   pw  c
10  5.4  3.7  1.5  0.2  1
     sl   sw   pl   pw  c
11  4.8  3.4  1.6  0.2  1
     sl   sw   pl   pw  c
12  4.8  3.0  1.4  0.1  1
     sl   sw   pl   pw  c
13  4.3  3.0  1.1  0.1  1
     sl   sw   pl   pw  c
14  5.8  4.0  1.2  0.2  1
     sl   sw   pl   pw  c
15  5.7  4.4  1.5  0.4  1
     sl   sw   pl   pw  c
16  5.4  3.9  1.3  0.4  1
     sl   sw   pl   pw  c
17  5.1  3.5  1.4  0.3  1
     sl   sw   pl   pw  c
18  5.7  3.8  1.7  0.3  1
     sl   sw   pl   pw  c
19  5.1  3.8  1.5  0.3  1
     sl   sw   pl   pw  c
20  5.4  3.4  1.7  0.2  1
     sl   sw   pl   pw  c
21  5.1  3.7  1.5  0.4  1
     sl   sw   pl   pw  c
22  4.6  3.6  1.0  0.2  1
     sl   sw   pl   pw  c
23  5.1  3.3  1.7  0.5  1
     sl   sw   pl   pw  c
24  4.8  3.4  1.9  0.2  1
     sl   sw   pl   pw  c
25  5.0  3.0  1.6  0.2  1
     sl   sw   pl   pw  c
26  5.0  3.4  1.6  0.4  1
     sl   sw   pl   pw  c
27  5.2  3.5  1.5  0.2  1
     sl   sw   pl   pw  c
28  5.2  3.4  1.4  0.2  1
     sl   sw   pl   pw  c
29  4.7  3.2  1.6  0.2  1
     sl   sw   pl   pw  c
30  4.8  3.1  1.6  0.2  1
     sl   sw   pl   pw  c
31  5.4  3.4  1.5  0.4  1
     sl   sw   pl   pw  c
32  5.2  4.1  1.5  0.1  1
     sl   sw   pl   pw  c
33  5.5  4.2  1.4  0.2  1
     sl   sw   pl   pw  c
34  4.9  3.1  1.5  0.1  1
     sl   sw   pl   pw  c
35  5.0  3.2  1.2  0.2  1
     sl   sw   pl   pw  c
36  5.5  3.5  1.3  0.2  1
     sl   sw   pl   pw  c
37  4.9  3.1  1.5  0.1  1
     sl   sw   pl   pw  c
38  4.4  3.0  1.3  0.2  1
     sl   sw   pl   pw  c
39  5.1  3.4  1.5  0.2  1
     sl   sw   pl   pw  c
40  5.0  3.5  1.3  0.3  1
     sl   sw   pl   pw  c
41  4.5  2.3  1.3  0.3  1
     sl   sw   pl   pw  c
42  4.4  3.2  1.3  0.2  1
     sl   sw   pl   pw  c
43  5.0  3.5  1.6  0.6  1
     sl   sw   pl   pw  c
44  5.1  3.8  1.9  0.4  1
     sl   sw   pl   pw  c
45  4.8  3.0  1.4  0.3  1
     sl   sw   pl   pw  c
46  5.1  3.8  1.6  0.2  1
     sl   sw   pl   pw  c
47  4.6  3.2  1.4  0.2  1
     sl   sw   pl   pw  c
48  5.3  3.7  1.5  0.2  1
     sl   sw   pl   pw  c
49  5.0  3.3  1.4  0.2  1
     sl   sw   pl   pw  c
50  7.0  3.2  4.7  1.4  2
     sl   sw   pl   pw  c
51  6.4  3.2  4.5  1.5  2
     sl   sw   pl   pw  c
52  6.9  3.1  4.9  1.5  2
     sl   sw   pl   pw  c
53  5.5  2.3  4.0  1.3  2
     sl   sw   pl   pw  c
54  6.5  2.8  4.6  1.5  2
     sl   sw   pl   pw  c
55  5.7  2.8  4.5  1.3  2
     sl   sw   pl   pw  c
56  6.3  3.3  4.7  1.6  2
     sl   sw   pl   pw  c
57  4.9  2.4  3.3  1.0  2
     sl   sw   pl   pw  c
58  6.6  2.9  4.6  1.3  2
     sl   sw   pl   pw  c
59  5.2  2.7  3.9  1.4  2
     sl   sw   pl   pw  c
60  5.0  2.0  3.5  1.0  2
     sl   sw   pl   pw  c
61  5.9  3.0  4.2  1.5  2
     sl   sw   pl   pw  c
62  6.0  2.2  4.0  1.0  2
     sl   sw   pl   pw  c
63  6.1  2.9  4.7  1.4  2
     sl   sw   pl   pw  c
64  5.6  2.9  3.6  1.3  2
     sl   sw   pl   pw  c
65  6.7  3.1  4.4  1.4  2
     sl   sw   pl   pw  c
66  5.6  3.0  4.5  1.5  2
     sl   sw   pl   pw  c
67  5.8  2.7  4.1  1.0  2
     sl   sw   pl   pw  c
68  6.2  2.2  4.5  1.5  2
     sl   sw   pl   pw  c
69  5.6  2.5  3.9  1.1  2
     sl   sw   pl   pw  c
70  5.9  3.2  4.8  1.8  2
     sl   sw   pl   pw  c
71  6.1  2.8  4.0  1.3  2
     sl   sw   pl   pw  c
72  6.3  2.5  4.9  1.5  2
     sl   sw   pl   pw  c
73  6.1  2.8  4.7  1.2  2
     sl   sw   pl   pw  c
74  6.4  2.9  4.3  1.3  2
     sl   sw   pl   pw  c
75  6.6  3.0  4.4  1.4  2
     sl   sw   pl   pw  c
76  6.8  2.8  4.8  1.4  2
     sl   sw   pl   pw  c
77  6.7  3.0  5.0  1.7  2
     sl   sw   pl   pw  c
78  6.0  2.9  4.5  1.5  2
     sl   sw   pl   pw  c
79  5.7  2.6  3.5  1.0  2
     sl   sw   pl   pw  c
80  5.5  2.4  3.8  1.1  2
     sl   sw   pl   pw  c
81  5.5  2.4  3.7  1.0  2
     sl   sw   pl   pw  c
82  5.8  2.7  3.9  1.2  2
     sl   sw   pl   pw  c
83  6.0  2.7  5.1  1.6  2
     sl   sw   pl   pw  c
84  5.4  3.0  4.5  1.5  2
     sl   sw   pl   pw  c
85  6.0  3.4  4.5  1.6  2
     sl   sw   pl   pw  c
86  6.7  3.1  4.7  1.5  2
     sl   sw   pl   pw  c
87  6.3  2.3  4.4  1.3  2
     sl   sw   pl   pw  c
88  5.6  3.0  4.1  1.3  2
     sl   sw   pl   pw  c
89  5.5  2.5  4.0  1.3  2
     sl   sw   pl   pw  c
90  5.5  2.6  4.4  1.2  2
     sl   sw   pl   pw  c
91  6.1  3.0  4.6  1.4  2
     sl   sw   pl   pw  c
92  5.8  2.6  4.0  1.2  2
     sl   sw   pl   pw  c
93  5.0  2.3  3.3  1.0  2
     sl   sw   pl   pw  c
94  5.6  2.7  4.2  1.3  2
     sl   sw   pl   pw  c
95  5.7  3.0  4.2  1.2  2
     sl   sw   pl   pw  c
96  5.7  2.9  4.2  1.3  2
     sl   sw   pl   pw  c
97  6.2  2.9  4.3  1.3  2
     sl   sw   pl   pw  c
98  5.1  2.5  3.0  1.1  2
     sl   sw   pl   pw  c
99  5.7  2.8  4.1  1.3  2
      sl   sw   pl   pw  c
100  6.3  3.3  6.0  2.5  3
      sl   sw   pl   pw  c
101  5.8  2.7  5.1  1.9  3
      sl   sw   pl   pw  c
102  7.1  3.0  5.9  2.1  3
      sl   sw   pl   pw  c
103  6.3  2.9  5.6  1.8  3
      sl   sw   pl   pw  c
104  6.5  3.0  5.8  2.2  3
      sl   sw   pl   pw  c
105  7.6  3.0  6.6  2.1  3
      sl   sw   pl   pw  c
106  4.9  2.5  4.5  1.7  3
      sl   sw   pl   pw  c
107  7.3  2.9  6.3  1.8  3
      sl   sw   pl   pw  c
108  6.7  2.5  5.8  1.8  3
      sl   sw   pl   pw  c
109  7.2  3.6  6.1  2.5  3
      sl   sw   pl   pw  c
110  6.5  3.2  5.1  2.0  3
      sl   sw   pl   pw  c
111  6.4  2.7  5.3  1.9  3
      sl   sw   pl   pw  c
112  6.8  3.0  5.5  2.1  3
      sl   sw   pl   pw  c
113  5.7  2.5  5.0  2.0  3
      sl   sw   pl   pw  c
114  5.8  2.8  5.1  2.4  3
      sl   sw   pl   pw  c
115  6.4  3.2  5.3  2.3  3
      sl   sw   pl   pw  c
116  6.5  3.0  5.5  1.8  3
      sl   sw   pl   pw  c
117  7.7  3.8  6.7  2.2  3
      sl   sw   pl   pw  c
118  7.7  2.6  6.9  2.3  3
      sl   sw   pl   pw  c
119  6.0  2.2  5.0  1.5  3
      sl   sw   pl   pw  c
120  6.9  3.2  5.7  2.3  3
      sl   sw   pl   pw  c
121  5.6  2.8  4.9  2.0  3
      sl   sw   pl   pw  c
122  7.7  2.8  6.7  2.0  3
      sl   sw   pl   pw  c
123  6.3  2.7  4.9  1.8  3
      sl   sw   pl   pw  c
124  6.7  3.3  5.7  2.1  3
      sl   sw   pl   pw  c
125  7.2  3.2  6.0  1.8  3
      sl   sw   pl   pw  c
126  6.2  2.8  4.8  1.8  3
      sl   sw   pl   pw  c
127  6.1  3.0  4.9  1.8  3
      sl   sw   pl   pw  c
128  6.4  2.8  5.6  2.1  3
      sl   sw   pl   pw  c
129  7.2  3.0  5.8  1.6  3
      sl   sw   pl   pw  c
130  7.4  2.8  6.1  1.9  3
      sl   sw   pl   pw  c
131  7.9  3.8  6.4  2.0  3
      sl   sw   pl   pw  c
132  6.4  2.8  5.6  2.2  3
      sl   sw   pl   pw  c
133  6.3  2.8  5.1  1.5  3
      sl   sw   pl   pw  c
134  6.1  2.6  5.6  1.4  3
      sl   sw   pl   pw  c
135  7.7  3.0  6.1  2.3  3
      sl   sw   pl   pw  c
136  6.3  3.4  5.6  2.4  3
      sl   sw   pl   pw  c
137  6.4  3.1  5.5  1.8  3
      sl   sw   pl   pw  c
138  6.0  3.0  4.8  1.8  3
      sl   sw   pl   pw  c
139  6.9  3.1  5.4  2.1  3
      sl   sw   pl   pw  c
140  6.7  3.1  5.6  2.4  3
      sl   sw   pl   pw  c
141  6.9  3.1  5.1  2.3  3
      sl   sw   pl   pw  c
142  5.8  2.7  5.1  1.9  3
      sl   sw   pl   pw  c
143  6.8  3.2  5.9  2.3  3
      sl   sw   pl   pw  c
144  6.7  3.3  5.7  2.5  3
      sl   sw   pl   pw  c
145  6.7  3.0  5.2  2.3  3
      sl   sw   pl   pw  c
146  6.3  2.5  5.0  1.9  3
      sl   sw   pl   pw  c
147  6.5  3.0  5.2  2.0  3
      sl   sw   pl   pw  c
148  6.2  3.4  5.4  2.3  3
      sl   sw   pl   pw  c
149  5.9  3.0  5.1  1.8  3

In [ ]:
Regresyondan çok farklı değil, sadece çıktılar kategorik.

Logistic Regression

  • Girdi: Öznitelik vektörü
  • Çıktı: Evet/Hayır cevabı
Sınıf Öznitelik1 Öznitelik2
0 5.7 3.1
1 -0.3 2
--- ---
$y_i$ $x_{i,1}$ $x_{i,2}$
--- ---
1 0.4 5

Yeni veri:

Sınıf Öznitelik1 Öznitelik2
? 4.8 3.2
? -0.7 2.4
--- ---
\begin{eqnarray} X & = & \begin{pmatrix} x_{1,1} & x_{1,2} & \dots & x_{1,D} \\ x_{2,1} & x_{2,2} & \dots & x_{2,D} \\ \vdots & \vdots & \vdots & \vdots \\ x_{i,1} & x_{i,2} & \dots & x_{i,D} \\ \vdots & \vdots & \vdots & \vdots \\ x_{N,1} & x_{N,2} & \dots & x_{N,D} \\ \end{pmatrix} = \begin{pmatrix} x_1^\top \\ x_2^\top \\ \dots \\ x_i^\top \\ \dots \\ x_N^\top \end{pmatrix} \\ {y} & = & \begin{pmatrix} y_1 \\ y_2 \\ \vdots \\ y_i \\ \vdots \\ y_N \end{pmatrix} \end{eqnarray}

İki sınıf: $y_i \in \{0,1\}$.

$$ \Pr\{y_i = 1\} = \pi_i = \sigma(x_i^\top w) $$

$\sigma(x)$: sigmoid fonksyonu \begin{eqnarray} \sigma(x) & = & \frac{1}{1+e^{-x}} \end{eqnarray}

\begin{eqnarray} \pi_i & = & \sigma(x_i^\top w) \\ y_i & \sim &\mathcal{BE}(\pi) \end{eqnarray}

Her etiket için bir para atıyoruz ve paranın $1$ gelme olasılığını özniteliklere bağlıyoruz.

\begin{eqnarray} p(y|\pi) = \pi^y(1-\pi)^{1-y} \end{eqnarray}

In [46]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import scipy as sc
import pandas as pd

from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets
from IPython.display import clear_output, display, HTML
from matplotlib import rc

import scipy as sc
import scipy.optimize as opt


def sigmoid(x):
    return 1/(1+np.exp(-x))

def inv_sigmoid(p=0.5):
    xs = opt.bisect(lambda x: sigmoid(x)-p, a=-100, b=100)
    return xs

def inv_sigmoid1D(w, b, p=0.5):
    xs = opt.bisect(lambda x: sigmoid(w*x+b)-p, a=-100, b=100)
    return xs
    
fig = plt.figure(figsize=(6,4))
ax = fig.gca()
ax.set_ylim([-0.1,1.1])
x = np.linspace(-10,10,100)
ax.set_xlim([-10,10])

ln = plt.Line2D(x, sigmoid(x))
ln2 = plt.axvline([0], ls= ':', color='k')
ln_left = plt.axvline([0], ls= ':', color='b')
ln_right = plt.axvline([0], ls= ':', color='r')

ax.add_line(ln)
plt.close(fig)
ax.set_xlabel('$x$')
ax.set_ylabel('$\sigma(wx + b)$')

def plot_fun(w, b):
    ln.set_ydata(sigmoid(w*x+b))
    if np.abs(w)>0.00001:
        ln2.set_xdata(inv_sigmoid1D(w,b,0.5))
        ln_left.set_xdata(inv_sigmoid1D(w,b,0.25))
        ln_right.set_xdata(inv_sigmoid1D(w,b,0.75))
    display(fig)
    
res = interact(plot_fun, w=(-3, 3, 0.1), b=(-10.0,10.0,0.1))


Parametreleri öğrenmek

Verilen özniteliklerle etiketleri gözlemleme ihtimali:

$\begin{eqnarray} p(y_1, y_2, \dots, y_N|w, X ) &=& \left(\prod_{i : y_i=1} \sigma(x_i^\top w) \right) \left(\prod_{i : y_i=0}(1- \sigma(x_i^\top w)) \right) \end{eqnarray} $

$ \begin{eqnarray} {\cal L}(w) & = & \log p(y_1, y_2, \dots, y_N|w, x_1, x_2, \dots, x_N ) \\ & = & \sum_{i : y_i=1} \log \sigma(x_i^\top w) + \sum_{i : y_i=0} \log (1- \sigma(x_i^\top w)) \\ & = & \sum_{i : y_i=1} x_i^\top w - \sum_{i : y_i=1} \log(1+e^{x_i^\top w}) - \sum_{i : y_i=0}\log({1+e^{x_i^\top w}}) \\ & = & \sum_i y_i x_i^\top w - \sum_{i} \log(1+e^{x_i^\top w}) \\ & = & y^\top X w - \mathbf{1}^\top \text{logsumexp}(0, X w) \end{eqnarray} $

$ \begin{eqnarray} w^* & = & \arg\max_{w} {\cal L}(w) \end{eqnarray} $

$\mathbf{1}^T v = \sum_i v_i$.

$$ f = \text{logsumexp}(a, b) \equiv \log(e^a + e^b) $$$$ f_i = \text{logsumexp}(a_i, b_i) \equiv \log(e^{a_i} + e^{b_i}) $$

En küçük kare hata yaklaşımından farklı olarak burada gerçekten bayır çıkışı kullanmamız gerkiyor.

Hinge (Menteşe) hata fonksyonu $$ h(x) = \left\{ \begin{array}{cc} 0 & x < 0 \\x & x \geq 0 \end{array} \right. $$

$$ f_\alpha(x) = \frac{1}{\alpha}\text{logsumexp}(0, \alpha x) $$

$\alpha = 1$, orjinal logsumexp. Büyük $\alpha$: hinge


In [40]:
%matplotlib inline
import numpy as np
import matplotlib as mpl
import matplotlib.pylab as plt

def logsumexp(a,b):
    m = np.max([a,b])
    return m + np.log(np.exp(a-m) + np.exp(b-m))

def hinge(x):
    return x if x>0 else 0

xx = np.arange(-5,3,0.1)

plt.figure(figsize=(12,10))
for i,alpha in enumerate([1,2,5,10]):
    f = [logsumexp(0, alpha*z)/alpha for z in xx]
    h = [hinge(z) for z in xx]
    
    plt.subplot(2,2,i+1)
    plt.plot(xx, f, 'r')
    plt.plot(xx, h, 'k:')
    plt.xlabel('z')
    #plt.title('a = '+ str(alpha))
    if alpha==1:
        plt.legend([ 'logsumexp(0,z)','hinge(z)' ], loc=2 )    
    else:
        plt.legend([ 'logsumexp(0,{a} z)/{a}'.format(a=alpha),'hinge(z)' ], loc=2 )
  
    
plt.show()


Negatif olabilirlik azaltmak - Hata düşürmek

$$ - \mathcal{L}(\pi) = - \sum_i l_i(w) $$$$ E_i(w) \equiv -l_i(w) = - y_i x_i^\top w + \text{logsumexp}(0, x_i^\top w) = - y_i z_i + \text{logsumexp}(0, z_i) $$

In [21]:
xx = np.arange(-10,10,0.1)
y = 1
f = [-y*z + logsumexp(0, z) for z in xx]
f0 = [logsumexp(0, z) for z in xx]

plt.figure(figsize=(12,5))
plt.subplot(1,2,1)
plt.plot(xx, f, 'r')
plt.xlabel('$z_i$')
plt.ylabel('$-l_i$')
plt.title('Pozitif örnekler için hata. $y = $'+str(y))
plt.subplot(1,2,2)
plt.plot(xx, f0, 'r')
plt.xlabel('$z_i$')
plt.ylabel('$-l_i$')
plt.title('Negatif örnekler için hata. $y = 0$')

plt.show()


Sentetik veri kümesi üzerinde gösterim


In [47]:
%matplotlib inline
import numpy as np
import matplotlib as mpl
import matplotlib.pylab as plt

# Generate a random logistic regression problem

def sigmoid(t):
    return np.exp(t)/(1+np.exp(t))


def generate_toy_dataset(number_of_features=3, number_of_datapoints=20, styles = ['ob', 'xr']):

    D = number_of_features
    N = number_of_datapoints
    

    # Some random features
    X = 2*np.random.rand(N,D)-1
    X[:,0] = 1

    # Generate a random paramater vector
    w_true = np.random.randn(D,1)

    # Generate class labels
    pi = sigmoid(np.dot(X, w_true))
    y = np.array([1 if u else 0 for u in np.random.rand(N,1) < pi]).reshape((N))

    return X, y, w_true, D, N

In [48]:
styles = ['ob', 'xr']
X, y, w_true, D, N = generate_toy_dataset(number_of_features=3, number_of_datapoints=20, styles=styles)

xl = -1.5; xr = 1.5; yl = -1.5; yr = 1.5

fig = plt.figure(figsize=(5,5))
plt.plot(X[y==1,1],X[y==1,2],styles[1])
plt.plot(X[y==0,1],X[y==0,2],styles[0])

ax = fig.gca()
ax.set_ylim([yl, yr])
ax.set_xlim([xl, xr])
plt.show()



In [49]:
# Implement Gradient Descent
w = np.random.randn(D)

# Learnig rate
eta = 0.05

W = []
MAX_ITER = 200
for epoch in range(MAX_ITER):
    W.append(w)
    dL = np.dot(X.T, y-sigmoid(np.dot(X,w)))
    w = w + eta*dL

In [50]:
xl = -1.5
xr = 1.5
yl = -1.5
yr = 1.5

fig = plt.figure(figsize=(5,5))
ax = fig.gca()
ax.set_ylim([yl, yr])
ax.set_xlim([xl, xr])

plt.plot(X[y==1,1],X[y==1,2],styles[1])
plt.plot(X[y==0,1],X[y==0,2],styles[0])


ln = plt.Line2D([],[],color='k')
ln_left  = plt.Line2D([],[],ls= ':', color=styles[0][1])
ln_right = plt.Line2D([],[],ls= ':', color=styles[1][1])

ax.add_line(ln)
ax.add_line(ln_left)
ax.add_line(ln_right)
plt.close(fig)
ax.set_xlabel('$x_1$')
ax.set_ylabel('$x_2$')
ax.set_xticks(np.arange(xl,xr))
ax.set_yticks(np.arange(yl,yr))
ax.grid(True)
def plot_boundry(w0,w1,w2):
    if w1 != 0:
        xa = -(w0+w2*yl)/w1
        xb = -(w0+w2*yr)/w1
        ln.set_xdata([xa, xb])
        ln.set_ydata([yl, yr])

        xa = -(-inv_sigmoid(0.25) + w0+w2*yl)/w1
        xb = -(-inv_sigmoid(0.25) + w0+w2*yr)/w1
        ln_left.set_xdata([xa, xb])
        ln_left.set_ydata([yl, yr])

        xa = -(-inv_sigmoid(0.75) + w0+w2*yl)/w1
        xb = -(-inv_sigmoid(0.75) + w0+w2*yr)/w1
        ln_right.set_xdata([xa, xb])
        ln_right.set_ydata([yl, yr])
        
        
    elif w2!=0:
        ya = -(w0+w1*xl)/w2
        yb = -(w0+w1*xr)/w2
        ln.set_xdata([xl, xr])
        ln.set_ydata([ya, yb])

        ya = -(-inv_sigmoid(0.25) + w0+w1*xl)/w2
        yb = -(-inv_sigmoid(0.25) + w0+w1*xr)/w2
        ln_left.set_xdata([xl, xr])
        ln_left.set_ydata([ya, yb])

        ya = -(-inv_sigmoid(0.75) + w0+w1*xl)/w2
        yb = -(-inv_sigmoid(0.75) + w0+w1*xr)/w2
        ln_right.set_xdata([xl, xr])
        ln_right.set_ydata([ya, yb])      
        
    else:
        ln.set_xdata([])
        ln.set_ydata([])
        
    display(fig)

def plot_boundry_of_weight(iteration=0):
    i = iteration
    w = W[i]
    plot_boundry(w[0],w[1],w[2])
    
interact(plot_boundry_of_weight, iteration=(0,len(W)-1))


Out[50]:
<function __main__.plot_boundry_of_weight>