本文簡要介紹 python 語言中 numpy.mask_indices
的用法。
用法:
numpy.mask_indices(n, mask_func, k=0)
給定掩碼函數,返回訪問 (n, n) 數組的索引。
認為mask_func是一個函數,對於大小為 a 的方陣
(n, n)
帶有可能的偏移量參數k, 當被稱為mask_func(a, k)
返回一個在某些位置有零的新數組(函數如numpy.triu或者numpy.tril正是這樣做)。然後此函數返回非零值所在的索引。- n: int
返回的索引對於訪問形狀為 (n, n) 的數組是有效的。
- mask_func: 可調用的
其調用簽名與以下類似的函數numpy.triu,numpy.tril。那是,
mask_func(x, k)
返回一個布爾數組,形狀如下x.k是函數的可選參數。- k: 標量
傳遞給的可選參數mask_func。函數類似於numpy.triu,numpy.tril采用被解釋為偏移量的第二個參數。
- indices: 數組的元組。
n對應於位置的索引數組
mask_func(np.ones((n, n)), k)
為真。
參數:
返回:
注意:
例子:
這些是允許您訪問任何 3x3 數組的上三角部分的索引:
>>> iu = np.mask_indices(3, np.triu)
例如,如果 a 是一個 3x3 數組:
>>> a = np.arange(9).reshape(3, 3) >>> a array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> a[iu] array([0, 1, 2, 4, 5, 8])
偏移量也可以傳遞給屏蔽函數。這讓我們從主索引的第一個對角線右側開始索引:
>>> iu1 = np.mask_indices(3, np.triu, 1)
我們現在隻提取三個元素:
>>> a[iu1] array([1, 2, 5])
相關用法
- Python numpy ma.indices用法及代碼示例
- Python numpy matrix.A1用法及代碼示例
- Python numpy ma.zeros用法及代碼示例
- Python numpy matrix.T用法及代碼示例
- Python numpy matrix.I用法及代碼示例
- Python numpy ma.diff用法及代碼示例
- Python numpy mat用法及代碼示例
- Python numpy ma.mask_rowcols用法及代碼示例
- Python numpy ma.where用法及代碼示例
- Python numpy ma.zeros_like用法及代碼示例
- Python numpy ma.notmasked_contiguous用法及代碼示例
- Python numpy ma.concatenate用法及代碼示例
- Python numpy ma.apply_along_axis用法及代碼示例
- Python numpy matrix.partition用法及代碼示例
- Python numpy ma.compress_rowcols用法及代碼示例
- Python numpy matrix.transpose用法及代碼示例
- Python numpy ma.vstack用法及代碼示例
- Python numpy ma.atleast_3d用法及代碼示例
- Python numpy ma.count用法及代碼示例
- Python numpy matrix.itemsize用法及代碼示例
- Python numpy ma.fix_invalid用法及代碼示例
- Python numpy ma.mean用法及代碼示例
- Python numpy ma.argmax用法及代碼示例
- Python numpy matrix.newbyteorder用法及代碼示例
- Python numpy matrix.sort用法及代碼示例
注:本文由純淨天空篩選整理自numpy.org大神的英文原創作品 numpy.mask_indices。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。