博客园LaTex的测试,附带开启方法

开启方法

内容部分先不用管,我这边只是做个测试,过几天会讲这些的,先忽略


 

 

 

矩阵系列

1.创建特殊矩阵

1.1.创建全为0的矩阵

np.zeros(tuple)

$$\begin{bmatrix} 0&0&0 \\ 0&0&0 \\ 0&0&0 \end{bmatrix}$$

In [1]:
import numpy as np
In [2]:
help(np.zeros)
 
Help on built-in function zeros in module numpy.core.multiarray:

zeros(...)
    zeros(shape, dtype=float, order='C')
    
    Return a new array of given shape and type, filled with zeros.
    
    Parameters
    ----------
    shape : int or sequence of ints
        Shape of the new array, e.g., ``(2, 3)`` or ``2``.
    dtype : data-type, optional
        The desired data-type for the array, e.g., `numpy.int8`.  Default is
        `numpy.float64`.
    order : {'C', 'F'}, optional
        Whether to store multidimensional data in C- or Fortran-contiguous
        (row- or column-wise) order in memory.
    
    Returns
    -------
    out : ndarray
        Array of zeros with the given shape, dtype, and order.
    
    See Also
    --------
    zeros_like : Return an array of zeros with shape and type of input.
    ones_like : Return an array of ones with shape and type of input.
    empty_like : Return an empty array with shape and type of input.
    ones : Return a new array setting values to one.
    empty : Return a new uninitialized array.
    
    Examples
    --------
    >>> np.zeros(5)
    array([ 0.,  0.,  0.,  0.,  0.])
    
    >>> np.zeros((5,), dtype=int)
    array([0, 0, 0, 0, 0])
    
    >>> np.zeros((2, 1))
    array([[ 0.],
           [ 0.]])
    
    >>> s = (2,2)
    >>> np.zeros(s)
    array([[ 0.,  0.],
           [ 0.,  0.]])
    
    >>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype
    array([(0, 0), (0, 0)],
          dtype=[('x', '<i4'), ('y', '<i4')])

In [3]:
# 一维
np.zeros(5) # 完整写法:np.zeros((5,))
Out[3]:
array([0., 0., 0., 0., 0.])
In [4]:
# 可以指定类型
np.zeros(5,dtype=int)
Out[4]:
array([0, 0, 0, 0, 0])
In [5]:
# 二维
np.zeros((2,5))
Out[5]:
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
In [6]:
# 三维 ==> 可以这么理解,2个2*5(2行5列)的矩阵
np.zeros((2,2,5))
Out[6]:
array([[[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]]])
In [7]:
################### 扩展部分 ########################
In [8]:
# 建议用元组,官方文档都是元组,而且shape返回类型就是元组
array1 = np.zeros([2,3])
print(array1)
type(array1)
print(array1.shape) # shape返回类型就是元组
 
[[0. 0. 0.]
 [0. 0. 0.]]
(2, 3)
 

1.2.创建全为1的矩阵

np.ones(tuple) 用法和np.zeros(tuple)差不多

$$\begin{bmatrix} 1&1&1 \\ 1&1&1 \\ 1&1&1 \end{bmatrix}$$

In [9]:
help(np.ones)
 
Help on function ones in module numpy.core.numeric:

ones(shape, dtype=None, order='C')
    Return a new array of given shape and type, filled with ones.
    
    Parameters
    ----------
    shape : int or sequence of ints
        Shape of the new array, e.g., ``(2, 3)`` or ``2``.
    dtype : data-type, optional
        The desired data-type for the array, e.g., `numpy.int8`.  Default is
        `numpy.float64`.
    order : {'C', 'F'}, optional
        Whether to store multidimensional data in C- or Fortran-contiguous
        (row- or column-wise) order in memory.
    
    Returns
    -------
    out : ndarray
        Array of ones with the given shape, dtype, and order.
    
    See Also
    --------
    zeros, ones_like
    
    Examples
    --------
    >>> np.ones(5)
    array([ 1.,  1.,  1.,  1.,  1.])
    
    >>> np.ones((5,), dtype=int)
    array([1, 1, 1, 1, 1])
    
    >>> np.ones((2, 1))
    array([[ 1.],
           [ 1.]])
    
    >>> s = (2,2)
    >>> np.ones(s)
    array([[ 1.,  1.],
           [ 1.,  1.]])

In [10]:
# 一维
np.ones(5) # 完整写法 np.ones((5,))
Out[10]:
array([1., 1., 1., 1., 1.])
In [11]:
# 可以指定类型
np.ones(5,dtype=int)
Out[11]:
array([1, 1, 1, 1, 1])
In [12]:
# 二维,传一个shape元组
np.ones((2,5))
Out[12]:
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])
In [13]:
# 三维 可以理解为两个二维数组
np.ones((2,2,5))
Out[13]:
array([[[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]],

       [[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]]])
 

1.3.单位矩阵

先普及一个数学基础:任何矩阵 x 单位矩阵 都等于其本身

单位矩阵是个方阵,从左上角到右下角的对角线(称为主对角线)上的元素均为1。其他全都为0,eg:

$$\begin{bmatrix} 1&0&0 \\ 0&1&0 \\ 0&0&1 \end{bmatrix}$$

np.eye() 来定义(eye:眼睛)

扩展:np.eye(rows,columns=rows)

In [14]:
help(np.eye)
 
Help on function eye in module numpy.lib.twodim_base:

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 [15]:
# 定义一个2行的单位矩阵(列默认和行一致)
np.eye(2)
Out[15]:
array([[1., 0.],
       [0., 1.]])
In [16]:
np.eye(3,dtype=int)
Out[16]:
array([[1, 0, 0],
       [0, 1, 0],
       [0, 0, 1]])
In [17]:
# 定义一个5行5列的单位矩阵
np.eye(5)
Out[17]:
array([[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.]])
posted @ 2018-07-09 15:28  鲲逸鹏  阅读(1325)  评论(0编辑  收藏  举报