In [2]:
import numpy as np

In [5]:
#https://blog.dbrgn.ch/2013/3/26/perceptrons-in-python/

def function(x):
    return x**2

training_data = [
    (np.array([1, 1]), 1),
    (np.array([2, 1]), 4),
    (np.array([3, 1]), 9),
    (np.array([9, 1]), 81),
    (np.array([-2, 1]), 4),
    (np.array([-6, 1]), 36),
]

print(training_data)

weights = np.random.rand(2)
errors = []
eta = 0.3
n = 10

for i in range(n):
    x, expected = np.random.choice(training_data)
    result = np.dot(weights, x)
    error = expected - unit_step(result)
    errors.append(error)
    w += eta * error * x
    
for x, _ in training_data:
    result = dot(x, weights)
    print(result, function(result))


[(array([1, 1]), 1), (array([2, 1]), 4), (array([3, 1]), 9), (array([9, 1]), 81), (array([-2,  1]), 4), (array([-6,  1]), 36)]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-deba786b46ec> in <module>()
     21 
     22 for i in range(n):
---> 23     x, expected = np.random.choice(training_data)
     24     result = np.dot(weights, x)
     25     error = expected - unit_step(result)

mtrand.pyx in mtrand.RandomState.choice (numpy/random/mtrand/mtrand.c:10351)()

ValueError: a must be 1-dimensional

In [ ]: