Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Working with categorical variables, database identifiers, and discrete groups frequently requires set-theoretic operations—such as finding distinct values, computing intersections between user cohorts, and testing membership across master lists.
NumPy provides high-performance 1D set operations implemented on sorted arrays, executing orders of magnitude faster than standard Python set objects.
np.unique()np.unique(ar) finds all unique elements in an array and returns them in sorted order:
Output:
np.unique():np.unique() provides three powerful optional parameters:
return_counts=True: Returns the frequency count of each distinct element.return_index=True: Returns the index of the first occurrence of each unique element.return_inverse=True: Returns integer indices to reconstruct the original array from the unique values (categorical label encoding!).Output:
np.isin()To check whether elements in an array belong to a reference target list or cohort, use np.isin(element, test_elements). It returns a boolean array of the same shape:
NumPy implements standard mathematical set algebra on 1D arrays:
np.unique() return the distinct elements?np.unique() returns the occurrence frequency of each distinct value?np.intersect1d(arr1, arr2) compute?catalog = np.array([10, 20, 30, 40]), what is the output of np.isin(catalog, [20, 99])?return_inverse=True in np.unique() provide?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.