This article we play numpy numeric data type conversions
Import NumPy
Import NumPy as NP
One, casual play create a floating point group
>>> a = np.random.random (4)
Look at the information
>>>0.0945377, 0.52199916, 0.62490646, 0.21260126])>>> A.dtypedtype ('float64')>>> A.shape (4,)
Change the Dtype and find the array length doubled!
' float32 '>>> aarray ([ 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 the Dtype, the array length doubles again!
' float16 '>>>-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
' float '>>>0.0945377, 0.52199916, 0.62490646, 0.21260126])>> > a.shape (4,)>>> a.dtypedtype ('float64') )
Turn a into an integer and observe its information
' Int64 '>>> aarray ([4591476579734816328, 4602876970018897584, 4603803876586077261, 4596827787908854048], dtype=Int64)>>> A.shape (4,)
Change the Dtype and find the array length doubled!
' Int32 '>>>1637779016, 1069036447, -1764917584, 1071690807, -679822259 , 1071906619, -1611419360, 1070282372])>>> A.shape (8,)
Change the Dtype and find the array length doubled again!
' Int16 '>>> aarray ([-31160, 24990, 13215, 16312, 32432, -26931, -19401, 16352, -17331, -10374, -197, 16355, -20192, -24589, 13956, 16331], dtype= Int16)>>> A.shape (16,)
Change the Dtype and find the array length doubled again!
' int8 '>>> aarray ([ -122, -98, $ -97, Wuyi, -72, . -80, 126, -51, -106, -76, -32, $, -68, 122, -41,, 1, -29, -79, -13, 97,-124, Si , -53, dtype=int8)>>> A.shape (32,)
Change Dtype, find integer default int32!
' int '>>> a.dtypedtype ('int32')>>> 1637779016, 1069036447, -1764917584, 1071690807, -679822259, 1071906619,- 1611419360, 1070282372])>>> A.shape (8,)
Second, a different way to play
Many times we use NumPy to read data from a text file as an array of NumPy, and the default Dtype is float64.
But on some occasions we want some data columns as integers. If you change dtype= ' int ' directly, it will go wrong! The reason is as above, the array length doubled!!!
The following scenario assumes that we have the imported data. Our intention is to expect them to be integers, but in fact they are floating point numbers (float64)
>>> B = Np.array ([1., 2., 3., 4.]) >>> b.dtypedtype ('float64')
Useastype (int)Get an integer, anddo not change the array length
>>> C = b.astype (int)>>> CArray ([1, 2, 3, 4])
>>> C.shape
(8,) >>> c.dtypedtype ('int32')
If you change the dtype of B directly, B'sdouble the length, and that's not what we want (of course if you want to)
>>>1., 2., 3., 4.]) ' int '>>> b.dtypedtype ('int32')>>> Barray ([ 1072693248, 0, 1073741824, 0, 1074266112, 0, 1074790400 ])>>> B.shape (8,)
Iii. conclusion the data type conversion in NumPy,can'tDirect conversion of the original dataDtype! Only functions can be usedAstype ()。
NumPy Data type Dtype conversion