AI & Machine Learning Cheat Sheet
Free AI & Machine Learning cheat sheet: the most-used AI & Machine Learning syntax and methods at a glance — searchable and beginner-friendly.
Key Concepts
| Concept | Syntax | Example |
|---|---|---|
| Supervised learning | Labeled data → model | Classification, Regression |
| Unsupervised learning | Unlabeled data → patterns | Clustering, Dimensionality reduction |
| Reinforcement learning | Agent → actions → rewards | Game AI, Robotics |
| Transfer learning | Pre-trained model → fine-tune | Use BERT for your NLP task |
Learn it in a lesson: Introduction to AI & ML · Advanced NLP: Transformers, BERT, T5, LLaMA, Mistral
Common Algorithms
| Concept | Syntax | Example |
|---|---|---|
| Linear Regression | y = wx + b | Predicting house prices |
| Decision Tree | if-else rules from data | Customer churn prediction |
| K-Nearest Neighbors | Classify by nearest points | Image classification |
| Neural Network | Layers of weighted nodes | Deep learning, NLP |
| Random Forest | Ensemble of decision trees | Robust classification/regression |
Learn it in a lesson: Decision Trees & Random Forests · Linear Regression · Final AI Project — Build & Deploy a Full End-to-End ML System · Neural Networks Introduction
Python for ML
| Concept | Syntax | Example |
|---|---|---|
| NumPy array | np.array([1, 2, 3]) | import numpy as np |
| Pandas DataFrame | pd.DataFrame(data) | df = pd.read_csv('data.csv') |
| Train/test split | train_test_split(X, y) | from sklearn.model_selection import train_test_split |
| Fit model | model.fit(X_train, y_train) | model = LinearRegression(); model.fit(X, y) |
Learn it in a lesson: Python for Machine Learning
Data Preparation
| Concept | Syntax | Example |
|---|---|---|
| Normalization | (x - min) / (max - min) | Scale features to 0-1 range |
| Standardization | (x - mean) / std | Scale features to mean=0, std=1 |
| One-hot encoding | pd.get_dummies(df['col']) | Convert categories to binary columns |
| Handle missing | df.fillna() / df.dropna() | df['col'].fillna(df['col'].mean()) |
Learn it in a lesson: Data Preprocessing
Evaluation Metrics
| Concept | Syntax | Example |
|---|---|---|
| Accuracy | (TP+TN) / Total | Overall correctness |
| Precision | TP / (TP+FP) | Of predicted positive, how many correct? |
| Recall | TP / (TP+FN) | Of actual positive, how many found? |
| F1 Score | 2 * (P*R) / (P+R) | Harmonic mean of precision & recall |
Learn it in a lesson: Classification Basics
Working with Data
| Concept | Syntax | Example |
|---|---|---|
| Load a CSV | pd.read_csv(path) | df = pd.read_csv('sales.csv') |
| First look Do this before any modelling. Most bad models start with unread data. | df.head() / df.info() / df.describe() | df.info() # columns, types, how many are missing |
| Missing values Fill using the TRAINING set's median only, or you leak information into the test set. | df.isna().sum() / df.fillna() | df['age'] = df['age'].fillna(df['age'].median()) |
| Encode categories Never feed raw text labels to a numeric model. | pd.get_dummies | OneHotEncoder | X = pd.get_dummies(df, columns=['city']) |
| Scale features Fit on train, transform on test. Fitting on both is the classic leak. | StandardScaler() | X = StandardScaler().fit_transform(X) |
Learn it in a lesson: Data Preprocessing · Pandas: Inspecting Data: head, tail, info, describe
Training & Evaluation
| Concept | Syntax | Example |
|---|---|---|
| Split Set random_state so the split is the same next time. | train_test_split(X, y, test_size=.2, random_state=0) | X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=.2, random_state=0) |
| Fit and predict | .fit(X, y) / .predict(X) | model.fit(X_tr, y_tr); preds = model.predict(X_te) |
| Cross-validation One split can flatter a model by luck. Five tells you more. | cross_val_score(model, X, y, cv=5) | cross_val_score(model, X, y, cv=5).mean() |
| Confusion matrix Shows WHICH errors you make, which accuracy hides entirely. | confusion_matrix(y_true, y_pred) | confusion_matrix(y_te, preds) |
| Overfitting The model memorised. Simplify it, regularise it, or get more data. | train score >> test score | 1.00 on train, 0.71 on test |
| Class imbalance With 99% negatives, a model that always says no scores 99% and is useless. | class_weight='balanced' | LogisticRegression(class_weight='balanced') |
Learn it in a lesson: Evaluating AI Models: F1, ROC, Perplexity, BLEU, WER · Python for Machine Learning · Handling Imbalanced Data
LLMs & Prompting
| Concept | Syntax | Example |
|---|---|---|
| Temperature Lower is more repeatable, not more correct. | temperature: 0-2 | 0 for extraction, 0.7 for drafting |
| Token Both cost and context limits are counted in tokens, not words. | a chunk of text, roughly 4 characters of English | "unbelievable" is 3-4 tokens |
| Embedding | text to vector | cosine similarity compares meaning, not spelling |
| RAG Grounds an answer in your data instead of the model's memory. | retrieve, then generate | Search your documents, put the hits in the prompt |
| Hallucination Ask for sources you can check, then check them. | confident and wrong | An invented citation that looks perfectly real |
Learn it in a lesson: Large Language Models Architecture (GPT, LLaMA, Mistral) · Building Retrieval-Augmented Generation (RAG) Systems