Skip to content

np.unique() - Find unique elements in an array #50

Description

@TheWolf313

Syntax:
np.unique(ar, return_index=False, return_inverse=False, return_counts=False)

ar: Input array.

return_index: If True, also return the indices of the input array that give the unique values.

return_inverse: If True, also return the indices to reconstruct the original array from the unique array.

return_counts: If True, also return the number of times each unique value comes up.

Example:
import numpy as np
unique_vals = np.unique(np.array([1, 2, 2, 3, 4, 4, 4]))
print(unique_vals)

[1 2 3 4]

import numpy as np
arr = np.array([1, 2, 2, 3, 4, 4, 4])
values, counts = np.unique(arr, return_counts=True)
print("Unique values:", values)
print("Counts:", counts)

Unique values: [1 2 3 4]
Counts: [1 2 1 3]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions