0% found this document useful (0 votes)
7 views3 pages

NumPy Data Types Explained

NumPy offers a variety of numeric data types beyond those available in Python, including boolean, integer, float, and complex types, each with specific bit sizes and value ranges. The document explains how to create NumPy data type objects (dtypes) and structured data types that can map values, such as employee salaries. Examples are provided to illustrate the creation and usage of these data types in NumPy.

Uploaded by

ayush231225
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

NumPy Data Types Explained

NumPy offers a variety of numeric data types beyond those available in Python, including boolean, integer, float, and complex types, each with specific bit sizes and value ranges. The document explains how to create NumPy data type objects (dtypes) and structured data types that can map values, such as employee salaries. Examples are provided to illustrate the creation and usage of these data types in NumPy.

Uploaded by

ayush231225
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

NumPy Datatypes

The NumPy provides a higher range of numeric data types than that
provided by the Python. A list of numeric data types is given in the
following table.

SN Data type Description

1 bool_ It represents the boolean value indicating true or false. It is


stored as a byte.

2 int_ It is the default type of integer. It is identical to long type in


C that contains 64 bit or 32-bit integer.

3 intc It is similar to the C integer (c int) as it represents 32 or 64-


bit int.

4 intp It represents the integers which are used for indexing.

5 int8 It is the 8-bit integer identical to a byte. The range of the


value is -128 to 127.

6 int16 It is the 2-byte (16-bit) integer. The range is -32768 to


32767.

7 int32 It is the 4-byte (32-bit) integer. The range is -2147483648 to


2147483647.

8 int64 It is the 8-byte (64-bit) integer. The range is -


9223372036854775808 to 9223372036854775807.

9 uint8 It is the 1-byte (8-bit) unsigned integer.

10 uint16 It is the 2-byte (16-bit) unsigned integer.

11 uint32 It is the 4-byte (32-bit) unsigned integer.

12 uint64 It is the 8 bytes (64-bit) unsigned integer.

13 float_ It is identical to float64.

14 float16 It is the half-precision float. 5 bits are reserved for the


exponent. 10 bits are reserved for mantissa, and 1 bit is
reserved for the sign.

15 float32 It is a single precision float. 8 bits are reserved for the


exponent, 23 bits are reserved for mantissa, and 1 bit is
reserved for the sign.
16 float64 It is the double precision float. 11 bits are reserved for the
exponent, 52 bits are reserved for mantissa, 1 bit is used for
the sign.

17 complex_ It is identical to complex128.

18 complex6 It is used to represent the complex number where real and


4 imaginary part shares 32 bits each.

19 complex1 It is used to represent the complex number where real and


28 imaginary part shares 64 bits each.

NumPy dtype
All the items of a numpy array are data type objects also known as numpy
dtypes. A data type object implements the fixed size of memory
corresponding to an array.

We can create a dtype object by using the following syntax.

1. [Link](object, align, copy)

The constructor accepts the following object.

Object: It represents the object which is to be converted to the data type.

Align: It can be set to any boolean value. If true, then it adds extra
padding to make it equivalent to a C struct.

Copy: It creates another copy of the dtype object.

Example 1

1. import numpy as np
2. d = [Link](np.int32)
3. print(d)

Output:

int32

Example 2

1. import numpy as np
2. d = np.int32(i4)
3. print(d)

Output:

int32

Creating a Structured data type


We can create a map-like (dictionary) data type which contains the
mapping between the values. For example, it can contain the mapping
between employees and salaries or the students and the age, etc.

Consider the following example.

Example 1

1. import numpy as np
2. d = [Link]([('salary',[Link])])
3. print(d)

Output:

[('salary', '<="" pre="">

Example 2

1. import numpy as np
2. d=[Link]([('salary',[Link])])
3. arr = [Link]([(10000.12,),(20000.50,)],dtype=d)
4. print(arr['salary'])

Output:

[(10000.12,) (20000.5 ,)]

You might also like