Implementation of numpy Data Type dtype Conversion

  • 2021-11-01 04:12:35
  • OfStack

In this article, we play with the numerical data type conversion of numpy

Import numpy


>>> import numpy as np

STEP 1 Have fun

Generate a floating-point array


>>> a = np.random.random(4)

Look at the information


>>> a
array([ 0.0945377 ,  0.52199916,  0.62490646,  0.21260126])
>>> a.dtype
dtype('float64')
>>> a.shape
(4,)

Change dtype and find that the array length doubles!


>>> a.dtype = 'float32'
>>> a
array([  3.65532693e+20,   1.43907535e+00,  -3.31994873e-25,
         1.75549972e+00,  -2.75686653e+14,   1.78122652e+00,
        -1.03207532e-19,   1.58760118e+00], dtype=float32)
>>> a.shape
(8,)

Change dtype, and the array length doubles again!


>>> a.dtype = 'float16'
>>> a
array([ -9.58442688e-05,   7.19000000e+02,   2.38159180e-01,
         1.92968750e+00,              nan,  -1.66034698e-03,
        -2.63427734e-01,   1.96875000e+00,  -1.07519531e+00,
        -1.19625000e+02,              nan,   1.97167969e+00,
        -1.60156250e-01,  -7.76290894e-03,   4.07226562e-01,
         1.94824219e+00], dtype=float16)
>>> a.shape
(16,)

Change dtype = 'float' and find that the default is float64, and the length is changed back to the original 4


>>> a.dtype = 'float'
>>> a
array([ 0.0945377 ,  0.52199916,  0.62490646,  0.21260126])
>>> a.shape
(4,)
>>> a.dtype
dtype('float64')

Change a to an integer and observe its information


>>> a.dtype = 'int64'
>>> a
array([4591476579734816328, 4602876970018897584, 4603803876586077261,
       4596827787908854048], dtype=int64)
>>> a.shape
(4,)

Change dtype and find that the array length doubles!


>>> a.dtype = 'int32'
>>> a
array([ 1637779016,  1069036447, -1764917584,  1071690807,  -679822259,
        1071906619, -1611419360,  1070282372])
>>> a.shape
(8,)

Change dtype and find that the array length doubles again!


>>> a.dtype = 'int16'
>>> a
array([-31160,  24990,  13215,  16312,  32432, -26931, -19401,  16352,
       -17331, -10374,   -197,  16355, -20192, -24589,  13956,  16331], dtype=int16)
>>> a.shape
(16,)

Change dtype and find that the array length doubles again!


>>> a.dtype = 'int8'
>>> a
array([  72, -122,  -98,   97,  -97,   51,  -72,   63,  -80,  126,  -51,
       -106,   55,  -76,  -32,   63,   77,  -68,  122,  -41,   59,   -1,
        -29,   63,   32,  -79,  -13,  -97, -124,   54,  -53,   63], dtype=int8)
>>> a.shape
(32,)

Change dtype and find the integer default int32!


>>> a = np.random.random(4)

0

2. Change the game

Many times we use numpy to read data from a text file as an array of numpy, and the default dtype is float64.
But there are occasions where we want some data columns to be integers. If you change dtype = 'int' directly, you will make mistakes! For the above reason, the array length has doubled! ! !

The following scenario assumes that we get the imported data. We intended them to be integers, but in fact they were floating-point numbers (float64)


>>> a = np.random.random(4)

1

Use astype (int) to get integers without changing the length of the array


>>> a = np.random.random(4)

2

If you change the dtype of b directly, the length of b will double, which is not what we want (if you want, of course)


>>> b
array([ 1.,  2.,  3.,  4.])

>>> b.dtype = 'int'
>>> b.dtype
dtype('int32')
>>> b
array([         0, 1072693248,          0, 1073741824,          0,
       1074266112,          0, 1074790400])
>>> b.shape
(8,)

3. Conclusion

numpy data type conversion, can not directly change the original data dtype! You can only use the function astype ().


Related articles: