最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

numpy創(chuàng)建單位矩陣和對(duì)角矩陣的實(shí)例

 更新時(shí)間:2019年11月29日 09:35:23   投稿:jingxian  
今天小編就為大家分享一篇numpy創(chuàng)建單位矩陣和對(duì)角矩陣的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

在學(xué)習(xí)linear regression時(shí)經(jīng)常處理的數(shù)據(jù)一般多是矩陣或者n維向量的數(shù)據(jù)形式,所以必須對(duì)矩陣有一定的認(rèn)識(shí)基礎(chǔ)。

numpy中創(chuàng)建單位矩陣借助identity()函數(shù)。更為準(zhǔn)確的說(shuō),此函數(shù)創(chuàng)建的是一個(gè)n*n的單位數(shù)組,返回值的dtype=array數(shù)據(jù)形式。其中接受的參數(shù)有兩個(gè),第一個(gè)是n值大小,第二個(gè)為數(shù)據(jù)類型,一般為浮點(diǎn)型。單位數(shù)組的概念與單位矩陣相同,主對(duì)角線元素為1,其他元素均為零,等同于單位1。而要想得到單位矩陣,只要用mat()函數(shù)將數(shù)組轉(zhuǎn)換為矩陣即可。

>>> import numpy as np
>>> help(np.identity)
     
Help on function identity in module numpy:

identity(n, dtype=None)
  Return the identity array.
  
  The identity array is a square array with ones on
  the main diagonal.
  
  Parameters
  ----------
  n : int
    Number of rows (and columns) in `n` x `n` output.
  dtype : data-type, optional
    Data-type of the output. Defaults to ``float``.
  
  Returns
  -------
  out : ndarray
    `n` x `n` array with its main diagonal set to one,
    and all other elements 0.
  
  Examples
  --------
  >>> np.identity(3)
  array([[ 1., 0., 0.],
      [ 0., 1., 0.],
      [ 0., 0., 1.]])
>>> np.identity(5)
     
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.]])
>>> A = np.mat(np.identity(5))
     
>>> A
     
matrix([[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.]])


矩陣的運(yùn)算中還經(jīng)常使用對(duì)角陣,numpy中的對(duì)角陣用eye()函數(shù)來(lái)創(chuàng)建。eye()函數(shù)接受五個(gè)參數(shù),返回一個(gè)單位數(shù)組。第一個(gè)和第二個(gè)參數(shù)N,M分別對(duì)應(yīng)表示創(chuàng)建數(shù)組的行數(shù)和列數(shù),當(dāng)然當(dāng)你只設(shè)定一個(gè)值時(shí),就默認(rèn)了N=M。第三個(gè)參數(shù)k是對(duì)角線指數(shù),跟diagonal中的offset參數(shù)是一樣的,默認(rèn)值為0,就是主對(duì)角線的方向,上三角方向?yàn)檎?,下三角方向?yàn)樨?fù),可以取-n到+m的范圍。第四個(gè)參數(shù)是dtype,用于指定元素的數(shù)據(jù)類型,第五個(gè)參數(shù)是order,用于排序,有‘C'和‘F'兩個(gè)參數(shù),默認(rèn)值為‘C',為行排序,‘F'為列排序。返回值為一個(gè)單位數(shù)組。

>>> 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.]])

numpy中的diagonal()方法可以對(duì)n*n的數(shù)組和方陣取對(duì)角線上的元素,diagonal()接受三個(gè)參數(shù)。第一個(gè)offset參數(shù)是主對(duì)角線的方向,默認(rèn)值為0是主對(duì)角線,上三角方向?yàn)檎?,下三角方向?yàn)樨?fù),可以取-n到+n的范圍。第二個(gè)參數(shù)和第三個(gè)參數(shù)是在數(shù)組大于2維時(shí)指定一個(gè)2維數(shù)組時(shí)使用,默認(rèn)值axis1=0,axis2=1。

>>> help(A.diagonal)
     
Help on built-in function diagonal:

diagonal(...) method of numpy.matrix instance
  a.diagonal(offset=0, axis1=0, axis2=1)
  
  Return specified diagonals. In NumPy 1.9 the returned array is a
  read-only view instead of a copy as in previous NumPy versions. In
  a future version the read-only restriction will be removed.
  
  Refer to :func:`numpy.diagonal` for full documentation.
  
  See Also
  --------
  numpy.diagonal : equivalent function
>>> help(np.diagonal)
     
Help on function diagonal in module numpy:

