Question: Python error: GradientDescent.py:20: RuntimeWarning: overflow encountered in multiply D_m = (-2)*sum(x*(y-Y_pred)) GradientDescent.py:22: RuntimeWarning: invalid value encountered in double_scalars m = m-L*D_m nan nan I'm trying

Python error: GradientDescent.py:20: RuntimeWarning: overflow encountered in multiply D_m = (-2)*sum(x*(y-Y_pred)) GradientDescent.py:22: RuntimeWarning: invalid value encountered in double_scalars m = m-L*D_m nan nan

I'm trying to make a program that calculates linear regression using gradient descent. However, the program gives up and does not show the line on top of the scatter plot. How would I fix this error?

Code:

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

x = pd.read_csv('ex3x.csv')

x = x.iloc[:,0].values

y = pd.read_csv('ex3y.csv')

y = y.iloc[:,0].values

m = 0

c = 0

L = 0.0001 #The learning rate

epochs = 1000 #The number of iterations to perform gradient descent

n = float(len(x))

for i in range(epochs):

Y_pred = m*x + c

D_m = (-2)*sum(x*(y-Y_pred))

D_c = (-2)*sum(y-Y_pred)

m = m-L*D_m

c = c-L*D_c

print(m,c)

Y_pred = m*x+c

plt.scatter(x,y)

plt.plot([min(x),max(x)], [min(Y_pred),max(Y_pred)], color='red')

plt.show()Python error: GradientDescent.py:20: RuntimeWarning: overflow encountered in multiply D_m = (-2)*sum(x*(y-Y_pred)) GradientDescent.py:22:

13 L = 0.0001 #The learning rate epochs = 1000 #The number of iterations to perform gradient descent 16 n = float(len(x)) 18 for i in range(epochs): -.Y_pred = m*x + c ---D_m = (-2)*sum(x*(y-Y_pred)) D C = (-2) *sum(y-Y pred) m = m-L*Dm C = C-L*D_C T print(m,c) Y_pred = m*x+C 28 plt.scatter(x,y) 29plt.plot([min(x), max(x)], [min(Y_pred), max (Y_pred)], color='red'), 30 plt.show()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!