I have implemented stochastic gradient descent. But error is coming fluctuating in this.
Can you tell whags the error in this?
def fit( X, y, x_val, y_val):
“”"
Fitting (training) the logistic model.
Parameters
----------
X : 2-dimensional numpy array of shape (n_samples, n_features) which acts as training data.
y : 1-dimensional numpy array of shape (n_samples,) which acts as training labels.
Returns
-------
self : an instance of self
"""
theta = np.zeros(X.shape[1])
c=0
learning_rate =0.01
epochs =10000
training_loss=[]
validation_loss=[]
for i in range(1,epochs+1):
r = random.randint(0, X.shape[0]-1)
Xs = X[r]
ys = y[r]
#Xs=Xs.tolist()
#ys=ys.tolist()
y_hat = np.dot(Xs, theta)+c
htheta = 1 / (1 + np.exp((-1)*y_hat))
cost = np.sum((-ys) * np.log(htheta) - (1 - ys) * np.log(1 - htheta))
#Xs = np.asarray([Xs])
#ys = np.asarray([ys])
dtheta = np.dot((htheta - ys),Xs)
dc = htheta-ys
theta=theta -learning_rate*dtheta
#if (i==1):
#print(theta)
#print(cost)
c= c- learning_rate*dc
training_loss.append(cost)
#y_hat_test = np.dot(x_test, theta)+c
#print(y_hat_test.shape)
#htheta_test = sigmoid(y_hat_test)
#print(htheta_test.shape)
#cost_test = costfunction(y_test,htheta_test)
#validation_loss.append(cost_test)
plt.plot(training_loss)
#plt.plot(validation_loss)
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Logistic Regression : Learning rate= '+str(learning_rate))
plt.show()
# fit function has to return an instance of itself or else it won't work with test.py
return theta, c