_
_
NumPy Cheatsheet¶
• Installation
| 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 |
| _ | |
| oder Grundlegende Befehle |
Array Creation¶
| Command | Description |
|---|---|
| INLINE_CODE_18 | Create 1D array from list |
| INLINE_CODE_19 | Create 2D array from nested list |
| INLINE_CODE_20 | Create 3×4 array filled with zeros |
| INLINE_CODE_21 | Create 2×3 array filled with ones |
| INLINE_CODE_22 | Create 3×3 array filled with value 7 |
| INLINE_CODE_23 | Create 2×2 uninitialized array (faster) |
| INLINE_CODE_24 | Create array [0, 2, 4, 6, 8] with step |
| INLINE_CODE_25 | Create 5 evenly spaced values from 0 to 1 |
| INLINE_CODE_26 | Create 3×3 identity matrix |
| INLINE_CODE_27 | Create zeros array with same shape as arr |
| INLINE_CODE_28 | Create 3×4 array with random values [0,1) |
| INLINE_CODE_29 | Create 3×4 array with random integers [0,10) |
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 |
| _ | |
| ### Grundlegende mathematische Operationen |
| 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) |
| _ | |
| / Fortgeschrittene Nutzung |
Linear Algebra¶
| 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 |
| _ | |
| ### Statistische Funktionen |
| 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)¶
| Command | Description |
|---|---|
| INLINE_CODE_114, INLINE_CODE_115, INLINE_CODE_116 | Trigonometric functions |
| INLINE_CODE_117, INLINE_CODE_118, INLINE_CODE_119 | Inverse trigonometric functions |
| INLINE_CODE_120 | Exponential (e^x) |
| INLINE_CODE_121 | Natural logarithm |
| INLINE_CODE_122, INLINE_CODE_123 | Base-10 and base-2 logarithms |
| INLINE_CODE_124 | Raise to power (element-wise) |
| INLINE_CODE_125, INLINE_CODE_126 | Round up/down to nearest integer |
| INLINE_CODE_127 | Round to specified decimals |
| INLINE_CODE_128 | Clip values to range [min, max] |
| INLINE_CODE_129 | Sign of elements (-1, 0, or 1) |
| INLINE_CODE_130 | Element-wise maximum of two arrays |
| INLINE_CODE_131 | Element-wise minimum of two arrays |
Random Number Generation¶
| Command | Description |
|---|---|
| INLINE_CODE_132 | Set random seed for reproducibility |
| INLINE_CODE_133 | Random floats in [0, 1) with shape (3, 4) |
| INLINE_CODE_134 | Random standard normal distribution |
| INLINE_CODE_135 | Random integers in [0, 10) |
| INLINE_CODE_136 | Random floats in [0, 1) |
| INLINE_CODE_137 | Random sample from array |
| INLINE_CODE_138 | Shuffle array in-place |
| INLINE_CODE_139 | Random permutation (returns copy) |
| INLINE_CODE_140 | Uniform distribution [0, 10) |
| INLINE_CODE_141 | Normal distribution (mean=0, std=1) |
| INLINE_CODE_142 | Exponential distribution |
| INLINE_CODE_143 | Binomial distribution |
| INLINE_CODE_144 | Create new random generator (modern API) |
| INLINE_CODE_145 | Generate random floats with new API |
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) |
Konfiguration
Datentypen¶
NumPy unterstützt verschiedene Datentypen zur Speicheroptimierung:
# 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)
Optionen drucken¶
Konfigurieren Sie, wie Arrays angezeigt werden:
# 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 Handling¶
Konfigurieren Sie, wie NumPy numerische Fehler behandelt:
# 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")
Random Number Generator Konfiguration¶
# 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
Häufige Anwendungsfälle
Use Case 1: Data Normalization¶
Normalisieren Sie Daten auf Nullmittel und Einheitsvarianz (Standardisierung):
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: Bildbearbeitung¶
Manipulate Bilder als NumPy-Arrays:
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: Zeitreihenanalyse¶
Berechnung der bewegten Durchschnittswerte und Statistiken:
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-Betriebe für maschinelles Lernen¶
Implementierung grundlegender neuronaler Netzoperationen:
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: Statistische Analyse¶
Durchführung von Hypothesentests und statistischen Berechnungen:
```python Import numping als np
Beispieldaten: Testergebnisse aus zwei Gruppen¶
Gruppe_a = np.array([85, 88, 90, 92, 87, 89, 91, 86, 88, 90]) Gruppe_b = np.array([78, 82, 80, 85, 83, 81, 84, 79, 82, 80])
Deskriptive Statistiken¶
print(f)Group A - Bedeutung: {np.mean(group_a):.2f}, Std: {np.std(group_a):.2f}") print(f)Group B - Bedeutung: {np.mean(group_b):.2f}, Std: {np.std(group_b):.2f}")
T-test (manuelle Berechnung)¶
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)
Geplante Standardabweichung¶
- 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) druck(f"T-statistic: {t_statistic:.3f}")
Korrelationsanalyse¶
Korrelation = np.corrcoef(group_a, group_b)[0, 1] print(f)Beschreibung: {korrelation:.3f}")
Bootstrap Vertrauen Intervall¶
n_bootstrap = 10000 Bootstrap_means = np.array([[]