NumPy Data Types – Object and Parameters

Free NumPy course with real-time projects Start Now!!

Python has a basic range of data types in it. NumPy supports a wider range of data types as compared to Python. The greater variety of data types increase the functionalities of NumPy. The options for data type specification widens for the array elements.

NumPy Data Types

Data Types in Python

1. strings- used to represent text data, the data is given in quotes. E.g.”abcd”
2. integer- used for representing numbers. E.g. 1,3
3. float- used for real number representations. E.g. 1.2,5.6
4. boolean- used for true or false type output
5. complex- used for the complex plain representation of numbers. E.g. 1.0+2.0j, 1.5+2.5j

NumPy Built-in Data Types

We can reference the built-in data types in NumPy by particular character code.

  • i – integer
  • b – boolean
  • u – unsigned
  • f – float
  • c – complex float
  • m – timedelta
  • M – datetime
  • O – object
  • S – string
  • U – unicode string
  • V – void

Numpy Scalar Data Types

The following scalar data types are available in NumPy:

1. bool_ – It is used to return Boolean true or false values.
2. int_ – It is the default integer type (int64 or int32)
3. intc – It is identical to the integer in C (int32 or int64)
4. intp – It is the integer value used for indexing
5. int8 – It is for assigning 8-bit integer value (-128 to 127)
6. int16 – It is for assigning 16-bit integer value (-32768 to 32767)
7. int32 – It is for assigning 32-bit integer value (-2147483648 to 2147483647)
8. int32 – It is for assigning 64-bit integer value (-9223372036854775808 to 9223372036854775807)
9. uint8 – It is for assigning unsigned 8-bit integer value (0 to 255)
10. uint16 – It is for assigning unsigned 16-bit integer value (0 to 65535)
11. uint32 – It is for assigning unsigned 32-bit integer value (0 to 4294967295)
12. uint64 – It is for assigning unsigned 64-bit integer value (0 to 18446744073709551615)
13. float_ – It is to assign float values.
14. float16 – It is for half precision float values.
15. float32 – It is for single-precision float values.
16. float64 – It is for double-precision float values.
17. complex_ – It is to assign complex values.
18. complex64 – It is to represent two 32-bit float complex values (real and imaginary)
19. complex128 – It is to represent two 64-bit float complex values (real and imaginary)

NumPy Dataype Object

The arrays in Numpy are homogenous in nature. The elements of the array have the same data type. The data type of the elements is given by the dtype object. It is an object comprising of a combination of all the fundamental data types. It has the following syntax.

numpy.dtype(obj, align=False, copy=False)

Parameters

1. obj
Data that has to be converted to a data-type object.

2. align bool, optional
It is true if we have string or dictionary input.

3. copy bool, optional
We set this is we want to make a new copy of the data-type object.

np.dtype(np.int16)

Output

dtype(‘int16’)

Checking the Data Type of an array

The dtype property is useful to return the data type of the array object

arr = np.array([1, 2, 3, 4])
 print(arr.dtype)

Output

int64

Creating Arrays with a Defined Data Type

We use the array()function to create arrays, the function can take dtype as an argument that helps define the define the data type of the array elements.

import numpy as np
arr = np.array([1, 2, 3, 4], dtype='i4')
print(arr)
print(arr.dtype)

Output

[1 2 3 4]
int32

Element type casting

If we give a type to elements that cannot be cast then a value error will generate.

import numpy as np
 
arr = np.array(['a', '2', '3'], dtype='i')

Output

ValueError Traceback (most recent call last)
<ipython-input-4-3d3a7c165e54> in <module>()
1 import numpy as np
2 —->
3 arr = np.array([‘a’, ‘2’, ‘3’], dtype=’i’)

Changing Data Type of Existing Array

The most basic method to change the existing data type is to use the method.
This function creates a copy of the existing array. It then allows specifying the new data type for the copy as a parameter.
The data type can be specified using the respective character code.

arr = np.array([1.1, 2.1, 3.1])
newarr = arr.astype('i')
print(newarr)
print(newarr.dtype)

Output

[1 2 3]
int32

Summary

There are a few basic and advanced data types available in NumPy. It also has data types with bit size specification, which helps memory optimization.

There are a few data types that are dependent on the platforms (32-bit or 64-bit). We define data types in a manner to provide the best output in terms of memory and precision.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *