What does np.unique do, and how do parameters like return_counts and return_index work?

Quick Overview

Asks what NumPy's np.unique returns and how the return_counts, return_index and return_inverse flags and the axis parameter work. Tests whether you can read the extra arrays correctly and use them for frequency tables, label encoding and order-preserving deduplication.

What does np.unique do, and how do parameters like return_counts and return_index work?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

What does `np.unique` do, and how do parameters like `return_counts` and `return_index` work? ```hint Think in terms of a sort Picture the input sorted. Where each run of equal values starts, how long each run is, and where every original element ended up are the three pieces of bookkeeping the flags return. ``` ### What a Strong Answer Covers - What the default call returns, including the ordering and what happens to a multi-dimensional input - What `return_index`, `return_inverse` and `return_counts` each return, and the order of the returned arrays - How the inverse indices rebuild the input - Unique rows with `axis`, and how NaN values are handled - Typical uses such as frequency tables, label encoding and keeping the order of first appearance ### Follow-up Questions - How would you list the distinct values in the order they first appear? - How would you use the outputs to encode string labels as integers and decode them back? - What does `np.unique` cost in time, and when would `pd.unique` or `value_counts` be the better tool? - How would you find the most frequent value, and which value wins a tie?

Overview: Asks what NumPy's np.unique returns and how the return_counts, return_index and return_inverse flags and the axis parameter work. Tests whether you can read the extra arrays correctly and use them for frequency tables, label encoding and order-preserving deduplication.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

What does np.unique do, and how do parameters like return_counts and return_index work?

What a Strong Answer Covers Guidance

  • What the default call returns, including the ordering and what happens to a multi-dimensional input
  • What return_index , return_inverse and return_counts each return, and the order of the returned arrays
  • How the inverse indices rebuild the input
  • Unique rows with axis , and how NaN values are handled
  • Typical uses such as frequency tables, label encoding and keeping the order of first appearance

Follow-up Questions Guidance

  • How would you list the distinct values in the order they first appear?
  • How would you use the outputs to encode string labels as integers and decode them back?
  • What does np.unique cost in time, and when would pd.unique or value_counts be the better tool?
  • How would you find the most frequent value, and which value wins a tie?