1

As part of my ML uni course I am learning linear regression. Strangely, I came across the following problem and I am not sure how to go about it.

given are two vectors x and y :

x = np.linspace(x_min, x_max,50)
y = np.random.randint(-5/2,5/2, size=50)
y = y + 3 + 2*x 

I need to fill in the code in these two methods:

def linear_hypothesis(theta_0, theta_1):
    ''' Combines given arguments in a linear equation and returns it as a function

    Args:
        theta_0: first coefficient
        theta_1: second coefficient

    Returns:
        lambda that models a linear function based on theta_0, theta_1 and x
    ''' 
def mse_cost_function(x, y):
    ''' Implements MSE cost function as a function J(theta_0, theta_1) on given tranings data 

    Args:
        x: vector of x values 
        y: vector of ground truth values y 

    Returns:
        lambda J(theta_0, theta_1) that models the cost function
    '''

The above functions should then be called through the following code:

j = mse_cost_function(x, y)
print(j(2.1, 2.9))

And this is what confuses me. I am not sure which the return type of each function should and I dont understand what this line j(2.1, 2.9) is supposed to be doing since j is the return value of this method. could someone enlighten me? Thanks for any help !

hispaniccoder
  • 337
  • 2
  • 4
  • 12
  • it's written pretty clearly - `mse_cost_function` should return a function (a lambda), which is why doing `j(2.1, 2.9)` is perfectly reasonable – Ofer Sadan Oct 23 '19 at 20:37
  • In Python functions like almost everything else are ["first class objects"](https://stackoverflow.com/q/245192/7207392) meaning you can store them in variables pass them around as arguments or return values of other functions etc. – Paul Panzer Oct 23 '19 at 21:07

1 Answers1

1

mse_cost_function is a function that returns a function.

lambda J(theta_0, theta_1) that models the cost function

so j is a function. like any function (or more generally callable) it can get inputs (two in this case).

For more simple explanation, here is a function add_x that takes x and returns a new function that takes z and calculates z+x

def add_x(x):
  return lambda z: z+x


g = add_x(3)
print(type(g))  # -> <class 'function'>
print(g(2))  # 5
Lior Cohen
  • 5,570
  • 2
  • 14
  • 30