Numpy 的 diag(~)
方法返回輸入數組的對角線。
參數
1. a
| array-like
輸入數組。
2. k
| int
| optional
如果 k
為正,則返回頂部的下一個對角線。如果為負,則返回底部的對角線。默認情況下,k=0
。
返回值
輸入數組的對角線。
例子
考慮以下二維數組:
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
獲取主對角線:
np.diag(a)
array([1, 5, 9])
要使用 k=1
獲取對角線:
np.diag(a, k=1)
array([2, 6])
要使用 k=-2
獲取對角線:
np.diag(a, k=-2)
array([7])
相關用法
- Python NumPy diagflat方法用法及代碼示例
- Python NumPy diagonal方法用法及代碼示例
- Python distributed.protocol.serialize.register_generic用法及代碼示例
- Python distributed.get_task_metadata用法及代碼示例
- Python distributed.Client.gather用法及代碼示例
- Python distributed.recreate_tasks.ReplayTaskClient.recreate_task_locally用法及代碼示例
- Python distributed.diagnostics.plugin.SchedulerPlugin用法及代碼示例
- Python distributed.Client.ncores用法及代碼示例
- Python distributed.Client.retire_workers用法及代碼示例
- Python distributed.Client.unregister_worker_plugin用法及代碼示例
- Python distributed.fire_and_forget用法及代碼示例
- Python dir用法及代碼示例
- Python distributed.Client.set_metadata用法及代碼示例
- Python dictionary cmp()用法及代碼示例
- Python distributed.Client.scheduler_info用法及代碼示例
- Python distributed.Client.submit用法及代碼示例
- Python distributed.Client.compute用法及代碼示例
- Python distributed.SpecCluster.scale用法及代碼示例
- Python distributed.get_worker用法及代碼示例
- Python distributed.SpecCluster.scale_up用法及代碼示例
- Python difflib.unified_diff用法及代碼示例
- Python distributed.Client.nthreads用法及代碼示例
- Python distributed.comm.resolve_address用法及代碼示例
- Python distributed.Client.unpublish_dataset用法及代碼示例
- Python distributed.get_task_stream用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | diag method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。