I'm trying to code the following formula:
To do this I'm using the following function:
def predict(x, w):
"""
Function to predict y(x, w) based on the values of x and w.
Args:
x: a vector including all the values of the input variable x for a given set of point.
w: polynomial coefficient vector.
Returns: an array with the predicted values for y(x, w) function.
"""
x = np.array(x)
w = np.array(w)
# list of powers for x. {0, 1, 2 ... M}
powers = list(range(0, len(w)))
# apply the polynomial fitting function to each value of vectors x & w
sumatoria_list = sum(np.array([w[i] * (x ** i) for i in powers]))
# return final sum
return sumatoria_list
In the image below you can see examples of input and output:
Where:
w0 = [-0.17594739490150393]
w1 = [0.7237871780107422, -1.7994691458244925]
x = array([0. , 0.11111111, 0.22222222, 0.33333333, 0.44444444,
0.55555556, 0.66666667, 0.77777778, 0.88888889, 1. ])
So far the output of my function is correct, however, I'm trying to use lambda:
def predict1(x, w):
"""
Function to predict y(x, w) based on the values of x and w.
Args:
x: a vector including all the values of the input variable x for a given set of point.
w: polynomial coefficient vector.
Returns: an array with the predicted values for y(x, w).
"""
x = np.array(x)
w = np.array(w)
# list of powers for x. {0, 1, 2 ... M}
powers = list(range(0, len(w)))
# apply the polynomial fitting function to each value of vectors x & w
sumatoria_list = list(map(lambda x, w, i: w * (x ** i), x, w, powers))
# return final sum
return sumatoria_list
Nevertheless, it seems not to be working right. In the image below you can find examples of output using lambda
and map
in the function.
I think that I'm not quite understanding how to apply lambda to this specific problem so I would really appreciate your help!!