In [104]:
%matplotlib inline
import numpy as np
import pandas as pd
import inspect
import matplotlib.pyplot as plt
from perceptron import Perceptron
plt.style.use('ggplot')
In [2]:
print inspect.getsource(Perceptron)
In [4]:
inputs = np.array([0.2, 12.2,0.98])
pc = Perceptron(len(inputs), 0.5)
The perceptron was initialized with random weights to start.
In [5]:
pc
Out[5]:
In [55]:
def f(x):
return -2.*x + 10.
In [57]:
x = np.array([1,3,7,4])
y = f(x_arr)
In [105]:
plt.plot(x,y)
Out[105]:
In [121]:
y_test = np.random.randint(1,10,4)
y_test
Out[121]:
In [122]:
fig, axs = plt.subplots()
axs.plot(*zip(x, y_test), marker='o', ls='')
axs.plot(x,y)
axs.spines["top"].set_visible(False)
axs.spines["right"].set_visible(False)
axs.spines["left"].set_visible(False)
axs.spines["bottom"].set_visible(False)
In [125]:
training_data = (y < y_test).astype(int)
training_data
Out[125]:
In [126]:
x, y_test, training_data
Out[126]:
In [129]:
df = pd.DataFrame({'x':x, 'y':y_test, 'target':training_data})
In [130]:
df
Out[130]:
In [138]:
pc.train(np.array([1.0, x[0], y_test[0]]), training_data[0])
In [140]:
pc
Out[140]:
In [142]:
for i in xrange(4):
pc.train(np.array([1.0, x[i], y_test[i]]), training_data[i])
In [143]:
pc
Out[143]:
In [144]:
for i in xrange(4):
pc.train(np.array([1.0, x[i], y_test[i]]), training_data[i])
In [145]:
pc
Out[145]:
In [ ]: