Use Python for data analysis _ Numpy _ basics _ 2, _ numpy_2
Numpy data types include:
Int8, uint8, int16, uint16, int32, uint32, int64, uint64, float16, float32, float64, float128, complex64, complex128, complex256, bool, object, string _, unicode _
Astype
Display Methods for converting array types
For example:
NumPy array index and slice Index
Similar to the python list, there is basically no difference
Slice
The value of the sliced NumPy array changes the value of the source array of the NumPy array. The NumPy array slices are the views of the source array, rather than the newly copied array. From the example below, we can see that arr [] = 0, the array of arr has changed, and the value of the corresponding position of the data array has also changed.
In [101]: data = np.random.randn(4,4)In [102]: dataOut[102]:array([[-1.68867271, -0.89369286, -0.0288363 , 0.73855122], [-0.13084603, 0.43972144, 0.73542583, 1.99925332], [ 0.04291022, -0.91963212, 3.09214837, -0.6070068 ], [-0.01416294, -1.46576298, 1.42196278, 0.84758994]])In [103]: arr = data[2:,1:]In [104]: arrOut[104]:array([[-0.91963212, 3.09214837, -0.6070068 ], [-1.46576298, 1.42196278, 0.84758994]])In [105]: arr = 0In [106]: dataOut[106]:array([[-1.68867271, -0.89369286, -0.0288363 , 0.73855122], [-0.13084603, 0.43972144, 0.73542583, 1.99925332], [ 0.04291022, -0.91963212, 3.09214837, -0.6070068 ], [-0.01416294, -1.46576298, 1.42196278, 0.84758994]])In [107]: arrOut[107]: 0In [108]: arr = data[2:,1:]In [109]: arrOut[109]:array([[-0.91963212, 3.09214837, -0.6070068 ], [-1.46576298, 1.42196278, 0.84758994]])In [110]: arr == 0Out[110]:array([[False, False, False], [False, False, False]], dtype=bool)In [111]: arrOut[111]:array([[-0.91963212, 3.09214837, -0.6070068 ], [-1.46576298, 1.42196278, 0.84758994]])In [112]: arr[1,1]=0In [113]: arrOut[113]:array([[-0.91963212, 3.09214837, -0.6070068 ], [-1.46576298, 0. , 0.84758994]])In [114]: dataOut[114]:array([[-1.68867271, -0.89369286, -0.0288363 , 0.73855122], [-0.13084603, 0.43972144, 0.73542583, 1.99925332], [ 0.04291022, -0.91963212, 3.09214837, -0.6070068 ], [-0.01416294, -1.46576298, 0. , 0.84758994]])In [115]:
If you want to copy the slices of the NumPy array, you can use the show copy method copy ()
In [116]: dataOut[116]:array([[-1.68867271, -0.89369286, -0.0288363 , 0.73855122], [-0.13084603, 0.43972144, 0.73542583, 1.99925332], [ 0.04291022, -0.91963212, 3.09214837, -0.6070068 ], [-0.01416294, -1.46576298, 0. , 0.84758994]])In [117]: arr = dataIn [118]: arrOut[118]:array([[-1.68867271, -0.89369286, -0.0288363 , 0.73855122], [-0.13084603, 0.43972144, 0.73542583, 1.99925332], [ 0.04291022, -0.91963212, 3.09214837, -0.6070068 ], [-0.01416294, -1.46576298, 0. , 0.84758994]])In [119]: arr = np.copy(data)In [120]: arrOut[120]:array([[-1.68867271, -0.89369286, -0.0288363 , 0.73855122], [-0.13084603, 0.43972144, 0.73542583, 1.99925332], [ 0.04291022, -0.91963212, 3.09214837, -0.6070068 ], [-0.01416294, -1.46576298, 0. , 0.84758994]])
Boolean Index
Assume that each string corresponds to a row of data in the data array. Note that the length of the Boolean array must be the same as that of the indexed axis.
You can use a Boolean index to search for array values as follows:
In [140]: names = np. array (['aaa', 'bbb ', 'ccc', 'ddd ', 'Eee', 'fff'])
In [141]: data = np. random. randn (6, 4)
In [1, 142]: names
Out [142]:
Array (['aaa', 'bbb ', 'ccc', 'ddd ', 'Eee', 'fff'],
Dtype = '<u3 ')
In [143]: data
Out [143]:
Array ([[0.49394026,-0.65887621,-0.26946242, 0.22042355],
[-1.11606179,-1.94945158,-0.4866134, 0.67712409],
[-2.33792045, 0.01639887,-0.46020647, 0.84180777],
[-1.99622938, 1.937877,-0.17134376, 0.56915872],
[1.50980905, 0.07244016,-0.95650922, 1.23508517],
[0.74706519,-0.03149619,-0.38235363, 0.69786257])
In [144]: names = 'aaa'
Out [144]: array ([True, False], dtype = bool)
In [145]: data [names = 'aaa']
Out [145]: array ([0.49394026,-0.65887621,-0.26946242, 0.22042355])
In [146]: names = 'ccc'
Out [146]: array ([False, False,True, False], dtype = bool)
In [147]: data [names = 'ccc ']
Out [147]: array ([-2.33792045, 0.01639887,-0.46020647, 0.84180777])
Boolean array index combined with slice to find the value of the array:
In [148]: data[names=='aaa',2]Out[148]: array([-0.26946242])In [149]: data[names=='aaa',2:]Out[149]: array([[-0.26946242, 0.22042355]])In [150]: data[names=='aaa',1:]Out[150]: array([[-0.65887621, -0.26946242, 0.22042355]])
Reverse Lookup
In [155]: names !='aaa'Out[155]: array([False, True, True, True, True, True], dtype=bool)In [156]: data[names!='aaa']Out[156]:array([[-1.11606179, -1.94945158, -0.4866134 , 0.67712409], [-2.33792045, 0.01639887, -0.46020647, 0.84180777], [-1.99622938, 1.937877 , -0.17134376, 0.56915872], [ 1.50980905, 0.07244016, -0.95650922, 1.23508517], [ 0.74706519, -0.03149619, -0.38235363, 0.69786257]])
Combined search
In [171]: mask = (names == 'aaa')|(names == 'ccc')In [172]: maskOut[172]: array([ True, False, True, False, False, False], dtype=bool)In [173]: data[mask]Out[173]:array([[ 0.49394026, -0.65887621, -0.26946242, 0.22042355], [-2.33792045, 0.01639887, -0.46020647, 0.84180777]])
Fancy Index
In fact, it is to use an integer list or array for index search. Unlike array slices, a fancy index copies data to a new array.
Integer list
Create a two-dimensional array arr and input [3, 1], which means to display it in the order of arr [3,:] and arr [1.
In [203]: arr = np.array(([1,2,3,4],[2,3,4,5],[3,4,5,6],[7,8,9,10]))In [204]: arrOut[204]:array([[ 1, 2, 3, 4], [ 2, 3, 4, 5], [ 3, 4, 5, 6], [ 7, 8, 9, 10]])In [205]: arr[[3,1]]Out[205]:array([[ 7, 8, 9, 10], [ 2, 3, 4, 5]])
Input Multiple Integer Arrays
Multiple Integer arrays are input at a time, and a one-dimensional array is returned.
Array transpose to axis swap
Array transpose refers to A new array obtained by exchanging rows and columns of the original array.
For example:
The transpose is, the transpose is
Method 1: T
In [227]: arr = np.random.randn(10)In [228]: arrOut[228]:array([-1.42853867, 1.54300781, -0.74079757, -1.20272388, -1.00416459, -0.59571731, 1.16744662, 0.05739806, 1.01660691, -0.84625494])In [229]: arr.TOut[229]:array([-1.42853867, 1.54300781, -0.74079757, -1.20272388, -1.00416459, -0.59571731, 1.16744662, 0.05739806, 1.01660691, -0.84625494])In [230]: arr = np.random.randn(3,5)In [231]: arrOut[231]:array([[ 1.36114118, 0.48455027, 0.64847485, 0.01691785, -0.03622465], [-2.31302164, 1.14992892, -1.47836923, 1.08003907, -1.33663009], [-0.38005499, 1.3517217 , 2.52024026, -0.3576492 , 0.46016645]])In [232]: arr.TOut[232]:array([[ 1.36114118, -2.31302164, -0.38005499], [ 0.48455027, 1.14992892, 1.3517217 ], [ 0.64847485, -1.47836923, 2.52024026], [ 0.01691785, 1.08003907, -0.3576492 ], [-0.03622465, -1.33663009, 0.46016645]])
Method 2: transpose
3D array arr: 4 3*4 Arrays
In [275]: arr = np. arange (48). reshape (, 4)
In [1, 276]: arr
Out [276]:
Array ([[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23],
[[24, 25, 26, 27],
[28, 29, 30, 31],
[32, 33, 34, 35],
[[36, 37, 38, 39],
[40, 41, 42, 43],
[44, 45, 46, 47])
transpose
The true significance of the parameter lies inshape
The index (axis number) of the tuples ).
In [278]: arr.shapeOut[278]: (4, 3, 4)
Arr array index (axis Number): 0, 1, 2
The following is the swap by index 2, 0, 1
In [277]: arr.transpose(2,0,1) Out[277]: array([[[ 0, 4, 8], [12, 16, 20], [24, 28, 32], [36, 40, 44]], [[ 1, 5, 9], [13, 17, 21], [25, 29, 33], [37, 41, 45]], [[ 2, 6, 10], [14, 18, 22], [26, 30, 34], [38, 42, 46]], [[ 3, 7, 11], [15, 19, 23], [27, 31, 35], [39, 43, 47]]])
Then, we switch the numbers 0, 1, and 2 to the original one.
In [279]: arr.transpose(0,1,2)Out[279]:array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]], [[24, 25, 26, 27], [28, 29, 30, 31], [32, 33, 34, 35]], [[36, 37, 38, 39], [40, 41, 42, 43], [44, 45, 46, 47]]])
Method 3:
swapaxes
Swapaxes returns the view of the source array.
Compared with transpose, You need to input an index tuple (axis Number ),Swapaxes only needs a pair of index tuples (axis numbers ).
In [283]: arr.swapaxes(2,1)Out[283]:array([[[ 0, 4, 8], [ 1, 5, 9], [ 2, 6, 10], [ 3, 7, 11]], [[12, 16, 20], [13, 17, 21], [14, 18, 22], [15, 19, 23]], [[24, 28, 32], [25, 29, 33], [26, 30, 34], [27, 31, 35]], [[36, 40, 44], [37, 41, 45], [38, 42, 46], [39, 43, 47]]])