I am trying to implement a single neuron by using delta learning rule with a logistic activation function. My code is below.
import numpy as np
X = np.matrix('2; 4; 6; 8; 10; 15; 20; 25; 30; 40; 50; 60')
g = np.matrix('4.32323; 4.96276; 5.45565; 6.27151; 6.8552; 8.64987; 10.32581; 12.21393; 14.45659; 15.87602; 15.82488; 16.19419')
norm_fac=16.19419
y = [x / norm_fac for x in g]
class SingleNeuron (object):
def __init__(self, eta=0.01, n_iter=10):
self.eta=eta
self.n_iter=n_iter
def fit (self, X, y):
self.w_ = np.zeros (X.shape[1]+1)
self.cost_ = []
for i in range (self.n_iter):
output = self.net_input(X)
errors = (y - output)
self.w_[1:] += self.eta * X[0:].T.dot(errors)
self.w_[0] += self.eta * errors.sum ()
cost = (errors**2).sum() / 2.0
self.cost_.append(cost)
return self
def net_input(self, X):
return 1/(1+ np.exp (-(np.dot(X, self.w_[1]) + self.w_[0])))
def predict(self, X):
return self.net_input(X)
SN = SingleNeuron (eta = 0.1, n_iter = 10)
SN.fit (X, y)
However, when I run the code, I came across with the error : array_prepare must return an ndarray or subclass thereof which is otherwise identical to its input.
I am aware there is a question answered before (Numpy __array_prepare__ error), however it did not help me much. I appreciate any help greatly. Thank you