diagonal(a, offset=0, axis1=0, axis2=1)
  Return specified diagonals.
  
  If `a` is 2-D, returns the diagonal of `a` with the given offset,
  i.e., the collection of elements of the form ``a[i, i+offset]``. If
  `a` has more than two dimensions, then the axes specified by `axis1`
  and `axis2` are used to determine the 2-D sub-array whose diagonal is
  returned. The shape of the resulting array can be determined by
  removing `axis1` and `axis2` and appending an index to the right equal
  to the size of the resulting diagonals.
  
  In versions of NumPy prior to 1.7, this function always returned a new,
  independent array containing a copy of the values in the diagonal.
  
  In NumPy 1.7 and 1.8, it continues to return a copy of the diagonal,
  but depending on this fact is deprecated. Writing to the resulting
  array continues to work as it used to, but a FutureWarning is issued.
  
  Starting in NumPy 1.9 it returns a read-only view on the original array.
  Attempting to write to the resulting array will produce an error.
  
  In some future release, it will return a read/write view and writing to
  the returned array will alter your original array. The returned array
  will have the same type as the input array.
  
  If you don't write to the array returned by this function, then you can
  just ignore all of the above.
  
  If you depend on the current behavior, then we suggest copying the
  returned array explicitly, i.e., use ``np.diagonal(a).copy()`` instead
  of just ``np.diagonal(a)``. This will work with both past and future
  versions of NumPy.
  
  Parameters
  ----------
  a : array_like
    Array from which the diagonals are taken.
  offset : int, optional
    Offset of the diagonal from the main diagonal. Can be positive or
    negative. Defaults to main diagonal (0).
  axis1 : int, optional
    Axis to be used as the first axis of the 2-D sub-arrays from which
    the diagonals should be taken. Defaults to first axis (0).
  axis2 : int, optional
    Axis to be used as the second axis of the 2-D sub-arrays from
    which the diagonals should be taken. Defaults to second axis (1).
  
  Returns
  -------
  array_of_diagonals : ndarray
    If `a` is 2-D, then a 1-D array containing the diagonal and of the
    same type as `a` is returned unless `a` is a `matrix`, in which case
    a 1-D array rather than a (2-D) `matrix` is returned in order to
    maintain backward compatibility.
    
    If ``a.ndim > 2``, then the dimensions specified by `axis1` and `axis2`
    are removed, and a new axis inserted at the end corresponding to the
    diagonal.
  
  Raises
  ------
  ValueError
    If the dimension of `a` is less than 2.
  
  See Also
  --------
  diag : MATLAB work-a-like for 1-D and 2-D arrays.
  diagflat : Create diagonal arrays.
  trace : Sum along diagonals.
  
  Examples
  --------
  >>> a = np.arange(4).reshape(2,2)
  >>> a
  array([[0, 1],
      [2, 3]])
  >>> a.diagonal()
  array([0, 3])
  >>> a.diagonal(1)
  array([1])
  
  A 3-D example:
  
  >>> a = np.arange(8).reshape(2,2,2); a
  array([[[0, 1],
      [2, 3]],
      [[4, 5],
      [6, 7]]])
  >>> a.diagonal(0, # Main diagonals of two arrays created by skipping
  ...      0, # across the outer(left)-most axis last and
  ...      1) # the "middle" (row) axis first.
  array([[0, 6],
      [1, 7]])
  
  The sub-arrays whose main diagonals we just obtained; note that each
  corresponds to fixing the right-most (column) axis, and that the
  diagonals are "packed" in rows.
  
  >>> a[:,:,0] # main diagonal is [0 6]
  array([[0, 2],
      [4, 6]])
  >>> a[:,:,1] # main diagonal is [1 7]
  array([[1, 3],
      [5, 7]])
>>> A = np.random.randint(low=5, high=30, size=(5, 5))
     
>>> A
     
array([[25, 15, 26, 6, 22],
    [27, 14, 22, 16, 21],
    [22, 17, 10, 14, 25],
    [11, 9, 27, 20, 6],
    [24, 19, 19, 26, 14]])
>>> A.diagonal()
     
array([25, 14, 10, 20, 14])
>>> A.diagonal(offset=1)
     
array([15, 22, 14, 6])
>>> A.diagonal(offset=-2)
     
array([22, 9, 19])

以上這篇numpy創(chuàng)建單位矩陣和對(duì)角矩陣的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • pycharm安裝漢化包失敗的問(wèn)題及解決

    pycharm安裝漢化包失敗的問(wèn)題及解決

    這篇文章主要介紹了pycharm安裝漢化包失敗的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 零基礎(chǔ)寫python爬蟲之爬蟲的定義及URL構(gòu)成

    零基礎(chǔ)寫python爬蟲之爬蟲的定義及URL構(gòu)成

    俗話說(shuō)工欲善其事必先利其器,作為本系列文章的第一篇,我們同樣也需要先利其器,先把爬蟲的定義以及寫爬蟲所需要的基礎(chǔ)知識(shí)先介紹給大家。
    2014-11-11
  • 關(guān)于 Python opencv 使用中的 ValueError: too many values to unpack

    關(guān)于 Python opencv 使用中的 ValueError: too many values to unpack

    這篇文章主要介紹了關(guān)于 Python opencv 使用中的 ValueError: too many values to unpack,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • 理解Python中的絕對(duì)路徑和相對(duì)路徑

    理解Python中的絕對(duì)路徑和相對(duì)路徑

    本篇文章主要介紹了理解Python中的絕對(duì)路勁和相對(duì)路徑 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • python列表list的index方法的用法和實(shí)例代碼

    python列表list的index方法的用法和實(shí)例代碼

    這篇文章主要介紹了python列表list的index方法的用法和實(shí)例代碼,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • Python類裝飾器實(shí)現(xiàn)方法詳解

    Python類裝飾器實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了Python類裝飾器實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python類裝飾器的相關(guān)概念、原理、實(shí)現(xiàn)方法與使用技巧,需要的朋友可以參考下
    2018-12-12
  • 最新評(píng)論

    财经| 邓州市| 石嘴山市| 时尚| 合川市| 阆中市| 峨眉山市| 汉沽区| 吴江市| 高阳县| 沅陵县| 永嘉县| 汉阴县| 中卫市| 临沧市| 都江堰市| 湘乡市| 庆城县| 教育| 永州市| 曲阜市| 蓬莱市| 佛冈县| 和田县| 哈密市| 临西县| 曲沃县| 永定县| 萍乡市| 沭阳县| 合阳县| 平安县| 乌什县| 扶风县| 张掖市| 永清县| 佳木斯市| 夏邑县| 雅江县| 宣化县| 舒兰市|