What is the purpose and utility of the subok option in numpy.zeros_like()? What is the purpose and utility of the subok option in numpy.zeros_like()? numpy numpy

What is the purpose and utility of the subok option in numpy.zeros_like()?


What is the purpose and utility ... ?

Purpose :

The call-signature helps either pass-through the processed instance-type, as seen here:

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

On the contrary, if not willing to "reprocess" both the .shape and instantiate the same class, using subok = False, the produced *_alike() will not get the same class, as the "example" the process was given to make the *_alike()-generated output:

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

Utility :

These subok-flags are common in more numpy functions ( not only the *_like()-s, also in np.array( ... ) ), for the very same purpose, as it is pretty usefull for smart type-modifying code designs, where desired type of the product is known to the "generating"-process and the results are thus achieved without undue class-related overheads, if ex-post modifications were needed otherwise.