博客園LaTex的測試,附帶開啓方法

開啓方法

內容部分先不用管,我這邊只是作個測試,過幾天會講這些的,先忽略html


 

 

 

矩陣系列

1.建立特殊矩陣

1.1.建立全爲0的矩陣

np.zeros(tuple)python

$$\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)差很少app

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

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:ui

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

np.eye() 來定義(eye:眼睛)code

擴展:np.eye(rows,columns=rows)htm

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.]])
相關文章
相關標籤/搜索