numpy-best-practices▌
mindrally/skills · updated Apr 8, 2026
Expert guidelines for NumPy development, focusing on array programming, numerical computing, and performance optimization.
NumPy Best Practices
Expert guidelines for NumPy development, focusing on array programming, numerical computing, and performance optimization.
Code Style and Structure
- Write concise, technical Python code with accurate NumPy examples
- Prefer vectorized operations over explicit loops for performance
- Use descriptive variable names reflecting data content (e.g.,
weights,gradients,input_array) - Follow PEP 8 style guidelines for Python code
- Use functional programming patterns when appropriate
Array Creation and Manipulation
- Use appropriate array creation functions:
np.array(),np.zeros(),np.ones(),np.empty(),np.arange(),np.linspace() - Prefer
np.zeros()ornp.empty()for pre-allocation when array size is known - Use
np.concatenate(),np.vstack(),np.hstack()for combining arrays - Leverage broadcasting for operations on arrays with different shapes
Indexing and Slicing
- Use advanced indexing with boolean arrays for conditional selection
- Prefer views over copies when possible to save memory
- Use
np.where()for conditional element selection - Understand the difference between fancy indexing (creates copy) and basic slicing (creates view)
Data Types
- Specify appropriate data types explicitly using
dtypeparameter - Use
np.float32for memory-efficient computations when full precision is not needed - Be aware of integer overflow with fixed-size integer types
- Use
np.asarray()for type conversion without unnecessary copies
Performance Optimization
Vectorization
- Always prefer vectorized operations over Python loops
- Use NumPy universal functions (ufuncs) for element-wise operations
- Leverage
np.einsum()for complex tensor operations - Use
np.dot()or@operator for matrix multiplication
Memory Management
- Use
np.ndarray.flagsto check memory layout (C-contiguous vs Fortran-contiguous) - Prefer in-place operations with
outparameter when possible - Use memory-mapped arrays (
np.memmap) for large datasets - Be mindful of array copies vs views
Computation Efficiency
- Use
np.sum(),np.mean(),np.std()withaxisparameter for aggregations - Leverage
np.cumsum(),np.cumprod()for cumulative operations - Use
np.searchsorted()for efficient sorted array operations
Error Handling and Validation
- Validate input shapes and data types before computations
- Use assertions for dimension checking with informative messages
- Handle NaN and Inf values appropriately with
np.isnan(),np.isinf() - Use
np.errstate()context manager for controlling floating-point error handling
Random Number Generation
- Use
np.random.default_rng()for modern random number generation - Set seeds for reproducibility:
rng = np.random.default_rng(seed=42) - Prefer the new Generator API over legacy
np.randomfunctions - Use appropriate distributions:
rng.normal(),rng.uniform(),rng.choice()
Linear Algebra
- Use
np.linalgfor linear algebra operations - Leverage
np.linalg.solve()instead of computing inverse for linear systems - Use
np.linalg.eig(),np.linalg.svd()for decompositions - Check matrix condition with
np.linalg.cond()before inversion
Testing and Documentation
- Write unit tests using
pytestwithnp.testingassertions - Use
np.testing.assert_array_equal()for exact comparisons - Use
np.testing.assert_array_almost_equal()for floating-point comparisons - Include comprehensive docstrings following NumPy docstring format
Key Conventions
- Import as
import numpy as np - Use
snake_casefor variables and functions - Document array shapes in docstrings
- Profile code with
%timeitto identify bottlenecks
Ratings
4.7★★★★★67 reviews- ★★★★★Alexander Gill· Dec 24, 2024
Keeps context tight: numpy-best-practices is the kind of skill you can hand to a new teammate without a long onboarding doc.
- ★★★★★Hassan Sanchez· Dec 20, 2024
numpy-best-practices reduced setup friction for our internal harness; good balance of opinion and flexibility.
- ★★★★★Noor Taylor· Dec 16, 2024
numpy-best-practices has been reliable in day-to-day use. Documentation quality is above average for community skills.
- ★★★★★Nikhil Yang· Dec 8, 2024
Solid pick for teams standardizing on skills: numpy-best-practices is focused, and the summary matches what you get after install.
- ★★★★★Sakura Kim· Nov 15, 2024
numpy-best-practices has been reliable in day-to-day use. Documentation quality is above average for community skills.
- ★★★★★Noor Brown· Nov 11, 2024
Registry listing for numpy-best-practices matched our evaluation — installs cleanly and behaves as described in the markdown.
- ★★★★★Benjamin Shah· Nov 7, 2024
Keeps context tight: numpy-best-practices is the kind of skill you can hand to a new teammate without a long onboarding doc.
- ★★★★★Rahul Santra· Nov 3, 2024
Solid pick for teams standardizing on skills: numpy-best-practices is focused, and the summary matches what you get after install.
- ★★★★★James Zhang· Oct 26, 2024
We added numpy-best-practices from the explainx registry; install was straightforward and the SKILL.md answered most questions upfront.
- ★★★★★Pratham Ware· Oct 22, 2024
I recommend numpy-best-practices for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.
showing 1-10 of 67