Saltar a contenido

NumPy Cheatsheet

Instalación

Platform Command
Ubuntu/Debian INLINE_CODE_8
Ubuntu/Debian (pip) INLINE_CODE_9
macOS (pip) INLINE_CODE_10
macOS (Homebrew) INLINE_CODE_11
Windows (pip) INLINE_CODE_12
Anaconda (all platforms) INLINE_CODE_13
Specific version INLINE_CODE_14
With optimizations INLINE_CODE_15
Virtual environment INLINE_CODE_16
Verify installation INLINE_CODE_17

Comandos básicos

Array Creation

__TABLE_163_

Array Properties

Command Description
INLINE_CODE_30 Get dimensions of array (e.g., (3, 4))
INLINE_CODE_31 Get number of dimensions
INLINE_CODE_32 Get total number of elements
INLINE_CODE_33 Get data type of elements
INLINE_CODE_34 Get size of each element in bytes
INLINE_CODE_35 Get total bytes consumed by array
INLINE_CODE_36 Get transposed array
INLINE_CODE_37 Get length of first dimension

Array Indexing & Slicing

Command Description
INLINE_CODE_38 Access first element
INLINE_CODE_39 Access last element
INLINE_CODE_40 Slice elements at indices 1, 2, 3
INLINE_CODE_41 Get every other element
INLINE_CODE_42 Reverse array
INLINE_CODE_43 Access element at row 0, column 1 (2D)
INLINE_CODE_44 Slice rows 0-1, columns 1-2 (2D)
INLINE_CODE_45 Boolean indexing: get elements > 5
INLINE_CODE_46 Fancy indexing: get elements at indices 0, 2, 4
INLINE_CODE_47 Get indices where condition is True

Operaciones matemáticas básicas

Command Description
INLINE_CODE_48 Add scalar to all elements
INLINE_CODE_49 Multiply all elements by scalar
INLINE_CODE_50 Element-wise addition of arrays
INLINE_CODE_51 Element-wise multiplication
INLINE_CODE_52 Element-wise division
INLINE_CODE_53 Square all elements
INLINE_CODE_54 Square root of all elements
INLINE_CODE_55 Absolute value of all elements
INLINE_CODE_56 Sum of all elements
INLINE_CODE_57 Mean of all elements
INLINE_CODE_58 Standard deviation
INLINE_CODE_59 Minimum value
INLINE_CODE_60 Maximum value
INLINE_CODE_61 Index of minimum value
INLINE_CODE_62 Index of maximum value

Array Reshaping

Command Description
INLINE_CODE_63 Reshape to 3×4 (must have same total elements)
INLINE_CODE_64 Convert to 1D array (copy)
INLINE_CODE_65 Convert to 1D array (view, faster)
INLINE_CODE_66 Transpose array (swap dimensions)
INLINE_CODE_67 Add new dimension at specified axis
INLINE_CODE_68 Remove single-dimensional entries
INLINE_CODE_69 Resize array in-place (can change size)

Array Concatenation & Splitting

Command Description
INLINE_CODE_70 Concatenate arrays along existing axis
INLINE_CODE_71 Stack arrays vertically (row-wise)
INLINE_CODE_72 Stack arrays horizontally (column-wise)
INLINE_CODE_73 Stack 1D arrays as columns
INLINE_CODE_74 Split array into 3 equal parts
INLINE_CODE_75 Split array vertically into 2 parts
INLINE_CODE_76 Split array horizontally into 3 parts
INLINE_CODE_77 Split into 3 parts (allows unequal splits)

Advanced Usage

Algebra lineal

Command Description
INLINE_CODE_78 Dot product of two arrays
INLINE_CODE_79 Matrix multiplication (Python 3.5+)
INLINE_CODE_80 Matrix multiplication (explicit)
INLINE_CODE_81 Inverse of matrix
INLINE_CODE_82 Determinant of matrix
INLINE_CODE_83 Eigenvalues and eigenvectors
INLINE_CODE_84 Singular Value Decomposition
INLINE_CODE_85 Solve linear system Ax = b
INLINE_CODE_86 Compute vector/matrix norm
INLINE_CODE_87 Rank of matrix
INLINE_CODE_88 QR decomposition
INLINE_CODE_89 Cholesky decomposition
INLINE_CODE_90 Sum of diagonal elements
INLINE_CODE_91 Extract diagonal or create diagonal matrix

Funciones estadísticas

Command Description
INLINE_CODE_92 Median value
INLINE_CODE_93 Variance
INLINE_CODE_94 75th percentile
INLINE_CODE_95 0.75 quantile (same as 75th percentile)
INLINE_CODE_96 Correlation coefficient matrix
INLINE_CODE_97 Covariance matrix
INLINE_CODE_98 Compute histogram
INLINE_CODE_99 Count occurrences of each integer
INLINE_CODE_100 Weighted average
INLINE_CODE_101 Cumulative sum
INLINE_CODE_102 Cumulative product
INLINE_CODE_103 Discrete difference between consecutive elements

