In [2]:
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
In [4]:
X = np.array([
    [8000, 4000],
    [4000, 2000],
    [5000, 6000],
    [3000, 5000],
    [   0, 2000]
])

y = np.array([+1,+1,-1,-1,-1])

X[1]*X[2]
Out[4]:
array([20000000, 12000000])
In [3]:
for i in np.arange(0,len(X)):
    x_i = X[i]
    y_i = y[i]
    #print(i,x_i,y_i)
    if y_i==-1: # negative samples
        plt.scatter(x_i[0], x_i[1], s=120, marker='_', linewidths=2,color='r')
    if y_i==1: # positive samples
        plt.scatter(x_i[0], x_i[1], s=120, marker='+', linewidths=2,color='b')
plt.plot([1000,10000],[1000,6000]) # example hyperplane
plt.xlabel('income');
plt.ylabel('debt');
In [4]:
N = 100
X = np.random.rand(N,2)*100000

w_true = np.array([2,-1])
b_true = -30000
f_true = lambda x :np.sign(w_true[0]*x[0]+w_true[1]*x[1] + b_true);

y = np.zeros([N,1])
for i in np.arange(0,len(X)):
    x_i = X[i]
    y_i = f_true(x_i)
    y[i] = y_i
    #print(i,x_i,y_i)
    if y_i==-1: # negative samples
        plt.scatter(x_i[0], x_i[1], s=120, marker='_', linewidths=2,color='r')
    if y_i==1: # positive samples
        plt.scatter(x_i[0], x_i[1], s=120, marker='+', linewidths=2,color='b')
plt.xlabel('income');
plt.ylabel('debt');
plt.plot([(-b_true-100000*w_true[1])/w_true[0],-b_true/w_true[0]],[100000,0]); # example hyperplane
In [5]:
w = np.zeros(2)
b = np.zeros(1)
f = lambda w,b,x :np.sign(w[0]*x[0]+w[1]*x[1] + b);

acc = np.zeros([N,1])

print(w)
eta = 1e-5;
for i in np.arange(0,len(X)):
    x_i = X[i]
    y_i = y[i]
    print(x_i, y_i, f(w,b,x_i), w,b)
    for j in np.arange(0,len(X)):
      acc[i] = acc[i] + y[j]*f(w,b,X[j])/len(X);
    if(y_i != f(w,b,x_i)): # If not classified correctly, adjust the line to account for that point.
       b = b + 1e5*eta*y_i;
       w[0] = w[0] + eta*y_i*x_i[0];
       w[1] = w[1] + eta*y_i*x_i[1];


for i in np.arange(0,len(X)):
    x_i = X[i]
    y_i = f_true(x_i)
    if y_i==-1: # negative samples
        plt.scatter(x_i[0], x_i[1], s=120, marker='_', linewidths=2,color='r')
    if y_i==1: # positive samples
        plt.scatter(x_i[0], x_i[1], s=120, marker='+', linewidths=2,color='b')
plt.xlabel('income');
plt.ylabel('debt');
    
plt.plot([(-b_true-100000*w_true[1])/w_true[0],-b_true/w_true[0]],[100000,0]); # true hyperplane

plt.plot([(-b-100000*w[1])/w[0],-b/w[0]],[100000,0]); # estimated hyperplane
    
plt.figure();
plt.plot(acc); # estimated accuracy
plt.xlabel('iteration');
plt.ylabel('accuracy');
                
