In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to start Python Numpy, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Numpy is the most basic and powerful tool kit for scientific calculation and data processing in python language. For example, the data analysis tool pandas is also based on numpy, and the machine learning package scikit-learn also uses numpy methods extensively. This paper introduces all the core applications of n-dimensional array of Numpy in data processing and analysis.
1. How to build an numpy array
There are many ways to construct numpy array, and the more common method is to convert the list with np.array function.
# create an one-dimensional array from the list import numpy as nplist1 = [0Jing 1Jing 2Jing 3Jing 4] arr1d = np.array (list1) # print array and type print (type (arr1d)) arr1d [0 1 2 3 4]
The key difference between array and list is that array is based on vectorization operation, list is not, the data we deal with in actual project is generally matrix structure, the data is calculated in the form of row vector or column vector, vector calculation is based on array, so array is more widely used than list.
Function can be applied to every item in an array, not a list.
For example, you can't add 2 to every item of data in the list, which is wrong.
List1 + 2 # error
You can add 2 to a certain item of data in the array
# Add 2 to each element of arr1darr1d + cards > array ([2, 3, 4, 5, 6])
Another difference is that an already defined numpy array cannot increase the size of the array, only by defining another array, but the list can increase the size.
However, Numpy has more advantages, let's find out together.
Numpy can build two-dimensional arrays from lists in lists.
# Create a 2d array from a list of listslist2 = [0rect 1 array from a list of listslist2 2], [3je 4je 5], [6je 7je 8] arr2d = np.array (list2) arr2d# > array ([[0rect 1m 2], # > [3rect 4je 5], # > [6je 7je 8]))
You can also specify the type of the array through the dtype parameter. Some of the most common numpy types are: 'float','int','bool','str'' and 'object'.
# Create a float 2d arrayarr2d_f = np.array (list2, dtype='float') arr2d_f# > array ([[0.1,2.], # > [3.4.5.], # > [6.7.8]])
The decimal point of the output represents the float type, and you can also convert it to different types through the astype method.
# convert to 'int' type arr2d_f.astype (' int') # > array ([[0, 1, 2], # > [3, 4, 5], # > [6, 7, 8]]) # convert 'int' type first Then convert the 'str' type arr2d_f.astype (' int'). Astype ('str') # > array ([' 0,1','2'], # > ['3','4','5'], # > ['6,'7,'8'], # > dtype='U21')
Another difference is that the array requires all items to be of the same type, which list does not have. If you want an array to contain different types, set 'dtype'' to 'object''.
# build an array of Boolean types arr2d_b = np.array ([1,0,10], dtype='bool') arr2d_b# > array ([True, False, True], dtype=bool) # build an array containing numeric values and strings arr1d_obj = np.array ([1,'a'], dtype='object') arr1d_obj# > array ([1,'a'], dtype=object)
Finally, the tolist () function is used to convert the array to a list.
# Convert an array back to a listarr1d_obj.tolist () # > [1,'a']
Summarize the main differences between arrays and lists:
Array supports vectorization operation, list does not support
The array cannot be changed in length, and the list can
Each item in the array is of the same type, and list can have multiple types
An array of the same length takes up less space than a list
two。 How to observe the size and shape of array properties (shape)
An one-dimensional array is built from a list, a two-dimensional array arr2d is built from a list, a two-dimensional array has rows and columns, such as a matrix, and a three-dimensional array is built from a list embedded with two lists.
Given an array, how do we understand the properties of that array?
The properties of the array include:
Dimension of the array (ndim)
Shape of the array (shape)
Type of array (dtype)
Size of the array (size)
Representation of array elements (by index)
# define three rows and four columns of two-dimensional array list2 = [[1, 2, 3, 4], [3, 4, 5, 6], [5, 6, 7, 8]] arr2 = np.array (list2, dtype='float') arr2# > array ([[1, 2, 3, 4.], # > [3, 4, 5, 6.], # > [5, 6, 7, 8.]) # shape print ('Shape:') Arr2.shape) # Array type (dtype) print ('Datatype:', arr2.dtype) # Array size (size) print ('Size:', arr2.size) # Array Dimension (ndim) print ('Num Dimensions:', arr2.ndim) # take the third row of the array and three column elements print ('items of 3 line 3 column:', c [2Magne2]) # > Shape: (3) 4) # > Datatype: float64# > Size: 1 Num Dimensions > items of 3 line 3 column: 7
3. How to extract specific items from an array
The index of the array is counted from 0, similar to list. The numpy array selects a specific element through the parameters of square brackets.
# Select the first two rows and two columns of the matrix arr2 [: 2,: 2] list2 [: 2,: 2] # error # > array ([[1, 2.], # > [3, 4]])
The numpy array supports Boolean indexes, the Boolean index array is the same size as the pre-filter (array-to-be-filtered) array, the Boolean array contains only Ture and False variables, and the array index position corresponding to the Ture variable retains the pre-filtering value.
Arr2# > array ([[1, 2, 3, 4.], # > [3, 4, 5, 6.], # > [5, 6, 7, 8.]]) # whether each element of the array satisfies a certain condition Then get the output of Boolean type b = arr2 > 4b# > array ([[False, False], # > [False, False, True, True], # > [True, True]], dtype=bool) # take the value of the original array retained by the Boolean array arr2 [b] # > array ([5.6.6.5.6.6.7.8.])
3.1 how to reverse an array
# invert the row arr2 of the array [::-1,] # > array ([[5.6,7.,8.], # > [3.4,5.5.6.], # > [1.2,3.3.4.]]) # Reverse the row and column positions# reverses the row and column arr2 of the array [::-1,:-1] # > array ([[8.7,6.5,5.], # > [6.,5.4. 3.], # > [4., 3., 2., 1])
3.2 how to deal with missing values (missing) and infinity (infinite) values of arrays
Missing values can be represented by np.nan objects, and np.inf represents infinity. Here is an example of a two-dimensional array:
# insert nan variable and inf variable arr2 [1Power1] = np.nan # not a numberarr2 [1Magne2] = np.inf # infinitearr2# > array ([[1, 2, 3, 4.], # > [3, nan, inf, 6.], # > [5, 6, 7, 8]) # replace nan value and infmissing _ bool = np.isnan (arr2) with-1 | np.isinf (arr2) arr2 [missing _ bool] =-1 arr2# > array ([[1. 2., 3., 4.], # > [3.,-1.,-1., 6.], # > [5.6.7.8])
3.3 how to calculate the average, minimum and maximum values of an n-dimensional array
# average, maximum, minimum print ("Mean value is:", arr2.mean ()) print ("Max value is:", arr2.max ()) print ("Min value is:", arr2.min ()) # > Mean value is: 3.5833333333 > Max value is: 8.8 > Min value is:-1.0
If you want to find the minimum value of an array's rows or columns, use the np.amin function
# Row wise and column wise min# finds the minimum of rows and columns in the array # axis=0 represents the column, and 1 indicates the row print ("Column wise minimum:", np.amin (arr2, axis=0)) print ("Row wise minimum:", np.amin (arr2, axis=1)) # > Column wise minimum: [1.-1. -1. 4.] # > Row wise minimum: [1.-1. 5.]
Each element of the array is accumulated to get an one-dimensional array, which is the same size as a two-dimensional array.
# cumulative np.cumsum (arr2) # > array ([1.3,6.10.13.12.11.17.22.28.35.43.])
4. How to define a new array from an existing array
If you use the assignment operator to define a new array from the parent array, the new array shares the same memory space as the parent array, and if you change the value of the new array, the parent array changes accordingly.
To make the new array independent of the parent array, you need to use the copy () function. All parent arrays use the copy () method to build a new array.
# Assign portion of arr2 to arr2a. Doesn't really create a new array.# assigns the arr2 array to the new array arr2a The following method does not determine the corresponding position of the new array arr2a = arr2 [: 2jade 2] arr2a [: 1,: 1] = 100# arr2 and also changes the corresponding position of arr2# > array ([[100.2,3.3,4.], # > [3.1.1.1.6.], # > [5.6.7.8]) # assigns a portion of the arr2 array to the new array arr2barr2b = arr2 [: 2 : 2] .copy () arr2b [: 1,: 1] = 101 # arr2 does not change arr2# > array ([[100, 2, 3, 4.], # > [3,-1,-1, 6.], # > [5, 6, 7, 8]])
5. Reconstruction (reshaping) and flattening (flattening) of multidimensional arrays
Reshaping changes the arrangement of array items, that is, it changes the shape of the array without changing the dimension of the array.
Flattening is to convert a multi-dimensional array into an one-dimensional array.
# 3x4 array is reconstructed into 4x3 array arr2.reshape (4, 3) # > array ([[100, 2, 3.], # > [4, 3,-1.], # > [- 1, 6, 5.], # > [6, 7, 8.])
5.1 the difference between flatten () and ravel ()
There are two common methods for flattening arrays, flatten () and ravel (). The array processed by flatten is a reference to the parent array, so any change in the new array will also change the parent array, because it does not copy the array, memory efficiency is efficient, and ravel builds the new array by copying.
# flatten method arr2.flatten () # > array ([100, 2, 3, 4, 3,-1,-1, 6, 6, 6, 7, 8.]) # flatten method b1 = arr2.flatten () b1 [0] = 100 # changing the value of b1 does not affect arr2arr2# > array ([[100, 2, 3, 4.], # > [3,-1,-1, 6.], # > [5, 6. 7, 8.]) # ravel method b2 = arr2.ravel () b2 [0] = 101 # change b2 value Correspondingly change the ARR2 value arr2# > array ([[101.2,3.3,4.], # > [3.1.1.1.6.6], # > [5.6.7.8]])
6. How to generate sequence number (sequences), repeat number (repetitions) and random number (random) through numpy
The np.arrange function manually generates a specified number of sequences, just like ndarray.
# the default lower limit is 0print (np.arange (5)) # 0 to 9, and the default number of steps is 1print (np.arange (0,10)) # increasing steps 2print (np.arange (0,10,2)) # descending print (np.arange (10,0,-1)) # > [012 3 4] # > [0 1 2 3 4 5 7 7 8 9] # > [0 24 68] # > [10 9 8 7 6 4 2 1]
In the above example, the sequence number is generated by setting the initial position and the end position of the np.arrange. If we set the number of elements of the array, then the increment of the array can be calculated automatically.
If you build an array from 1 to 50, the array has 10 elements, and the np.linspace total action is used to calculate the recursive increment of the array.
# start position and end position are 1 and 50np.linspace (start=1, stop=50, num=10, dtype=int) # > array ([1, 6, 11, 17, 22, 28, 33, 39, 44, 50])
We note that the recursive increment of the above example is not equal, there are two values of 5 and 6, because the recursive increment is calculated by rounding algorithm (rounding). Similar to np.linspace, np.logspace grows on a logarithmic scale.
# set the precision of the array to two places after the decimal point np.set_printoptions (precision=2) # starts at 10 ^ 1 and ends at 10 ^ 50, the number of array elements is 10, np.logspace (start=1, stop=50, num=10, base=10) # > array ([1.00e+01, 2.78e+06, 7.74e+11, 2.15e+17, 5.99eq22 # > 1.67e+28, 4.64e+33, 1.29e+39, 3.59e+44, 1.00e+50])
The elements of the initialization array are all 1s or all zeros.
Np.zeros ([2jue 2]) # > array ([0.,0], # > [0.,0]]) np.ones ([2recover2]) # > array ([[1.1,1], # > [1.1,1]])
6.1 how to build the number of repeated sequences
Np.tile repeats the entire array or list n times, and np.repeat repeats each item of the array n times. A = [1Jing 2jue 3] # repeating array a twice print ('Tile:', np.tile (a, 2)) # repeating array a twice each print ('Repeat:', np.repeat (a, 2)) # > Tile: [1 2 3 1 3 3] # > Repeat: [1 1 2 2 3 3]
6.2 how to survive random numbers
The random module contains functions that can generate random numbers and statistical distributions of any array shape.
# generate a 2-row and 2-column random number print (np.random.rand (2Magne2)) # generate a normal distribution value of 2 rows and 2 columns with a mean of 0 and variance of 1) # generate a random integer print of 2 rows and 2 columns with a mean of 0 (np.random.randn (2)) # generate a 2-row and 2-column random integer print (np.random.randint (0) 10) Size= [2Mague 2]) # generate a random number print (np.random.random ()) # generate 2 rows and 2 columns of random number print (np.random.random (size= [2Magazine 2])) # print (np.random.choice) is sampled 10 times with equal probability from a given list (['a','e', 'iFei,' oasis,'u'] Size=10)) # sample print 10 times from the given list and the corresponding probability distribution (np.random.choice (['a','e','i','o','u'], size=10, p = [0.3,.1, 0.1,0.4) ) # picks more o's# > [[0.84 0.7] # > [0.52 0.8]] # > [[0.06-1.55] # > [0.47-0.04]] # > [[40] # > [87] # > 0.08737272495683] > [[0.450.78] # > [0.030.74]] # > [i'a`e'e'' 'a 'u'o'e'i'u'] # > ['o'a'e'a'o'a'o']
6.3How to get the unique items and number of unique in an array (counts)
The np.unique function removes the repeated elements in the array, sets the return_counts parameter to True, and gets the number of each item in the array.
# the scope of definition is [0pc10) An array of random integers np.random.seed (100) arr_rand = np.random.randint (0,10, size=10) print (arr_rand) # > [8 8 3 7 7 0 4 2 52] # an array of unique items and the corresponding number uniqs, counts= np.unique (arr_rand, return_counts=True) print ("Unique items:", uniqs) print ("Counts:" Counts) # > Unique items: [0 2 3 4 5 7 8] # > Counts: [1 2 1 1 1 2 2]
For those of you who are not familiar with the basic usage of numpy, you can take a look at it!
After reading the above, have you mastered how to get started with Python Numpy? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.