本文簡要介紹 python 語言中 numpy.diag
的用法。
用法:
numpy.diag(v, k=0)
提取對角線或構造對角行數組。
如果您使用此函數提取對角線並希望寫入結果數組,請參閱
numpy.diagonal
的更詳細文檔;它是否返回副本或視圖取決於您使用的 numpy 版本。- v: array_like
如果 v 是二維數組,則返回其 k-th 對角線的副本。如果 v 是一維數組,則返回一個二維數組,其中 v 在 k-th 對角線上。
- k: 整數,可選
有問題的對角線。默認值為 0。對主對角線上方的對角線使用 k>0,對主對角線下方的對角線使用 k<0。
- out: ndarray
提取的對角線或構造的對角行數組。
參數:
返回:
例子:
>>> x = np.arange(9).reshape((3,3)) >>> x array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
>>> np.diag(x) array([0, 4, 8]) >>> np.diag(x, k=1) array([1, 5]) >>> np.diag(x, k=-1) array([3, 7])
>>> np.diag(np.diag(x)) array([[0, 0, 0], [0, 4, 0], [0, 0, 8]])
相關用法
- Python numpy diagonal用法及代碼示例
- Python numpy diagflat用法及代碼示例
- Python numpy diag_indices用法及代碼示例
- Python numpy divmod用法及代碼示例
- Python numpy divide用法及代碼示例
- Python numpy digitize用法及代碼示例
- Python numpy disp用法及代碼示例
- Python numpy diff用法及代碼示例
- Python numpy dtype.isbuiltin用法及代碼示例
- Python numpy dtype.shape用法及代碼示例
- Python numpy dtype.ndim用法及代碼示例
- Python numpy dtype.alignment用法及代碼示例
- Python numpy dtype用法及代碼示例
- Python numpy dtype.names用法及代碼示例
- Python numpy dtype.__class_getitem__用法及代碼示例
- Python numpy dtype.flags用法及代碼示例
- Python numpy dtype.fields用法及代碼示例
- Python numpy dtype.subdtype用法及代碼示例
- Python numpy delete用法及代碼示例
- Python numpy dtype.descr用法及代碼示例
- Python numpy dec.setastest用法及代碼示例
- Python numpy datetime_as_string用法及代碼示例
- Python numpy dtype.kind用法及代碼示例
- Python numpy dtype.metadata用法及代碼示例
- Python numpy dsplit用法及代碼示例
注:本文由純淨天空篩選整理自numpy.org大神的英文原創作品 numpy.diag。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。