![]() |
NumPy is a powerful Python library that can manage different types of data. Here we will explore the Datatypes in NumPy and How we can check and create datatypes of the NumPy array. DataTypes in NumPyA data type in NumPy is used to specify the type of data stored in a variable. Here is the list of characters to represent data types available in NumPy.
The list of various types of data types provided by NumPy are given below:
Checking the Data Type of NumPy ArrayWe can check the datatype of Numpy array by using dtype. Then it returns the data type all the elements in the array. In the given example below we import NumPy library and craete an array using “array()” method with integer value. Then we store the data type of the array in a variable named “data_type” using the ‘dtype’ attribute, and after then we can finally, we print the data type. Python3
Output: Data type: int64
Create Arrays With a Defined Data TypeWe can create an array with a defined data type by specifying “dtype” attribute in numpy.array() method while initializing an array. In the below code we have created various types of defined arrays such as ‘float64’, ‘int32’, ‘complex128’, and ‘bool’. Python3
Output: Convert Data Type of NumPy ArraysWe can convert data type of an arrays from one type to another using astype() function. In the below code we have initialize an array with float type values. After that we have convert that float64 type array to int32 type using astype() function. Finally, print the array and their types of original array and new array. Python3
Output: |
Reffered: https://www.geeksforgeeks.org
Numpy |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |