We use cookies to understand how the site is used and to display ads. Analytics and advertising only run after you accept. You can change your choice anytime. Privacy policy

Skip to content
devvkit
$devvkit learn --librarie numpy-guide

NumPy Guide

[python][numerical][arrays]
Python
Install
pip install numpy

Core: ndarray: a homogeneous N-dimensional array object for fast numerical operations.

Vectorized operations run in C, 10-100x faster than Python loops.

Foundation for pandas, SciPy, scikit-learn, and TensorFlow.

Array Creation

Create array· From list.
import numpy as np
arr = np.array([1, 2, 3])
Arange· Range of values.
np.arange(0, 10, 2)  # [0, 2, 4, 6, 8]
Zeros/ones· Initialized arrays.
np.zeros((3, 4))
np.ones((2, 3))
np.eye(3)
Linspace· Evenly spaced.
np.linspace(0, 1, 5)  # [0, 0.25, 0.5, 0.75, 1]

Array Operations

Broadcasting· Shape-agnostic ops.
arr + 10
arr * 2
arr + np.array([10, 20, 30])
Boolean masking· Filter with mask.
arr[arr > 3]
np.where(arr > 3, 'high', 'low')

Math & Statistics

Stats· Sum, mean, std.
arr.sum()
arr.mean()
arr.std()
arr.max()
arr.min()
Dot product· Matrix multiplication.
np.dot(a, b)
a @ b  # Python 3.5+

Reshaping

Reshape· Change dimensions.
arr.reshape(2, 3)
arr.flatten()
arr.T  # transpose

Random

Random· Random numbers.
np.random.rand(3, 3)
np.random.randint(0, 10, size=5)
np.random.normal(0, 1, 100)