[0. 0.]
[97053.03339389 22427.82794353] [1.] [0.] [0. 0.] [0.]
[80435.40916405  8295.01294707] [1.] [1.] [0.97053033 0.22427828] [1.]
[50767.54993168 72071.58829919] [-1.] [1.] [0.97053033 0.22427828] [1.]
[ 9351.48861776 94834.49661303] [-1.] [-1.] [ 0.46285483 -0.4964376 ] [0.]
[37059.37291664 45273.00045785] [-1.] [-1.] [ 0.46285483 -0.4964376 ] [0.]
[95608.32865023 27685.67343102] [1.] [1.] [ 0.46285483 -0.4964376 ] [0.]
[77306.24616726 31284.7624471 ] [1.] [1.] [ 0.46285483 -0.4964376 ] [0.]
[59152.23859272 85561.80236124] [1.] [-1.] [ 0.46285483 -0.4964376 ] [0.]
[51492.25598674 20208.04241844] [1.] [1.] [1.05437722 0.35918042] [1.]
[41647.4804632 89701.5372456] [-1.] [1.] [1.05437722 0.35918042] [1.]
[35887.88323418 27769.24443573] [1.] [1.] [ 0.63790242 -0.53783495] [0.]
[31788.71769109 43658.108874  ] [-1.] [-1.] [ 0.63790242 -0.53783495] [0.]
[15088.33788289 45079.83125852] [-1.] [-1.] [ 0.63790242 -0.53783495] [0.]
[49623.06075321 50216.56482966] [1.] [1.] [ 0.63790242 -0.53783495] [0.]
[70359.16003724 64071.84614696] [1.] [1.] [ 0.63790242 -0.53783495] [0.]
[24772.54866064 29332.6180079 ] [-1.] [1.] [ 0.63790242 -0.53783495] [0.]
[25282.73289662 58101.12344987] [-1.] [-1.] [ 0.39017693 -0.83116113] [-1.]
[ 3086.74952006 74921.68442431] [-1.] [-1.] [ 0.39017693 -0.83116113] [-1.]
[ 2463.52137768 33683.86763774] [-1.] [-1.] [ 0.39017693 -0.83116113] [-1.]
[ 7096.99967549 98879.4954445 ] [-1.] [-1.] [ 0.39017693 -0.83116113] [-1.]
[84532.50572365 59298.21325474] [1.] [-1.] [ 0.39017693 -0.83116113] [-1.]
[55153.5320843  78612.21254578] [1.] [1.] [ 1.23550199 -0.238179  ] [0.]
[71902.7448152  12859.41087013] [1.] [1.] [ 1.23550199 -0.238179  ] [0.]
[83606.17494214  9884.67796961] [1.] [1.] [ 1.23550199 -0.238179  ] [0.]
[5.73462826e+04 3.57815926e+01] [1.] [1.] [ 1.23550199 -0.238179  ] [0.]
[28144.99467035 65858.15552739] [-1.] [1.] [ 1.23550199 -0.238179  ] [0.]
[86709.11334476 37300.20274438] [1.] [1.] [ 0.95405204 -0.89676056] [-1.]
[9266.42577418 6262.42197281] [-1.] [1.] [ 0.95405204 -0.89676056] [-1.]
[64239.2647198 72147.8074786] [1.] [-1.] [ 0.86138778 -0.95938477] [-2.]
[51897.3452667  23708.08024071] [1.] [1.] [ 1.50378043 -0.2379067 ] [-1.]
[79122.02479851 77816.53806582] [1.] [1.] [ 1.50378043 -0.2379067 ] [-1.]
[68757.9201731   1940.63894647] [1.] [1.] [ 1.50378043 -0.2379067 ] [-1.]
[50527.95425687  5875.52467979] [1.] [1.] [ 1.50378043 -0.2379067 ] [-1.]
[16816.30443456 90055.27262038] [-1.] [1.] [ 1.50378043 -0.2379067 ] [-1.]
[ 5037.79630672 67980.59261331] [-1.] [-1.] [ 1.33561738 -1.13845943] [-2.]
[52932.07125409 16611.24165692] [1.] [1.] [ 1.33561738 -1.13845943] [-2.]
[36226.9724458  29795.01451009] [1.] [1.] [ 1.33561738 -1.13845943] [-2.]
[77510.26235482 56487.80282139] [1.] [1.] [ 1.33561738 -1.13845943] [-2.]
[41858.67201302 71402.80998513] [-1.] [-1.] [ 1.33561738 -1.13845943] [-2.]
[26660.63456634 93857.48290889] [-1.] [-1.] [ 1.33561738 -1.13845943] [-2.]
[95262.16843189 31292.88210197] [1.] [1.] [ 1.33561738 -1.13845943] [-2.]
[45544.47064119 82666.05275856] [-1.] [-1.] [ 1.33561738 -1.13845943] [-2.]
[18239.78830456 39156.39577277] [-1.] [-1.] [ 1.33561738 -1.13845943] [-2.]
[32158.22390534 23750.05990672] [1.] [1.] [ 1.33561738 -1.13845943] [-2.]
[16966.92813994 66212.50157673] [-1.] [-1.] [ 1.33561738 -1.13845943] [-2.]
[65129.87662394 60134.63221379] [1.] [1.] [ 1.33561738 -1.13845943] [-2.]
[49941.36608182 22794.40800811] [1.] [1.] [ 1.33561738 -1.13845943] [-2.]
[31009.51268364 80421.33290404] [-1.] [-1.] [ 1.33561738 -1.13845943] [-2.]
[59459.97059693 66769.76883372] [1.] [1.] [ 1.33561738 -1.13845943] [-2.]
[31378.21160407 65283.27345092] [-1.] [-1.] [ 1.33561738 -1.13845943] [-2.]
[ 4100.62680803 42990.34956354] [-1.] [-1.] [ 1.33561738 -1.13845943] [-2.]
[40684.86256758 88032.29665204] [-1.] [-1.] [ 1.33561738 -1.13845943] [-2.]
[87008.07946074 66195.874848  ] [1.] [1.] [ 1.33561738 -1.13845943] [-2.]
[23279.72749041 71324.23881296] [-1.] [-1.] [ 1.33561738 -1.13845943] [-2.]
[85477.37851214 11918.79979028] [1.] [1.] [ 1.33561738 -1.13845943] [-2.]
[12694.00584342  5937.44107776] [-1.] [1.] [ 1.33561738 -1.13845943] [-2.]
[76257.29984746 47120.33709803] [1.] [1.] [ 1.20867733 -1.19783384] [-3.]
[30940.41444317 55220.3890158 ] [-1.] [-1.] [ 1.20867733 -1.19783384] [-3.]
[85139.30730409 90014.33794578] [1.] [-1.] [ 1.20867733 -1.19783384] [-3.]
[60802.39467772  1727.48052361] [1.] [1.] [ 2.0600704  -0.29769046] [-2.]
[86568.94701844 44145.62207631] [1.] [1.] [ 2.0600704  -0.29769046] [-2.]
[24618.68987679 40659.40390023] [-1.] [1.] [ 2.0600704  -0.29769046] [-2.]
[43597.03116925 67961.32402672] [-1.] [1.] [ 1.8138835 -0.7042845] [-3.]
[44806.47668693 73887.72825532] [-1.] [-1.] [ 1.37791319 -1.38389774] [-4.]
[85763.20554342 94613.41701547] [1.] [-1.] [ 1.37791319 -1.38389774] [-4.]
[88159.94386836 82752.81054862] [1.] [1.] [ 2.23554524 -0.43776357] [-3.]
[55088.87880222 11638.10847853] [1.] [1.] [ 2.23554524 -0.43776357] [-3.]
[59219.96164721 16735.8406202 ] [1.] [1.] [ 2.23554524 -0.43776357] [-3.]
[43704.74461613 89033.20618615] [-1.] [1.] [ 2.23554524 -0.43776357] [-3.]
[80400.10403215 34523.38196505] [1.] [1.] [ 1.7984978  -1.32809563] [-4.]
[96189.98328706 75278.09191778] [1.] [1.] [ 1.7984978  -1.32809563] [-4.]
[10676.04417877 42734.45444991] [-1.] [-1.] [ 1.7984978  -1.32809563] [-4.]
[83279.55813468 45686.78739502] [1.] [1.] [ 1.7984978  -1.32809563] [-4.]
[96021.3959188  52819.04162285] [1.] [1.] [ 1.7984978  -1.32809563] [-4.]
[20366.17708563 16998.73130162] [-1.] [1.] [ 1.7984978  -1.32809563] [-4.]
[37718.34699366 22976.24711325] [1.] [1.] [ 1.59483603 -1.49808294] [-5.]
[57056.37861889 27025.70210089] [1.] [1.] [ 1.59483603 -1.49808294] [-5.]
[24386.98743261 97562.24760258] [-1.] [-1.] [ 1.59483603 -1.49808294] [-5.]
[87413.02038326 86237.93094232] [1.] [1.] [ 1.59483603 -1.49808294] [-5.]
[57577.38384139 89379.79776309] [-1.] [-1.] [ 1.59483603 -1.49808294] [-5.]
[72969.79663118 34212.23194757] [1.] [1.] [ 1.59483603 -1.49808294] [-5.]
[75221.94847686 30691.51165508] [1.] [1.] [ 1.59483603 -1.49808294] [-5.]
[83156.46855868 63957.01736217] [1.] [1.] [ 1.59483603 -1.49808294] [-5.]
[67849.15039207 38612.73804449] [1.] [1.] [ 1.59483603 -1.49808294] [-5.]
[76158.94140911 36417.36128548] [1.] [1.] [ 1.59483603 -1.49808294] [-5.]
[44456.85423731 39413.6790743 ] [1.] [1.] [ 1.59483603 -1.49808294] [-5.]
[39164.35658944 91311.36827697] [-1.] [-1.] [ 1.59483603 -1.49808294] [-5.]
[11059.02618481  3874.08692268] [-1.] [1.] [ 1.59483603 -1.49808294] [-5.]
[92434.38160836 40249.33579065] [1.] [1.] [ 1.48424577 -1.53682381] [-6.]
[37106.30917636 72226.10952384] [-1.] [-1.] [ 1.48424577 -1.53682381] [-6.]
[30347.43908605 45937.16591932] [-1.] [-1.] [ 1.48424577 -1.53682381] [-6.]
[61759.42771448 52480.07408106] [1.] [1.] [ 1.48424577 -1.53682381] [-6.]
[38870.16319871 88393.81216438] [-1.] [-1.] [ 1.48424577 -1.53682381] [-6.]
[90862.38103098 48082.6093396 ] [1.] [1.] [ 1.48424577 -1.53682381] [-6.]
[15742.98856825 79034.5054899 ] [-1.] [-1.] [ 1.48424577 -1.53682381] [-6.]
[79162.29340549 17903.90404601] [1.] [1.] [ 1.48424577 -1.53682381] [-6.]
[15840.02607426  4793.79078747] [-1.] [1.] [ 1.48424577 -1.53682381] [-6.]
[91430.07237764 15873.50632497] [1.] [1.] [ 1.3258455  -1.58476172] [-7.]
[75965.11028872  4360.67903496] [1.] [1.] [ 1.3258455  -1.58476172] [-7.]
[86772.38951616 19242.29980142] [1.] [1.] [ 1.3258455  -1.58476172] [-7.]