Numpy 简明教程

NumPy - Matrix Library

NumPy 包含一个矩阵库 numpy.matlib 。此模块具有返回矩阵(而非 ndarray 对象)的函数。

NumPy package contains a Matrix library numpy.matlib. This module has functions that return matrices instead of ndarray objects.

matlib.empty()

matlib.empty() 函数返回一个新矩阵,而不初始化条目。此函数使用以下参数。

The matlib.empty() function returns a new matrix without initializing the entries. The function takes the following parameters.

numpy.matlib.empty(shape, dtype, order)

其中,

Where,

Sr.No.

Parameter & Description

1

shape int or tuple of int defining the shape of the new matrix

2

Dtype Optional. Data type of the output

3

order C or F

Example

import numpy.matlib
import numpy as np

print np.matlib.empty((2,2))
# filled with random data

它将生成如下输出:

It will produce the following output −

[[ 2.12199579e-314,   4.24399158e-314]
 [ 4.24399158e-314,   2.12199579e-314]]

numpy.matlib.zeros()

此函数返回填充有零的矩阵。

This function returns the matrix filled with zeros.

import numpy.matlib
import numpy as np
print np.matlib.zeros((2,2))

它将生成如下输出:

It will produce the following output −

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

numpy.matlib.ones()

此函数返回填充有 1 的矩阵。

This function returns the matrix filled with 1s.

import numpy.matlib
import numpy as np
print np.matlib.ones((2,2))

它将生成如下输出:

It will produce the following output −

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

numpy.matlib.eye()

此函数返回对角元素为 1、其他元素为 0 的矩阵。此函数使用以下参数。

This function returns a matrix with 1 along the diagonal elements and the zeros elsewhere. The function takes the following parameters.

numpy.matlib.eye(n, M,k, dtype)

其中,

Where,

Sr.No.

Parameter & Description

1

n The number of rows in the resulting matrix

2

M The number of columns, defaults to n

3

k Index of diagonal

4

dtype Data type of the output

Example

import numpy.matlib
import numpy as np
print np.matlib.eye(n = 3, M = 4, k = 0, dtype = float)

它将生成如下输出:

It will produce the following output −

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

numpy.matlib.identity()

numpy.matlib.identity() 函数返回给定大小的单位矩阵。单位矩阵是一个所有对角元素均为 1 的方阵。

The numpy.matlib.identity() function returns the Identity matrix of the given size. An identity matrix is a square matrix with all diagonal elements as 1.

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

它将生成如下输出:

It will produce the following output −

[[ 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()

numpy.matlib.rand() 函数返回一个填充了随机值的给定大小的矩阵。

The numpy.matlib.rand() function returns a matrix of the given size filled with random values.

Example

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

它将生成如下输出:

It will produce the following output −

[[ 0.82674464  0.57206837  0.15497519]
 [ 0.33857374  0.35742401  0.90895076]
 [ 0.03968467  0.13962089  0.39665201]]

Note 矩阵始终是二维的,而 ndarray 是一个 n 维数组。这两个对象是可相互转换的。

Note that a matrix is always two-dimensional, whereas ndarray is an n-dimensional array. Both the objects are inter-convertible.

Example

import numpy.matlib
import numpy as np

i = np.matrix('1,2;3,4')
print i

它将生成如下输出:

It will produce the following output −

[[1  2]
 [3  4]]

Example

import numpy.matlib
import numpy as np

j = np.asarray(i)
print j

它将生成如下输出:

It will produce the following output −

[[1  2]
 [3  4]]

Example

import numpy.matlib
import numpy as np

k = np.asmatrix (j)
print k

它将生成如下输出:

It will produce the following output −

[[1  2]
 [3  4]]