微信扫一扫打赏支持

python机器学习库numpy---4.3、n维数组的创建(特殊数组)

python机器学习库numpy---4.3、n维数组的创建(特殊数组)

一、总结

一句话总结:

numpy中常用的特殊矩阵 主要有ones(全1)、zeros(全0)、eye(单位矩阵)、empty(未初始化)

 

 

 

二、n维数组的创建(特殊数组)

博客对应课程的视频位置:4.3、n维数组的创建(特殊数组)-范仁义-读书编程笔记
https://www.fanrenyi.com/video/38/344

 

4.3、n维数组的创建(特殊数组)

numpy中常用的特殊矩阵 主要有ones(全1)、zeros(全0)、eye(单位矩阵)、empty(未初始化)

In [2]:
# 需要一个表示矩阵维度的元组做参数
arr=np.ones((2,3))
print(arr)
[[1. 1. 1.]
 [1. 1. 1.]]
In [4]:
arr=np.zeros((3,2))
print(arr)
[[0. 0.]
 [0. 0.]
 [0. 0.]]
In [5]:
arr=np.eye(3)
print(arr)
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
In [6]:
help(np.eye)
Help on function eye in module numpy:

eye(N, M=None, k=0, dtype=<class 'float'>, order='C')
    Return a 2-D array with ones on the diagonal and zeros elsewhere.
    
    Parameters
    ----------
    N : int
      Number of rows in the output.
    M : int, optional
      Number of columns in the output. If None, defaults to `N`.
    k : int, optional
      Index of the diagonal: 0 (the default) refers to the main diagonal,
      a positive value refers to an upper diagonal, and a negative value
      to a lower diagonal.
    dtype : data-type, optional
      Data-type of the returned array.
    order : {'C', 'F'}, optional
        Whether the output should be stored in row-major (C-style) or
        column-major (Fortran-style) order in memory.
    
        .. versionadded:: 1.14.0
    
    Returns
    -------
    I : ndarray of shape (N,M)
      An array where all elements are equal to zero, except for the `k`-th
      diagonal, whose values are equal to one.
    
    See Also
    --------
    identity : (almost) equivalent function
    diag : diagonal 2-D array from a 1-D array specified by the user.
    
    Examples
    --------
    >>> np.eye(2, dtype=int)
    array([[1, 0],
           [0, 1]])
    >>> np.eye(3, k=1)
    array([[0.,  1.,  0.],
           [0.,  0.,  1.],
           [0.,  0.,  0.]])

In [7]:
arr=np.empty((4,6))
print(arr)
[[3.854e-321 3.854e-321 2.945e-321 2.945e-321 2.925e-321 2.925e-321]
 [3.814e-321 3.814e-321 4.763e-321 4.763e-321 4.427e-321 4.427e-321]
 [4.664e-321 4.664e-321 5.336e-321 5.336e-321 5.810e-321 5.810e-321]
 [5.830e-321 5.830e-321 5.850e-321 5.850e-321 4.545e-321 4.545e-321]]
In [ ]:
 
 
posted @ 2020-08-23 18:20  范仁义  阅读(361)  评论(0编辑  收藏  举报