Search code examples
pythonmachine-learninggradient-descent

Batch Gradient descent algorithm showing 'int' not iterable error


lr = 0.1
n_iterations = 1000
m = 5

theta = np.array([[1000],[989],[123],[3455]])

for iterations in n_iterations:
    gradients = 2/m * X_b.T.dot(X_b.dot(theta) - y)
    theta = theta - lr * gradients
    
theta

After executing the code it says an error 'int' is not iterable.

More data:

X_b = np.asanyarray(df[['area', 'bedrooms', 'age']])

from a csv file

and Im using the three params(area,bedrooms,age) to predict the price i.e y

Please help me in that error


Solution

  • n_iterations is an int, which is not iterable like the error says. I think you want to loop n_iterations times.

    Try range for that like this:

    lr = 0.1
    n_iterations = 1000
    m = 5
    
    theta = np.array([[1000],[989],[123],[3455]])
    
    for iterations in range(n_iterations):
        gradients = 2/m * X_b.T.dot(X_b.dot(theta) - y)
        theta = theta - lr * gradients
        
    theta