| name | NumPy |
| slug | numpy |
| version | 1.0.0 |
| homepage | https://clawic.com/skills/numpy |
| description | Write fast, memory-efficient numerical code with arrays, broadcasting, vectorization, and linear algebra. |
| metadata | {"clawdbot":{"emoji":"🔢","requires":{"bins":["python3"]},"os":["linux","darwin","win32"]}} |
Setup
On first use, read setup.md for integration guidelines. Creates ~/numpy/ to store preferences and snippets.
When to Use
User needs numerical computing in Python. Agent handles array operations, mathematical computations, linear algebra, and data manipulation with NumPy.
Architecture
Memory lives in ~/numpy/. See memory-template.md for structure.
~/numpy/
├── memory.md # Preferences + common patterns used
└── snippets/ # User's saved code patterns
Quick Reference
| Topic | File |
|---|
| Setup process | setup.md |
| Memory template | memory-template.md |
Core Rules
1. Vectorize First
Never use Python loops for array operations. NumPy's vectorized operations are 10-100x faster.
result = []
for x in arr:
result.append(x * 2)
result = arr * 2
2. Understand Broadcasting
Broadcasting allows operations on arrays of different shapes. Know the rules:
- Dimensions align from the right
- Size-1 dimensions stretch to match
- Missing dimensions treated as size-1
a = np.array([[1], [2], [3]])
b = np.array([10, 20, 30, 40])
result = a + b
3. Prefer Views Over Copies
Slicing returns views (same memory). Use .copy() only when needed.
b = a[::2]
b = a[::2].copy()
4. Use Appropriate Dtypes
Choose the smallest dtype that fits your data. Saves memory and speeds up computation.
arr = np.array(data, dtype=np.uint8)
arr = np.array(data, dtype=np.float32)
5. Axis Awareness
Most functions accept axis parameter. Know your axes:
axis=0: operate along rows (down columns)
axis=1: operate along columns (across rows)
axis=None or omit: operate on flattened array
arr = np.array([[1, 2], [3, 4]])
np.sum(arr, axis=0)
np.sum(arr, axis=1)
6. Leverage Built-in Functions
NumPy has optimized functions for common operations. Don't reinvent them.
| Need | Use |
|---|
| Element-wise math | np.sin, np.exp, np.log |
| Statistics | np.mean, np.std, np.median |
| Linear algebra | np.dot, np.linalg.* |
| Sorting | np.sort, np.argsort |
| Searching | np.where, np.searchsorted |
NumPy Traps
Shape Mismatches
a = np.array([1, 2, 3])
b = np.array([[1, 2, 3]])
c = np.array([[1], [2], [3]])
a.reshape(-1, 1)
a[np.newaxis, :]
Silent Type Coercion
arr = np.array([1, 2, 3])
arr[0] = 1.9
arr = np.array([1, 2, 3], dtype=np.float64)
View vs Copy Confusion
arr = np.array([1, 2, 3, 4, 5])
view = arr[1:4]
copy = arr[[1, 2, 3]]
Broadcasting Surprises
a = np.array([1, 2, 3])
b = np.array([1, 2])
a + b
a = np.zeros((3, 4))
b = np.array([1, 2, 3])
a + b
a + b.reshape(-1, 1)
In-Place Operations
np.sort(arr)
arr.sort()
arr = np.sort(arr)
Essential Patterns
Create Arrays
np.zeros((3, 4))
np.ones((3, 4))
np.full((3, 4), 7)
np.eye(3)
np.arange(0, 10, 2)
np.linspace(0, 1, 5)
np.random.rand(3, 4)
np.random.randn(3, 4)
Reshape and Stack
arr.reshape(2, 6)
arr.flatten()
arr.ravel()
np.concatenate([a, b])
np.stack([a, b])
np.vstack([a, b])
np.hstack([a, b])
Boolean Indexing
arr = np.array([1, 5, 3, 8, 2])
mask = arr > 3
arr[mask]
arr[arr > 3] = 0
np.where(arr > 3, 1, 0)
Linear Algebra
np.dot(a, b)
a @ b
np.linalg.inv(a)
np.linalg.det(a)
np.linalg.eig(a)
np.linalg.solve(a, b)
Security & Privacy
Data that stays local:
- All computations run locally
- Code patterns saved in ~/numpy/
This skill does NOT:
- Send data externally
- Access files outside ~/numpy/
- Require network connectivity
Related Skills
Install with clawhub install <slug> if user confirms:
data — data processing workflows
math — mathematical computations
statistics — statistical analysis
Feedback
- If useful:
clawhub star numpy
- Stay updated:
clawhub sync