Python ML Quick Reference

The NumPy, Pandas, and scikit-learn one-liners you reach for every day - organized by task so you spend less time searching and more time building.

This is the desk reference for the Python operations you use constantly in ML work. Organized by library and task, not alphabetically. Bookmark it and return when you need the syntax.

NumPy

Array Creation

python
import numpy as np np.zeros((3, 4)) # 3×4 array of zeros np.ones((3, 4)) # 3×4 array of ones np.eye(4) # 4×4 identity matrix np.arange(0, 10, 0.5) # [0.0, 0.5, 1.0, ..., 9.5] np.linspace(0, 1, 100) # 100 points from 0 to 1 np.random.seed(42) np.random.randn(100, 5) # 100×5 standard normal np.random.uniform(0, 1, (3, 4)) # uniform random np.random.randint(0, 10, (5,)) # random integers

Array Operations

python
a = np.array([1, 2, 3, 4, 5]) b = np.array([2, 3, 4, 5, 6]) a + b # element-wise addition a * b # element-wise multiplication a @ b # dot product np.dot(a, b) # same as above A = np.random.randn(3, 4) B = np.random.randn(4, 2) C = A @ B # matrix multiply → shape (3, 2) A.T # transpose A.reshape(2, 6) # reshape (must be same total size) A.flatten() # to 1D A[:, 0] # first column A[1, :] # second row A[:2, 1:3] # slice rows 0-1, cols 1-2

Useful Math Operations

python
np.sum(A) # sum all elements np.sum(A, axis=0) # sum along rows → shape (4,) np.sum(A, axis=1) # sum along columns → shape (3,) np.mean(A, axis=0) np.std(A, axis=0) np.max(A), np.argmax(A) # max value and its index np.sort(A, axis=0) np.argsort(A) # indices that would sort A np.exp(a) # element-wise exponent np.log(a) # natural log np.sqrt(a) np.abs(a) np.clip(a, 0, 1) # clamp to [0, 1] np.linalg.norm(a) # L2 norm np.linalg.norm(a, ord=1) # L1 norm np.linalg.inv(A) # matrix inverse np.linalg.eig(A) # eigenvalues and eigenvectors

Broadcasting

python
# Broadcasting lets you operate on arrays of different shapes a = np.array([1, 2, 3]) # shape (3,) b = np.array([[1], [2]]) # shape (2, 1) a + b # shape (2, 3) - broadcast # Normalize rows of a matrix X = np.random.randn(100, 10) X_norm = (X - X.mean(axis=0)) / X.std(axis=0)

Pandas

Loading Data

python
import pandas as pd df = pd.read_csv('data.csv') df = pd.read_csv('data.csv', index_col=0, parse_dates=['date_col']) df = pd.read_parquet('data.parquet') df = pd.read_json('data.json')

Exploration

python
df.head(10) # first 10 rows df.tail(5) # last 5 rows df.shape # (rows, cols) df.dtypes # data type of each column df.describe() # count, mean, std, quartiles df.info() # non-null counts + dtypes df.isnull().sum() # count of NaN per column df.nunique() # count of unique values per column df['col'].value_counts() # frequency of each value

Selection and Filtering

python
df['col'] # single column (Series) df[['col1', 'col2']] # multiple columns (DataFrame) df.loc[idx] # label-based row selection df.iloc[0:5] # integer-based row selection df.loc[df['age'] > 25] # filter rows by condition df.query('age > 25 and income > 50000') # SQL-like filter # Multiple conditions df[(df['age'] > 25) & (df['income'] > 50000)] df[df['city'].isin(['NYC', 'SF', 'LA'])]

Data Cleaning

python
df.dropna() # drop rows with any NaN df.dropna(subset=['col']) # drop only if 'col' is NaN df.fillna(0) # fill NaN with 0 df.fillna(df.mean()) # fill with column mean df['col'].fillna(method='ffill') # forward fill df.drop_duplicates() df.drop_duplicates(subset=['id']) df['col'] = df['col'].astype(float) df['date'] = pd.to_datetime(df['date']) df['col'] = df['col'].str.lower().str.strip() df.rename(columns={'old': 'new'})

Aggregation and GroupBy

python
df.groupby('category')['revenue'].sum() df.groupby('category').agg({'revenue': 'sum', 'count': 'count'}) df.groupby(['city', 'category']).mean() df.pivot_table(values='sales', index='month', columns='category', aggfunc='sum') # Apply a custom function df.groupby('category')['score'].apply(lambda x: x.quantile(0.9))

Merging

python
pd.merge(df1, df2, on='user_id', how='left') # left join pd.merge(df1, df2, on='user_id', how='inner') # inner join pd.merge(df1, df2, left_on='id', right_on='user_id') pd.concat([df1, df2], axis=0) # stack rows pd.concat([df1, df2], axis=1) # stack columns

scikit-learn

Data Splitting and Preprocessing

python
from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler, MinMaxScaler, LabelEncoder from sklearn.impute import SimpleImputer X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42, stratify=y ) scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) # use training stats imputer = SimpleImputer(strategy='mean') X_imputed = imputer.fit_transform(X)

Feature Engineering

python
from sklearn.preprocessing import OneHotEncoder, PolynomialFeatures from sklearn.feature_extraction.text import TfidfVectorizer enc = OneHotEncoder(sparse_output=False, handle_unknown='ignore') X_encoded = enc.fit_transform(X_categorical) poly = PolynomialFeatures(degree=2, include_bias=False) X_poly = poly.fit_transform(X) tfidf = TfidfVectorizer(max_features=10000, ngram_range=(1, 2)) X_text = tfidf.fit_transform(texts)

