agentleFS
MindrallyMindrally/skills/numpy-best-practices/SKILL.md

numpy-best-practices

Skill275 starsChanged 52 days agoApache-2.0View on GitHub

Best practices for NumPy array programming, numerical computing, and performance optimization in Python

1 other project uses a version of this, 1 of them edited.

Adopted by

Most recently taken up by

The file

Show its rules and text

We split this skill into the rules it gives an agent

34 rules. About 0 are common, 10 are distinctive, and 24 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈10

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 24

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Write concise, technical Python code with accurate NumPy examples

    line 15
  2. Distinctive:

    Prefer vectorized operations over explicit loops for performance

    line 16
  3. Very specific:

    Use descriptive variable names reflecting data content (e.g., weights, gradients, input_array)

    line 17
  4. Distinctive:

    Follow PEP 8 style guidelines for Python code

    line 18
  5. Distinctive:

    Use functional programming patterns when appropriate

    line 19
  6. Very specific:

    Use appropriate array creation functions: np.array(), np.zeros(), np.ones(), np.empty(), np.arange(), np.linspace()

    line 23
  7. Very specific:

    Prefer np.zeros() or np.empty() for pre-allocation when array size is known

    line 24
  8. Very specific:

    Use np.concatenate(), np.vstack(), np.hstack() for combining arrays

    line 25
  9. Distinctive:

    Use advanced indexing with boolean arrays for conditional selection

    line 30
  10. Distinctive:

    Prefer views over copies when possible to save memory

    line 31
  11. Very specific:

    Use np.where() for conditional element selection

    line 32
  12. Very specific:

    Use np.float32 for memory-efficient computations when full precision is not needed

    line 38
  13. Very specific:

    Use np.asarray() for type conversion without unnecessary copies

    line 40
  14. Distinctive:

    Always prefer vectorized operations over Python loops

    line 46
  15. Distinctive:

    Use NumPy universal functions (ufuncs) for element-wise operations

    line 47
  16. Very specific:

    Use np.dot() or @ operator for matrix multiplication

    line 49
  17. Very specific:

    Use np.ndarray.flags to check memory layout (C-contiguous vs Fortran-contiguous)

    line 53
  18. Very specific:

    Prefer in-place operations with out parameter when possible

    line 54
  19. Very specific:

    Use memory-mapped arrays (np.memmap) for large datasets

    line 55
  20. Very specific:

    Use np.sum(), np.mean(), np.std() with axis parameter for aggregations

    line 60
  21. Very specific:

    Use np.searchsorted() for efficient sorted array operations

    line 62
  22. Distinctive:

    Use assertions for dimension checking with informative messages

    line 67
  23. Very specific:

    Use np.errstate() context manager for controlling floating-point error handling

    line 69
  24. Very specific:

    Use np.random.default_rng() for modern random number generation

    line 73
  25. Very specific:

    Prefer the new Generator API over legacy np.random functions

    line 75
  26. Very specific:

    Use appropriate distributions: rng.normal(), rng.uniform(), rng.choice()

    line 76
  27. Very specific:

    Use np.linalg for linear algebra operations

    line 80
  28. Very specific:

    Use np.linalg.eig(), np.linalg.svd() for decompositions

    line 82
  29. Very specific:

    Check matrix condition with np.linalg.cond() before inversion

    line 83
  30. Very specific:

    Write unit tests using pytest with np.testing assertions

    line 87
  31. Very specific:

    Use np.testing.assert_array_equal() for exact comparisons

    line 88
  32. Very specific:

    Use np.testing.assert_array_almost_equal() for floating-point comparisons

    line 89
  33. Very specific:

    Use snake_case for variables and functions

    line 95
  34. Distinctive:

    Document array shapes in docstrings

    line 96

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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() or np.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 dtype parameter
  • Use np.float32 for 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.flags to check memory layout (C-contiguous vs Fortran-contiguous)
  • Prefer in-place operations with out parameter 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() with axis parameter 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.random functions
  • Use appropriate distributions: rng.normal(), rng.uniform(), rng.choice()

Linear Algebra

  • Use np.linalg for 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 pytest with np.testing assertions
  • 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_case for variables and functions
  • Document array shapes in docstrings
  • Profile code with %timeit to identify bottlenecks

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.