48th Numpy 常见数组

1.全0数组

np.zeros(shape, dtype=float, order='C')

指定长度的一维数组

>>> np.zeros(5)

array([ 0.,  0.,  0.,  0.,  0.])

二维数组

>>> np.zeros((2, 1))

array([[ 0.],

       [ 0.]])

指定类型

a=np.zeros((5), dtype=int)

2.全1矩阵  同理

np.ones()

3.单位矩阵

生成n×n单位矩阵:                    a=eye(n):

生成m×n单位矩阵:                   a=eye([m,n]) 或  a=eye(m,n)

生成与A大小相同的单位矩阵:  a=eye(size(A))

4.空矩阵

np.empty()

方法同全0矩阵

5.对角矩阵

np.diag([1,2,3])

3×3 对角线商是123的矩阵

np.diag([1,2,3],3)

3+3=6×6 对角线右偏移3个单位

另一种写法:

np.diag([1,2,3],k=3)

 6.特定值填充数组

np.full 构造一个数组,

用指定值填充其元素

.full(shape, fill_value, dtype=None, order='C')

shape:int 或者 int元组

fill_value:填充到数组中的值''

举例:

np.full((2,4),10)

out: array([[10, 10, 10, 10],

                     [10, 10, 10, 10]])

原文地址:https://www.cnblogs.com/erdou/p/11706393.html