1

Problem

I have written this code, but this is giving errors:

RuntimeWarning: overflow encountered in multiply

t2_temp = sum(x*(y_temp - y))

RuntimeWarning: overflow encountered in double_scalars

t1_temp =  sum(y_temp - y)

Should I use feature scaling or is there something wrong in my code?

Code

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def gradient_descent(x,y,t1,t2,repeat,alpha):
    n = x.size
    for i in range(repeat):
        y_temp = x*t2 + t1
        t1_temp =  sum(y_temp - y)
        t2_temp = sum(x*(y_temp - y))
        t1 = t1 - alpha * (t1_temp/n)
        t2 = t2 - alpha * (t2_temp/n)

    return [t1,t2]


d = pd.read_csv("train.csv")

x = d['GrLivArea']
y = d['SalePrice']

x = (np.array(x.values))
y = (np.array(y.values))


alpha = 0.047
repeat = 3000

theta = [1.23154644,1.654132398]

tt = gradient_descent(x,y,theta[0],theta[1],repeat,alpha)

print("FINISH")
2
  • 2
    Try scaling your values in the columns by a factor, and run it. Most likely the column values are too big, causing overflow. Commented Mar 22, 2019 at 4:36
  • Which version of Python are you using? Commented Jan 11, 2021 at 9:51

1 Answer 1

1

Your code is fine; it is x and y that are too large. You can perform standardisation or some other form of scaling.

Sign up to request clarification or add additional context in comments.

6 Comments

I am applying but for testing data i am getting wrong output. Feature scaling will give the value of theta1 and theta2 which we can use as it is in our equation or testing data should also be processed further ?
After getting value of theta1 and theta2 i am putting it into equation and i am checking my test data , that is basically plot size , so the output which should be price is also scaled down.
If you have not scaled your y values, then the scaling of x should only affect the coefficients, not the results. Have your coefficients converged? (check if the gradients are near 0)
Yes i have scaled both x and y and my coefficients are also converging both are near 0.
@K.Bakshi well, in that case you would need to scale y up again.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.