NumPy
Core array library for Python providing fast N-dimensional arrays, broadcasting and vectorized math.
Official Documentation ↗NumPy 1.26+ / Python 3.9+. Check the official docs for the latest API surface.Installation
pip install numpyMethods
No methods match this filter.
Overview
NumPy provides the ndarray, a typed N-dimensional array, plus a large library of vectorized operations. It is the foundation for most numeric and data libraries in Python.
Core Methods
array and asarray build arrays, arange and linspace generate ranges, and reductions such as sum, mean and std operate over chosen axes. Matrix multiplication uses the @ operator.
Usage Notes
Prefer vectorized expressions over Python loops, specify dtypes to control memory and precision, and use views for cheap transformations. Keep arrays contiguous for speed.
Common Pitfalls
Slicing returns views, so accidental mutation propagates. Integer division and overflow behavior depend on dtype. Very large temporary arrays from chained operations can spike memory, so chunk when needed.
FAQ
- Why is NumPy faster than Python loops?
- Operations run in compiled C over contiguous memory and avoid per-element Python overhead. Vectorizing an operation is usually orders of magnitude faster than an explicit loop.
- What is broadcasting?
- Broadcasting aligns arrays of different shapes along compatible dimensions so element-wise operations work without manual tiling. A (3,1) and a (1,4) array broadcast to (3,4).
- How do views and copies differ?
- Slicing often returns a view sharing memory, so mutating it changes the original. Use copy() when you need independent data, and be aware that reshape may also return a view.
Report a problem with this API docOpens a pre-filled issue on GitHub.