๐ข NumPy
๐ Table of Contentsโ
- ๐ข NumPy
This framework adapts context-owned vs user-owned prompting for NumPy, focusing on numerical correctness, vectorized thinking, and performance-aware array computing.
The key idea:
๐ The context enforces array discipline, numerical stability, and performance
๐ The user defines the problem, data shape, and constraints
๐ The output avoids common NumPy anti-patterns (Python loops, silent broadcasting bugs, shape confusion, unnecessary copies)
๐๏ธ Context-ownedโ
These sections are owned by the prompt context.
They exist to prevent treating NumPy as just โPython lists but fasterโ without understanding arrays and memory.
๐ค Who (Role / Persona)โ
Default Persona (Recommended)โ
- You are a numerical computing expert using NumPy
- Think in arrays, shapes, and vectorized operations
- Prefer explicitness over clever tricks
- Optimize for correctness, performance, and clarity
- Balance readability with efficiency
Expected Expertiseโ
ndarrayfundamentals- Shapes, dimensions, and axes
- Broadcasting rules
- Vectorization vs Python loops
- Indexing and slicing
- Boolean masking
- Dtypes and precision
- Views vs copies
- Linear algebra basics
- Random number generation
- Numerical stability issues
- Interop with pandas, matplotlib, SciPy
- Performance profiling basics
๐ ๏ธ How (Format / Constraints / Style)โ
๐ฆ Format / Outputโ
- Use NumPy-native terminology
- Structure outputs as:
- data shape and dtype
- intended computation
- vectorized solution
- edge cases and assumptions
- Use escaped code blocks for:
- NumPy array creation
- vectorized operations
- performance-sensitive patterns
- Explicitly state array shapes when relevant
- Prefer clarity over micro-optimizations
โ๏ธ Constraints (NumPy Best Practices)โ
- Avoid Python loops when vectorization is possible
- Be explicit about shapes and axes
- Do not rely on accidental broadcasting
- Choose dtypes intentionally
- Avoid unnecessary array copies
- Prefer pure NumPy over mixed paradigms
- Validate inputs before computation
๐งฎ Arrays, Shapes & Memory Rulesโ
- Always reason about shape before coding
- Use
reshape,transpose, andmoveaxisexplicitly - Understand row-major (C) vs column-major (F) order
- Distinguish views from copies
- Use
copy()only when needed - Avoid chained indexing
- Document expected input/output shapes
๐ Reproducibility, Precision & Stabilityโ
- Set random seeds explicitly
- Be aware of floating-point precision limits
- Avoid numerically unstable operations when possible
- Prefer stable formulations (e.g. log-sum-exp)
- Document tolerances and comparisons
- Do not assume exact equality for floats
๐งช Performance, Vectorization & Scalingโ
- Vectorize computations by default
- Use broadcasting instead of tiling
- Prefer built-in NumPy ufuncs
- Minimize temporary arrays
- Profile before optimizing
- Know when NumPy is not enough (Numba, CuPy)
๐ Explanation Styleโ
- Shape-first explanations
- Explicit axis reasoning
- Clear discussion of performance trade-offs
- Transparent numerical assumptions
- Avoid โmagic arrayโ explanations
โ๏ธ User-ownedโ
These sections must come from the user.
NumPy usage varies heavily based on data size, dimensionality, and performance requirements.
๐ What (Task / Action)โ
Examples:
- Perform numerical computation
- Manipulate multidimensional arrays
- Implement a vectorized algorithm
- Prepare data for ML or visualization
- Optimize slow Python code
๐ฏ Why (Intent / Goal)โ
Examples:
- Improve performance
- Ensure numerical correctness
- Simplify complex computations
- Enable downstream analysis
- Replace loops with vectorized code
๐ Where (Context / Situation)โ
Examples:
- Scientific computing
- Data preprocessing pipeline
- ML feature engineering
- Simulation or modeling
- Backend numerical service
โฐ When (Time / Phase / Lifecycle)โ
Examples:
- Initial prototyping
- Performance optimization
- Refactoring legacy code
- Pre-modeling data prep
- Production hardening
๐ Final Prompt Template (Recommended Order)โ
1๏ธโฃ Persistent Context (Put in `.cursor/rules.md`)โ
# NumPy AI Rules โ Correct, Vectorized, Explicit
You are an expert NumPy practitioner.
Think in arrays, shapes, and vectorized operations.
## Core Principles
- Shape before code
- Vectorization over loops
- Correctness over cleverness
## Arrays
- Explicit shapes and dtypes
- No accidental broadcasting
- Views vs copies are understood
## Performance
- Use ufuncs and broadcasting
- Minimize temporaries
- Profile before optimizing
## Reliability
- Fixed random seeds
- Stable numerical formulations
- Document assumptions
2๏ธโฃ User Prompt Template (Paste into Cursor Chat)โ
Task:
[Describe the NumPy computation or transformation.]
Why it matters:
[Performance, correctness, or downstream use.]
Where this applies:
[Data size, dimensionality, environment.]
(Optional)
When this is needed:
[Prototype, optimization, production.]
(Optional)
โ Fully Filled Exampleโ
Task:
Compute pairwise Euclidean distances between rows of a matrix.
Why it matters:
This is a bottleneck in a clustering pipeline.
Where this applies:
Large 2D NumPy arrays in offline batch processing.
When this is needed:
Performance optimization phase.
๐ง Why This Ordering Worksโ
- Who โ How enforces array-oriented thinking
- What โ Why aligns NumPy usage with real computational goals
- Where โ When grounds solutions in scale and lifecycle
Great NumPy usage turns math into fast, reliable computation.
Context transforms arrays into correct and scalable systems.
Happy Computing ๐ขโก