Broadcasting & Advanced Indexing

Command Description
INLINE_CODE_104 Broadcasting: add array to each row
INLINE_CODE_105 Explicitly broadcast array to shape
INLINE_CODE_106 Add new axis for broadcasting (e.g., INLINE_CODE_107)
INLINE_CODE_108 Take elements at specified indices
INLINE_CODE_109 Put values at specified indices
INLINE_CODE_110 Choose values based on conditions
INLINE_CODE_111 Choose elements from multiple arrays
INLINE_CODE_112 Select elements using boolean array
INLINE_CODE_113 Extract elements satisfying condition

Universal Functions (ufuncs)

__TABLE_172_

Random Number Generation

__TABLE_173_

Memory & Performance

Command Description
INLINE_CODE_146 Create deep copy of array
INLINE_CODE_147 Create view (shares memory with original)
INLINE_CODE_148 Convert array to different data type
INLINE_CODE_149 Return contiguous array in memory
INLINE_CODE_150 Get memory layout information
INLINE_CODE_151 Check if arrays share memory
INLINE_CODE_152 Check if arrays might share memory
INLINE_CODE_153 Get total bytes consumed
INLINE_CODE_154 Get size including overhead (import sys)

Configuración

Tipos de datos

NumPy admite varios tipos de datos para la optimización de la memoria:

# Integer types
np.int8      # -128 to 127
np.int16     # -32,768 to 32,767
np.int32     # -2^31 to 2^31-1
np.int64     # -2^63 to 2^63-1
np.uint8     # 0 to 255
np.uint16    # 0 to 65,535

# Float types
np.float16   # Half precision
np.float32   # Single precision
np.float64   # Double precision (default)

# Complex types
np.complex64  # Two 32-bit floats
np.complex128 # Two 64-bit floats

# Boolean
np.bool_     # True or False

# String types
np.str_      # Unicode string
np.bytes_    # Byte string

# Create array with specific dtype
arr = np.array([1, 2, 3], dtype=np.float32)

Configure cómo se muestran los arrays:

# Set print options
np.set_printoptions(
    precision=3,        # Decimal places
    suppress=True,      # Suppress scientific notation
    threshold=1000,     # Max elements before summarizing
    edgeitems=3,        # Items at start/end when summarizing
    linewidth=120       # Characters per line
)

# Example: suppress scientific notation
np.set_printoptions(suppress=True)
print(np.array([1e-10, 1e10]))  # [0. 10000000000.]

# Reset to defaults
np.set_printoptions()

Error de manipulación

Configure cómo NumPy maneja errores numéricos:

# Set error handling
np.seterr(
    divide='warn',    # Division by zero: 'ignore', 'warn', 'raise', 'call'
    over='warn',      # Overflow: 'ignore', 'warn', 'raise', 'call'
    under='ignore',   # Underflow
    invalid='warn'    # Invalid operation (e.g., sqrt(-1))
)

# Context manager for temporary settings
with np.errstate(divide='ignore'):
    result = np.array([1, 2]) / 0  # No warning in this block

# Check for errors after operations
np.seterr(all='ignore')
result = np.array([1]) / 0
if np.isinf(result).any():
    print("Infinity detected")

Configuración del generador de números aleatorios

# Legacy API (older code)
np.random.seed(42)

# Modern API (recommended)
rng = np.random.default_rng(seed=42)

# Use different algorithms
from numpy.random import PCG64, Philox, MT19937
rng = np.random.Generator(PCG64(seed=42))  # Default, fastest
rng = np.random.Generator(Philox(seed=42))  # Parallel streams
rng = np.random.Generator(MT19937(seed=42)) # Legacy compatibility

Common Use Cases

Use Case 1: Data Normalization

Normalizar los datos a cero media y varianza unitaria (estandarización):

import numpy as np

# Sample data
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=float)

# Standardize: (x - mean) / std
mean = np.mean(data, axis=0)
std = np.std(data, axis=0)
normalized = (data - mean) / std

print(normalized)
# [[-1.22474487 -1.22474487 -1.22474487]
#  [ 0.          0.          0.        ]
#  [ 1.22474487  1.22474487  1.22474487]]

# Min-Max normalization: (x - min) / (max - min)
min_val = np.min(data, axis=0)
max_val = np.max(data, axis=0)
normalized_minmax = (data - min_val) / (max_val - min_val)

Use Case 2: Image Processing

Manipular imágenes como arrays NumPy:

import numpy as np
from PIL import Image

# Load image as array
img = np.array(Image.open('photo.jpg'))
print(f"Shape: {img.shape}")  # (height, width, channels)

# Convert to grayscale
gray = np.mean(img, axis=2).astype(np.uint8)

# Crop image (top-left 100x100 pixels)
cropped = img[:100, :100]

# Flip image vertically
flipped = np.flipud(img)

# Rotate 90 degrees
rotated = np.rot90(img)

# Adjust brightness (add value to all pixels)
brighter = np.clip(img + 50, 0, 255).astype(np.uint8)

# Apply threshold
threshold = 128
binary = np.where(gray > threshold, 255, 0).astype(np.uint8)

# Save result
Image.fromarray(binary).save('processed.jpg')

Use Case 3: Time Series Analysis

Calcular promedios y estadísticas móviles:

import numpy as np

# Sample time series data
prices = np.array([100, 102, 101, 105, 107, 106, 108, 110, 109, 111])

# Simple moving average (window size 3)
window = 3
moving_avg = np.convolve(prices, np.ones(window)/window, mode='valid')
print(f"Moving average: {moving_avg}")

# Calculate returns (percentage change)
returns = np.diff(prices) / prices[:-1] * 100
print(f"Returns: {returns}")

# Cumulative returns
cumulative_returns = np.cumprod(1 + returns/100) - 1
print(f"Cumulative returns: {cumulative_returns}")

# Volatility (rolling standard deviation)
def rolling_std(arr, window):
    return np.array([np.std(arr[i:i+window]) for i in range(len(arr)-window+1)])

volatility = rolling_std(returns, window=3)
print(f"Volatility: {volatility}")

# Detect outliers (values > 2 std from mean)
mean = np.mean(prices)
std = np.std(prices)
outliers = np.abs(prices - mean) > 2 * std
print(f"Outliers at indices: {np.where(outliers)[0]}")

Use Case 4: Matrix Operations for Machine Learning

Implementar operaciones básicas de redes neuronales:

import numpy as np

# Initialize weights and biases
np.random.seed(42)
input_size, hidden_size, output_size = 4, 5, 3
W1 = np.random.randn(input_size, hidden_size) * 0.01
b1 = np.zeros((1, hidden_size))
W2 = np.random.randn(hidden_size, output_size) * 0.01
b2 = np.zeros((1, output_size))

# Sample input (batch of 10 samples)
X = np.random.randn(10, input_size)

# Forward pass
def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def softmax(x):
    exp_x = np.exp(x - np.max(x, axis=1, keepdims=True))
    return exp_x / np.sum(exp_x, axis=1, keepdims=True)

# Hidden layer
z1 = np.dot(X, W1) + b1
a1 = sigmoid(z1)

# Output layer
z2 = np.dot(a1, W2) + b2
a2 = softmax(z2)

print(f"Output shape: {a2.shape}")  # (10, 3)
print(f"Output probabilities sum to 1: {np.allclose(np.sum(a2, axis=1), 1)}")

# Calculate loss (cross-entropy)
y_true = np.array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0])  # True labels
y_one_hot = np.eye(output_size)[y_true]
loss = -np.mean(np.sum(y_one_hot * np.log(a2 + 1e-8), axis=1))
print(f"Loss: {loss}")

Use Case 5: Statistical Analysis

Realizar pruebas de hipótesis y cálculos estadísticos:

``python importación numpy como np

Datos de muestra: puntajes de prueba de dos grupos

group_a = np.array([85, 88, 90, 92, 87, 89, 91, 86, 88, 90]) group_b = np.array([78, 82, 80, 85, 83, 81, 84, 79, 82, 80])

Estadísticas descriptivas

print(f"Group A - Significado: {np.mean(group_a):.2f}, Std: {np.std(group_a):.2f}") print(f"Group B - Significa: {np.mean(group_b):.2f}, Std: {np.std(group_b):.2f}")

T-test (Cálculo manual) n_a, n_b = len(group_a), len(group_b) mean_a, mean_b = np.mean(group_a), np.mean(group_b) var_a, var_b = np.var(group_a, ddof=1), np.var(group_b, ddof=1)

Desviación estándar agrupada

pooled_std = np.sqrt(((n_a - 1) * var_a + (n_b - 1) * var_b) / (n_a + n_b - 2)) t_statistic = (mean_a - mean_b) / (pooled_std * np.sqrt(1/n_a + 1/n_b) print(f"T-statistic: {t_statistic:.3f}")

Análisis de correlación

correlación = np.corrcoef(group_a, group_b)[0, 1] print(f"Correlation: {correlation:.3f})

Intervalo de confianza de arranque

n_bootstrap = 10000 bootstrap_means = np.array([[