Training Models

python
from sklearn.linear_model import LogisticRegression, Ridge, Lasso from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier from sklearn.svm import SVC from sklearn.pipeline import Pipeline # Simple training model = RandomForestClassifier(n_estimators=100, random_state=42, n_jobs=-1) model.fit(X_train, y_train) preds = model.predict(X_test) proba = model.predict_proba(X_test)[:, 1] # positive class probability # Pipeline (prevents data leakage) pipe = Pipeline([ ('scaler', StandardScaler()), ('model', LogisticRegression(max_iter=1000)) ]) pipe.fit(X_train, y_train) pipe.predict(X_test)

Evaluation

python
from sklearn.metrics import ( accuracy_score, precision_score, recall_score, f1_score, roc_auc_score, classification_report, confusion_matrix, mean_squared_error, mean_absolute_error, r2_score ) # Classification print(classification_report(y_test, preds)) auc = roc_auc_score(y_test, proba) cm = confusion_matrix(y_test, preds) # Regression rmse = mean_squared_error(y_test, preds, squared=False) mae = mean_absolute_error(y_test, preds) r2 = r2_score(y_test, preds)

Cross-Validation and Hyperparameter Tuning

python
from sklearn.model_selection import cross_val_score, GridSearchCV, RandomizedSearchCV # 5-fold cross-validation scores = cross_val_score(model, X, y, cv=5, scoring='roc_auc') print(f"AUC: {scores.mean():.3f} ± {scores.std():.3f}") # Grid search param_grid = { 'n_estimators': [50, 100, 200], 'max_depth': [None, 5, 10], 'min_samples_leaf': [1, 2, 5] } gs = GridSearchCV(RandomForestClassifier(), param_grid, cv=5, scoring='roc_auc', n_jobs=-1) gs.fit(X_train, y_train) print(gs.best_params_) # Randomized search (better for large spaces) from scipy.stats import randint param_dist = {'n_estimators': randint(50, 500), 'max_depth': [None, 5, 10, 20]} rs = RandomizedSearchCV(model, param_dist, n_iter=20, cv=5, random_state=42) rs.fit(X_train, y_train)

Quick Recipes

Normalize embeddings

python
from numpy.linalg import norm embeddings_normalized = embeddings / norm(embeddings, axis=1, keepdims=True)

Most common values in a column

python
df['col'].value_counts().head(10)

Correlation heatmap

python
import seaborn as sns import matplotlib.pyplot as plt sns.heatmap(df.corr(), annot=True, fmt='.2f', cmap='coolwarm') plt.tight_layout() plt.show()

Save and load a scikit-learn model

python
import joblib joblib.dump(model, 'model.pkl') model = joblib.load('model.pkl')

Stratified k-fold for imbalanced datasets

python
from sklearn.model_selection import StratifiedKFold skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42) for train_idx, val_idx in skf.split(X, y): X_fold_train, X_fold_val = X[train_idx], X[val_idx] y_fold_train, y_fold_val = y[train_idx], y[val_idx]

Common Mistakes

Using df.iterrows() for feature computation. iterrows() iterates over DataFrame rows as Python objects, which is up to 100x slower than vectorized pandas or NumPy operations that operate on entire columns at once. For any operation that can be expressed as a column-level transformation (arithmetic, string operations, conditionals), use vectorized pandas methods or np.where. Reserve loops for logic that cannot be vectorized.

Forgetting to copy DataFrames before in-place modifications. When you assign a slice of a DataFrame to a new variable (df2 = df[df['col'] > 0]) you may get a view, not a copy. Modifying df2 can silently modify df, or raise a SettingWithCopyWarning that is easy to ignore but hides a real data integrity bug. Always call .copy() explicitly when you intend to modify a subset independently.

Mixing up fit vs fit_transform in sklearn pipelines. fit computes the transformation parameters (mean, std, PCA components) from the data. fit_transform computes and then applies them. The critical rule: call fit_transform on training data, then call transform only on validation and test data. Calling fit_transform on test data leaks test set statistics into the scaling parameters, producing optimistic evaluation results.

What to Practice Next

  • Find a loop in your own code that uses iterrows() or iterates over DataFrame rows; rewrite it as a vectorized operation and measure the speedup using %timeit in a Jupyter notebook.
  • Add .copy() to every slice assignment in a data preprocessing script and run it through pandas.options.mode.copy_on_write = True to surface any remaining silent modification bugs.
  • Build a minimal sklearn Pipeline with a StandardScaler and a LogisticRegression; verify that calling fit_transform on test data leaks by comparing model coefficients when the scaler is fit on train vs. fit on test.

Related Posts

More posts

Model Selection Guide: When to Use Which ML Algorithm

A practical decision framework for choosing the right machine learning algorithm - from linear models to gradient boosting to neural networks - based on your data, constraints, and goals.

#decision-tree#model-selection#reference#algorithms

Evaluation Metrics Guide: Which Metric to Use and When

Accuracy is rarely the right metric. This guide explains every major ML evaluation metric - classification, regression, ranking, and generation - with clear guidance on when to use each one.

#regression#evaluation#metrics#ranking#reference#classification

Math for ML: The Cheat Sheet Every Practitioner Needs

The math you actually use in ML - vectors, matrices, gradients, probability, and the key calculus rules - distilled into a single reference you can return to again and again.

#reference#mathematics#linear-algebra#probability#calculus