Numpy study notes

catalogue

1, Using Numpy to create a new two-dimensional matrix

2, Numerical operation:

3, Sort operation

1. Define a matrix

2,   np.sort() function

3. axis parameter

4. np.argsort() function

  5. np.linspace() function

6. NP. Lexport() function

4, Array operation

1 np.arange()

2. Array. Shape() function

3 add dimension and compress useless dimension information

4. The transpose operation has the same effect as the transfer() and. T() functions

5, Array connection

1 np.concatenate() function connection. Note that the passed in parameters need to be in the form of tuples

2 flattening operation, NP. Flatten(), NP. Travel()

6, Array generation

1np.arange()

np.logspace()

2 generate grid coordinates

1, Using Numpy to create a new two-dimensional matrix

Input:

wang_array = np.array([[1,2,3],[4,5,6]])
wang_array

2, Numerical operation:

# Find the mean of all elements of the matrix
wang_array.mean()

# Summation of all elements of a matrix
wang_array.sum()

# Calculate the standard deviation of all elements of the matrix
wang_array.std()

# Variance of all elements of matrix
wang_array.var()

# Modification of high and low thresholds for all elements of the matrix
wang_array.clip(2,4)

# All elements of the matrix are rounded
wang_array.round()
wang_array.round(decimals = 1) #Keep one decimal place

  All the above operations are performed on all elements in the matrix. If you perform operations such as summation and standard deviation on rows or columns in the matrix, you can use the axis=0 or 1 parameter in the function. If axis=0, it means to operate along the column, and if axis=1, it means to operate along the row.

3, Sort operation

1. Define a matrix

In [15]:
wang_array = np.array([[1.1,2.2,3.3],
                       [4.2,4.4,1.2],
                       [1.0,2.1,3.2]])
wang_array

Out[15]:
array([[1.1, 2.2, 3.3],
       [4.2, 4.4, 1.2],
       [1. , 2.1, 3.2]])

2,   np.sort() function

Yes, wang_array sorts all elements

#Yes, wang_array sorts all elements
In [18]:
np.sort(wang_array)


Out[18]:
array([[1.1, 2.2, 3.3],
       [1.2, 4.2, 4.4],
       [1. , 2.1, 3.2]])

3. axis parameter

Similarly, if the axis parameter is added, row sorting or column sorting will be performed respectively according to the value of the parameter  

#Similarly, if the axis parameter is added, row sorting or column sorting will be performed according to the value of the parameter,
#For example, when axis=0, each column is sorted, and when axis=1, each row is sorted
In [6]:
np.sort(wang_array,axis=0)


Out[6]:
array([[1. , 2.1, 1.2],
       [1.1, 2.2, 3.2],
       [4.2, 4.4, 3.3]])

4. np.argsort() function

Gets the index value of the original value after sorting  

#Gets the index value of the original value after sorting
In [7]:
np.argsort(wang_array)


Out[7]:
array([[0, 1, 2],
       [2, 0, 1],
       [0, 1, 2]], dtype=int64)

  5. np.linspace() function

A row vector of 5 elements is generated, and the elements are equidistant.

In [11]:
wang_array1 = np.linspace(0,10,5)
wang_array1

Out[11]:
array([ 0. ,  2.5,  5. ,  7.5, 10. ])

Insert some numbers into the vector, such as [1,3,6,7,9], or the index position after insertion, and use np.searchsorted() function

In [14]:
values = np.array([1,3,6,7,9])
np.searchsorted(wang_array1,values)


Out[14]:
array([1, 2, 3, 3, 4], dtype=int64)

6. NP. Lexport() function

Sort the matrix. First, the first column is arranged in descending order, then the third column is arranged in ascending order, and then the first column is arranged in descending order

In [30]:
wang_array2 = np.array([[2,2,6,9],
                       [4,1,4,3],
                       [3,5,1,7],
                       [2,2,4,4]])
wang_array2
Out[30]:
array([[2, 2, 6, 9],
       [4, 1, 4, 3],
       [3, 5, 1, 7],
       [2, 2, 4, 4]])


#Sort the matrix. First, the first column is arranged in descending order, and then the third column is arranged in ascending order
In [38]:
index = np.lexsort([-1*wang_array2[:,0],wang_array2[:,2]])
In [39]:
wang_array2 = wang_array2[index]
wang_array2
Out[39]:
array([[3, 5, 1, 7],
       [4, 1, 4, 3],
       [2, 2, 4, 4],
       [2, 2, 6, 9]])

4, Array operation

1 np.arange()

wang_array.shape

#Generate 0-9 vectors, 10 elements
In [41]:
wang_array =np.arange(10)
wang_array

Out[41]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

2. Array. Shape() function

The length of the output vector or the number of rows and columns of the matrix

#Displays the length of the vector
In [42]:
wang_array.shape
#Because wang_array is a vector, so the output is only 1 number
Out[42]:
(10,)

#By assigning a value to the shape of a one-dimensional matrix, the number of rows and columns of the matrix can be modified, such as:
In [44]:
wang_array.shape=2,5
wang_array

Out[44]:
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

3 add dimension and compress useless dimension information

The compressed dimension uses the squeeze() function

#Add dimension
In [47]:
_array = wang_array[np.newaxis,:]
wang_array

