AI / ML›DS & AI Cheat Sheet
Data Science & AI Cheat Sheet
Every function, method, and syntax you need — Pandas, NumPy, SQL, Excel, Matplotlib, Scikit-Learn, PyTorch, and LangGraph. All in one searchable reference.
8 Libraries
400+ Functions
Live Search
Pandas
DataFrame manipulation, cleaning, aggregation, and I/O
I/O — Reading & Writing
| Function | Description | Example |
|---|---|---|
| pd.read_csv() | Read CSV file into DataFrame | df = pd.read_csv('data.csv') |
| pd.read_excel() | Read Excel file into DataFrame | df = pd.read_excel('data.xlsx') |
| pd.read_json() | Read JSON into DataFrame | df = pd.read_json('data.json') |
| pd.read_sql() | Read SQL query into DataFrame | df = pd.read_sql(query, conn) |
| pd.read_parquet() | Read Parquet file | df = pd.read_parquet('data.parquet') |
| df.to_csv() | Write DataFrame to CSV | df.to_csv('out.csv', index=False) |
| df.to_excel() | Write DataFrame to Excel | df.to_excel('out.xlsx') |
| df.to_parquet() | Write DataFrame to Parquet | df.to_parquet('out.parquet') |
| df.to_dict() | Convert DataFrame to dict | d = df.to_dict(orient='records') |
Inspection & Exploration
| Function | Description | Example |
|---|---|---|
| df.head(n) | First n rows (default 5) | df.head(10) |
| df.tail(n) | Last n rows | df.tail(5) |
| df.info() | Column types, nulls, memory | df.info() |
| df.describe() | Summary stats (count, mean…) | df.describe(include='all') |
| df.shape | Tuple of (rows, columns) | rows, cols = df.shape |
| df.dtypes | Data type of each column | df.dtypes |
| df.columns | List of column names | list(df.columns) |
| df.nunique() | Count of unique values per col | df.nunique() |
| df.value_counts() | Frequency count | df['city'].value_counts(normalize=True) |
| df.memory_usage() | Memory used per column | df.memory_usage(deep=True) |
| df.sample(n) | Random sample of n rows | df.sample(5, random_state=42) |
Selection & Filtering
| Function | Description | Example |
|---|---|---|
| df['col'] | Select single column (Series) | df['age'] |
| df[['a','b']] | Select multiple columns | df[['name', 'age']] |
| df.loc[row, col] | Label-based indexing | df.loc[0:5, 'age'] |
| df.iloc[row, col] | Integer-based indexing | df.iloc[0:5, 1:3] |
| df.at[row, col] | Single value by label | df.at[0, 'name'] |
| df.iat[row, col] | Single value by position | df.iat[0, 1] |
| df.query(expr) | Filter using string expression | df.query('age > 30 & city=="NY"') |
| df[df.col > val] | Boolean mask filter | df[df['age'] > 30] |
| df.where(cond) | Keep values where True, else NaN | df.where(df > 0) |
| df.filter() | Filter by column/row labels | df.filter(like='price') |
Cleaning & Transformation
| Function | Description | Example |
|---|---|---|
| df.isnull() | Boolean mask of null values | df.isnull().sum() |
| df.dropna() | Drop rows with any null | df.dropna(subset=['age']) |
| df.fillna(val) | Fill nulls with value/method | df.fillna({'age': 0, 'name': 'N/A'}) |
| df.interpolate() | Interpolate missing numeric vals | df['price'].interpolate() |
| df.duplicated() | Boolean mask of duplicate rows | df.duplicated(subset=['id']) |
| df.drop_duplicates() | Remove duplicate rows | df.drop_duplicates(keep='first') |
| df.rename() | Rename columns/index | df.rename(columns={'old':'new'}) |
| df.astype() | Cast column to new type | df['age'].astype(int) |
| df.replace() | Replace values | df.replace({'Y':1, 'N':0}) |
| df.clip() | Clip values to min/max | df['score'].clip(0, 100) |
| df.drop() | Drop rows or columns | df.drop(columns=['col1','col2']) |
| df.reset_index() | Reset row index | df.reset_index(drop=True) |
| df.set_index() | Set a column as index | df.set_index('id') |
Aggregation, Merging & Reshaping
| Function | Description | Example |
|---|---|---|
| df.sort_values() | Sort by column values | df.sort_values('age', ascending=False) |
| df.groupby() | Group rows & aggregate | df.groupby('city')['salary'].mean() |
| df.agg() | Apply multiple aggregations | df.groupby('dept').agg({'sal':['mean','max']}) |
| df.pivot_table() | Create a pivot table | pd.pivot_table(df, values='sal', index='dept') |
| df.melt() | Wide to long format | df.melt(id_vars=['id'], value_vars=['q1','q2']) |
| df.pivot() | Long to wide format | df.pivot(index='date', columns='item', values='val') |
| df.apply() | Apply function along axis | df['bmi'].apply(lambda x: 'high' if x>25 else 'ok') |
| df.map() | Map values via dict/function | df['grade'].map({'A':4,'B':3,'C':2}) |
| df.transform() | Transform keeping shape | df.groupby('dept')['sal'].transform('mean') |
| df.merge() | SQL-style join two DataFrames | pd.merge(df1, df2, on='id', how='left') |
| pd.concat() | Stack DataFrames row/colwise | pd.concat([df1, df2], ignore_index=True) |
| df.assign() | Add/overwrite columns fluently | df.assign(full_name=df.first+' '+df.last) |
| df.eval() | Evaluate expression string | df.eval('profit = revenue - cost') |
| df.explode() | Explode list column to rows | df.explode('tags') |
| df.value_counts() | Frequency count | df['city'].value_counts(normalize=True) |
| pd.cut() | Bin continuous data | pd.cut(df['age'], bins=[0,18,65,100]) |
| pd.get_dummies() | One-hot encode categoricals | pd.get_dummies(df['color']) |
| df.crosstab() | Cross-tabulation of two cols | pd.crosstab(df['dept'], df['gender']) |
String Methods (df.str.*)
| Function | Description | Example |
|---|---|---|
| df.str.lower() | Lowercase all strings | df['name'].str.lower() |
| df.str.upper() | Uppercase all strings | df['name'].str.upper() |
| df.str.strip() | Strip whitespace | df['name'].str.strip() |
| df.str.contains() | Check if string contains pattern | df['email'].str.contains('@gmail') |
| df.str.replace() | Replace substring | df['text'].str.replace('old','new') |
| df.str.split() | Split string to list | df['full_name'].str.split(' ', expand=True) |
| df.str.len() | Length of each string | df['name'].str.len() |
| df.str.extract() | Extract regex groups | df['phone'].str.extract(r'(\d{3})') |
| df.str.startswith() | Check prefix | df['col'].str.startswith('pre') |
DateTime Methods (df.dt.*)
| Function | Description | Example |
|---|---|---|
| pd.to_datetime() | Parse string to datetime | pd.to_datetime(df['date']) |
| dt.year / .month / .day | Extract date components | df['date'].dt.year |
| dt.dayofweek | Day of week (0=Mon) | df['date'].dt.dayofweek |
| dt.hour / .minute | Extract time components | df['ts'].dt.hour |
| df.resample() | Resample time series | df.resample('M').mean() |
| df.shift() | Shift values by periods | df['price'].shift(1) |
| df.diff() | Difference between rows | df['price'].diff() |
| df.rolling(n) | Rolling window calculation | df['price'].rolling(7).mean() |
| df.expanding() | Expanding window | df['price'].expanding().mean() |
| df.ewm() | Exponentially weighted mean | df['price'].ewm(span=7).mean() |
NumPy
Array creation, manipulation, math, and linear algebra
Array Creation
| Function | Description | Example |
|---|---|---|
| np.array() | Create array from list/tuple | np.array([[1,2],[3,4]]) |
| np.zeros(shape) | Array of zeros | np.zeros((3, 4)) |
| np.ones(shape) | Array of ones | np.ones((3, 4), dtype=float) |
| np.full(shape, val) | Fill array with value | np.full((2,3), 7) |
| np.eye(n) | Identity matrix | np.eye(4) |
| np.arange() | Evenly spaced int range | np.arange(0, 10, 2) |
| np.linspace() | Evenly spaced floats | np.linspace(0, 1, 100) |
| np.random.rand() | Uniform random [0, 1) | np.random.rand(3, 4) |
| np.random.randn() | Standard normal random | np.random.randn(100) |
| np.random.randint() | Random integers | np.random.randint(0, 10, size=(3,3)) |
| np.random.seed() | Set random seed | np.random.seed(42) |
Shape & Manipulation
| Function | Description | Example |
|---|---|---|
| arr.reshape() | Reshape without copying | arr.reshape(20, 20) |
| arr.ravel() | Flatten to 1D (view) | arr.ravel() |
| arr.flatten() | Flatten to 1D (copy) | arr.flatten() |
| arr.T / arr.transpose() | Transpose axes | arr.T |
| np.expand_dims() | Add new axis | np.expand_dims(arr, axis=0) |
| np.squeeze() | Remove size-1 axes | np.squeeze(arr) |
| np.concatenate() | Join arrays along axis | np.concatenate([a, b], axis=0) |
| np.stack() | Stack along new axis | np.stack([a, b], axis=1) |
| np.hstack() | Stack horizontally | np.hstack([a, b]) |
| np.vstack() | Stack vertically | np.vstack([a, b]) |
| np.split() | Split array | np.split(arr, 3) |
Math & Statistics
| Function | Description | Example |
|---|---|---|
| np.sum() | Sum all or along axis | np.sum(arr, axis=0) |
| np.mean() | Mean | np.mean(arr, axis=1) |
| np.median() | Median | np.median(arr) |
| np.std() | Standard deviation | np.std(arr, ddof=1) |
| np.min() / np.max() | Min / Max value | arr.min(axis=0) |
| np.argmin() / argmax() | Index of min / max | np.argmin(arr) |
| np.cumsum() | Cumulative sum | np.cumsum(arr) |
| np.abs() | Absolute value | np.abs(arr) |
| np.sqrt() | Square root | np.sqrt(arr) |
| np.exp() | Exponential (e^x) | np.exp(arr) |
| np.log() | Natural log | np.log(arr) |
| np.dot() | Dot product / matrix mul | np.dot(A, B) |
| np.matmul() / @ | Matrix multiplication | A @ B |
| np.where() | Conditional select | np.where(arr > 0, arr, 0) |
| np.unique() | Unique values + counts | np.unique(arr, return_counts=True) |
| np.sort() | Sort array | np.sort(arr, axis=0) |
| np.percentile() | Percentile value | np.percentile(arr, 75) |
| np.corrcoef() | Correlation matrix | np.corrcoef(x, y) |
Linear Algebra
| Function | Description | Example |
|---|---|---|
| np.linalg.inv() | Matrix inverse | np.linalg.inv(A) |
| np.linalg.det() | Determinant | np.linalg.det(A) |
| np.linalg.eig() | Eigenvalues & eigenvectors | vals, vecs = np.linalg.eig(A) |
| np.linalg.svd() | Singular Value Decomposition | U, S, V = np.linalg.svd(A) |
| np.linalg.norm() | Vector / matrix norm | np.linalg.norm(arr) |
| np.linalg.solve() | Solve linear system Ax=b | x = np.linalg.solve(A, b) |
| np.linalg.pinv() | Pseudo-inverse | np.linalg.pinv(A) |
SQL
Querying, aggregation, joins, window functions, and CTEs
Basic Querying
| Syntax | Description | Example |
|---|---|---|
| SELECT col1, col2 | Choose columns to return | SELECT name, age FROM users |
| SELECT DISTINCT | Remove duplicates | SELECT DISTINCT city FROM customers |
| WHERE condition | Filter rows | WHERE salary > 50000 AND dept = 'Eng' |
| IN (list) | Match any value in list | WHERE country IN ('US','UK','CA') |
| BETWEEN a AND b | Range filter (inclusive) | WHERE date BETWEEN '2024-01-01' AND '2024-12-31' |
| LIKE pattern | Pattern matching | WHERE email LIKE '%@gmail.com' |
| IS NULL / IS NOT NULL | Null checks | WHERE phone IS NOT NULL |
| ORDER BY col | Sort results | ORDER BY salary DESC, name ASC |
| LIMIT n / OFFSET n | Limit + paginate results | LIMIT 10 OFFSET 20 |
| AS alias | Rename column/table | SELECT salary * 1.1 AS adjusted_salary |
Aggregation & Grouping
| Syntax | Description | Example |
|---|---|---|
| COUNT(*) | Count all rows | SELECT COUNT(*) FROM orders |
| SUM(col) | Sum of column | SELECT SUM(amount) FROM payments |
| AVG(col) | Average of column | SELECT AVG(score) FROM results |
| MIN(col) / MAX(col) | Min / Max value | SELECT MIN(price), MAX(price) FROM products |
| GROUP BY col | Group rows for aggregation | SELECT dept, AVG(salary) FROM emp GROUP BY dept |
| HAVING condition | Filter groups (after GROUP BY) | HAVING COUNT(*) > 5 AND AVG(sal) > 60000 |
Joins & Set Operations
| Syntax | Description | Example |
|---|---|---|
| INNER JOIN | Rows matching in both tables | FROM a INNER JOIN b ON a.id = b.id |
| LEFT JOIN | All left + matched right | FROM a LEFT JOIN b ON a.id = b.id |
| RIGHT JOIN | All right + matched left | FROM a RIGHT JOIN b ON a.id = b.id |
| FULL OUTER JOIN | All rows from both tables | FROM a FULL OUTER JOIN b ON a.id = b.id |
| SELF JOIN | Join table with itself | FROM emp e1 JOIN emp e2 ON e1.mgr_id = e2.id |
| UNION | Combine + deduplicate results | SELECT city FROM a UNION SELECT city FROM b |
| UNION ALL | Combine keeping duplicates | SELECT id FROM a UNION ALL SELECT id FROM b |
| INTERSECT | Rows in both result sets | SELECT id FROM a INTERSECT SELECT id FROM b |
| EXCEPT / MINUS | Rows in first but not second | SELECT id FROM a EXCEPT SELECT id FROM b |
Window Functions
| Function | Description | Example |
|---|---|---|
| ROW_NUMBER() | Sequential row number | ROW_NUMBER() OVER (PARTITION BY dept ORDER BY sal DESC) |
| RANK() | Rank with gaps for ties | RANK() OVER (ORDER BY score DESC) |
| DENSE_RANK() | Rank without gaps | DENSE_RANK() OVER (PARTITION BY dept ORDER BY sal) |
| NTILE(n) | Divide into n equal buckets | NTILE(4) OVER (ORDER BY salary) |
| LAG(col, n) | Value from n rows before | LAG(price, 1) OVER (ORDER BY date) |
| LEAD(col, n) | Value from n rows ahead | LEAD(price, 1) OVER (ORDER BY date) |
| FIRST_VALUE() | First value in window | FIRST_VALUE(price) OVER (PARTITION BY id ORDER BY date) |
| SUM() OVER () | Running total | SUM(amount) OVER (ORDER BY date) |
| AVG() OVER () | Moving average | AVG(price) OVER (ORDER BY date ROWS 6 PRECEDING) |
| PARTITION BY | Define window partitions | OVER (PARTITION BY dept ORDER BY salary) |
Advanced — CTEs, Subqueries & Functions
| Syntax | Description | Example |
|---|---|---|
| WITH cte AS (...) | Common Table Expression | WITH top AS (SELECT * FROM emp ORDER BY sal DESC LIMIT 10) |
| CASE WHEN | Conditional logic | CASE WHEN sal>100k THEN 'high' ELSE 'low' END |
| COALESCE(a, b) | First non-null value | COALESCE(phone, email, 'N/A') |
| NULLIF(a, b) | Return null if a equals b | NULLIF(score, 0) |
| CAST(col AS type) | Type conversion | CAST(price AS DECIMAL(10,2)) |
| SUBSTRING() | Extract part of string | SUBSTRING(name, 1, 3) |
| CONCAT() | Join strings | CONCAT(first_name, ' ', last_name) |
| DATE_TRUNC() | Truncate date to unit | DATE_TRUNC('month', created_at) |
| EXTRACT() | Extract date part | EXTRACT(YEAR FROM order_date) |
| EXISTS | Check if subquery has rows | WHERE EXISTS (SELECT 1 FROM orders WHERE user_id=u.id) |
| EXPLAIN | Show query execution plan | EXPLAIN SELECT * FROM large_table WHERE id > 100 |
Excel
Lookup, math, text, logic, date functions & dynamic arrays
Lookup & Reference
| Function | Description | Example |
|---|---|---|
| VLOOKUP(val,range,col,0) | Vertical exact match lookup | =VLOOKUP(A2,Sheet2!A:C,2,0) |
| XLOOKUP(val,arr,ret) | Modern flexible lookup | =XLOOKUP(A2,B:B,C:C,"Not Found") |
| INDEX(range,row,col) | Return value at position | =INDEX(A1:D10,3,2) |
| MATCH(val,range,0) | Position of value in range | =MATCH("Bob",A:A,0) |
| INDEX+MATCH | Two-way flexible lookup | =INDEX(C:C,MATCH(A2,B:B,0)) |
| HLOOKUP(val,range,row) | Horizontal lookup | =HLOOKUP("Q1",A1:D5,3,0) |
| OFFSET(ref,rows,cols) | Dynamic cell reference | =OFFSET(A1,3,2) |
| INDIRECT(ref_text) | Reference from text string | =INDIRECT("Sheet2!A"&B1) |
Math & Statistical
| Function | Description | Example |
|---|---|---|
| SUM(range) | Sum values | =SUM(A1:A100) |
| SUMIF(range,crit,sum) | Sum with condition | =SUMIF(B:B,"NY",C:C) |
| SUMIFS() | Sum with multiple conditions | =SUMIFS(D:D,B:B,"NY",C:C,">1000") |
| AVERAGEIF() | Average with condition | =AVERAGEIF(A:A,">18",B:B) |
| COUNTIF(range,crit) | Count with condition | =COUNTIF(B:B,"NY") |
| COUNTIFS() | Count with multiple conditions | =COUNTIFS(A:A,">18",B:B,"NY") |
| LARGE(range, k) | k-th largest value | =LARGE(A1:A100,3) |
| RANK(val,range) | Rank of value in range | =RANK(A2,A:A,0) |
| ROUND(val, digits) | Round to decimal places | =ROUND(3.14159, 2) |
| MOD(num, div) | Remainder after division | =MOD(10,3) |
Text Manipulation
| Function | Description | Example |
|---|---|---|
| TEXTJOIN(del,T,range) | Join with delimiter | =TEXTJOIN(", ",TRUE,A1:A5) |
| LEFT(text, n) | Extract left n chars | =LEFT(A2,3) |
| RIGHT(text, n) | Extract right n chars | =RIGHT(A2,4) |
| MID(text,start,n) | Extract from middle | =MID(A2,5,3) |
| LEN(text) | String length | =LEN(A2) |
| TRIM(text) | Remove extra spaces | =TRIM(A2) |
| PROPER(text) | Title Case | =PROPER("john doe") |
| SUBSTITUTE(t,old,new) | Replace substring | =SUBSTITUTE(A2,"-","/") |
| TEXT(val,format) | Format number as text | =TEXT(A2,"$#,##0.00") |
| FIND(sub,text) | Position of substring | =FIND("@",A2) |
Logic & Conditionals
| Function | Description | Example |
|---|---|---|
| IF(test,true,false) | Conditional value | =IF(A2>100,"High","Low") |
| IFS(t1,v1,t2,v2) | Multiple conditions | =IFS(A2>90,"A",A2>80,"B",TRUE,"C") |
| AND(cond1,cond2) | All conditions true | =IF(AND(A2>0,B2>0),"Both","No") |
| OR(cond1,cond2) | Any condition true | =IF(OR(A2="NY",A2="LA"),"Coast","Mid") |
| IFERROR(val,err) | Handle formula errors | =IFERROR(A2/B2,0) |
| IFNA(val,na_val) | Handle #N/A errors | =IFNA(VLOOKUP(A2,B:C,2,0),"Not found") |
| SWITCH(expr,v1,r1) | Switch statement | =SWITCH(A2,1,"Jan",2,"Feb","Other") |
Dynamic Array Functions (Excel 365)
| Function | Description | Example |
|---|---|---|
| FILTER(arr,cond) | Dynamic filtered range | =FILTER(A:B,C:C="NY") |
| SORT(range,col,ord) | Dynamic sorted range | =SORT(A1:C10,2,-1) |
| SORTBY(range,by) | Sort by another range | =SORTBY(A:B,C:C,-1) |
| UNIQUE(range) | Unique values list | =UNIQUE(A2:A100) |
| SEQUENCE(r,c,start) | Generate number sequence | =SEQUENCE(12,1,1,1) |
| VSTACK(r1,r2) | Stack ranges vertically | =VSTACK(A1:C5,Sheet2!A1:C3) |
| BYROW(range,fn) | Apply LAMBDA to each row | =BYROW(A1:C3,LAMBDA(r,SUM(r))) |
| LAMBDA(x,formula) | Create custom function | =LAMBDA(x,x*x)(5) |
Matplotlib
Chart types, subplots, labels, annotations and styling
Chart Types
| Function | Description | Example |
|---|---|---|
| plt.plot(x, y) | Line chart | plt.plot(x, y, color='blue', linewidth=2) |
| plt.scatter(x, y) | Scatter plot | plt.scatter(x, y, c=colors, s=sizes, alpha=0.7) |
| plt.bar(x, height) | Vertical bar chart | plt.bar(cats, vals, color='steelblue') |
| plt.barh(y, width) | Horizontal bar chart | plt.barh(labels, values) |
| plt.hist(x, bins) | Histogram | plt.hist(data, bins=30, density=True, alpha=0.7) |
| plt.pie(sizes) | Pie chart | plt.pie(sizes, labels=lbls, autopct='%1.1f%%') |
| plt.boxplot(data) | Box & whisker plot | plt.boxplot([grp1, grp2], labels=['A','B']) |
| plt.violinplot(data) | Violin plot | plt.violinplot(data, showmeans=True) |
| plt.fill_between(x,y1,y2) | Shade area between lines | plt.fill_between(x, y1, y2, alpha=0.3) |
| plt.errorbar(x,y,yerr) | Error bar plot | plt.errorbar(x, y, yerr=err, fmt='o') |
| plt.stackplot(x, y1,y2) | Stacked area chart | plt.stackplot(x, y1, y2, labels=['A','B']) |
| plt.imshow(data) | Display 2D array as image | plt.imshow(matrix, cmap='viridis') |
| plt.contourf() | Filled contour plot | plt.contourf(X, Y, Z, cmap='RdBu') |
| plt.hexbin(x,y) | Hexagonal bin plot | plt.hexbin(x, y, gridsize=30, cmap='Blues') |
Figures, Subplots & Layout
| Function | Description | Example |
|---|---|---|
| plt.figure(figsize) | Create figure with size | fig = plt.figure(figsize=(10, 6)) |
| plt.subplots(r,c) | Grid of subplots | fig, axes = plt.subplots(2, 3, figsize=(12,8)) |
| ax.twinx() | Second y-axis on same plot | ax2 = ax.twinx() |
| plt.tight_layout() | Auto-fix layout overlap | plt.tight_layout(pad=2.0) |
| plt.GridSpec() | Fine-grained grid control | gs = GridSpec(2, 2, width_ratios=[1,2]) |
Labels, Annotations & Styling
| Function | Description | Example |
|---|---|---|
| plt.xlabel(text) | X-axis label | plt.xlabel('Time (s)', fontsize=12) |
| plt.ylabel(text) | Y-axis label | plt.ylabel('Price ($)') |
| plt.title(text) | Plot title | plt.title('Sales Over Time', fontsize=14, fontweight='bold') |
| plt.legend() | Show legend | plt.legend(loc='upper right', framealpha=0.9) |
| plt.xlim() / ylim() | Set axis limits | plt.xlim(0, 100); plt.ylim(-1, 1) |
| plt.xticks() / yticks() | Custom tick marks | plt.xticks(range(0,100,10), rotation=45) |
| plt.grid() | Add grid lines | plt.grid(True, linestyle='--', alpha=0.5) |
| plt.axhline(y) | Horizontal line | plt.axhline(y=0, color='red', linestyle='--') |
| plt.annotate(text) | Annotate with arrow | plt.annotate('Peak', xy=(5,10), xytext=(7,12), arrowprops={}) |
| plt.savefig(fname) | Save to file | plt.savefig('chart.png', dpi=150, bbox_inches='tight') |
| plt.style.use() | Apply a style theme | plt.style.use('seaborn-v0_8-darkgrid') |
| plt.rcParams[] | Set global defaults | plt.rcParams['font.size'] = 12 |
Scikit-Learn
Preprocessing, models, evaluation, pipelines and tuning
Preprocessing & Encoding
| Function | Description | Example |
|---|---|---|
| train_test_split() | Split into train/test sets | X_tr,X_te,y_tr,y_te = train_test_split(X,y,test_size=0.2) |
| StandardScaler() | Standardize (zero mean, unit var) | scaler = StandardScaler(); X_sc = scaler.fit_transform(X) |
| MinMaxScaler() | Scale to [0, 1] | MinMaxScaler().fit_transform(X) |
| RobustScaler() | Scale robust to outliers | RobustScaler().fit_transform(X) |
| LabelEncoder() | Encode target labels as int | le = LabelEncoder(); y = le.fit_transform(y) |
| OneHotEncoder() | One-hot encode categories | OneHotEncoder(sparse=False).fit_transform(X) |
| SimpleImputer() | Fill missing values | SimpleImputer(strategy='mean').fit_transform(X) |
| PolynomialFeatures() | Create polynomial features | PolynomialFeatures(degree=2).fit_transform(X) |
| ColumnTransformer() | Apply transforms per column | ColumnTransformer([('sc',StandardScaler(),[0,1])]) |
Models — Regression, Classification & Clustering
| Model | Description | Example |
|---|---|---|
| LinearRegression() | Ordinary least squares | LinearRegression().fit(X_tr, y_tr) |
| Ridge(alpha) | L2 regularized regression | Ridge(alpha=1.0).fit(X_tr, y_tr) |
| Lasso(alpha) | L1 regularized regression | Lasso(alpha=0.1).fit(X_tr, y_tr) |
| LogisticRegression() | Binary/multi-class classifier | LogisticRegression(C=1.0, max_iter=1000) |
| SVC(kernel) | Support vector classifier | SVC(kernel='rbf', C=1.0, gamma='scale') |
| RandomForestClassifier() | Ensemble of decision trees | RandomForestClassifier(n_estimators=100) |
| GradientBoostingClassifier() | Gradient boosting trees | GradientBoostingClassifier(n_estimators=200) |
| XGBClassifier() | Extreme gradient boosting | XGBClassifier(n_estimators=200, learning_rate=0.1) |
| GaussianNB() | Naive Bayes classifier | GaussianNB().fit(X_tr, y_tr) |
| MLPClassifier() | Neural network classifier | MLPClassifier(hidden_layer_sizes=(100,50)) |
| KMeans(k) | K-Means clustering | KMeans(n_clusters=5, random_state=42) |
| DBSCAN() | Density-based clustering | DBSCAN(eps=0.5, min_samples=5) |
| IsolationForest() | Anomaly detection | IsolationForest(contamination=0.05) |
Dimensionality Reduction & Feature Selection
| Function | Description | Example |
|---|---|---|
| PCA(n) | Principal Component Analysis | pca = PCA(n_components=2); X_red = pca.fit_transform(X) |
| TSNE(n) | t-SNE for visualization | TSNE(n_components=2, perplexity=30).fit_transform(X) |
| SelectKBest() | Select k best features | SelectKBest(f_classif, k=10).fit_transform(X, y) |
| RFE() | Recursive feature elimination | RFE(estimator=clf, n_features_to_select=5) |
| VarianceThreshold() | Remove low-variance features | VarianceThreshold(threshold=0.01) |
Evaluation, Tuning & Pipelines
| Function | Description | Example |
|---|---|---|
| accuracy_score() | Classification accuracy | accuracy_score(y_te, y_pred) |
| precision_score() | Precision | precision_score(y_te, y_pred, average='macro') |
| recall_score() | Recall / sensitivity | recall_score(y_te, y_pred) |
| f1_score() | F1 harmonic mean of P & R | f1_score(y_te, y_pred, average='weighted') |
| roc_auc_score() | AUC of ROC curve | roc_auc_score(y_te, y_proba) |
| confusion_matrix() | Confusion matrix | confusion_matrix(y_te, y_pred) |
| classification_report() | Full classification metrics | print(classification_report(y_te, y_pred)) |
| mean_squared_error() | MSE / RMSE for regression | mean_squared_error(y_te, y_pred, squared=False) |
| r2_score() | R-squared coefficient | r2_score(y_te, y_pred) |
| cross_val_score() | K-fold cross-validation | cross_val_score(clf, X, y, cv=5, scoring='f1') |
| GridSearchCV() | Exhaustive hyperparameter search | GridSearchCV(clf, param_grid, cv=5, scoring='roc_auc') |
| RandomizedSearchCV() | Random hyperparameter search | RandomizedSearchCV(clf, params, n_iter=50, cv=5) |
| Pipeline(steps) | Chain transforms + estimator | Pipeline([('sc', StandardScaler()), ('clf', SVC())]) |
PyTorch
Tensors, neural network layers, optimizers, training loop, data loading
Tensor Creation & Manipulation
| Function | Description | Example |
|---|---|---|
| torch.tensor(data) | Create tensor from data | x = torch.tensor([[1.,2.],[3.,4.]]) |
| torch.zeros(shape) | Zeros tensor | torch.zeros(3, 4, dtype=torch.float32) |
| torch.ones(shape) | Ones tensor | torch.ones(3, 4) |
| torch.rand(shape) | Uniform random [0,1) | torch.rand(100, 10) |
| torch.randn(shape) | Standard normal random | torch.randn(batch_size, 128) |
| torch.arange() | Range tensor | torch.arange(0, 10, step=2) |
| torch.from_numpy(arr) | Tensor from NumPy array | x = torch.from_numpy(np_array) |
| tensor.numpy() | Convert tensor to NumPy | arr = x.cpu().numpy() |
| tensor.item() | Python scalar from 1-elem tensor | loss_val = loss.item() |
| tensor.to(device) | Move to device | x = x.to('cuda') |
| tensor.view(shape) | Reshape (contiguous) | x.view(batch_size, -1) |
| tensor.reshape(shape) | Reshape (safe) | x.reshape(-1, 28*28) |
| tensor.permute(dims) | Reorder dimensions | x.permute(0, 2, 3, 1) |
| tensor.squeeze(dim) | Remove size-1 dimension | x.squeeze(0) |
| tensor.unsqueeze(dim) | Add size-1 dimension | x.unsqueeze(1) |
| torch.cat(tensors, dim) | Concatenate along axis | torch.cat([a, b], dim=0) |
| torch.stack(tensors) | Stack along new axis | torch.stack([a, b], dim=1) |
| torch.matmul(a, b) | Matrix multiplication | out = torch.matmul(W, x) + b |
| tensor.detach() | Detach from computation graph | y = x.detach() |
| torch.no_grad() | Context: disable gradients | with torch.no_grad(): out = model(x) |
Neural Network Layers & Losses
| Module | Description | Example |
|---|---|---|
| nn.Module | Base class for all models | class Net(nn.Module): def __init__(self): super().__init__() |
| nn.Linear(in, out) | Fully-connected layer | nn.Linear(784, 256) |
| nn.Conv2d(in,out,k) | 2D convolution | nn.Conv2d(3, 64, kernel_size=3, padding=1) |
| nn.MaxPool2d(k) | Max pooling | nn.MaxPool2d(kernel_size=2, stride=2) |
| nn.BatchNorm2d(c) | Batch normalization (2D) | nn.BatchNorm2d(64) |
| nn.Dropout(p) | Dropout regularization | nn.Dropout(p=0.5) |
| nn.Embedding(n,d) | Embedding lookup table | nn.Embedding(vocab_size, embed_dim) |
| nn.LSTM(in,h) | Long Short-Term Memory | nn.LSTM(input_size=128, hidden_size=256, num_layers=2) |
| nn.MultiheadAttention() | Multi-head self-attention | nn.MultiheadAttention(embed_dim=512, num_heads=8) |
| nn.TransformerEncoder() | Transformer encoder stack | nn.TransformerEncoder(encoder_layer, num_layers=6) |
| nn.Sequential(*layers) | Chain layers sequentially | nn.Sequential(nn.Linear(784,256), nn.ReLU(), nn.Linear(256,10)) |
| nn.CrossEntropyLoss() | Classification loss | criterion = nn.CrossEntropyLoss() |
| nn.BCEWithLogitsLoss() | Binary classification loss | nn.BCEWithLogitsLoss() |
| nn.MSELoss() | Mean squared error loss | criterion = nn.MSELoss() |
Optimizers & Training Loop
| Function | Description | Example |
|---|---|---|
| optim.Adam() | Adam optimizer | optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-4) |
| optim.AdamW() | Adam with weight decay fix | optim.AdamW(model.parameters(), lr=2e-4) |
| optim.SGD() | Stochastic gradient descent | optim.SGD(model.parameters(), lr=0.01, momentum=0.9) |
| optimizer.zero_grad() | Clear accumulated gradients | optimizer.zero_grad() |
| loss.backward() | Compute gradients | loss.backward() |
| optimizer.step() | Update model parameters | optimizer.step() |
| nn.utils.clip_grad_norm_() | Clip gradient norms | nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0) |
| CosineAnnealingLR() | Cosine LR annealing | CosineAnnealingLR(optimizer, T_max=100) |
| model.train() | Set to training mode | model.train() # enables dropout, batchnorm |
| model.eval() | Set to eval mode | model.eval() # disables dropout, batchnorm |
| torch.save(state_dict) | Save model weights | torch.save(model.state_dict(), 'model.pt') |
| torch.load() | Load model weights | model.load_state_dict(torch.load('model.pt')) |
Data Loading & Transforms
| Function | Description | Example |
|---|---|---|
| Dataset.__getitem__() | Custom dataset indexing | def __getitem__(self, idx): return X[idx], y[idx] |
| Dataset.__len__() | Dataset length | def __len__(self): return len(self.data) |
| DataLoader(ds, batch) | Batch + shuffle dataset | DataLoader(ds, batch_size=32, shuffle=True, num_workers=4) |
| transforms.Compose() | Chain image transforms | transforms.Compose([transforms.ToTensor(), transforms.Normalize()]) |
| transforms.Normalize() | Normalize image tensor | transforms.Normalize(mean=[0.5], std=[0.5]) |
| torchvision.datasets.* | Built-in datasets | datasets.MNIST('./data', train=True, download=True) |
| torchvision.models.* | Pretrained models | models.resnet50(pretrained=True) |
LangGraph
State graphs, memory, HITL, tools, messages and subgraphs
Core State & Graph Primitives
| Function | Description | Example |
|---|---|---|
| StateGraph(schema) | Create graph with typed state | graph = StateGraph(MyState) |
| MessageGraph() | Graph using message list state | graph = MessageGraph() |
| TypedDict | Define state schema | class MyState(TypedDict): messages: list; count: int |
| Annotated[T, reducer] | Field with custom reducer | messages: Annotated[list, operator.add] |
| END | Terminal node constant | graph.add_edge('node', END) |
| START | Entry node constant | graph.add_edge(START, 'first_node') |
Graph Building & Execution
| Function | Description | Example |
|---|---|---|
| graph.add_node(name, fn) | Register a node function | graph.add_node('agent', call_llm) |
| graph.add_edge(a, b) | Static directional edge | graph.add_edge('tool', 'agent') |
| graph.add_conditional_edges() | Route based on output | graph.add_conditional_edges('agent', route_fn, {'tool':'tool','end':END}) |
| graph.compile() | Compile to runnable | app = graph.compile() |
| graph.compile(checkpointer) | Compile with memory | app = graph.compile(checkpointer=MemorySaver()) |
| app.invoke(input) | Run graph synchronously | result = app.invoke({'messages': [HumanMessage('Hi')]}) |
| app.stream(input) | Stream graph step by step | for chunk in app.stream(input, config): print(chunk) |
| app.ainvoke(input) | Async graph invocation | result = await app.ainvoke(input, config) |
Memory & Persistence
| Function | Description | Example |
|---|---|---|
| MemorySaver() | In-memory thread checkpointer | checkpointer = MemorySaver() |
| SqliteSaver.from_conn() | SQLite persistent memory | SqliteSaver.from_conn_string('memory.db') |
| thread_id in config | Isolate conversation sessions | config = {'configurable': {'thread_id': 'user-123'}} |
| app.get_state(config) | Read current graph state | state = app.get_state(config) |
| app.update_state(cfg, val) | Manually patch state | app.update_state(config, {'count': 5}) |
| app.get_state_history(cfg) | Full state history | for s in app.get_state_history(config): print(s) |
Human-in-the-Loop (HITL)
| Function | Description | Example |
|---|---|---|
| interrupt_before=['node'] | Pause before node runs | app = graph.compile(interrupt_before=['human_review']) |
| interrupt_after=['node'] | Pause after node runs | app = graph.compile(interrupt_after=['agent']) |
| app.update_state(cfg, v) | Inject human feedback | app.update_state(config, {'approved': True}) |
| NodeInterrupt(msg) | Raise interrupt from node | raise NodeInterrupt('Need human approval') |
| app.invoke(None, config) | Resume after interrupt | app.invoke(None, config) |
Tools & Messages
| Function | Description | Example |
|---|---|---|
| ToolNode(tools) | Built-in node to execute tools | tool_node = ToolNode([search, calculator]) |
| tools_condition(state) | Route to tool or END | graph.add_conditional_edges('agent', tools_condition) |
| @tool decorator | Define a LangChain tool | @tool def search(q: str) -> str: return web_search(q) |
| bind_tools(tools) | Bind tools to LLM | llm_with_tools = llm.bind_tools([search, calculator]) |
| HumanMessage(content) | User message | HumanMessage(content='What is the weather?') |
| AIMessage(content) | LLM response message | AIMessage(content='The weather is...') |
| SystemMessage(content) | System prompt message | SystemMessage(content='You are a helpful assistant') |
| ToolMessage | Result message from a tool | ToolMessage(content=result, tool_call_id=call.id) |
Subgraphs, Fan-out & Advanced Routing
| Function | Description | Example |
|---|---|---|
| Send(node, state) | Fan-out: send to multiple | return [Send('process', {'item': x}) for x in items] |
| Command(goto, update) | Dynamic routing + update | return Command(goto='agent', update={'count': n+1}) |
| InjectedState | Pass full state to tool | def tool(query: str, state: Annotated[State, InjectedState]) |
| RunnableConfig | Pass config to nodes | def node(state, config: RunnableConfig): ... |