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

How to use math functions on numpy arrays?

I have the following numpy array:

x=np.linspace(1,10,20)

I tried applying a mathematical function on it:

y=x**3-4*x**2+30

It worked as expected and generated an array. However, when I try using a math function it does not seem to work:

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

y=math.sin(x)

TypeError: only size-1 arrays can be converted to Python scalars

How can I apply the function on the array efficiently (without having to use a loop)?

>Solution :

You can use the numpy equivelent. numpy.sin:

y = np.sin(x)
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