Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

Is there a difference between numpy.square and using the ** operator on a Numpy array?

From what I can see it yields the same result.

Any differences in the efficiency of execution?

An example for clarification:

In [1]: import numpy as np 

In [2]: A = np.array([[2, 2],[2, 2]]) 

In [3]: np.square(A) 

Out[3]: array([[4, 4], [4, 4]]) 

In [4]: A ** 2 

Out[4]: array([[4, 4], [4, 4]])

1 Answer

0 votes
by (106k points)

To know the difference between numpy.square and ** you can check the execution time to get clear picture of it:-

>>import numpy as np 

>>A = np.array([[2, 2],[2, 2]]) 

>>%timeit np.square(A) 

1000000 loops, best of 3: 923 ns per loop 

>>%timeit A ** 2 

1000000 loops, best of 3: 668 ns per loop

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
asked Dec 11, 2020 in Python by laddulakshana (16.4k points)
0 votes
1 answer

Browse Categories

...