In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to implement ndarray multi-dimensional array by NumPy". In the operation of actual cases, many people will encounter this dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Brief introduction
One of the most important functions of NumPy is to manipulate multi-dimensional arrays, which are also called ndarray. We can do a series of complex mathematical operations on the basis of ndarray.
Create ndarray
There are many ways to create a ndarray, and we can use np.random to randomly generate data:
Import numpy as np# Generate some random datadata = np.random.randn (2,3) dataarray ([[0.0929, 0.2817, 0.769], [1.2464, 1.0072,-1.2962]])
In addition to random creation, you can also create from list:
Data1 = [6,7.5,8,0,1] arr1 = np.array (data1) array ([6,7.5,8,0,0. , 1.])
Create a multidimensional array from list:
Data2 = [[1,2,3,4], [5,6,7,8]] arr2 = np.array (data2) array ([[1,2,3,4], [5,6,7,8]])
Use np.zeros to create an array with an initial value of 0:
Np.zeros (10) array ([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
Create a 2-dimensional array:
Np.zeros ((3,6)) array ([[0, 0, 0, 0, 0, 0.], [0, 0, 0, 0, 0.], [0, 0, 0, 0, 0.])
Create a 3-dimensional array using empty:
Np.empty ((2,3,2) array ([0, 0.], [0, 0.], [0, 0.], [0, 0.], [0, 0.], [0, 0.]])
Note that here we see that the value of the array created by empty is 0, which is not certain. Empty will randomly pick space from memory to return, there is no guarantee that there are no values in these spaces. So after using empty to create arrays, before using them, remember to initialize them.
Use arange to create an array of scope classes:
Np.arange (15) array ([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14])
Specify the dtype of the elements in the array:
Arr1 = np.array ([1,2,3], dtype=np.float64) arr2 = np.array ([1,2,3], dtype=np.int32) ndarray attribute
You can get the shape of the array through data.shape.
Data.shape (2,3)
Obtain dimension information through ndim:
Arr2.ndim2
Specific data types can be obtained through data.dtype.
Type conversion of elements in data.dtypedtype ('float64') ndarray
After you have created a type of ndarray, you can also convert it:
Arr = np.array ([1,2,3,4,5]) arr.dtypedtype ('int64') float_arr = arr.astype (np.float64) float_arr.dtypedtype (' float64')
Above we use astype to convert ndarray of type int64 to type float64.
If the range of the conversion type does not match, the operation is truncated automatically:
Arr = np.array ([3.7,1.2,2.6,0.5,12.9,10.1]) arr.astype (np.int32) array ([3,-1,-2,0,12,10], dtype=int32)
Note that the decimal is truncated here, not rounded up or down.
Mathematical Operation of ndarray
Arrays can operate either with constants or with arrays:
Arr = np.array ([[1,2,3.], [4.5,5.6.]) arr * arrarray ([[1.,4.,9.], [16.,25.36.]]) arr + 10array ([[11.12.13.], [14.15.15.16.]]) arr-arrarray ([[0.,0.0.0], [0.,0.]. 0.]) 1 / arrarray ([[1., 0.3333], [0.25,0.2, 0.1667]]) arr * * 0.5array ([[1., 1.4142, 1.7321], [2., 2.2361, 2.4495]])
You can also compare between arrays, comparing the size of each element in the array:
Arr2 = np.array ([[0.,4.1,1.], [7.,2.,12.]) arr2 > arrarray ([[False, True, False], [True, False, True]]) index and slices
Basic use
Let's take a look at the basic use of index and slicing. Index is basically used in the same way as a normal array, accessing an element in the array.
It should be noted that the elements in the array returned after slicing are references to the elements in the original array, and modifying the array of slices will affect the original array.
# build an one-dimensional array arr = np.arange (10) array ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # index visit arr [5] slice visit arr [5:8] array ([5, 6, 7]) # slice modify arr [5:8] = 12array ([0, 1, 2, 3, 4, 12, 12, 12, 12, 8 9]) # slice can modify the value of the original array arr_slice = arr [5:8] arr_slice [1] = 12345arrarray ([0, 1, 2, 3, 4, 12, 12345, 12, 8, 9]) # build a two-dimensional array arr2d = np.array ([[1, 2, 3], [4, 5, 6], [7, 8, 9]) arr2d [2] array ([7, 8]) ) # index two-dimensional array arr2d [0] [2] "index two-dimensional array arr2d [0,2]" builds a three-dimensional array arr3d = np.array ([1,2,3], [4,5,6], [[7,8,9], [10,11,12]]) arr3darray ([1,2,3], [4,5,6]], [[7,8,9]]) [10,11,12]]) # index 3D array arr3d [0] array ([1,2,3], [4,5,6]]) # copy is a hard copy Old_values = arr3d [0] .copy () arr3d [0] = 42arr3darray ([42,42,42], [42,42,42], [[7,8,9], [10,11,12]) arr3d [0] = old_valuesarr3darray ([1,2,3], [4,5,6]], [[7,8,6]) 9], [10,11,12]]) # index 3D array arr3d [1,0] array ([7,8,9]) x = arr3d [1] xarray ([[7,8,9], [10,11,12]]) x [0] array ([7,8,9]) index with slice
Slice can also be used as an index, and what is used as an index is an index range value.
Slice as a representation of index can take many forms.
With a beginning and an end, it means that the index starts at 1 and ends at 6-1:
Arr [1:6] array ([1,2,3,4,64])
If there is no beginning or end, it means that the index starts at 0 and ends with-1:
Arr2d [: 2] array ([[1,2,3], [4,5,6]])
If there is no end, it means to start from the beginning to the end of all the data:
Arr2d [: 2,1:] array ([[2,3], [5,6]]) arr2d [1,: 2] array ([4,5]) boolean index
Index can also use a Boolean value to indicate whether to select the data for this index.
Let's first look at how to build an array of type boolean:
Names = np.array (['Bob',' Joe', 'Will',' Bob', 'Will',' Joe', 'Joe']) names =' Bob'array ([True, False, False, True, False, False, False])
Above we return an array containing only True and False by comparison.
This array can be accessed as an index value:
# construct a 7 * 4 array data = np.random.randn (7,4) array ([[0.275, 0.2289, 1.3529, 0.8864], [- 2.0016,-0.3718, 1.669,-0.4386], [- 0.5397, 0.477, 3.2489,-1.0212], [- 0.5771,0.1241,0.3026,0.5238] [0.0009, 1.3438,-0.7135,-0.8312], [- 2.3702,-1.8608,-0.8608, 0.5601], [- 1.2659, 0.1198,-1.0635,0.3329]) # access through the boolean array: data [names = = 'Bob'] array ([[0.275,0.2289,1.3529,0.8864]) [- 0.5771, 0.1241, 0.3026, 0.5238])
When indexing a row, you can also index columns:
Data [names = = 'Bob', 3] array ([0.8864, 0.5238])
You can use the ~ symbol to reverse:
Data [names = = 'Bob'] array ([- 2.0016,-0.3718, 1.669,-0.4386], [- 0.5397, 0.477, 3.2489,-1.0212], [0.0009, 1.3438,-0.7135,-0.8312], [- 2.3702,-1.8608,-0.8608,0.5601], [- 1.2659] 0.1198,-1.0635, 0.3329]])
We can set values through a Boolean array, which is very useful in a real project:
Data [data < 0] = 0array ([[0.275, 0.2289, 1.3529, 0.8864], [0. , 0. , 1.669, 0. ], [0. , 0.477, 3.2489, 0. ], [0. , 0.1241, 0.3026, 0.5238], [0.0009, 1.3438, 0. , 0. ], [0. , 0. , 0. , 0.5601], [0. , 0.1198, 0. , 0.3329]) data [names! = 'Joe'] = 7array ([[7., 7., 7., 7.], [0. , 0. , 1.669, 0. ], [7., 7., 7., 7.], [7., 7., 7., 7.], [7., 7., 7., 7.], [0. , 0. , 0. , 0.5601], [0. , 0.1198, 0. , 0.3329]]) Fancy indexing
Fancy indexing, also known as fancy indexing, refers to indexing using an array of integers.
For example, let's first create an array of 8 * 4:
Arr = np.empty ((8,4)) for i in range (8): arr [I] = iarrarray ([[0.,0.,0.,0.], [1.1.1.1.1.], [2.2.2.2.2.], [3.3.3.3.3.], [4.4.4.4.4.4], [5.5.5.5.]. 5.], [6, 6, 6.], [7, 7, 7, 7.])
Then use an array of integers to index, and the rows will be selected in the specified order:
Arr [[4, 3, 0, 6]] array ([[4, 4, 4.], [3, 3, 3, 3.], [0, 0, 0.], [6, 6, 6, 6.]])
You can also use negative values to index:
Arr [[- 3,-5,-7]] array ([[5, 5, 5, 5.], [3, 3, 3, 3.], [1, 1, 1.])
Fancy indexes can also be combined to use:
Arr = np.arange (32). Reshape (8, 4) arrarray ([[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]])
Above we build an array of 8 * 4.
Arr [[1,5,7,2], [0,3,1,2]] array ([4,23,29,10])
Then take the first value of their second column, the third value of column 6, and so on. Finally, we get a 1-dimensional array.
Array transformation
We can transform between arrays of different dimensions, and we can also convert the axis of the array.
The reshape method can convert an array to any shape:
Arr = np.arange (15). Reshape ((3,5)) arrarray ([[0,1,2,3,4], [5,6,7,8,9], [10,11,12,13,14])
The array also provides a T command to swap the axes of the array:
Arr.Tarray ([[0,5,10], [1,6,11], [2,7,12], [3,8,13], [4,9,14]])
For high-dimensional arrays, you can use transpose to transpose the axis:
Arr = np.arange (16). Reshape ((2, 2, 4) arrarray ([0, 1, 2, 3], [4, 5, 6, 7]], [[8, 9, 10, 11], [12, 13, 14, 15]]) arr.transpose ((1, 0, 2)) array ([[0, 1, 2, 3], [8, 9, 10]) 11]], [4, 5, 6, 7], [12, 13, 14, 15]])
How do you understand the transpose ((1,0,2)) above?
The meaning is to switch the x _ ray y-axis and keep the z-axis unchanged.
Above we created a three-dimensional, three-axis array by using the reshape ((2,2,4)) method. Its shape is 2 * 2 * 4.
First, let's take a look at the correspondence:
(0d0)-"[0,1,2,3]
(0pr 1)-"[4, 5, 6, 7]
(1d0)-"[8, 9, 10, 11]
(1)-"[12, 13, 14, 15]
After conversion:
(0d0)-"[0,1,2,3]
(0d1)-"[8, 9, 10, 11]
(1)-"[4, 5, 6, 7]
(1)-"[12, 13, 14, 15]
So we get the results above.
The axis conversion of multidimensional arrays may be more complex, which we all understand.
You can also use swapaxes to exchange two axes, and the above example can be rewritten as follows:
Arr.swapaxes (0Jol 1) "how NumPy implements ndarray multidimensional arrays" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.