Home > Enterprise >  How to use math functions on numpy arrays?
How to use math functions on numpy arrays?

Time:10-11

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:

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)?

CodePudding user response:

You can use the numpy equivelent. numpy.sin:

y = np.sin(x)
  • Related