What is the purpose and utility of the subok option in numpy.zeros_like()?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Switch On Looping
--
Chapters
00:00 What Is The Purpose And Utility Of The Subok Option In Numpy.Zeros_like()?
00:45 Accepted Answer Score 6
01:59 Thank you
--
Full question
https://stackoverflow.com/questions/5054...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #class #numpy #subclass
#avk47
ACCEPTED ANSWER
Score 6
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 useful 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.