Numpy 的 unique(~)
方法返回一個 Numpy 數組,其中包含輸入數組的已排序唯一值。
參數
1. a
| array-like
輸入數組。
2. return_index
| boolean
| optional
是否返回唯一值的索引。默認情況下,return_index=False
。
3. return_inverse
| boolean
| optional
是否返回可用於重建輸入數組的索引。檢查下麵的示例以進行說明。默認情況下,return_inverse=False
。
4. return_counts
| boolean
| optional
是否返回每個值出現的次數。默認情況下,return_counts=False
。
5. axis
| int
或 None
| optional
沿著其尋找唯一值的軸。
軸 |
意義 |
---|---|
0 |
查找唯一的行 |
1 |
查找獨特的列 |
None |
尋找獨特的值 |
默認情況下,axis=None
。
返回值
包含輸入值的唯一值的 Numpy 數組。您還將獲得其他數組,具體取決於您是否將任何 return_
參數標記為 True
。
例子
基本用法
一維案例
要查找一維數組中的所有唯一值:
a = np.array([4,5,6,5])
np.unique(a)
array([4, 5, 6])
2D案例
考慮以下二維數組:
a = np.array([[4,5],[6,5]])
a
array([[4, 5],
[6, 5]])
要查找二維數組中的所有唯一值:
np.unique(a)
array([4, 5, 6])
獲取唯一值的索引
要獲取唯一值的索引:
a = np.array([4,5,6,5])
arr_unique_values, arr_index = np.unique(a, return_index=True)
print(arr_unique_values)
print(arr_index)
[4 5 6]
[0 1 2]
獲取逆索引
獲取逆索引:
a = np.array([4,5,6,5])
arr_unique_values, arr_inverse = np.unique(a, return_inverse=True)
print(arr_unique_values)
print(arr_inverse)
[4 5 6]
[0 1 2 1]
這裏,可以使用逆來重建原始值:
arr_unique_values[arr_inverse]
array([4, 5, 6, 5])
獲取計數
要獲取計數,請設置 return_counts=True
:
a = np.array([4,5,6,5])
arr_unique_values, arr_counts = np.unique(a, return_counts=True)
print(arr_unique_values)
print(arr_counts)
[4 5 6]
[1 2 1]
在這裏,我們看到值 5 在原始數組中出現了兩次。
查找唯一的行
考慮以下二維數組:
a = np.array([[4,5],[6,5],[4,5]])
a
array([[4, 5],
[6, 5],
[4, 5]])
在這裏,我們看到索引 0 和索引 2 處的行是重複的。要獲取所有唯一的行:
np.unique(a, axis=0)
array([[4, 5],
[6, 5]])
尋找獨特的列
考慮以下二維數組:
a = np.array([[4,5,4],[6,8,6]])
a
array([[4, 5, 4],
[6, 8, 6]])
在這裏,我們看到索引 0 和索引 2 處的列是重複的。要獲取所有唯一列:
np.unique(a, axis=1)
array([[4, 5],
[6, 8]])
相關用法
- Python Pandas unique方法用法及代碼示例
- Python unittest.mock.AsyncMock.assert_awaited_once_with用法及代碼示例
- Python unittest.TestCase.assertWarnsRegex用法及代碼示例
- Python unittest.mock.Mock.__class__用法及代碼示例
- Python unittest.TestCase.assertRaisesRegex用法及代碼示例
- Python unittest.mock.call用法及代碼示例
- Python unittest.mock.Mock.method_calls用法及代碼示例
- Python unittest.mock.Mock.call_args_list用法及代碼示例
- Python unittest.mock.AsyncMock.assert_any_await用法及代碼示例
- Python unittest.mock.Mock.assert_called用法及代碼示例
- Python unittest.TestCase.assertRaises用法及代碼示例
- Python unittest.TestCase.tearDownClass用法及代碼示例
- Python unittest.mock.Mock.assert_not_called用法及代碼示例
- Python unittest.IsolatedAsyncioTestCase用法及代碼示例
- Python unittest.TestCase.setUpClass用法及代碼示例
- Python unittest.mock.Mock.mock_calls用法及代碼示例
- Python unittest.mock.Mock.call_args用法及代碼示例
- Python unittest.mock.Mock.assert_has_calls用法及代碼示例
- Python unittest.mock.AsyncMock.assert_awaited_with用法及代碼示例
- Python unittest.mock.Mock.configure_mock用法及代碼示例
- Python unittest.mock.Mock.called用法及代碼示例
- Python unittest.mock.Mock.side_effect用法及代碼示例
- Python unittest.mock.Mock.assert_called_once_with用法及代碼示例
- Python unittest.mock.AsyncMock.assert_has_awaits用法及代碼示例
- Python unittest.mock.AsyncMock.assert_awaited_once用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | unique method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。