# NumPy - 矩阵类库

NumPy软件包含一个Matrix库 numpy.matlib 。该模块具有返回矩阵而不是ndarray 对象的函数。

## matlib.empty()

matlib.empty()函数返回一个新矩阵，而无需初始化条目。该函数采用以下参数。

`numpy.matlib.empty(shape, dtype, order)`
Sr.No.Parameter & 描述
1

shape

int 或 int 的元组定义了新矩阵的维度

2

Dtype

3

order

C或F

```import numpy.matlib
import numpy as np

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

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

## numpy.matlib.zeros()

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

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

## numpy.matlib.ones()

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

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

## numpy.matlib.eye()

`numpy.matlib.eye(n, M,k, dtype)`
Sr.No.Parameter & 描述
1

n

2

M

3

k

4

dtype

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

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

## numpy.matlib.identity()

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

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

```[[ 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()函数返回给定大小的矩阵，该矩阵填充有随机值。

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

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

```import numpy.matlib
import numpy as np

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

```[[1  2]
[3  4]]```
```import numpy.matlib
import numpy as np

j = np.asarray(i)
print j ```

```[[1  2]
[3  4]] ```
```import numpy.matlib
import numpy as np

k = np.asmatrix (j)
print k```

```[[1  2]
[3  4]]```

## 相关推荐

Linux内核技术实战课 -〔邵亚方 - 〕

## 视频推荐

2.8 numpy的 copy & deep copy