Numpy

From G1Joshi/Agent-Skills · View interactive version

Numpy is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert NumPy numerical computing assistance covering ndarrays, broadcasting, linear algebra, vectorization, and memory views. Use when writing ultra-fast scientific calculations and array operations in Python.

How to use this skill

Skill file

NumPy

NumPy is the foundational library for scientific computing in Python, providing multidimensional array objects, vectorized mathematical operations, and unified C/C++ API bindings.

When to Use

Quick Start

import numpy as np

# Vectorized matrix operations with broadcasting
a = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)
b = np.array([10, 20, 30])

# Broadcast addition across rows
c = a + b
print("Result shape:", c.shape)
print("Row-wise sum:", np.sum(c, axis=1))

Core Concepts

Vectorization, Broadcasting & Masking

Eliminating slow Python loops through vector operations:

import numpy as np

# Fast random number generation with modern Generator API
rng = np.random.default_rng(seed=42)

# Generate 2D array of sensor readings
readings = rng.normal(loc=25.0, scale=3.0, size=(1000, 5))

# Broadcasting: subtract sensor baseline offsets (shape (5,)) from matrix (1000, 5)
baselines = np.array([24.0, 25.0, 24.5, 25.5, 26.0])
calibrated = readings - baselines

# Boolean masking: identify readings exceeding alert threshold
anomaly_mask = np.abs(calibrated) > 6.0
anomaly_count = np.count_nonzero(anomaly_mask)

print(f"Total readings: {readings.size}, Anomalies detected: {anomaly_count}")

Linear Algebra & Matrix Decomposition with np.linalg

Solving linear equations and singular value decomposition:

# System of equations: Ax = b
A = np.array([[3.0, 1.0], [1.0, 2.0]])
b = np.array([9.0, 8.0])

# Solve for x: [x0, x1]
x = np.linalg.solve(A, b)
print("Solution x:", x)

# Singular Value Decomposition (SVD)
U, S, Vt = np.linalg.svd(A)
print("Singular values:", S)

Memory Layouts: Views vs. Copies

Understanding strides and C-contiguous memory:

arr = np.arange(12, dtype=np.int32).reshape((3, 4))
print("C-Contiguous:", arr.flags['C_CONTIGUOUS'])

# Slice creates a view (shared memory buffer)
sub_view = arr[:2, :2]
sub_view[0, 0] = 999
print("Original modified via view:", arr[0, 0] == 999) # True

# Explicit copy allocates independent memory
safe_copy = arr[:2, :2].copy()
safe_copy[0, 0] = 0
print("Original unchanged by copy:", arr[0, 0] == 999) # True

Common Patterns

Efficient Memory Views and Strided Slicing

Problem: Unnecessary memory duplication when working with sub-arrays of large numerical datasets.

Solution:

Leverage NumPy non-copying array views and boolean indexing:

data = np.arange(1_000_000)

# Creates a view, not a memory copy
view = data[::2]
print(view.base is data) # True - shares identical memory buffer

# Fast boolean masking without Python loops
positive_values = data[data > 500_000]

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :----------------------------------------------------------------- | :------------------------------------------------------------- | :------------------------------------------------------------------------- |

| ValueError: operands could not be broadcast together with shapes | Incompatible array dimensions along non-singleton axes. | Align shapes using np.newaxis or .reshape(-1, 1). |

| MemoryError: Unable to allocate array with shape | Array size exceeds available system RAM in standard precision. | Use lower precision dtype (e.g. np.float32 or np.int16) or use memmap. |

| IndexError: too many indices for array | Accessing 2D index on 1D array. | Check array dimensions with arr.ndim and arr.shape. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.