Matrix related functions in numpy

Create a matrix

For the numpy module in python, the ndarray object provided by it is generally used. Creating an ndarray object is as simple as passing a list as a parameter. E.g

import numpy as np #Introduce numpy library

#Create a one-dimensional narray object 
a = np.array([1,2,3,4,5 ])

#Create a two-dimensional narray object 
a2 = np.array([[1,2,3,4,5],[6,7,8,9,10 ]])

#Create a multidimensional object and its analogy
Get the number of rows and columns of a matrix

import numpy as np
a = np.array([[1,2,3,4,5],[6,7,8,9,10]])

print (a.shape) #The result returns a tuple (2L, 5L) 
print (a.shape[0]) #Get the number of rows, return 2 
print (a.shape[1]) #Get the number of columns, return 5
Matrix interception

// row and column interception
import numpy as np
a = np.array([[1,2,3,4,5],[6,7,8,9,10]])

print (a[0:1]) #Intercept the first line, return [[1 2 3 4 5]] 
print (a[1,2:5]) #Intercept the second line, third and fourth columns, return [ 8 9]

print (a[1,:]) #Intercept the second line and return [ 6 7 8 9 10]
import numpy as np

// conditional interception
a = np.array([[1,2,3,4,5],[6,7,8,9,10]])
b = a[a>6] #Intercept elements greater than 6 in matrix a, the range is a one-dimensional array 
print (b) #Return [ 7 8 9 10]

#In fact, the Boolean statement first generates a Boolean matrix, and passes the Boolean matrix into [] (square brackets) to achieve interception 
print (a>6 ) #Return [ [ False 
 False False False False]

 [False  True  True  True  True]]
import numpy as np

a = np.array([[1,2,3,4,5],[6,7,8,9,10 ]])
 print (a)
 #The starting matrix is 
​​[[ 1 2 3 4 5 ]
 [ 6  7  8  9 10]]

a[a >6] = 0
 print (a)
 #The matrix is 
​​[[1 2 3 4 5 ] after clearing if greater than 6
 [6 0 0 0 0]]


matrix merge

import numpy as np

a1 = np.array([[1,2],[3,4]])
a2 = np.array([[5,6],[7,8]])

# ! Note that when the parameter is passed in, it should be passed in the form of a list list or tuple tuple 
print (np.hstack([a1,a2])) #Horizontal 
 merge , the return result is as follows 
[[1 2 5 6 ]
 [3 4 7 8]]

print (np.vstack((a1,a2))) #Vertical
 merge , the return result is as follows 
[[1 2 ]
 [3 4]
 [5 6]
 [7 8]]
Via function matrix

arange

copy code
import numpy as np

a = np.arange(10) #The default starts from 0 to 10 (excluding 10), and the step size is 1 
print (a) #Return [0 1 2 3 4 5 6 7 8 9] 

a1 = np.arange(5 ,10) #Start from 5 to 10 (excluding 10), step size is 1 
print (a1) #Return [5 6 7 8 9] 

a2 = np.arange(5,20,2) #Start from 5 to 20 (excluding 20), step size is 2 
print (a2) #return [ 5 7 9 11 13 15 17 19]
copy code

 linspace 

Linspace() is very similar to matlab's linspace. It is used to create a specified number of equally spaced sequences and actually generate an arithmetic sequence.

import numpy as np

a = np.linspace(0,10,7) #Generate an arithmetic sequence with the first digit being 0, the last digit being 10, and containing 7 numbers 
print (a) 
 #Result [ 0. 1.66666667 3.33333333 
5. 6.66666667 8.33333333 10. ]

 logspace 

linspace is used to generate arithmetic progressions, while logspace is used to generate arithmetic progressions. 
The following example is used to generate a proportional sequence of 5 numbers with the first digit 10 0 and the last digit 10 2 .

import numpy as np

a = np.logspace(0,2,5 )
 print (a)
 #result [ 1. 3.16227766 10. 31.6227766 
100. ]
 

 ones、zeros、eye、empty 

ones creates a matrix of all 1s 
zeros creates a matrix of all 0s 
eye creates a unit matrix 
empty creates an empty matrix (actually has values)

copy code
import numpy as np

a_ones = np.ones((3,4)) #Create a 3*4 matrix of all 1s 
print (a_ones) #Result
 [ [ 
1. 1. 1. 1 .]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]]

a_zeros = np.zeros((3,4)) #Create a 3*4 all-zero matrix 
print (a_zeros) #Result
 [ [ 0. 0. 0. 0.]

 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]

a_eye = np.eye(3) #Create a 3rd order identity matrix 
print (a_eye) #Result
 [ 1 
. 0. 0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]

a_empty = np.empty((3,4)) #Create a 3*4 empty matrix 
print (a_empty) #Result
 [ [ 
1.78006111e-306 -3.13259416e-294 4.71524461e-309 1.94927842e+289 ]
 [  2.10230387e-309   5.42870216e+294   6.73606381e-310   3.82265219e-297]
 [   6.24242356e-309 1.07034394e-296 2.12687797e+183 6.88703165e-315]]
copy code

 fromstring 

The fromstring() method can convert a string into an ndarray object. This method is useful when the string needs to be digitized, and the ascii code sequence of the string can be obtained.

a = " abcdef " 
b = np.fromstring(a,dtype=np.int8) #because a character is 8, so specify the dtype as np.int8 
print (b) #return [ 97 98 99 100 101 102]

 fromfunction 

The fromfunction() method can generate the elements of the matrix according to the row number and column number of the matrix. 
For example, to create a matrix, each element in the matrix is ​​the sum of the row and column numbers.

copy code
import numpy as np

def func(i,j): 
    return i+j

a = np.fromfunction(func,(5,6 )) 
 #The first parameter is the specified function, the second parameter is the list list or tuple tuple, indicating the size of the matrix 
print (a)
 #Return [ 
[ 0. 1 .2.3.4.5 .]
 [ 1.  2.  3.  4.  5.  6.]
 [ 2.  3.  4.  5.  6.  7.]
 [ 3.  4.  5.  6.  7.  8.]
 [ 4. 5. 6. 7. 8. 9 .]]
 #Note that the column numbers of the row numbers here all start from 0


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325856515&siteId=291194637