Python numpy.square与** [英] Python numpy.square vs **
本文介绍了Python numpy.square与**的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
numpy.square
和在Numpy数组上使用**
运算符之间是否有区别?
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.
执行效率有何不同?
澄清示例:
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]])
推荐答案
您可以检查执行时间以获取清晰的图片
You can check the execution time to get clear picture of it
In [2]: import numpy as np
In [3]: A = np.array([[2, 2],[2, 2]])
In [7]: %timeit np.square(A)
1000000 loops, best of 3: 923 ns per loop
In [8]: %timeit A ** 2
1000000 loops, best of 3: 668 ns per loop
这篇关于Python numpy.square与**的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文