I'm new to Machine Learning. My Perceptron code works but just for the first training example. Also, the b_final
is n-dimensional array instead of being a scalar. Can you help me with these issues? The X.shape = (150,4)
and y.shape = (100,)
.
The dataset is "Iris dataset".
m, n = X.shape
def weighted_sum(X,w,b):
for i in range(m):
f_Xwb = np.dot(X[i],w) + b
return f_Xwb
def Prediction(X,initial_w,initial_b):
return np.where(weighted_sum(X,initial_w,initial_b) > 0 , 1 , -1)
def weights(X , y , w , b , epochs , eta):
for i in range(epochs):
for xi,yi in zip(X,y):
w = w - eta * ((Prediction(xi,initial_w,initial_b)-yi) * xi)
b = b - eta * (Prediction(xi,initial_w,initial_b)-yi)
return w , b
initial_w = np.ones(n)
initial_b = 1
w_final , b_final = weights(X,y,initial_w,initial_b,50,0.3)
print(w_final,b_final)
y_prediction = Prediction(X,w_final,b_final)
y_prediction
Also, the y_prediction
is n-dimensional array instead of being a scalar.