Web Technologies - NumPy

Back to Course

Lesson Description

Lession - #297 NumPy Data Types

NumPy has a few additional data types, and allude to data types with one person, similar to i for integers, u for unsigned integers and so on.
The following is a list of all data types in NumPy and the characters used to address them.

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

Checking the Data Type of an Array

The NumPy array object has a property called dtype that profits the data kind of the array :
Get the data type of an array object:
import numpy as np

arr = np.array([1, 2, 3, 4]>

Get the data type of an array containing strings:
import numpy as np

arr = np.array(['apple', 'banana', 'cherry']>

Making Arrays With a Defined Data Type

We utilize the array(>
function to make arrays, this function can take a discretionary contention: dtype that permits us to characterize the normal data type of the array components:
Make an array with data type string:
import numpy as np

arr = np.array([1, 2, 3, 4], dtype='S'>

Changing over Data Type on Existing Arrays

The most ideal way to change the data type of a current array, is to make a duplicate of the array with the astype(>
The astype(>
function makes a duplicate of the array, and permits you to indicate the data type as a parameter.
The data type can be deter mined utilizing a string, similar to 'f' for float, 'i' for integer and so on or you can utilize the data type straightforwardly like float for float and int for integer.
Change data type from float to integer by involving 'i' as parameter value:
import numpy as np

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

Scientific Library for Python. ... pip install scipy ... SciPy (pronounced “Sigh Pie”>
is open-source software for mathematics, science
Python List Slicing ... With this operator, one can specify where to start the slicing, where to end, and specify the step.
There are two ways to create empty NumPy array can be created: numpy.zeros and numpy.empty .
NumPy Data Types · i – integer · b – boolean · u – unsigned · f – float · c – complex float · m – timedelta · M – datetime · O – object ...
Numpy Factorial Function · Python numpy numpy.math.factorial(num>
method accepts a positive integer number as a argument.