I am trying to run a number of iteration values and alpha values using for loop inside the gradient descent function. But I am getting this error.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-42-c9c041ea6c72> in <module>
      8 
      9 #running the Gradient Descent and cost function
---> 10 beta,cost = gradientDescent(X,y,theta,iters,alpha)
     11 print(beta)
     12 

<ipython-input-42-c9c041ea6c72> in gradientDescent(X, y, theta, iters, alpha)
      3     cost = np.zeros(iters)
      4     for i in range(iters):
----> 5         theta = theta - (alpha/len(X)) * np.sum(X * (X @ theta.T - y), axis=0)
      6         cost[i] = computeCost(X, y, theta)
      7     return theta,cost

TypeError: unsupported operand type(s) for /: 'function' and 'int'
theta = np.random.rand(1,15)
list = [0.01,0.03,0.05,0.1,0.001,0.003]
iters = 20
def alpha():
    for alpha in list:
        return alpha
def lm(X,y,theta):
    pred = X @ theta.T
    error = np.sqrt((pred-y)**2/len(X))
    rmse = np.sum(error)/len(X)
    return pred,rmse
def computeCost(X,y,theta):
    cost = np.power(((X @ theta.T)-y),2)
    return np.sum(cost)/(2 * len(X))
def gradientDescent(X,y,theta,iters,alpha):
    cost = np.zeros(iters)
    for i in range(iters):
        theta = theta - (alpha/len(X)) * np.sum(X * (X @ theta.T - y), axis=0)
        cost[i] = computeCost(X, y, theta)
    return theta,cost

#running the Gradient Descent and cost function
beta,cost = gradientDescent(X,y,theta,iters,alpha)
print(beta)

finalCost = computeCost(X,y,beta)
print(finalCost)

Related posts

Recent Viewed