Hi , I am getting below error when I execute the code in google colab.
0.947265625
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-149-3d91d5365e49> in <module>()
----> 1 wt_matrix = perceptron.fit(X_train, Y_train, 10000)
8 frames
/usr/local/lib/python3.6/dist-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
83
84 """
---> 85 return array(a, dtype, copy=False, order=order)
86
87
TypeError: float() argument must be a string or a number, not 'dict_values'
Below is the code
class Perceptron:
def __init__ (self):
self.w = None
self.b = None
def model(self, x):
return 1 if (np.dot(self.w, x) >= self.b) else 0
def predict(self, X):
Y = []
for x in X:
result = self.model(x)
Y.append(result)
return np.array(Y)
def fit(self, X, Y, epochs = 1, lr = 1):
self.w = np.ones(X.shape[1])
self.b = 0
accuracy = {}
max_accuracy = 0
wt_matrix = []
for i in range(epochs):
for x, y in zip(X, Y):
y_pred = self.model(x)
if y == 1 and y_pred == 0:
self.w = self.w + lr * x
self.b = self.b - lr * 1
elif y == 0 and y_pred == 1:
self.w = self.w - lr * x
self.b = self.b + lr * 1
wt_matrix.append(self.w)
accuracy[i] = accuracy_score(self.predict(X), Y)
if (accuracy[i] > max_accuracy):
max_accuracy = accuracy[i]
chkptw = self.w
chkptb = self.b
self.w = chkptw
self.b = chkptb
print(max_accuracy)
plt.plot(accuracy.values())
plt.ylim([0, 1])
plt.show()
return np.array(wt_matrix)
perceptron = Perceptron()
wt_matrix = perceptron.fit(X_train, Y_train, 10000,0.01)