def train (features, labels, weights, learning_rate, iter) :
history_loss=[]
for i in range (iter):
weights = update_weight(features,labels,weights,learning_rate)
loss = loss_function(features,labels,weights)
history_loss.append(loss)
#print("Loss in {} is : {}".format(i, history_loss[i]))
return weights,history_loss
# Thêm cột 1 vào dữ liệu x
x = np.hstack((np.ones((N, 1)), x))
w = np.array([0., 0.1, 0.1]).reshape(-1, 1)
# Số lần lặp bước 2
numOfIteration = 100000
cost = np.zeros((numOfIteration, 1))
learning_rate = 0.01
w,loss = train(x,y,w,learning_rate,numOfIteration)
x_test = [1,5,9]
temp= predict(x_test,w)
print("Value will be predicted for student who slept {} hours and studied {} hours".format(x_test[1],x_test[2]))
if (decisionBoundary(temp)==1) :
print("Predict value is {}. So this student will pass!!".format(decisionBoundary(temp)))
else: print("Predict value is {}. So this student will fail!!".format(decisionBoundary(temp)))
plt.scatter(true_x,true_y,marker="o",c="y",edgecolors='none', s=30, label='Pass')
plt.scatter(false_x,false_y,marker="o",c="r",edgecolors='none', s=30, label='Fail')
plt.legend(loc=1)
plt.xlabel('Studied')
plt.ylabel('Slept')
plt.show()
yTime_series = np.array([i for i in range(numOfIteration)])
plt.plot(yTime_series,loss)
plt.xlabel("Time")
plt.ylabel("Loss")
plt.show()