Skip to main content

NumPy data types in python

NumPy data types in python

Hi guys welcome, welcome to another exciting session in this platform. If you are new here, please do checkout our previous articles. Also do checkout our YouTube to watch the full video tutorial.

In this article we are going to discuss about the different types of NumPy data types.

most of the data types in python are the same as those we mentioned in the earlier articles. Some of which are: strings, floats, integers, complex, Boolean and complex.

But coming to NumPy, we have a great variety of numerical data types. These data types are represented with their first letters: The table below gives shows some of the different types of NumPy data types

datatype

Parameter/character

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 type (void)

V

How to check the data types of an array

Just like in creating NumPy arrays, we used different functions. NumPy data types also have a function called dtype that we can used to check the datatype used in a function. The output returned is the data type used in a code or a program.

Example 

import numpy as np
arr = np.array([4, 8, 12, 16])
print(arr.dtype)

OUTPUT

NumPy data types in python
import numpy as np
arr = np.array(['ana', 'kesso', 'georgette'])
print(arr.dtype)
OUTPUT
NumPy data types in python

Create an array with a specific data type

In NumPy, we are able to convert data types into arrays, sequence of python numbers into their data types. Some of which are accepted by dtype NumPy function as arguments. These arguments are the target types

Example

import numpy as np
arr = np.array([4, 8, 12, 16], dtype='i4')
print(arr)
print(arr.dtype)

OUTPUT

NumPy data types in python

As we have seen in the output, the data of type that we have now is “int32”.

Let’s take a case where a value cannot to be converted to data. This mostly happened when the argument is incorrect.

Example

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

OUTPUT

NumPy data types in python
We mostly used the astype method (some cases we also used the type itself) to convert data types into python.

To convert the data type of an array, we can use the preferable method which is the astype method. We can also the type itself.

That being said, we are going to see how to convert existing arrays to data type. We are going to use the astype

We are going to the take given the array and convert it to the data type that we want.

This method allows you to input parameters of the type of data type you want and make duplicate of them. These parameters are the first letters of each of the data type of data. 

For example: 'f' for float, ‘i’ for integer. Another way is just by using the data types directly.

Let’s take an example where we are able to change the data type to float, by using ‘I’ as parameter.

Example

import numpy as np
arr = np.array([2.5, 3.5, 4.5])
newarr = arr.astype(int)
print(newarr)
print(newarr.dtype)

OUTPUT

NumPy data types in python

Let’s take an example where we able to convert an integer to a Boolean expression.

import numpy as np
arr = np.array([2, 0.5, 9])
newarr = arr.astype(bool)
print(newarr)
print(newarr.dtype)

OUTPUT

NumPy data types in python
I hope this session in the articles was great and you have seen how the conversion of data types works. If there is any problem or worries, please leave them in the comment section. Happy coding guys.

Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...