Linear Regression From Scratch

0.0(0)
studied byStudied by 16 people
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/12

flashcard set

Earn XP

Description and Tags

How to create a Linear Regression ML model with only python and numpy.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

13 Terms

1
New cards

Linear Regression Model with 1 Variable

The goal is to find the best w and b parameters that fit the data

<p>The goal is to find the best w and b parameters that fit the data</p>
2
New cards

Cost Function for 1 variable Linear Regression Model

Find w and b that give you the smallest cost value. This gives you the parameters that best fit the data.

<p>Find w and b that give you the smallest cost value. This gives you the parameters that best fit the data. </p>
3
New cards

Model Prediction for 1 variable

Equation for a line, with an intercept b and a slope w.

<p>Equation for a line, with an intercept b and a slope w.</p>
4
New cards

Exercise 1 objective: complete compute_cost

Iterate over the training examples for each example and compute,

  • the prediction of the model for that example

  • the cost for that example

  • return the total cost over all examples

  • what is m?

knowt flashcard image
5
New cards


def compute_cost(x, y, w, b):

	# number of training examples

	__________________________

	# you need to return this variable correctly

	___________________________

	# start loop here

	____________________________

	return total_cost
def compute_cost(x, y, w, b): 

    # number of training examples
    m = x.shape[0] 
    
    # You need to return this variable correctly
    total_cost = 0
    
    # start loop here
    for i in range(m):
        f_wb = w*x[i] + b
        cost = (f_wb - y[i])**2
        total_cost += cost
    total_cost /= 2*m
    
    return total_cost
6
New cards

Gradient Descent Algorithm for parameters w and b for linear regression

parameters w and b are both updated simultaneously.

<p>parameters w and b are both updated simultaneously.</p>
7
New cards

Derivative of

J(w,b)/db and J(w,b)/dw

knowt flashcard image
8
New cards

Exercise 2 objective: complete compute_gradient

iterate over the training examples and for each example compute:

  • prediction of the model for that example

  • gradient for parameters w and b for that example

  • return total gradient update for all examples

knowt flashcard image
9
New cards
def compute_gradient(x, y, w, b):

	# number of training examples

	_________________________

	# return the following variables correctly

	_________________________

	# start loop here

	_________________________

	#_____
	#_____

	return dj_dw, dj_db
def compute_gradient(x, y, w, b): 
   
    # Number of training examples
    m = x.shape[0]
    
    # return the following variables correctly
    dj_dw = 0
    dj_db = 0
    
    # start loop here
    for i in range(m):
        f_wb = w*x[i] + b
        dj_dw += (f_wb - y[i])*x[i]
        dj_db += f_wb - y[i]
    dj_dw /= m
    dj_db /= m
 
        
    return dj_dw, dj_db
10
New cards

Code

def compute_cost(x, y, w, b):

# number of training examples

____________________

# You need to return this variable correctly

total_cost = 0

### START CODE HERE ###

for i in range(m):

______________________

______________________

______________________

______________________

### END CODE HERE ###

return total_cost

def compute_cost(x, y, w, b):

# number of training examples

m = x.shape[0]

# You need to return this variable correctly

total_cost = 0

### START CODE HERE ###

for i in range(m):

f_wb = w*x[i] + b

cost = (f_wb - y[i])**2

total_cost += cost

total_cost /= 2*m

### END CODE HERE ###

return total_cost

11
New cards

Code

def compute_gradient(x, y, w, b):

# Number of training examples

m = x.shape[0]

# You need to return the following variables correctly

dj_dw = 0

dj_db = 0

### START CODE HERE ###

for i in range(m):

f_wb = w*x[i] + b

dj_dw += (f_wb - y[i])*x[i]

dj_db += f_wb - y[i]

dj_dw /= m

dj_db /= m

### END CODE HERE ###

return dj_dw, dj_db

12
New cards
term image
knowt flashcard image
13
New cards
term image
knowt flashcard image