October 2019
Beginner to intermediate
674 pages
16h 43m
English
All the array-creation functions described earlier (except the functions for arrays based on existing arrays—diag, triu, tril, and tile) have an argument dtype to define the data type of the array.
Let's create an array without predefining the data type, and then check for its data type:
In [80]: np.array([-2, -1, 0, 1, 2]).dtypeOut [80]: dtype('int32')
Now, let's define the same array by also setting its data type to float:
In [81]: np.array([-2, -1, 0, 1, 2], dtype = "float")Out [81]: array([-2., -1., 0., 1., 2.])
It is seen that the elements of the array are all casted as floating points. It is also possible to cast this array as a string:
In [83]: np.array([-2, -1, 0, 1, 2], dtype = "str")Out[83]: array(['-2', '-1', ...
Read now
Unlock full access