NumPy – Data Types

NumPy - Data Types

In this chapter, we will discuss about NumPy Data Type. NumPy supports a much greater variety of numerical types than Python does. The following table shows different scalar data types defined in NumPy.

Sr.No.Data Types & Description
1bool_Boolean (True or False) stored as a byte
2int_Default integer type (same as C long; normally either int64 or int32)
3incidental to C int (normally int32 or int64)
4int integer used for indexing (same as C ssize_t; normally either int32 or int64)
5int8Byte (-128 to 127)
6int16Integer (-32768 to 32767)
7int32Integer (-2147483648 to 2147483647)
8int64Integer (-9223372036854775808 to 9223372036854775807)
9uint8Unsigned integer (0 to 255)
10uint16Unsigned integer (0 to 65535)
11uint32Unsigned integer (0 to 4294967295)
12uint64Unsigned integer (0 to 18446744073709551615)
13float_Shorthand for float64
14float16Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
15float32Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
16float64Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
17complex_Shorthand for complex128
18complex64Complex number, represented by two 32-bit floats (real and imaginary components)
19complex128Complex number, represented by two 64-bit floats (real and imaginary components)

NumPy numerical types are instances of dtype (data-type) objects, each having unique characteristics. The dtypes are available as np.bool_, np.float32, etc.

NumPy Data Type Objects (dtype)

A data type object describes the interpretation of a fixed block of memory corresponding to an array, depending on the following aspects βˆ’

  • Type of data (integer, float or Python object)
  • Size of data
  • Byte order (little-endian or big-endian)
  • In case of structured type, the names of fields, data type of each field and part of the memory block taken by each field.
  • If data type is a subarray, its shape and data type

The byte order is decided by prefixing ‘<‘ or ‘>’ to data type. ‘<‘ means that encoding is little-endian (least significant is stored in smallest address). ‘>’ means that encoding is big-endian (the most significant byte is stored in the smallest address).

A dtype object is constructed using the following syntax βˆ’

numpy.dtype(object, align, copy)

The parameters are βˆ’

  • Object βˆ’ To be converted to data type object
  • Align βˆ’ If true, adds padding to the field to make it similar to C-struct
  • Copy βˆ’ Makes a new copy of dtype object. If false, the result is reference to builtin data type object

Example 1

# using array-scalar type 
import numpy as np 
dt = np.dtype(np.int32) 
print dt

The output is as follows βˆ’

int32

Example 2

#int8, int16, int32, int64 can be replaced by equivalent string 'i1', 'i2','i4', etc. 
import numpy as np 

dt = np.dtype('i4')
print dt 

The output is as follows βˆ’

int32

Example 3

# using endian notation 
import numpy as np 
dt = np.dtype('>i4') 
print dt

The output is as follows βˆ’

>i4

The following examples show the use of structured data types. Here, the field name and the corresponding scalar data type are to be declared.

Example 4

# first create structured data type 
import numpy as np 
dt = np.dtype([('age',np.int8)]) 
print dt 

The output is as follows βˆ’

[('age', 'i1')] 

Example 5

# now apply it to ndarray object 
import numpy as np 

dt = np.dtype([('age',np.int8)]) 
a = np.array([(10,),(20,),(30,)], dtype = dt) 
print a

The output is as follows βˆ’

[(10,) (20,) (30,)]

Example 6

# file name can be used to access content of age column 
import numpy as np 

dt = np.dtype([('age',np.int8)]) 
a = np.array([(10,),(20,),(30,)], dtype = dt) 
print a['age']

The output is as follows βˆ’

[10 20 30]

Example 7

The following examples define a structured data type calledΒ studentΒ with a string field ‘name’, anΒ integer fieldΒ ‘age’, and aΒ float fieldΒ ‘marks’. This dtype is applied to the ndarray object.

import numpy as np 
student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')]) 
print student

The output is as follows βˆ’

[('name', 'S20'), ('age', 'i1'), ('marks', '<f4')])

Example 8

import numpy as np 

student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')]) 
a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student) 
print a

The output is as follows βˆ’

[('abc', 21, 50.0), ('xyz', 18, 75.0)]

Each built-in data type has a character code that uniquely identifies it.

  • ‘b’ βˆ’ boolean
  • ‘i’ βˆ’ (signed) integer
  • ‘u’ βˆ’ unsigned integer
  • ‘f’ βˆ’ floating-point
  • ‘c’ βˆ’ complex-floating point
  • ‘m’ βˆ’ timedelta
  • ‘M’ βˆ’ datetime
  • ‘O’ βˆ’ (Python) objects
  • ‘S’, ‘a’ βˆ’ (byte-)string
  • ‘U’ βˆ’ Unicode
  • ‘V’ βˆ’ raw data (void)

Next Topic- Click Here

This Post Has One Comment

Leave a Reply