I’m allocating a (possibly large) matrix of zeros with Python and numpy. I plan to put unsigned integers from 1 to N in it.
N is quite variable: could easily range from 1 all the way up to a million, perhaps even more.
I know N prior to matrix initialisation. How can I choose the data type of my matrix such that I know it can hold (unsigned) integers of size N?
Furthermore, I want to pick the smallest such data type that will do.
For example, if N was 1000, I’d pick np.dtype('uint16'). If N is 240, uint16 would work, but uint8 would also work and is the smallest data type I can use to hold the numbers.
This is how I initialise the array. I’m looking for the SOMETHING_DEPENDING_ON_N:
import numpy as np
# N is known by some other calculation.
lbls = np.zeros( (10,20), dtype=np.dtype( SOMETHING_DEPENDING_ON_N ) )
cheers!
Aha!
Just realised numpy v1.6.0+ has np.min_scalar_type, documentation. D’oh! (although the answers are still useful because I don’t have 1.6.0).
What about writing a simple function to do the job?
Example usage: