本文簡要介紹 python 語言中 numpy.trace
的用法。
用法:
numpy.trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None)
返回沿數組對角線的總和。
如果a是二維的,返回給定偏移量沿對角線的和,即元素的和
a[i,i+offset]
對於所有我。如果 a 具有兩個以上的維度,則由 axis1 和 axis2 指定的軸用於確定返回其軌跡的二維子數組。結果數組的形狀與移除了axis1和axis2的a的形狀相同。
- a: array_like
輸入數組,從中獲取對角線。
- offset: 整數,可選
對角線與主對角線的偏移量。可以是正麵的也可以是負麵的。默認為 0。
- axis1, axis2: 整數,可選
用作對角線的二維子陣列的第一和第二軸的軸。默認值是 a 的前兩個軸。
- dtype: dtype,可選
確定返回數組和元素相加的累加器的數據類型。如果 dtype 的值為 None 並且 a 是精度小於默認整數精度的整數類型,則使用默認整數精度。否則,精度與 a 相同。
- out: ndarray,可選
放置輸出的數組。它的類型被保留,它必須具有正確的形狀來保存輸出。
- sum_along_diagonals: ndarray
如果 a 是二維的,則返回沿對角線的和。如果 a 具有更大的維度,則返回沿對角線的和數組。
參數:
返回:
例子:
>>> np.trace(np.eye(3)) 3.0 >>> a = np.arange(8).reshape((2,2,2)) >>> np.trace(a) array([6, 8])
>>> a = np.arange(24).reshape((2,2,2,3)) >>> np.trace(a).shape (2, 3)
相關用法
- Python numpy transpose用法及代碼示例
- Python numpy trapz用法及代碼示例
- Python numpy trim_zeros用法及代碼示例
- Python numpy tri用法及代碼示例
- Python numpy true_divide用法及代碼示例
- Python numpy triu_indices用法及代碼示例
- Python numpy triu用法及代碼示例
- Python numpy tril用法及代碼示例
- Python numpy tril_indices用法及代碼示例
- Python numpy trunc用法及代碼示例
- Python numpy testing.rundocs用法及代碼示例
- Python numpy testing.assert_warns用法及代碼示例
- Python numpy testing.assert_array_almost_equal_nulp用法及代碼示例
- Python numpy testing.assert_array_less用法及代碼示例
- Python numpy testing.assert_raises用法及代碼示例
- Python numpy testing.assert_almost_equal用法及代碼示例
- Python numpy tile用法及代碼示例
- Python numpy testing.assert_approx_equal用法及代碼示例
- Python numpy tanh用法及代碼示例
- Python numpy testing.assert_allclose用法及代碼示例
- Python numpy testing.decorators.slow用法及代碼示例
- Python numpy testing.suppress_warnings用法及代碼示例
- Python numpy take用法及代碼示例
- Python numpy testing.assert_string_equal用法及代碼示例
- Python numpy testing.run_module_suite用法及代碼示例
注:本文由純淨天空篩選整理自numpy.org大神的英文原創作品 numpy.trace。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。