Introduction and use of numpy.unique function in Python

Insert image description here

introduce

np.uniqueis a function in the NumPy library that is used to obtain unique values ​​from an array and can return some information about these unique values. The following is np.uniquea detailed introduction to the function:

grammar:

unique_values = np.unique(arr, return_index=False, return_inverse=False, return_counts=False, axis=None)
  • arr: The input array from which unique values ​​are to be extracted.
  • return_index: Optional parameter, if set to Truereturns an index array containing the index of the first occurrence of a unique value in the input array. Default is False.
  • return_inverse: Optional parameter, if set to Truereturns an integer array containing the index of each element in the original array into the array of unique values. This can be used to restore an array of unique values ​​back to the original array. Default is False.
  • return_counts: Optional parameter, if set to Truereturns an integer array containing the number of times each unique value occurs in the input array. Default is False.
  • axis: Optional parameter that specifies on which axis to find unique values. Defaults to if not specified, Nonemeaning to find unique values ​​in the entire array.

return value:

np.uniqueThe function returns a NumPy array containing unique values. Depending on the parameters set, it may also return one or more additional arrays, such as an index array, a reverse index array, and a count array.

Example:

import numpy as np

arr = np.array([3, 1, 2, 2, 3, 1, 4, 5, 5])

# 获取唯一值数组
unique_values = np.unique(arr)
# 输出: array([1, 2, 3, 4, 5])

# 获取唯一值的索引数组
unique_indices = np.unique(arr, return_index=True)[1]
# 输出: array([1, 2, 0, 6, 7], dtype=int64)

# 获取逆向索引数组,用于还原原始数组
inverse_indices = np.unique(arr, return_inverse=True)[1]
# 输出: array([2, 0, 1, 1, 2, 0, 3, 4, 4], dtype=int64)

# 获取唯一值的出现次数数组
value_counts = np.unique(arr, return_counts=True)[1]
# 输出: array([2, 2, 2, 1, 2], dtype=int64)

In the above example, np.uniquethe function is used to obtain a unique value and can optionally return the index, inverse index, and occurrence count, which information can be used in a variety of data processing and analysis tasks.

Supplement[1]

The purpose of the code value_counts = np.unique(arr, return_counts=True)[1]is to count arrthe number of occurrences of each unique value in an array and store the results in value_countsa variable named .

Let me explain each part of this code:

  1. np.unique(arr, return_counts=True): This part calls the NumPy np.uniquefunction and passes the parameters return_counts=True. This means that the function returns an array containing the unique values ​​and an array containing the number of occurrences of each unique value.

  2. [1]: This part extracts the second element from the result returned by the function. In Python, indexing of lists and arrays starts from 0, so [1]means getting the second element in the result, which is the array containing the number of occurrences.

So, the end result value_countsis a arrNumPy array containing the number of occurrences of each unique value in array . For example, if arris [3, 1, 2, 2, 3, 1, 4, 5, 5], then value_countsmay be [2, 2, 2, 1, 2], indicating that the number 1 appears twice, the number 2 appears twice, the number 3 appears twice, the number 4 appears once, and the number 5 appears twice. This information is useful for statistics and analyzing the frequency of values ​​in the data.

Guess you like

Origin blog.csdn.net/weixin_74850661/article/details/132816963