Forum

Notifications
Clear all

Multiple linear regression-DataCamp Exercise  

   RSS

0
Hi, can someone help me understand this part of the code in the 'Multiple Linear Regression' exercise on data camp? I'm not sure about the part that is bold in the code below.
1) Why is there no argument attached to lambda (before the ':')  
2) Where does the function 'print_results()' come from? it's not a built-in function the way that print() is. Correct me if I'm wrong..
 
# Perform minimization and print trainable variables
for j in range(10):
opt.minimize(lambda: loss_function(params), var_list=[params])
print_results(params)
 
 
Why is it not:
for j in range(10):
opt.minimize(loss_function(params)[j], var_list=[params][j])
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Full code:
 
# Define the linear regression model
def linear_regression(params, feature1 = size_log, feature2 = bedrooms):
return params[0] + feature1*params[1] + feature2*params[2]
 
# Define the loss function
def loss_function(params, targets = price_log, feature1 = size_log, feature2 = bedrooms):
# Set the predicted values
predictions = linear_regression(params, feature1, feature2)
  
# Use the mean absolute error loss
return keras.losses.mae(targets, predictions)
 
# Define the optimize operation
opt = keras.optimizers.Adam()
 
# Perform minimization and print trainable variables
for j in range(10):
opt.minimize(lambda: loss_function(params), var_list=[params])
print_results(params)
 
 
1 Answer
0

Hey @mmun-y,

Great questions.

1) Why is there no argument attached to lambda (before the ':')  

This happens when the lambda function doesn't take any input. It always has the same output, which is loss_function(params)

Some examples to illustrate:

lambda x: x +5 is a function which takes an input, adds 5 and outputs it.

lambda : 5 is a function which always returns 5.

2) Where does the function 'print_results()' come from? it's not a built-in function the way that print() is. Correct me if I'm wrong..

You're right! It's predefined in DataCamp beforehand, just like plot_results in the lesson just before this one.

@siowy
oh I see.. thanks!! it was hard to find examples on the internet of lambda without argument, they usually give examples similar to your first one: lambda x: x +5 etc..

happy to help 🙂

Share:

Delete your account