Numpy Set Ops

by cuba6112

testing

Set-theoretic operations for finding unique elements, membership testing, and array intersections. Triggers: unique, isin, intersect1d, setdiff1d, union1d.

Skill Details

Repository Files

4 files in this skill directory


name: numpy-set-ops description: Set-theoretic operations for finding unique elements, membership testing, and array intersections. Triggers: unique, isin, intersect1d, setdiff1d, union1d.

Overview

NumPy provides vectorized set operations for 1D arrays and multidimensional subarrays. These tools allow for deduplication, membership testing, and finding differences/intersections between datasets.

When to Use

  • Deduplicating rows in a large feature matrix.
  • Filtering a dataset to exclude a list of forbidden values.
  • Synchronizing two datasets by finding their intersection.
  • Compressing data by storing unique values and their index mappings.

Decision Tree

  1. Need to find non-duplicate elements?
    • Use np.unique.
  2. Need to reconstruct the original array from unique values?
    • Set return_inverse=True in np.unique.
  3. Checking if elements exist in another list?
    • Use np.isin(data, target_list).

Workflows

  1. Finding Unique Rows in a Dataset

    • Create a 2D array.
    • Call np.unique(arr, axis=0).
    • Inspect the result to see the deduplicated records.
  2. Reconstructing an Array from Sets

    • Call u, inv = np.unique(arr, return_inverse=True).
    • Store 'u' and 'inv' separately (useful for data compression).
    • Rebuild the original array using u[inv].
  3. Filtering by Membership

    • Define a 'forbidden' set of values.
    • Generate a boolean mask using ~np.isin(data, forbidden).
    • Filter the data: clean_data = data[mask].

Non-Obvious Insights

  • Flattening by Default: Set operations work on flattened 1D versions of input arrays unless an axis is explicitly specified.
  • NaN Handling: Like sorting, unique treats NaN as a value and sorts it to the end of the unique output.
  • Lexicographic Row Sort: When axis=0 is used in unique, the resulting unique rows are sorted lexicographically.

Evidence

  • "Returns the sorted unique elements of an array." Source
  • "isin(element, test_elements...)... broadcasting over element only." Source

Scripts

  • scripts/numpy-set-ops_tool.py: Routines for unique row detection and inverse reconstruction.
  • scripts/numpy-set-ops_tool.js: Simulated set intersection logic.

Dependencies

  • numpy (Python)

References

Related Skills

Dbt Transformation Patterns

Master dbt (data build tool) for analytics engineering with model organization, testing, documentation, and incremental strategies. Use when building data transformations, creating data models, or implementing analytics engineering best practices.

testingdocumenttool

Senior Data Scientist

World-class data science skill for statistical modeling, experimentation, causal inference, and advanced analytics. Expertise in Python (NumPy, Pandas, Scikit-learn), R, SQL, statistical methods, A/B testing, time series, and business intelligence. Includes experiment design, feature engineering, model evaluation, and stakeholder communication. Use when designing experiments, building predictive models, performing causal analysis, or driving data-driven decisions.

designtestingdata

Hypogenic

Automated hypothesis generation and testing using large language models. Use this skill when generating scientific hypotheses from datasets, combining literature insights with empirical data, testing hypotheses against observational data, or conducting systematic hypothesis exploration for research discovery in domains like deception detection, AI content detection, mental health analysis, or other empirical research tasks.

testingdata

Ux Researcher Designer

UX research and design toolkit for Senior UX Designer/Researcher including data-driven persona generation, journey mapping, usability testing frameworks, and research synthesis. Use for user research, persona creation, journey mapping, and design validation.

designtestingtool

Hypogenic

Automated LLM-driven hypothesis generation and testing on tabular datasets. Use when you want to systematically explore hypotheses about patterns in empirical data (e.g., deception detection, content analysis). Combines literature insights with data-driven hypothesis testing. For manual hypothesis formulation use hypothesis-generation; for creative ideation use scientific-brainstorming.

testingdata

Data Engineering Data Driven Feature

Build features guided by data insights, A/B testing, and continuous measurement using specialized agents for analysis, implementation, and experimentation.

testingdata

Dbt Transformation Patterns

Master dbt (data build tool) for analytics engineering with model organization, testing, documentation, and incremental strategies. Use when building data transformations, creating data models, or implementing analytics engineering best practices.

testingdocumenttool

Dashboard Design

USE THIS SKILL FIRST when user wants to create and design a dashboard, ESPECIALLY Vizro dashboards. This skill enforces a 3-step workflow (requirements, layout, visualization) that must be followed before implementation. For implementation and testing, use the dashboard-build skill after completing Steps 1-3.

designtestingworkflow

Ux Researcher Designer

UX research and design toolkit for Senior UX Designer/Researcher including data-driven persona generation, journey mapping, usability testing frameworks, and research synthesis. Use for user research, persona creation, journey mapping, and design validation.

designtestingtool

Performance Testing

Benchmark indicator performance with BenchmarkDotNet. Use for Series/Buffer/Stream benchmarks, regression detection, and optimization patterns. Target 1.5x Series for StreamHub, 1.2x for BufferList.

testing

Skill Information

Category:Technical
Last Updated:12/26/2025