6

I need to create a very large numpy array that will hold non-negative integer values. I know in advance what the largest integer will be, so I want to try to use the smallest datatype possible. So far I have the following:

>>> import numpy as np
>>> def minimal_type(max_val, types=[np.uint8,np.uint16,np.uint32,np.uint64]):
    ''' finds the minimal data type needed to correctly store the given max_val
        returns None if none of the provided types are sufficient
    '''
    for t in types:
        if max_val <= np.iinfo(t).max:
            return t
    return None

>>> print(minimal_type(42))
<class 'numpy.uint8'>
>>> print(minimal_type(255))
<class 'numpy.uint8'>
>>> print(minimal_type(256))
<class 'numpy.uint16'>
>>> print(minimal_type(4200000000))
<class 'numpy.uint32'>
>>> 

Is there a numpy builtin way to achieve this functionality?

1 Answer 1

12

It's numpy.min_scalar_type. Examples from the docs:

>>> np.min_scalar_type(10)
dtype('uint8')
>>> np.min_scalar_type(-260)
dtype('int16')
>>> np.min_scalar_type(3.1)
dtype('float16')
>>> np.min_scalar_type(1e50)
dtype('float64')
>>> np.min_scalar_type(np.arange(4,dtype='f8'))
dtype('float64')

You might not be interested in the behavior for floats, but I'm including it anyway for other people who come across the question, particularly since the use of float16 and the lack of float->int demotion might be surprising.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.