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 arrayFrom list.
import numpy as np
arr = np.array([1, 2, 3])
ArangeRange of values.
np.arange(0, 10, 2)  # [0, 2, 4, 6, 8]
Zeros/onesInitialized arrays.
np.zeros((3, 4))
np.ones((2, 3))
np.eye(3)
LinspaceEvenly spaced.
np.linspace(0, 1, 5)  # [0, 0.25, 0.5, 0.75, 1]

Array Operations

BroadcastingShape-agnostic ops.
arr + 10
arr * 2
arr + np.array([10, 20, 30])
Boolean maskingFilter with mask.
arr[arr > 3]
np.where(arr > 3, 'high', 'low')

Math & Statistics

StatsSum, mean, std.
arr.sum()
arr.mean()
arr.std()
arr.max()
arr.min()
Dot productMatrix multiplication.
np.dot(a, b)
a @ b  # Python 3.5+

Reshaping

ReshapeChange dimensions.
arr.reshape(2, 3)
arr.flatten()
arr.T  # transpose

Random

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