Wednesday, May 27, 2020

Numpy Datatypes

Numpy Datatypes


There are two interrelated concepts regarding the “type” of NumPy ndarray items,

  • The “type” provided by Python, that are the true Python objects extracted from the Python class hierarchy. 
  • The data-type(dtype) provided by the NumPy‏. The dtype object provides the specifications about how to interpret the memory for an item. It is an instance of a single dtype class. Every dtype object has a type attribute that specifies the Python object returned when the item is selected from the array.

NumPy Datatypes


 Basic Type Available NumPy TypesDescription 
 Boolean bool 1 byte in size
 Integer int8, int16,int32,int64,
int128,int
 int defaults to the size of the int in
C for the platform
Unsigned
Integer
 unit8, uint16, uint32,uint64,
uint128,unit
 uint defaults to the size of the uint
in C for the platform 
 Float float32, float64, float,
longfloat
 float is double-precision floating-point
value(64-bits). longfloat for larger precision
and platform dependent
 Complexcomplex64,complex128,
complex
 for complex and real both parts, each is represented
by the single-precision(32-bit) value, total 64-bits
 String str, Unicode unicode is always UTF32
 Object object Represents item in NumPy array as Python object
 Records void arbitrary data structures stored in an array


Built-in Scaler types


built-in datatype numpy






There are built-in data type objects available in NumPy, but user-defined new data-type objects can also be created.

The following is a list of available dtype and their representation symbol

  • integer-i
  • boolean-b
  • unsigned integer-u
  • float-f
  • complex float-c
  • timedelta-m
  • datetime-M
  • object-O
  • string-S
  • unicode string-U fixed chunk of memory for other types ( void )-V

Getting the dtype


import numpy as np
nums = np.array([12,10,5,10])
strs = np.array(['pyhton','Java','C','C++'])
flts = np.array([34.5,7.4,0.95])
b = np.array([True,True,False])

#dtypes
print(nums.dtype) #int64
print(strs.dtype) #<U6
print(flts.dtype) #float64
print(b.dtype)    #bool

Creating an array with defined dtype


The numpy.array() function can take an optional argument, dtype that makes us specify the expected dtype of the array items.

import numpy as np
strs = np.array(['pyhton','Java','C','C++'], dtype='S')
print(strs.dtype)  #|S6

It is possible to specify the size(in bytes) for i, u, f, S, and U size as well.

import numpy as np
nums = np.array([15,20,25,30], dtype='i4')
numssmall = np.array([15,20,25,30], dtype='i2')
strs = np.array(['pyhton','Java','C','C++'], dtype='S2')
print(nums.dtype)  #|int32
print(numssmall.dtype)  #int16
print(strs.dtype)  #|S2

*The ValueError is raised if the array can not be converted into the specified dtype. For example,

import numpy as np
nums = np.array([15,20,25,30,'message'], dtype='i') #ValueError

Modifying the dtype of an existing array


The easiest way to change the dtype of the existing array is to make a copy of the existing array using the astype() function and pass the dtype as a parameter.

import numpy as np
fls = np.array([15.2,2.20,25.5,30.5])
nums=fls.astype('i')
print(nums)

#output
[15  2 25 30]
OR
import numpy as np
nums = np.array([12,0,15,20])
bls=nums.astype('bool')
print(bls)

#output
[ True False  True  True]
OR

import numpy as np
nums = np.array([12,0,15,20])
strs=nums.astype('str')
print(strs)

#output
['12' '0' '15' '20']


Creating customized datatypes


We can create a structured array formed with customized datatypes very easily with dtype keyword in the array() method.


import numpy as np
dtype = [('name', 'S10'), ('height', float), ('age', int)]
values = [('Shiva', 1.8, 41), ('Akshar', 1.9, 38),
('Virat', 1.7, 38)]
arr = np.array(values, dtype=dtype)  #create a structured array
print('=======Custom array======\n',arr)     

#output
=======Custom array======
[(b'Shiva', 1.8, 41) (b'Akshar', 1.9, 38) (b'Virat', 1.7, 38)]