numpy.eye

來源:https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.eye.html


numpy.eye

numpy.eye(N,?M=None,?k=0,?dtype=<class 'float'>,?order='C')?[source]

????????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.? ????????????????????????New in version 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.]])


Source Code 來源:https://github.com/numpy/numpy/blob/v1.15.1/numpy/lib/twodim_base.py#L140-L194

def eye(N, M=None, k=0, dtype=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.]])

? ? """

? ? if M is None:

? ? ? ? M = N

? ? m = zeros((N, M), dtype=dtype, order=order)

? ? if k >= M:

? ? ? ? return m

? ? if k >= 0:

? ? ? ? i = k

? ? else:

? ? ? ? i = (-k) * M

? ? m[:M-k].flat[i::M+1] = 1

? ? return m

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,817評論 0 10
  • 000. np.empty Parameters: shape : int or sequence of ints...
    龐貝船長閱讀 1,088評論 0 2
  • 基礎(chǔ)篇NumPy的主要對象是同種元素的多維數(shù)組。這是一個所有的元素都是一種類型、通過一個正整數(shù)元組索引的元素表格(...
    oyan99閱讀 5,286評論 0 18
  • 樓尖斑舊雕圖美,哥特房屋塞滿歐。 百歲民宅風(fēng)味厚,教堂肅穆史悠悠。 街石磚瓦風(fēng)霜長,無損無凹信步溜。 文化傳承城市...
    徐一村閱讀 420評論 0 5
  • 由于種種原因,簡書等第三方平臺博客不再保證能夠同步更新,歡迎移步 GitHub:https://github.co...
    萌面大道閱讀 591評論 10 2

友情鏈接更多精彩內(nèi)容