AI / MLDS & 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
FunctionDescriptionExample
pd.read_csv()Read CSV file into DataFramedf = pd.read_csv('data.csv')
pd.read_excel()Read Excel file into DataFramedf = pd.read_excel('data.xlsx')
pd.read_json()Read JSON into DataFramedf = pd.read_json('data.json')
pd.read_sql()Read SQL query into DataFramedf = pd.read_sql(query, conn)
pd.read_parquet()Read Parquet filedf = pd.read_parquet('data.parquet')
df.to_csv()Write DataFrame to CSVdf.to_csv('out.csv', index=False)
df.to_excel()Write DataFrame to Exceldf.to_excel('out.xlsx')
df.to_parquet()Write DataFrame to Parquetdf.to_parquet('out.parquet')
df.to_dict()Convert DataFrame to dictd = df.to_dict(orient='records')
Inspection & Exploration
FunctionDescriptionExample
df.head(n)First n rows (default 5)df.head(10)
df.tail(n)Last n rowsdf.tail(5)
df.info()Column types, nulls, memorydf.info()
df.describe()Summary stats (count, mean…)df.describe(include='all')
df.shapeTuple of (rows, columns)rows, cols = df.shape
df.dtypesData type of each columndf.dtypes
df.columnsList of column nameslist(df.columns)
df.nunique()Count of unique values per coldf.nunique()
df.value_counts()Frequency countdf['city'].value_counts(normalize=True)
df.memory_usage()Memory used per columndf.memory_usage(deep=True)
df.sample(n)Random sample of n rowsdf.sample(5, random_state=42)
Selection & Filtering
FunctionDescriptionExample
df['col']Select single column (Series)df['age']
df[['a','b']]Select multiple columnsdf[['name', 'age']]
df.loc[row, col]Label-based indexingdf.loc[0:5, 'age']
df.iloc[row, col]Integer-based indexingdf.iloc[0:5, 1:3]
df.at[row, col]Single value by labeldf.at[0, 'name']
df.iat[row, col]Single value by positiondf.iat[0, 1]
df.query(expr)Filter using string expressiondf.query('age > 30 & city=="NY"')
df[df.col > val]Boolean mask filterdf[df['age'] > 30]
df.where(cond)Keep values where True, else NaNdf.where(df > 0)
df.filter()Filter by column/row labelsdf.filter(like='price')
Cleaning & Transformation
FunctionDescriptionExample
df.isnull()Boolean mask of null valuesdf.isnull().sum()
df.dropna()Drop rows with any nulldf.dropna(subset=['age'])
df.fillna(val)Fill nulls with value/methoddf.fillna({'age': 0, 'name': 'N/A'})
df.interpolate()Interpolate missing numeric valsdf['price'].interpolate()
df.duplicated()Boolean mask of duplicate rowsdf.duplicated(subset=['id'])
df.drop_duplicates()Remove duplicate rowsdf.drop_duplicates(keep='first')
df.rename()Rename columns/indexdf.rename(columns={'old':'new'})
df.astype()Cast column to new typedf['age'].astype(int)
df.replace()Replace valuesdf.replace({'Y':1, 'N':0})
df.clip()Clip values to min/maxdf['score'].clip(0, 100)
df.drop()Drop rows or columnsdf.drop(columns=['col1','col2'])
df.reset_index()Reset row indexdf.reset_index(drop=True)
df.set_index()Set a column as indexdf.set_index('id')
Aggregation, Merging & Reshaping
FunctionDescriptionExample
df.sort_values()Sort by column valuesdf.sort_values('age', ascending=False)
df.groupby()Group rows & aggregatedf.groupby('city')['salary'].mean()
df.agg()Apply multiple aggregationsdf.groupby('dept').agg({'sal':['mean','max']})
df.pivot_table()Create a pivot tablepd.pivot_table(df, values='sal', index='dept')
df.melt()Wide to long formatdf.melt(id_vars=['id'], value_vars=['q1','q2'])
df.pivot()Long to wide formatdf.pivot(index='date', columns='item', values='val')
df.apply()Apply function along axisdf['bmi'].apply(lambda x: 'high' if x>25 else 'ok')
df.map()Map values via dict/functiondf['grade'].map({'A':4,'B':3,'C':2})
df.transform()Transform keeping shapedf.groupby('dept')['sal'].transform('mean')
df.merge()SQL-style join two DataFramespd.merge(df1, df2, on='id', how='left')
pd.concat()Stack DataFrames row/colwisepd.concat([df1, df2], ignore_index=True)
df.assign()Add/overwrite columns fluentlydf.assign(full_name=df.first+' '+df.last)
df.eval()Evaluate expression stringdf.eval('profit = revenue - cost')
df.explode()Explode list column to rowsdf.explode('tags')
df.value_counts()Frequency countdf['city'].value_counts(normalize=True)
pd.cut()Bin continuous datapd.cut(df['age'], bins=[0,18,65,100])
pd.get_dummies()One-hot encode categoricalspd.get_dummies(df['color'])
df.crosstab()Cross-tabulation of two colspd.crosstab(df['dept'], df['gender'])
String Methods (df.str.*)
FunctionDescriptionExample
df.str.lower()Lowercase all stringsdf['name'].str.lower()
df.str.upper()Uppercase all stringsdf['name'].str.upper()
df.str.strip()Strip whitespacedf['name'].str.strip()
df.str.contains()Check if string contains patterndf['email'].str.contains('@gmail')
df.str.replace()Replace substringdf['text'].str.replace('old','new')
df.str.split()Split string to listdf['full_name'].str.split(' ', expand=True)
df.str.len()Length of each stringdf['name'].str.len()
df.str.extract()Extract regex groupsdf['phone'].str.extract(r'(\d{3})')
df.str.startswith()Check prefixdf['col'].str.startswith('pre')
DateTime Methods (df.dt.*)
FunctionDescriptionExample
pd.to_datetime()Parse string to datetimepd.to_datetime(df['date'])
dt.year / .month / .dayExtract date componentsdf['date'].dt.year
dt.dayofweekDay of week (0=Mon)df['date'].dt.dayofweek
dt.hour / .minuteExtract time componentsdf['ts'].dt.hour
df.resample()Resample time seriesdf.resample('M').mean()
df.shift()Shift values by periodsdf['price'].shift(1)
df.diff()Difference between rowsdf['price'].diff()
df.rolling(n)Rolling window calculationdf['price'].rolling(7).mean()
df.expanding()Expanding windowdf['price'].expanding().mean()
df.ewm()Exponentially weighted meandf['price'].ewm(span=7).mean()
NumPy
Array creation, manipulation, math, and linear algebra
Array Creation
FunctionDescriptionExample
np.array()Create array from list/tuplenp.array([[1,2],[3,4]])
np.zeros(shape)Array of zerosnp.zeros((3, 4))
np.ones(shape)Array of onesnp.ones((3, 4), dtype=float)
np.full(shape, val)Fill array with valuenp.full((2,3), 7)
np.eye(n)Identity matrixnp.eye(4)
np.arange()Evenly spaced int rangenp.arange(0, 10, 2)
np.linspace()Evenly spaced floatsnp.linspace(0, 1, 100)
np.random.rand()Uniform random [0, 1)np.random.rand(3, 4)
np.random.randn()Standard normal randomnp.random.randn(100)
np.random.randint()Random integersnp.random.randint(0, 10, size=(3,3))
np.random.seed()Set random seednp.random.seed(42)
Shape & Manipulation
FunctionDescriptionExample
arr.reshape()Reshape without copyingarr.reshape(20, 20)
arr.ravel()Flatten to 1D (view)arr.ravel()
arr.flatten()Flatten to 1D (copy)arr.flatten()
arr.T / arr.transpose()Transpose axesarr.T
np.expand_dims()Add new axisnp.expand_dims(arr, axis=0)
np.squeeze()Remove size-1 axesnp.squeeze(arr)
np.concatenate()Join arrays along axisnp.concatenate([a, b], axis=0)
np.stack()Stack along new axisnp.stack([a, b], axis=1)
np.hstack()Stack horizontallynp.hstack([a, b])
np.vstack()Stack verticallynp.vstack([a, b])
np.split()Split arraynp.split(arr, 3)
Math & Statistics
FunctionDescriptionExample
np.sum()Sum all or along axisnp.sum(arr, axis=0)
np.mean()Meannp.mean(arr, axis=1)
np.median()Mediannp.median(arr)
np.std()Standard deviationnp.std(arr, ddof=1)
np.min() / np.max()Min / Max valuearr.min(axis=0)
np.argmin() / argmax()Index of min / maxnp.argmin(arr)
np.cumsum()Cumulative sumnp.cumsum(arr)
np.abs()Absolute valuenp.abs(arr)
np.sqrt()Square rootnp.sqrt(arr)
np.exp()Exponential (e^x)np.exp(arr)
np.log()Natural lognp.log(arr)
np.dot()Dot product / matrix mulnp.dot(A, B)
np.matmul() / @Matrix multiplicationA @ B
np.where()Conditional selectnp.where(arr > 0, arr, 0)
np.unique()Unique values + countsnp.unique(arr, return_counts=True)
np.sort()Sort arraynp.sort(arr, axis=0)
np.percentile()Percentile valuenp.percentile(arr, 75)
np.corrcoef()Correlation matrixnp.corrcoef(x, y)
Linear Algebra
FunctionDescriptionExample
np.linalg.inv()Matrix inversenp.linalg.inv(A)
np.linalg.det()Determinantnp.linalg.det(A)
np.linalg.eig()Eigenvalues & eigenvectorsvals, vecs = np.linalg.eig(A)
np.linalg.svd()Singular Value DecompositionU, S, V = np.linalg.svd(A)
np.linalg.norm()Vector / matrix normnp.linalg.norm(arr)
np.linalg.solve()Solve linear system Ax=bx = np.linalg.solve(A, b)
np.linalg.pinv()Pseudo-inversenp.linalg.pinv(A)
SQL
Querying, aggregation, joins, window functions, and CTEs
Basic Querying
SyntaxDescriptionExample
SELECT col1, col2Choose columns to returnSELECT name, age FROM users
SELECT DISTINCTRemove duplicatesSELECT DISTINCT city FROM customers
WHERE conditionFilter rowsWHERE salary > 50000 AND dept = 'Eng'
IN (list)Match any value in listWHERE country IN ('US','UK','CA')
BETWEEN a AND bRange filter (inclusive)WHERE date BETWEEN '2024-01-01' AND '2024-12-31'
LIKE patternPattern matchingWHERE email LIKE '%@gmail.com'
IS NULL / IS NOT NULLNull checksWHERE phone IS NOT NULL
ORDER BY colSort resultsORDER BY salary DESC, name ASC
LIMIT n / OFFSET nLimit + paginate resultsLIMIT 10 OFFSET 20
AS aliasRename column/tableSELECT salary * 1.1 AS adjusted_salary
Aggregation & Grouping
SyntaxDescriptionExample
COUNT(*)Count all rowsSELECT COUNT(*) FROM orders
SUM(col)Sum of columnSELECT SUM(amount) FROM payments
AVG(col)Average of columnSELECT AVG(score) FROM results
MIN(col) / MAX(col)Min / Max valueSELECT MIN(price), MAX(price) FROM products
GROUP BY colGroup rows for aggregationSELECT dept, AVG(salary) FROM emp GROUP BY dept
HAVING conditionFilter groups (after GROUP BY)HAVING COUNT(*) > 5 AND AVG(sal) > 60000
Joins & Set Operations
SyntaxDescriptionExample
INNER JOINRows matching in both tablesFROM a INNER JOIN b ON a.id = b.id
LEFT JOINAll left + matched rightFROM a LEFT JOIN b ON a.id = b.id
RIGHT JOINAll right + matched leftFROM a RIGHT JOIN b ON a.id = b.id
FULL OUTER JOINAll rows from both tablesFROM a FULL OUTER JOIN b ON a.id = b.id
SELF JOINJoin table with itselfFROM emp e1 JOIN emp e2 ON e1.mgr_id = e2.id
UNIONCombine + deduplicate resultsSELECT city FROM a UNION SELECT city FROM b
UNION ALLCombine keeping duplicatesSELECT id FROM a UNION ALL SELECT id FROM b
INTERSECTRows in both result setsSELECT id FROM a INTERSECT SELECT id FROM b
EXCEPT / MINUSRows in first but not secondSELECT id FROM a EXCEPT SELECT id FROM b
Window Functions
FunctionDescriptionExample
ROW_NUMBER()Sequential row numberROW_NUMBER() OVER (PARTITION BY dept ORDER BY sal DESC)
RANK()Rank with gaps for tiesRANK() OVER (ORDER BY score DESC)
DENSE_RANK()Rank without gapsDENSE_RANK() OVER (PARTITION BY dept ORDER BY sal)
NTILE(n)Divide into n equal bucketsNTILE(4) OVER (ORDER BY salary)
LAG(col, n)Value from n rows beforeLAG(price, 1) OVER (ORDER BY date)
LEAD(col, n)Value from n rows aheadLEAD(price, 1) OVER (ORDER BY date)
FIRST_VALUE()First value in windowFIRST_VALUE(price) OVER (PARTITION BY id ORDER BY date)
SUM() OVER ()Running totalSUM(amount) OVER (ORDER BY date)
AVG() OVER ()Moving averageAVG(price) OVER (ORDER BY date ROWS 6 PRECEDING)
PARTITION BYDefine window partitionsOVER (PARTITION BY dept ORDER BY salary)
Advanced — CTEs, Subqueries & Functions
SyntaxDescriptionExample
WITH cte AS (...)Common Table ExpressionWITH top AS (SELECT * FROM emp ORDER BY sal DESC LIMIT 10)
CASE WHENConditional logicCASE WHEN sal>100k THEN 'high' ELSE 'low' END
COALESCE(a, b)First non-null valueCOALESCE(phone, email, 'N/A')
NULLIF(a, b)Return null if a equals bNULLIF(score, 0)
CAST(col AS type)Type conversionCAST(price AS DECIMAL(10,2))
SUBSTRING()Extract part of stringSUBSTRING(name, 1, 3)
CONCAT()Join stringsCONCAT(first_name, ' ', last_name)
DATE_TRUNC()Truncate date to unitDATE_TRUNC('month', created_at)
EXTRACT()Extract date partEXTRACT(YEAR FROM order_date)
EXISTSCheck if subquery has rowsWHERE EXISTS (SELECT 1 FROM orders WHERE user_id=u.id)
EXPLAINShow query execution planEXPLAIN SELECT * FROM large_table WHERE id > 100
Excel
Lookup, math, text, logic, date functions & dynamic arrays
Lookup & Reference
FunctionDescriptionExample
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+MATCHTwo-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
FunctionDescriptionExample
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
FunctionDescriptionExample
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
FunctionDescriptionExample
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)
FunctionDescriptionExample
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
FunctionDescriptionExample
plt.plot(x, y)Line chartplt.plot(x, y, color='blue', linewidth=2)
plt.scatter(x, y)Scatter plotplt.scatter(x, y, c=colors, s=sizes, alpha=0.7)
plt.bar(x, height)Vertical bar chartplt.bar(cats, vals, color='steelblue')
plt.barh(y, width)Horizontal bar chartplt.barh(labels, values)
plt.hist(x, bins)Histogramplt.hist(data, bins=30, density=True, alpha=0.7)
plt.pie(sizes)Pie chartplt.pie(sizes, labels=lbls, autopct='%1.1f%%')
plt.boxplot(data)Box & whisker plotplt.boxplot([grp1, grp2], labels=['A','B'])
plt.violinplot(data)Violin plotplt.violinplot(data, showmeans=True)
plt.fill_between(x,y1,y2)Shade area between linesplt.fill_between(x, y1, y2, alpha=0.3)
plt.errorbar(x,y,yerr)Error bar plotplt.errorbar(x, y, yerr=err, fmt='o')
plt.stackplot(x, y1,y2)Stacked area chartplt.stackplot(x, y1, y2, labels=['A','B'])
plt.imshow(data)Display 2D array as imageplt.imshow(matrix, cmap='viridis')
plt.contourf()Filled contour plotplt.contourf(X, Y, Z, cmap='RdBu')
plt.hexbin(x,y)Hexagonal bin plotplt.hexbin(x, y, gridsize=30, cmap='Blues')
Figures, Subplots & Layout
FunctionDescriptionExample
plt.figure(figsize)Create figure with sizefig = plt.figure(figsize=(10, 6))
plt.subplots(r,c)Grid of subplotsfig, axes = plt.subplots(2, 3, figsize=(12,8))
ax.twinx()Second y-axis on same plotax2 = ax.twinx()
plt.tight_layout()Auto-fix layout overlapplt.tight_layout(pad=2.0)
plt.GridSpec()Fine-grained grid controlgs = GridSpec(2, 2, width_ratios=[1,2])
Labels, Annotations & Styling
FunctionDescriptionExample
plt.xlabel(text)X-axis labelplt.xlabel('Time (s)', fontsize=12)
plt.ylabel(text)Y-axis labelplt.ylabel('Price ($)')
plt.title(text)Plot titleplt.title('Sales Over Time', fontsize=14, fontweight='bold')
plt.legend()Show legendplt.legend(loc='upper right', framealpha=0.9)
plt.xlim() / ylim()Set axis limitsplt.xlim(0, 100); plt.ylim(-1, 1)
plt.xticks() / yticks()Custom tick marksplt.xticks(range(0,100,10), rotation=45)
plt.grid()Add grid linesplt.grid(True, linestyle='--', alpha=0.5)
plt.axhline(y)Horizontal lineplt.axhline(y=0, color='red', linestyle='--')
plt.annotate(text)Annotate with arrowplt.annotate('Peak', xy=(5,10), xytext=(7,12), arrowprops={})
plt.savefig(fname)Save to fileplt.savefig('chart.png', dpi=150, bbox_inches='tight')
plt.style.use()Apply a style themeplt.style.use('seaborn-v0_8-darkgrid')
plt.rcParams[]Set global defaultsplt.rcParams['font.size'] = 12
Scikit-Learn
Preprocessing, models, evaluation, pipelines and tuning
Preprocessing & Encoding
FunctionDescriptionExample
train_test_split()Split into train/test setsX_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 outliersRobustScaler().fit_transform(X)
LabelEncoder()Encode target labels as intle = LabelEncoder(); y = le.fit_transform(y)
OneHotEncoder()One-hot encode categoriesOneHotEncoder(sparse=False).fit_transform(X)
SimpleImputer()Fill missing valuesSimpleImputer(strategy='mean').fit_transform(X)
PolynomialFeatures()Create polynomial featuresPolynomialFeatures(degree=2).fit_transform(X)
ColumnTransformer()Apply transforms per columnColumnTransformer([('sc',StandardScaler(),[0,1])])
Models — Regression, Classification & Clustering
ModelDescriptionExample
LinearRegression()Ordinary least squaresLinearRegression().fit(X_tr, y_tr)
Ridge(alpha)L2 regularized regressionRidge(alpha=1.0).fit(X_tr, y_tr)
Lasso(alpha)L1 regularized regressionLasso(alpha=0.1).fit(X_tr, y_tr)
LogisticRegression()Binary/multi-class classifierLogisticRegression(C=1.0, max_iter=1000)
SVC(kernel)Support vector classifierSVC(kernel='rbf', C=1.0, gamma='scale')
RandomForestClassifier()Ensemble of decision treesRandomForestClassifier(n_estimators=100)
GradientBoostingClassifier()Gradient boosting treesGradientBoostingClassifier(n_estimators=200)
XGBClassifier()Extreme gradient boostingXGBClassifier(n_estimators=200, learning_rate=0.1)
GaussianNB()Naive Bayes classifierGaussianNB().fit(X_tr, y_tr)
MLPClassifier()Neural network classifierMLPClassifier(hidden_layer_sizes=(100,50))
KMeans(k)K-Means clusteringKMeans(n_clusters=5, random_state=42)
DBSCAN()Density-based clusteringDBSCAN(eps=0.5, min_samples=5)
IsolationForest()Anomaly detectionIsolationForest(contamination=0.05)
Dimensionality Reduction & Feature Selection
FunctionDescriptionExample
PCA(n)Principal Component Analysispca = PCA(n_components=2); X_red = pca.fit_transform(X)
TSNE(n)t-SNE for visualizationTSNE(n_components=2, perplexity=30).fit_transform(X)
SelectKBest()Select k best featuresSelectKBest(f_classif, k=10).fit_transform(X, y)
RFE()Recursive feature eliminationRFE(estimator=clf, n_features_to_select=5)
VarianceThreshold()Remove low-variance featuresVarianceThreshold(threshold=0.01)
Evaluation, Tuning & Pipelines
FunctionDescriptionExample
accuracy_score()Classification accuracyaccuracy_score(y_te, y_pred)
precision_score()Precisionprecision_score(y_te, y_pred, average='macro')
recall_score()Recall / sensitivityrecall_score(y_te, y_pred)
f1_score()F1 harmonic mean of P & Rf1_score(y_te, y_pred, average='weighted')
roc_auc_score()AUC of ROC curveroc_auc_score(y_te, y_proba)
confusion_matrix()Confusion matrixconfusion_matrix(y_te, y_pred)
classification_report()Full classification metricsprint(classification_report(y_te, y_pred))
mean_squared_error()MSE / RMSE for regressionmean_squared_error(y_te, y_pred, squared=False)
r2_score()R-squared coefficientr2_score(y_te, y_pred)
cross_val_score()K-fold cross-validationcross_val_score(clf, X, y, cv=5, scoring='f1')
GridSearchCV()Exhaustive hyperparameter searchGridSearchCV(clf, param_grid, cv=5, scoring='roc_auc')
RandomizedSearchCV()Random hyperparameter searchRandomizedSearchCV(clf, params, n_iter=50, cv=5)
Pipeline(steps)Chain transforms + estimatorPipeline([('sc', StandardScaler()), ('clf', SVC())])
PyTorch
Tensors, neural network layers, optimizers, training loop, data loading
Tensor Creation & Manipulation
FunctionDescriptionExample
torch.tensor(data)Create tensor from datax = torch.tensor([[1.,2.],[3.,4.]])
torch.zeros(shape)Zeros tensortorch.zeros(3, 4, dtype=torch.float32)
torch.ones(shape)Ones tensortorch.ones(3, 4)
torch.rand(shape)Uniform random [0,1)torch.rand(100, 10)
torch.randn(shape)Standard normal randomtorch.randn(batch_size, 128)
torch.arange()Range tensortorch.arange(0, 10, step=2)
torch.from_numpy(arr)Tensor from NumPy arrayx = torch.from_numpy(np_array)
tensor.numpy()Convert tensor to NumPyarr = x.cpu().numpy()
tensor.item()Python scalar from 1-elem tensorloss_val = loss.item()
tensor.to(device)Move to devicex = 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 dimensionsx.permute(0, 2, 3, 1)
tensor.squeeze(dim)Remove size-1 dimensionx.squeeze(0)
tensor.unsqueeze(dim)Add size-1 dimensionx.unsqueeze(1)
torch.cat(tensors, dim)Concatenate along axistorch.cat([a, b], dim=0)
torch.stack(tensors)Stack along new axistorch.stack([a, b], dim=1)
torch.matmul(a, b)Matrix multiplicationout = torch.matmul(W, x) + b
tensor.detach()Detach from computation graphy = x.detach()
torch.no_grad()Context: disable gradientswith torch.no_grad(): out = model(x)
Neural Network Layers & Losses
ModuleDescriptionExample
nn.ModuleBase class for all modelsclass Net(nn.Module): def __init__(self): super().__init__()
nn.Linear(in, out)Fully-connected layernn.Linear(784, 256)
nn.Conv2d(in,out,k)2D convolutionnn.Conv2d(3, 64, kernel_size=3, padding=1)
nn.MaxPool2d(k)Max poolingnn.MaxPool2d(kernel_size=2, stride=2)
nn.BatchNorm2d(c)Batch normalization (2D)nn.BatchNorm2d(64)
nn.Dropout(p)Dropout regularizationnn.Dropout(p=0.5)
nn.Embedding(n,d)Embedding lookup tablenn.Embedding(vocab_size, embed_dim)
nn.LSTM(in,h)Long Short-Term Memorynn.LSTM(input_size=128, hidden_size=256, num_layers=2)
nn.MultiheadAttention()Multi-head self-attentionnn.MultiheadAttention(embed_dim=512, num_heads=8)
nn.TransformerEncoder()Transformer encoder stacknn.TransformerEncoder(encoder_layer, num_layers=6)
nn.Sequential(*layers)Chain layers sequentiallynn.Sequential(nn.Linear(784,256), nn.ReLU(), nn.Linear(256,10))
nn.CrossEntropyLoss()Classification losscriterion = nn.CrossEntropyLoss()
nn.BCEWithLogitsLoss()Binary classification lossnn.BCEWithLogitsLoss()
nn.MSELoss()Mean squared error losscriterion = nn.MSELoss()
Optimizers & Training Loop
FunctionDescriptionExample
optim.Adam()Adam optimizeroptim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-4)
optim.AdamW()Adam with weight decay fixoptim.AdamW(model.parameters(), lr=2e-4)
optim.SGD()Stochastic gradient descentoptim.SGD(model.parameters(), lr=0.01, momentum=0.9)
optimizer.zero_grad()Clear accumulated gradientsoptimizer.zero_grad()
loss.backward()Compute gradientsloss.backward()
optimizer.step()Update model parametersoptimizer.step()
nn.utils.clip_grad_norm_()Clip gradient normsnn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
CosineAnnealingLR()Cosine LR annealingCosineAnnealingLR(optimizer, T_max=100)
model.train()Set to training modemodel.train() # enables dropout, batchnorm
model.eval()Set to eval modemodel.eval() # disables dropout, batchnorm
torch.save(state_dict)Save model weightstorch.save(model.state_dict(), 'model.pt')
torch.load()Load model weightsmodel.load_state_dict(torch.load('model.pt'))
Data Loading & Transforms
FunctionDescriptionExample
Dataset.__getitem__()Custom dataset indexingdef __getitem__(self, idx): return X[idx], y[idx]
Dataset.__len__()Dataset lengthdef __len__(self): return len(self.data)
DataLoader(ds, batch)Batch + shuffle datasetDataLoader(ds, batch_size=32, shuffle=True, num_workers=4)
transforms.Compose()Chain image transformstransforms.Compose([transforms.ToTensor(), transforms.Normalize()])
transforms.Normalize()Normalize image tensortransforms.Normalize(mean=[0.5], std=[0.5])
torchvision.datasets.*Built-in datasetsdatasets.MNIST('./data', train=True, download=True)
torchvision.models.*Pretrained modelsmodels.resnet50(pretrained=True)
LangGraph
State graphs, memory, HITL, tools, messages and subgraphs
Core State & Graph Primitives
FunctionDescriptionExample
StateGraph(schema)Create graph with typed stategraph = StateGraph(MyState)
MessageGraph()Graph using message list stategraph = MessageGraph()
TypedDictDefine state schemaclass MyState(TypedDict): messages: list; count: int
Annotated[T, reducer]Field with custom reducermessages: Annotated[list, operator.add]
ENDTerminal node constantgraph.add_edge('node', END)
STARTEntry node constantgraph.add_edge(START, 'first_node')
Graph Building & Execution
FunctionDescriptionExample
graph.add_node(name, fn)Register a node functiongraph.add_node('agent', call_llm)
graph.add_edge(a, b)Static directional edgegraph.add_edge('tool', 'agent')
graph.add_conditional_edges()Route based on outputgraph.add_conditional_edges('agent', route_fn, {'tool':'tool','end':END})
graph.compile()Compile to runnableapp = graph.compile()
graph.compile(checkpointer)Compile with memoryapp = graph.compile(checkpointer=MemorySaver())
app.invoke(input)Run graph synchronouslyresult = app.invoke({'messages': [HumanMessage('Hi')]})
app.stream(input)Stream graph step by stepfor chunk in app.stream(input, config): print(chunk)
app.ainvoke(input)Async graph invocationresult = await app.ainvoke(input, config)
Memory & Persistence
FunctionDescriptionExample
MemorySaver()In-memory thread checkpointercheckpointer = MemorySaver()
SqliteSaver.from_conn()SQLite persistent memorySqliteSaver.from_conn_string('memory.db')
thread_id in configIsolate conversation sessionsconfig = {'configurable': {'thread_id': 'user-123'}}
app.get_state(config)Read current graph statestate = app.get_state(config)
app.update_state(cfg, val)Manually patch stateapp.update_state(config, {'count': 5})
app.get_state_history(cfg)Full state historyfor s in app.get_state_history(config): print(s)
Human-in-the-Loop (HITL)
FunctionDescriptionExample
interrupt_before=['node']Pause before node runsapp = graph.compile(interrupt_before=['human_review'])
interrupt_after=['node']Pause after node runsapp = graph.compile(interrupt_after=['agent'])
app.update_state(cfg, v)Inject human feedbackapp.update_state(config, {'approved': True})
NodeInterrupt(msg)Raise interrupt from noderaise NodeInterrupt('Need human approval')
app.invoke(None, config)Resume after interruptapp.invoke(None, config)
Tools & Messages
FunctionDescriptionExample
ToolNode(tools)Built-in node to execute toolstool_node = ToolNode([search, calculator])
tools_condition(state)Route to tool or ENDgraph.add_conditional_edges('agent', tools_condition)
@tool decoratorDefine a LangChain tool@tool def search(q: str) -> str: return web_search(q)
bind_tools(tools)Bind tools to LLMllm_with_tools = llm.bind_tools([search, calculator])
HumanMessage(content)User messageHumanMessage(content='What is the weather?')
AIMessage(content)LLM response messageAIMessage(content='The weather is...')
SystemMessage(content)System prompt messageSystemMessage(content='You are a helpful assistant')
ToolMessageResult message from a toolToolMessage(content=result, tool_call_id=call.id)
Subgraphs, Fan-out & Advanced Routing
FunctionDescriptionExample
Send(node, state)Fan-out: send to multiplereturn [Send('process', {'item': x}) for x in items]
Command(goto, update)Dynamic routing + updatereturn Command(goto='agent', update={'count': n+1})
InjectedStatePass full state to tooldef tool(query: str, state: Annotated[State, InjectedState])
RunnableConfigPass config to nodesdef node(state, config: RunnableConfig): ...