Skip to main content
skill CX Codex

numpy-best-practices — Claude Code skill: NumPy rules for your agent

A skill that gives your agent NumPy ground rules: vectorize instead of looping, pick dtypes, watch memory, use np.linalg. Useful whenever Python code crunches numbers.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add Mindrally/skills --skill numpy-best-practices -a claude-code
Open on GitHub

What numpy-best-practices does

The skill is a set of instructions your agent picks up while writing or editing NumPy code. It covers style, array creation, indexing, dtypes, performance, error handling, random number generation, linear algebra and testing. There are no scripts or external tools inside, just text rules.

What changes in practice. The agent stops reaching for element-wise loops and writes vectorized operations and ufuncs instead. When the array size is known it allocates with np.zeros() or np.empty(). For linear systems it uses np.linalg.solve() rather than inverting a matrix. The rules also call out explicit dtype, integer overflow, the view-versus-copy distinction, and np.errstate() for floating-point error control. Random numbers go through np.random.default_rng(), not the legacy np.random functions.

The skill installs nothing into your project, so NumPy has to be present in your environment or the agent's examples will not run. For tests it points at pytest with assertions from np.testing. One thing to know: if you deliberately keep a Python loop over an array, the agent will keep pushing you toward vectorization, and you will have to say so explicitly.

What it helps with

  • rewrite an element-wise loop as a vectorized NumPy operation
  • choose a dtype and avoid integer overflow
  • cut down array copies to save memory
  • solve a linear system without inverting the matrix
  • get reproducible random numbers from the Generator API

Example prompts for the agent

  • Rewrite this loop over the array using NumPy vectorization
  • Review my NumPy code for unnecessary copies and wrong dtypes
  • Write pytest tests for this function using np.testing assertions

How to install numpy-best-practices

1Skills CLI (Vercel)

npx skills add Mindrally/skills --skill numpy-best-practices -a claude-code

2Manually: put the files in place

~/.claude/skills/numpy-best-practices/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/numpy-best-practices/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about numpy-best-practices

How do I install numpy-best-practices in Claude?

Run npx skills add Mindrally/skills --skill numpy-best-practices -a claude-code. The skill file then lands at ~/.claude/skills/numpy-best-practices/SKILL.md.

What does numpy-best-practices do?

It hands your agent a rulebook for NumPy work: vectorization, memory layout, dtypes, linear algebra, random number generation and testing. The agent applies it while writing or editing that code.

Does it need anything set up in my project?

The skill itself is only instructions. For the agent's examples to run, NumPy must be installed in your environment.

Source text

SKILL.md · Apache-2.0
---
name: numpy-best-practices
description: Best practices for NumPy array programming, numerical computing, and performance optimization in Python
metadata:
  maintainer: Mindrally
  source: https://github.com/Mindrally/skills
---

# 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

© Mindrally · Apache-2.0 · Mindrally/skills

Similar skills

Everything for AI and ML →

From the same set168

python-testing Guides the coding assistant to write Python tests using pytest, TDD principles, hypothesis, and mocks. Useful for unit and integration testing. CX Codex +1 1,000installs vuejs-typescript-best-practices Hands your agent a rulebook for Vue.js and TypeScript: Composition API, Pinia, VueUse, Tailwind, plus Vite build tuning. Useful once a Vue codebase outgrows one person's style. CX Codex +1 1,000installs drizzle-orm A skill that gives your agent Drizzle ORM conventions: schema as code, inferred types, queries and migrations. CX Codex +1 1,000installs elasticsearch-best-practices Provides rules and code patterns for Elasticsearch index mappings, shard planning, and search query optimization directly to your agent. CX Codex +1 999installs python This skill tells your coding agent how to write Python: PEP 8, type hints, early returns, async for I/O. Useful when the agent edits or generates code in a Python repo. CX Codex +1 983installs nlp-natural-language-processing Technical guidelines for natural language processing tasks using transformers, spaCy, and NLTK. Guides agents on proper tokenization, entity extraction, and vector search. CX Codex +1 980installs ui-design Gives the agent a checklist for interfaces: accessibility, responsive layout, performance. Useful when it writes or reviews frontend code. CX Codex +1 981installs internationalization-i18n This skill gives the agent a rulebook for internationalization: translations, locale-aware dates and numbers, RTL layout. Useful when an app ships in more than one language. CX Codex +1 978installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor