Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
numpy.trim_zeros() in Python
Next article icon

numpy.unique()finds the unique elements of an array. It is often used in data analysis to eliminate duplicate values and return only the distinct values in sorted order.Example:

Python
importnumpyasnpa=np.array([1,2,2,3,4,4,4])res=np.unique(a)print(res)

Output
[1 2 3 4]

Explanation: numpy.unique() removes duplicates and returns only the unique sorted values from the array.

Syntax

numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)

Parameter:

Parameter

Description

ar

Input array flattened if not 1-D unless axis is specified.

return_index

If True, returns indices of first occurrences of unique values.

return_inverse

If True, returns indices to reconstruct the original array.

return_counts

If True, returns the count of each unique value.

axis

Operates along the given axis and if None, the array is flattened.

Returns:

  • A sorted 1-D array of unique values.
  • Optional arrays depending on return_index, return_inverse, and return_counts.

Examples

Example 1: In this example, we use the return_counts=True parameter to get both the unique elements and how many times each value appears in the array.

Python
importnumpyasnpa=np.array([1,2,2,3,3,3])res,counts=np.unique(a,return_counts=True)print("Unique:",res)print("Counts:",counts)

Output
Unique: [1 2 3]Counts: [1 2 3]

Explanation: np.unique(a, return_counts=True) returns both unique values and how many times each occurs.

Example 2: In this example, we use the return_inverse=True parameter to get an array that can be used to reconstruct the original array using the unique values.

Python
importnumpyasnpa=np.array([3,1,2,1])unique,inverse=np.unique(a,return_inverse=True)print("Unique:",unique)print("Inverse:",inverse)

Output
Unique: [1 2 3]Inverse: [2 0 1 0]

Explanation: Inverse array contains indices such thatunique[inverse]reconstructs the original array.

Example 3:In this example, we use the return_index=Trueparameter to find the indices of the first occurrences of the unique values in the original array.

Python
importnumpyasnpa=np.array([4,3,3,2,1,2])unique,indices=np.unique(a,return_index=True)print("Unique:",unique)print("Indices:",indices)

Output
Unique: [1 2 3 4]Indices: [4 3 1 0]

Explanation: Indices array tells the index in the original array where each unique element first appeared.


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp