mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-27 00:46:43 +00:00
Infinite loop was fixed. (#1105)
* Infinite loop was fixed. Removed issue of unused variables. * Update logistic_regression.py * Update logistic_regression.py * correct spacing according to PEP8
This commit is contained in:
parent
c92d06bf1f
commit
32c0418f63
@ -40,34 +40,20 @@ def logistic_reg(
|
|||||||
alpha,
|
alpha,
|
||||||
X,
|
X,
|
||||||
y,
|
y,
|
||||||
num_steps,
|
|
||||||
max_iterations=70000,
|
max_iterations=70000,
|
||||||
):
|
):
|
||||||
converged = False
|
|
||||||
iterations = 0
|
|
||||||
theta = np.zeros(X.shape[1])
|
theta = np.zeros(X.shape[1])
|
||||||
|
|
||||||
while not converged:
|
for iterations in range(max_iterations):
|
||||||
z = np.dot(X, theta)
|
z = np.dot(X, theta)
|
||||||
h = sigmoid_function(z)
|
h = sigmoid_function(z)
|
||||||
gradient = np.dot(X.T, h - y) / y.size
|
gradient = np.dot(X.T, h - y) / y.size
|
||||||
theta = theta - alpha * gradient
|
theta = theta - alpha * gradient # updating the weights
|
||||||
z = np.dot(X, theta)
|
z = np.dot(X, theta)
|
||||||
h = sigmoid_function(z)
|
h = sigmoid_function(z)
|
||||||
J = cost_function(h, y)
|
J = cost_function(h, y)
|
||||||
iterations += 1 # update iterations
|
if iterations % 100 == 0:
|
||||||
weights = np.zeros(X.shape[1])
|
print(f'loss: {J} \t') # printing the loss after every 100 iterations
|
||||||
for step in range(num_steps):
|
|
||||||
scores = np.dot(X, weights)
|
|
||||||
predictions = sigmoid_function(scores)
|
|
||||||
if step % 10000 == 0:
|
|
||||||
print(log_likelihood(X,y,weights)) # Print log-likelihood every so often
|
|
||||||
return weights
|
|
||||||
|
|
||||||
if iterations == max_iterations:
|
|
||||||
print('Maximum iterations exceeded!')
|
|
||||||
print('Minimal cost function J=', J)
|
|
||||||
converged = True
|
|
||||||
return theta
|
return theta
|
||||||
|
|
||||||
# In[68]:
|
# In[68]:
|
||||||
@ -78,8 +64,8 @@ if __name__ == '__main__':
|
|||||||
y = (iris.target != 0) * 1
|
y = (iris.target != 0) * 1
|
||||||
|
|
||||||
alpha = 0.1
|
alpha = 0.1
|
||||||
theta = logistic_reg(alpha,X,y,max_iterations=70000,num_steps=30000)
|
theta = logistic_reg(alpha,X,y,max_iterations=70000)
|
||||||
print(theta)
|
print("theta: ",theta) # printing the theta i.e our weights vector
|
||||||
|
|
||||||
|
|
||||||
def predict_prob(X):
|
def predict_prob(X):
|
||||||
@ -105,3 +91,4 @@ if __name__ == '__main__':
|
|||||||
)
|
)
|
||||||
|
|
||||||
plt.legend()
|
plt.legend()
|
||||||
|
plt.show()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user