NumPy matrix library functions


chapter


NumPy comprising a matrix library NumPy.matlib, a function module for processing the matrix rather than ndarray object.

NumPy in, ndarray n-dimensional arrays may be, different from this, the matrix is ​​always two-dimensional, but the two objects can be converted to each other.

matlib.empty()

empty()The function returns a new matrix, but does not initialize the matrix elements.

numpy.matlib.empty(shape, dtype, order)
  • shape defining a new matrix shape or int int tuple
  • dtype Optionally, the specified data type matrix
  • the Order C or F

Examples

import numpy.matlib 
import numpy as np 

a = np.matlib.empty((2,2))
print (a)

Export

[[6.91241356e-310 1.37748664e-316]
 [6.91240378e-310 6.91240378e-310]]

You can see, the matrix elements are random values.

numpy.matlib.zeros()

zeros()The function returns a new matrix, the matrix elements are initialized to zero.

Examples

import numpy.matlib 
import numpy as np 

a = np.matlib.zeros((2,2))
print (a)

Export

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

numpy.matlib.ones()

zeros()The function returns a new matrix, the matrix elements are initialized to one.

Examples

import numpy.matlib 
import numpy as np 

a = np.matlib.zeros((2,2))
print (a)

Export

[[ 1.  1.] 
 [ 1.  1.]] 

numpy.matlib.eye()

eye()The function returns a matrix of element 1 are on the diagonal, the rest are zero. This function accepts the following arguments.

numpy.matlib.eye(n, M, k, dtype)
  • n Returns the number of rows in the matrix
  • M number of columns, as n
  • k diagonal starting index
  • dtype Data Matrix type

Examples

import numpy.matlib 
import numpy as np 

print('对角线的开始索引为0:')
print(np.matlib.eye(n = 3, M = 4, k = 0, dtype = float))
print('\n')

print('对角线的开始索引为1:')
print(np.matlib.eye(n = 3, M = 4, k = 1, dtype = float))
print('\n')

Export

对角线的开始索引为0:
 [[1. 0. 0. 0.]
  [0. 1. 0. 0.]
  [0. 0. 1. 0.]]

对角线的开始索引为1:
[[0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]

numpy.matlib.identity()

identity()Function returns the size of the matrix. Matrix is ​​a diagonal elements are all square 1.

Examples

import numpy.matlib 
import numpy as np 
print (np.matlib.identity(5, dtype = float))

Export

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

## numpy.matlib.rand()

rand()The function returns a matrix of specified size, wherein the filling random values.

Examples

import numpy.matlib 
import numpy as np 
print (np.matlib.rand(3,3))

Export

[[0.5413199  0.5749519  0.19755942]
 [0.57128833 0.24267348 0.65186677]
 [0.08517    0.9238393  0.15061818]]

Guess you like

Origin www.cnblogs.com/jinbuqi/p/11356116.html