Out[47]:
array([[[0, 1],
        [2, 3],
        [4, 5],
        [6, 7],
        [8, 9]]])
#After adding a dimension, the dimension information of the matrix is displayed
In [48]:
wang_array.shape

Out[48]:
(1, 5, 2)

#The corresponding dimension information is invalid for compression
In [52]:
wang_array=wang_array.squeeze()
wang_array

Out[52]:
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])

In [53]:
wang_array.shape
Out[53]:
(5, 2)

4. The transpose operation has the same effect as the transfer() and. T() functions

#The effect expressed by the transfer(),. T() functions is the same
In [54]:
wang_array.transpose()

Out[54]:
array([[0, 2, 4, 6, 8],
       [1, 3, 5, 7, 9]])

In [56]:
wang_array.T

Out[56]:
array([[0, 2, 4, 6, 8],
       [1, 3, 5, 7, 9]])

5, Array connection

1 np.concatenate() function connection. Note that the passed in parameters need to be in the form of tuples

axis=0 means to connect by column, and axis=1 means to connect by row

--There are also two equivalent connection methods, np.vstack(),np.hstack()

#a. B two matrices are connected by np.concatenate() function. Note that the passed in parameters need to be in the form of tuples
#axis=0 means to connect by column, and axis=1 means to connect by row
In [57]:
a = np.array([[1,3,43,21],
             [21,32,5,2]])
b = np.array([[10,213,2143,121],
             [241,332,52,22]])

In [61]:
c = np.concatenate((a,b),axis = 0)
c


Out[61]:
array([[   1,    3,   43,   21],
       [  21,   32,    5,    2],
       [  10,  213, 2143,  121],
       [ 241,  332,   52,   22]])

#There are also two equivalent connection methods, np.vstack(),np.hstack()
In [63]:
d = np.vstack((a,b))
d

Out[63]:
array([[   1,    3,   43,   21],
       [  21,   32,    5,    2],
       [  10,  213, 2143,  121],
       [ 241,  332,   52,   22]])


In [64]:
e = np.hstack((a,b))
e
Out[64]:
array([[   1,    3,   43,   21,   10,  213, 2143,  121],
       [  21,   32,    5,    2,  241,  332,   52,   22]])

2 flattening operation, NP. Flatten(), NP. Travel()

# The flatten() and travel() functions are equivalent
In [65]:
a.flatten()
Out[65]:
array([ 1,  3, 43, 21, 21, 32,  5,  2])

In [67]:
a.ravel()
Out[67]:
array([ 1,  3, 43, 21, 21, 32,  5,  2])

#Note that the value of a remains unchanged despite the operation of A. flatten(), a. travel()
In [68]:
a
Out[68]:
array([[ 1,  3, 43, 21],
       [21, 32,  5,  2]])

6, Array generation

1np.arange(),np.linspace(),np.logspace()

#np.arang()
#Produce an array between 2-20 (the interval is closed on the left and open on the right) with a step size of 2. The data type of each element is integer by default
In [71]:
wang_array =np.arange(2,20,2)
wang_array

Out[71]:
array([ 2,  4,  6,  8, 10, 12, 14, 16, 18])

#Produce an array between 2-20 (the interval is closed on the left and open on the right) with step size of 2. The data type of each element is floating point
In [72]:
np.arange(2,20,2,dtype=np.float32)

Out[72]:
array([ 2.,  4.,  6.,  8., 10., 12., 14., 16., 18.], dtype=float32)

#Produce 5 elements equidistant between 0-10
In [73]:
np.linspace(0,10,5)

Out[73]:
array([ 0. ,  2.5,  5. ,  7.5, 10. ])

#The default is based on 10
In [75]:
np.logspace(0,1,5)
Out[75]:
array([ 1.        ,  1.77827941,  3.16227766,  5.62341325, 10.        ])

2 generate grid coordinates

x,y = np.meshgrid(x,y)

In [82]:
x = np.linspace(-10,10,5)
x

Out[82]:
array([-10.,  -5.,   0.,   5.,  10.])

In [83]:
y = np.linspace(-10,10,5)
y

Out[83]:
array([-10.,  -5.,   0.,   5.,  10.])

In [84]:
x,y = np.meshgrid(x,y)
x

Out[84]:
array([[-10.,  -5.,   0.,   5.,  10.],
       [-10.,  -5.,   0.,   5.,  10.],
       [-10.,  -5.,   0.,   5.,  10.],
       [-10.,  -5.,   0.,   5.,  10.],
       [-10.,  -5.,   0.,   5.,  10.]])

In [85]:
y
Out[85]:
array([[-10., -10., -10., -10., -10.],
       [ -5.,  -5.,  -5.,  -5.,  -5.],
       [  0.,   0.,   0.,   0.,   0.],
       [  5.,   5.,   5.,   5.,   5.],
       [ 10.,  10.,  10.,  10.,  10.]])

  2 row vector, column vector np.r_[],np.c_ [] function

In [86]:
np.r_[0:10:1]

Out[86]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [87]:
np.c_[0:10:1]

Out[87]:
array([[0],
       [1],
       [2],
       [3],
       [4],
       [5],
       [6],
       [7],
       [8],
       [9]])

Tags: Python MySQL

Posted on Wed, 24 Nov 2021 21:21:21 -0500 by manishsinha27