iwrestledthebeartwice
iwrestledthebeartwice

Reputation: 734

Invalid index to scalar variable. How to fix this?

So, I checked other questions and answers about this and I didn't understand the reason, could you please help me with this, please

I'm learning linear regression and I implemented the code for Linear Regression with two variables. This implementation should predict the sum of two numbers but it's giving an error

Implemented code here:

import matplotlib.pyplot as plt
import numpy as np

x = np.array([[1,1],[1,2],[2,3],[3,4],[4,5],[5,6]])
y = np.array([2,3,5,7,9,11])

#hypothesis
def hypothesis(theta, x):
    return theta[0] + theta[1]*x[0] + theta[2]*x[1]

#cost function J(t0, t1, t2)
def cost(theta, x, y):
    m = x.shape[0]
    error = 0
    for i in range(m):
        d = x[i]
        hx = hypothesis(theta, d)
        error = error + (hx - y[i])**2
    return error

#differentiation of cost function
def diffGradient(theta, x, y):
    grad = np.zeros((3,))
    m = x.shape[0]
    for i in range(m):
        hx = hypothesis(theta, x)
        grad[0] = grad[0] + (hx - y[i])
        grad[1] = grad[1] + (hx - y[i])*x[0]
        grad[2] = grad[2] + (hx - y[i])*x[1]
    return 0

#gradient descent funtion
def gradientDescent(x, y, learning_rate = 0.001):
    theta = [-2.0,0.0,1.0]
    iter = 100
    error_list = []
    theta_list = []
    for i in range(iter):
        d = x[i]
        grad = diffGradient(theta, d, y)
        e = cost(theta, d, y)
        error_list.append(e)
        theta_list.append((theta[0],theta[1],theta[2]))
        #simultaneous update
        theta[0] = theta[0] - learning_rate*grad[0]
        theta[1] = theta[1] - learning_rate*grad[1]
        theta[2] = theta[2] - learning_rate*grad[2]
    return theta, theta_list, error_list

final_theta, theta_list, error_list = gradientDescent(x,y)

After the above line, I'm getting this error

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-56-bda7687d0af9> in <module>
----> 1 final_theta, theta_list, error_list = gradientDescent(x,y)

<ipython-input-55-033133fbfbd5> in gradientDescent(x, y, learning_rate)
      8         d = x[i]
      9         grad = diffGradient(theta, d, y)
---> 10         e = cost(theta, d, y)
     11         error_list.append(e)
     12         theta_list.append((theta[0],theta[1],theta[2]))

<ipython-input-41-6a07f4b81c9c> in cost(theta, x, y)
      5     for i in range(m):
      6         d = x[i]
----> 7         hx = hypothesis(theta, d)
      8         error = error + (hx - y[i])**2
      9     return error

<ipython-input-27-43ce9d7c567b> in hypothesis(theta, x)
      1 #hypothesis
      2 def hypothesis(theta, x):
----> 3     return theta[0] + theta[1]*x[0] + theta[2]*x[1]

IndexError: invalid index to scalar variable.

I don't know what I'm doing wrong. Any help would be appreciated.

Upvotes: 2

Views: 4453

Answers (1)

hpaulj
hpaulj

Reputation: 231530

What's the x that you pass to

gradientDescent(x,y)

Look at the traceback and try to figure out what variable has the problem - the problem is that you can't index a scalar, a number. It has to be a list or array. Trace the problem variable back up through your code.

In the traceback:

  • in the problem line you use x[0] and x[1]. What's x at this point?

  • In the calling function it's d, which is set with d = x[i]

  • In gradientDescent the passed variable is again called d, and set as d = x[i]

So you have 3 levels of indexing. Does the original x support that?

You have to understand the problem before you try a fix.

Upvotes: 1

Related Questions