3

I'm trying to code the following formula:

enter image description here

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:

enter image description here

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.

enter image description here

I think that I'm not quite understanding how to apply lambda to this specific problem so I would really appreciate your help!!

brenda
  • 656
  • 8
  • 24
  • in your correct code, are you getting a vector output for `sum(np.array([w[i] * (x ** i) for i in powers]))`? That doesn't seem right! – anurag Jan 21 '21 at 05:45

1 Answers1

0

I think it should work now,

Requested Solution

Code Syntax

w = [0.7237871780107422, -1.7994691458244925]
x = [0., 0.11111111, 0.22222222, 0.33333333, 0.44444444, 0.55555556, 0.66666667, 0.77777778, 0.88888889, 1.        ]

powers = list(range(0, len(w)))
print("power list: ", powers)
print("w: ", w)
lista = list(map(lambda rng, x: w[rng] * (x ** rng) ,powers, x))

print(lista)

Output

power list:  [0, 1]
w:  [0.7237871780107422, -1.7994691458244925]

[0.7237871780107422, -0.19994101420331123]

[Program finished]

Explanation

The reason for not giving you the required output is you can't operate pow() or operating full list with others, you have to iterate both lists to give you the required output.

For example:

Giving listA = [1, 2, 3], and listB = [1, 2, 2]. If you want to operate both lists, you've to iterate them, first.

Code Syntax

listC = list(map(lambda x, y: x ** y, listA, listB))

Output

[1, 4, 9]

[Program finished]

Or, you can use only map() for mathematical operation

Code Syntax

listC2 = list(map(pow, listA, listB))

Output

[1, 4, 9]

[Program finished]
Ahmed
  • 796
  • 1
  • 5
  • 16
  • It is not working. I'm getting as output what is shown in the third picture while my desired output is what is shown in the second image. Also, both x & w are arrays. – brenda Jan 21 '21 at 13:51
  • if that so, the `powers` length must be with the `x` not `w` according to your second image. now, this's a glitch – Ahmed Jan 21 '21 at 14:01
  • you `w` variable has only 2 values in its list, So, the question is how do you have 10 values as variable `x` length, not `w` length!!!! – Ahmed Jan 21 '21 at 14:02