Introduction to the
The Python language defines only one type of a particular data class (e.g., only one integer type, one floating point type, etc.). This is handy in normal applications where you don’t need to focus on how data is represented in your computer. However, for scientific calculations, we need a more precise type of control.
In NumPy, 24 new Python Scalar types have been introduced to describe the data more accurately. These types can be used directly with arrays in NumPy, hence the name Array Scalar type.
The 24 Scalar types will be discussed in detail in this article.
A hierarchy of scalar types
Let’s take a look at the scalar type hierarchy:
The square with solid lines above is the scalar type. These scalar types can all be accessed via np.type, such as:
In [130]: np.intc
Out[130]: numpy.int32
Copy the code
If you’re careful, you might ask, “No, there are only 22 types in the solid box. What are the other two types?”
The other two are inTP and UINTP, which represent integer Pointers.
Note that the array Scalars type is immutable.
We can use isinstance to check the hierarchy of these array scalars.
For example, if val is an array scalar object, isinstance (val, np.generic) will return True. If val is a complex numeric type, isinstance (val, np.plexfloating) will return True.
Built-in Scalar type
We use the following table to show the built-in Scalar types and their corresponding C or Python types. The character code in the last column is the character representation of the type, which is used in some cases such as building dType.
boolean
type | describe | The character code |
---|---|---|
bool_ |
compatible: Python bool | '? ' |
bool8 |
8 bits |
Integers
type | describe | The character code |
---|---|---|
byte |
compatible: C char | 'b' |
short |
compatible: C short | 'h' |
intc |
compatible: C int | 'i' |
int_ |
compatible: Python int | 'l' |
longlong |
compatible: C long long | 'q' |
intp |
large enough to fit a pointer | 'p' |
int8 |
8 bits | |
int16 |
16 bits | |
int32 |
32 bits | |
int64 |
64 bits |
Unsigned integers
type | describe | The character code |
---|---|---|
ubyte |
compatible: C unsigned char | 'B' |
ushort |
compatible: C unsigned short | 'H' |
uintc |
compatible: C unsigned int | 'I' |
uint |
compatible: Python int | 'L' |
ulonglong |
compatible: C long long | 'Q' |
uintp |
large enough to fit a pointer | 'P' |
uint8 |
8 bits | |
uint16 |
16 bits | |
uint32 |
32 bits | |
uint64 |
64 bits |
Floating-point numbers
type | describe | The character code |
---|---|---|
half |
'e' |
|
single |
compatible: C float | 'f' |
double |
compatible: C double | |
float_ |
compatible: Python float | 'd' |
longfloat |
compatible: C long float | 'g' |
float16 |
16 bits | |
float32 |
32 bits | |
float64 |
64 bits | |
float96 |
96 bits, platform? | |
float128 |
128 bits, platform? |
Complex floating-point numbers
type | describe | The character code |
---|---|---|
csingle |
'F' |
|
complex_ |
compatible: Python complex | 'D' |
clongfloat |
'G' |
|
complex64 |
two 32-bit floats | |
complex128 |
two 64-bit floats | |
complex192 |
two 96-bit floats, platform? | |
complex256 |
two 128-bit floats, platform? |
Python objects
type | describe | The character code |
---|---|---|
object_ |
any Python object | 'O' |
For the object type object_ in the array, the stored data is actually a reference to a Python object, so their object types must be consistent.
Although the reference is stored, the object itself is returned when the value is accessed.
You can see that for numeric types, int,uint,float,complex, followed by a specific array, represents a specific length.
Intp and uINTP are two Pointers to integers.
Some types are essentially equivalent to Python’s native types, and in fact inherit from Python’s native types:
Array scalar type | Related Python type |
---|---|
int_ |
IntType (Python 2 only) |
float_ |
FloatType |
complex_ |
ComplexType |
bytes_ |
BytesType |
unicode_ |
UnicodeType |
One exception is bool_, which is very similar to Python’s BooleanType, but does not inherit from it. Python BooleanType is not allowed to be inherited. The length of the underlying data store is also different.
Although in Python a bool is a subclass of int. However, bool_ is not a subclass of INT_ in NumPy and bool_ is not even a number type.
In Python3, int_ no longer inherits int from Python3, because int is no longer a fixed-length integer.
The default NumPy data type is float_.
Variable length data type
The following three data types are of variable length,
type | describe | The character code |
---|---|---|
bytes_ |
compatible: Python bytes | 'S#' |
unicode_ |
compatible: Python unicode/str | 'U#' |
void |
'V#' |
The # in the character code represents a number.
The character codes described above need to be modified as appropriate for compatibility with other Python modules, such as structs:
C – > S1, b – > b, 1 – > b, s – > h, w – > h, and u – > I.
This article is available at www.flydean.com/03-python-n…
The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!
Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!