Data type conversion and ndarray

ndarray data types

Ndarray basic data types as shown below, using the data type named "digital type name +" indicates the form of digital data bits representing the bit length. In a computer the bit is a care bit minimum data unit, a length byte Byte is equal to 8 bits, i.e., 1Byte = 8bit. int64 8 denotes an integer byte length, float64 represented as an 8-byte double-precision floating-point length. The same types of elements in memory or disk space is occupied by the same, so when processing huge amounts of data, if the data type is set unreasonable, will lead to a waste of memory or disk storage space, and affect the computational efficiency. But for starters, no need to concern the length of each type, there is only need to know ndarray integer, float, string, boolean, the object type.

Ndarray data type conversion

The biggest difference is ndarray list with Python list can be stored in different types of data elements, data types and ndarray requires all elements must be consistent. Numpy automatically recognizes the type of data ndarray, if the data is inconsistent Numpy all elements will be automatically converted into a suitable data type.

Figure above has created three ndarray objects, arr1 consists of three integers, arr2 arr1 in the second element of the integer to floating-point, to ensure that the same data type, other elements Numpy will arr2 are converted into float, the ARR3 arr2 the third element to the string type, the same Numpy automatically converts to a string of other elements. Integer -> float -> string can be automatically converted, if the order can not be reversed automatically converted, because it may cause loss of data, we can use astype () function manually cast. Principle astype () function is to first copy the original array in memory, data type conversion operations are carried out in the array's copy will not affect the original array.

注意上图中将arr4由浮点型转换成整数型时,每个元素的小数位都被自动截掉,实际应用中可能影响数据质量;只有当所有字符串元素都表示整数型或浮点型时,才能使用astype( )函数将字符转型转换成整数型或浮点型,否则会报ValueError错误;直接把arr6转换成整数型也会报错。

ndarray元素访问

ndarray可以用索引来访问元素,一维数组的索引访问方式与Python的list相同,多维数组只需要在索引中增加位置即可。下图中倒数第2行arr9[2]表示一维数组arr9中的第3个元素“15”,最后一行arr10[1,1]表示二维数组arr10的第2行第2个元素“25”。

同样ndarray可以使用切片访问多个元素。下图中第2行生成了0-9的整数组成的一维数组arr11,第4行进行切片操作,将数组第6-9个元素取出赋值给变量arr_slice;注意:切片是数组的视图,任何对切片的修改都会导致原数组的元素变化,第6行将切片第1个元素的值改为“0”,原数组arr11也随之发生了变化。如果你不想使用视图而只是对数组切片进行复制,需要使用copy( )函数。

切片中使用m:n的形式,表示[m, n)的半开半闭的范围。m,n分别表示切片的开始位置和结束位置,m若省略表示从第一个元素开始,n若省略表示到最后一个元素结束,m,n都省略表示取得该维度所有元素。熟练掌握切片对学习numpy很有必要,请读者参照下图分析切片的原理:

一维数组可以看做是个列表,二维数组可以看成是一个矩阵,可是对于多维数组,不少初学者可能会觉得不容易理解,特别是在进行切片操作时更是如此。下图用10以内的随机自然数填充了一个三维数组arr12,从打印的结果可以观察到,三维数组实际上由2个二维数组组成,arr12[0]表示第1个二维数组,arr12[0, 1]表示第1个二维数组的第2行,arr12[0, 1, 2]表示第1个二维数组第2行的第3个元素。

对多维数组的切片也跟二维数组是类似的,请读者自行分析下列几个表达式的切片结果:

 

Guess you like

Origin www.cnblogs.com/liuys635/p/11201827.html