what does numpy.vectorize(lambda x:1 – x^3) do?

I’m new to python and also to numpy package. I was wondering what does this specific line really do.

a = numpy.vectorize(lambda x:1 - x^3)

I’ve searched about vectorize function but didn’t really get what it does.

I’m familiar with julia if there is any instance in julia that does what this line does I could understand it faster and better.

>Solution :

The keyword lambda on Python is used to declare an anonymous function. So the statement

a = lambda x: 1 - x**3

Is mathematically equivalent to:

a(x) = 1 - x^3

The vectorize function on NumPy is used to apply a function element-wise in an array. So, suppose you have an array x with elements [1,2,3], the result of a on x is:

[1-1^3,1-2^3,1-3^3]

I am not an expert in Julia, but I believe that the equivalent would be this:

a = function(x)
        1 - x^3
    end

And then, if you want to it use the same way Python would after the vectorize function, you would add a "." after the function name:

a.([1,2,3])

Leave a Reply