Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: confusion between types and dtypes

Suppose I enter:

a = uint8(200)
a*2

Then the result is 400, and it is recast to be of type uint16.

However:

a = array([200],dtype=uint8)
a*2

and the result is

array([144], dtype=uint8)

The multiplication has been performed modulo 256, to ensure that the result stays in one byte.

I'm confused about "types" and "dtypes" and where one is used in preference to another. And as you see, the type may make a significant difference in the output.

Can I, for example, create a single number of dtype uint8, so that operations on that number will be performed modulo 256? Alternatively, can I create an array of type (not dtype) uint8 so that operations on it will produce values outside the range 0-255?

like image 812
Alasdair Avatar asked Jan 05 '15 13:01

Alasdair


1 Answers

The simple, high-level answer is that NumPy layers a second type system atop Python's type system.

When you ask for the type of an NumPy object, you get the type of the container--something like numpy.ndarray. But when you ask for the dtype, you get the (numpy-managed) type of the elements.

>>> from numpy import *
>>> arr = array([1.0, 4.0, 3.14])
>>> type(arr)
<type 'numpy.ndarray'>
>>> arr.dtype
dtype('float64')

Sometimes, as when using the default float type, the element data type (dtype) is equivalent to a Python type. But that's equivalent, not identical:

>>> arr.dtype == float
True
>>> arr.dtype is float
False

In other cases, there is no equivalent Python type. For example, when you specified uint8. Such data values/types can be managed by Python, but unlike in C, Rust, and other "systems languages," managing values that align directly to machine data types (like uint8 aligns closely with "unsigned bytes" computations) is not the common use-case for Python.

So the big story is that NumPy provides containers like arrays and matrices that operate under its own type system. And it provides a bunch of highly useful, well-optimized routines to operate on those containers (and their elements). You can mix-and-match NumPy and normal Python computations, if you use care.

There is no Python type uint8. There is a constructor function named uint8, which when called returns a NumPy type:

>>> u = uint8(44)
>>> u
44
>>> u.dtype
dtype('uint8')
>>> type(u)
<type 'numpy.uint8'>

So "can I create an array of type (not dtype) uint8...?" No. You can't. There is no such animal. You can do computations constrained to uint8 rules without using NumPy arrays (a.k.a. NumPy scalar values). E.g.:

>>> uint8(44 + 1000)
20
>>> uint8(44) + uint8(1000)
20

But if you want to compute values mod 256, it's probably easier to use Python's mod operator:

>> (44 + 1000) % 256
20

Driving data values larger than 255 into uint8 data types and then doing arithmetic is a rather backdoor way to get mod-256 arithmetic. If you're not careful, you'll either cause Python to "upgrade" your values to full integers (killing your mod-256 scheme), or trigger overflow exceptions (because tricks that work great in C and machine language are often flagged by higher level languages).

like image 58
Jonathan Eunice Avatar answered Oct 14 '22 19:10

Jonathan Eunice