Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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()image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Experts Guide To SQL

Authors: Frank Lusardi

1st Edition

0070390029, 978-0070390027

More Books

Students also viewed these Databases questions