Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用numpy.identity比使用numpy.eye有什么优势?_Python_Arrays_Performance_Numpy - Fatal编程技术网

Python 使用numpy.identity比使用numpy.eye有什么优势?

Python 使用numpy.identity比使用numpy.eye有什么优势?,python,arrays,performance,numpy,Python,Arrays,Performance,Numpy,在查看了numpy的手册页之后,我认为identity是eye的一个特例,因为它的选项较少(例如eye可以填充移位的对角线,identity不能),但运行速度似乎更快。但是,无论是小型阵列还是大型阵列,情况都不是这样: >>> np.identity(3) array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0.,

在查看了
numpy
的手册页之后,我认为
identity
eye
的一个特例,因为它的选项较少(例如
eye
可以填充移位的对角线,
identity
不能),但运行速度似乎更快。但是,无论是小型阵列还是大型阵列,情况都不是这样:

>>> np.identity(3)                                                  
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
>>> np.eye(3)                                                       
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
>>> timeit.timeit("import numpy; numpy.identity(3)", number = 10000)
0.05699801445007324
>>> timeit.timeit("import numpy; numpy.eye(3)", number = 10000)     
0.03787708282470703
>>> timeit.timeit("import numpy", number = 10000)                   
0.00960087776184082
>>> timeit.timeit("import numpy; numpy.identity(1000)", number = 10000)
11.379066944122314
>>> timeit.timeit("import numpy; numpy.eye(1000)", number = 10000)     
11.247124910354614

那么,使用
identity
eye
有什么好处呢?

identity
只调用
eye
,因此数组的构造方式没有区别。以下是代码:

正如您所说,主要区别在于,使用
eye
可以偏移对角线,而
identity
仅填充主对角线


由于单位矩阵是数学中的一种常见构造,因此使用
单位
的主要优点似乎仅是其名称。

要查看示例中的差异,请运行以下代码:

import numpy as np

#Creates an array of 4 x 4 with the main diagonal of 1

arr1 = np.eye(4)
print(arr1)

print("\n")

#or you can change the diagonal position

arr2 = np.eye(4, k=1)  # or try with another number like k= -2
print(arr2)

print("\n")

#but you can't change the diagonal in identity

arr3 = np.identity(4)
print(arr3)

np。恒等式-返回一个方阵(2D数组的特例),该方阵是一个恒等式矩阵,主对角线(即“k=0”)为1,其他值为0。您不能在此处更改对角线
k

np。eye-返回一个2D-ARRAY,该数组填充对角线,即可设置的“k”为1,其余为0

因此,主要优势取决于需求。如果你想要一个恒等式矩阵,你可以马上找到恒等式,或者调用np。眼睛将其余部分留待默认设置

但是,如果您需要特定形状/大小的1和0矩阵,或者需要对对角线进行控制,则可以使用eye方法

就像矩阵是数组的特例一样,
np.identity
matrix也是
np.eye

参考资料:

你知道为什么在许多张量库中,标识被称为“眼睛”吗?@aeduG:好问题-我真的不知道。如果非要我猜的话,那是因为恒等式矩阵通常用数学符号表示,但这个字母不能用在编程语言中,因为它可能会混淆为虚单位
I
eye
是同音词,我想
eye
identity
更容易打字。
import numpy as np

#Creates an array of 4 x 4 with the main diagonal of 1

arr1 = np.eye(4)
print(arr1)

print("\n")

#or you can change the diagonal position

arr2 = np.eye(4, k=1)  # or try with another number like k= -2
print(arr2)

print("\n")

#but you can't change the diagonal in identity

arr3 = np.identity(4)
print(arr3)