numpy.zeros_like() 中 subok 选项的目的和用途是什么?

uho*_*hoh 4 python numpy class subclass

使用 numpy\'s Zeros_like和相关函数,有一个选项

\n
\n

subok:布尔值,可选。

\n

numpy.zeros_like(a, dtype=None, order=\'K\', subok=True)

\n

如果为True,则新创建的数组将使用\xe2\x80\x98a\xe2\x80\x99的子类类型,否则为基类数组。默认为 True。

\n
\n

我假设所有 numpy 数组都是类ndarray,并且我从来不需要详细查看数组的子类。在什么情况下我可能想选择不使用相同的子类,而指定使用基类?

\n

use*_*197 5

目的用途什么?...?

目的 :

调用签名有助于传递已处理的实例类型,如下所示:

>>> np.array( np.mat( '1 2; 3 4' ),    # array-to-"process"
              subok = True             # FLAG True to ["pass-through"] the type
              )
matrix([[1, 2],
        [3, 4]])                       # RESULT is indeed the instance of matrix
Run Code Online (Sandbox Code Playgroud)

相反,如果不愿意“重新处理” 并.shape实例化同一个类,则使用subok = False,生成的*_alike()将不会获得相同的类,如给出生成输出的过程的“示例” *_alike()

type(                np.mat( '1 2;3 4' ) )   # <class 'numpy.matrixlib.defmatrix.matrix'>
type( np.array(      np.mat( '1 2;3 4' ) ) ) # <type 'numpy.ndarray'>
type( np.zeros_like( np.mat( '1 2;3 4' ) ) ) # <class 'numpy.matrixlib.defmatrix.matrix'>

>>> np.zeros_like(   np.mat( '1 2;3 4' ), subok = True  )
matrix([[0, 0],
        [0, 0]])
>>> np.zeros_like(   np.mat( '1 2;3 4' ), subok = False )
array([[0, 0],
       [0, 0]])
Run Code Online (Sandbox Code Playgroud)

公用事业 :

这些subok- 标志在更多函数中很常见numpy(不仅是*_like()-s,也在np.array( ... )),出于同样的目的,因为它对于智能类型修改代码设计非常有用,其中产品的所需类型已知为“生成” “如果需要事后修改,因此可以在没有不当的类相关开销的情况下实现过程和结果。