Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading