AI and machine learning basics
What you’ll learn
Section titled “What you’ll learn”- How artificial intelligence, machine learning, deep learning and large language models fit inside one another.
- The four learning styles (supervised, unsupervised, reinforcement, self-supervised) and what each needs from your data.
- The workflow every ML project follows, shown with a small scikit-learn model you can run.
- Why accuracy alone misleads, and which metric to choose instead.
- When machine learning is the wrong tool, and the mistakes that break models in production.
Summary
Section titled “Summary”Artificial intelligence (AI) is the broad goal: software that performs tasks we associate with human judgment. Machine learning (ML) is the dominant way to get there: instead of writing rules, you give an algorithm examples and it fits a function to them. Deep learning (DL) is ML with many-layered neural networks, and large language models (LLMs) are deep networks trained to predict the next token of text. Each term is a subset of the one before it.
The practical skill is less about algorithms than about discipline: framing the problem, holding out honest test data, picking a metric that matches the business cost, and watching the model after release.
AI, ML, DL and LLMs: how the terms nest
Section titled “AI, ML, DL and LLMs: how the terms nest”| Term | What it means | Typical example |
|---|---|---|
| AI | Any technique that makes software act intelligently, including hand-written rules and search | A rules engine that routes support tickets |
| Machine learning | Models that learn a mapping from data instead of from explicit rules | Gradient-boosted trees that predict late deliveries |
| Deep learning | ML with multi-layer neural networks that learn their own features | An image model that reads damaged-parcel photos |
| Generative AI and LLMs | Deep models that produce new text, code, images or audio | A chat assistant that drafts replies to customers |
Two further terms appear in discussion but describe systems that do not exist today: artificial general intelligence (AGI), a system that matches people across intellectual tasks, and artificial superintelligence (ASI), one that exceeds them. Every system in production today is narrow AI, including the largest LLMs: they are broad, but they are still trained for and evaluated on bounded tasks.
A useful rule: a classical ML model works on features you engineered, a deep model learns features from raw input, and an LLM is a pre-trained deep model you adapt with prompts or retrieved context rather than by training it yourself.
Four ways a model learns
Section titled “Four ways a model learns”Supervised learning fits labeled examples (input and known answer). Classification predicts a category (spam or not), regression predicts a number (a price). It needs labels, which are often the expensive part. Common algorithms: linear and logistic regression, decision trees, random forests, gradient boosting, neural networks.
Unsupervised learning finds structure in unlabeled data: clustering customers into segments (k-means, DBSCAN), reducing dimensions (PCA), or flagging anomalies. There is no ground truth, so evaluation depends on whether the result is useful to a person or a downstream task.
Reinforcement learning (RL) trains an agent that acts in an environment and receives rewards or penalties. It suits sequential decisions such as game play and robot control. It needs a simulator or a safe way to explore, which is why it is rare in ordinary business software.
Self-supervised learning creates the labels from the data itself: hide the next word and predict it. This is how LLMs are pre-trained, and it is what lets them learn from text that nobody labeled.
The ML workflow, with a runnable example
Section titled “The ML workflow, with a runnable example”Every project follows the same loop:
- Define the problem and the success metric in business terms.
- Collect and label data.
- Explore the data and establish a trivial baseline.
- Prepare features, and split into training and test sets before any fitting.
- Train a model.
- Evaluate on data the model has never seen.
- Deploy, monitor, and retrain when the data shifts.
The example below runs steps 3 to 6 for an invented online shop, OrdersPortal, that wants to predict which orders will arrive late. The data is synthetic so the script is self-contained.
To run it: Python 3.11 or later, pip install scikit-learn (verified with scikit-learn 1.9.1 and NumPy 2.4.6), then python late_orders.py.
# late_orders.py - predict late deliveries for an invented OrdersPortal shopimport numpy as npfrom sklearn.dummy import DummyClassifierfrom sklearn.linear_model import LogisticRegressionfrom sklearn.metrics import classification_reportfrom sklearn.model_selection import train_test_splitfrom sklearn.pipeline import make_pipelinefrom sklearn.preprocessing import StandardScaler
rng = np.random.default_rng(42)n = 2_000
# Features: distance to customer (km), items in the order, warehouse backlog (orders)distance_km = rng.uniform(1, 400, n)items = rng.integers(1, 12, n)backlog = rng.integers(0, 300, n)
# Synthetic ground truth: long distance and a busy warehouse make lateness likelierscore = 0.012 * distance_km + 0.015 * backlog + 0.1 * items - 8.0late = rng.random(n) < 1 / (1 + np.exp(-score))
X = np.column_stack([distance_km, items, backlog])X_train, X_test, y_train, y_test = train_test_split( X, late, test_size=0.25, random_state=42, stratify=late)
baseline = DummyClassifier(strategy="most_frequent").fit(X_train, y_train)model = make_pipeline(StandardScaler(), LogisticRegression()).fit(X_train, y_train)
print(f"late share in data: {late.mean():.1%}")print(f"baseline accuracy: {baseline.score(X_test, y_test):.3f}")print(f"model accuracy: {model.score(X_test, y_test):.3f}")print(classification_report(y_test, model.predict(X_test), target_names=["on time", "late"]))
new_order = [[350, 6, 280]] # far away, warehouse under pressureprint(f"P(late) for the new order: {model.predict_proba(new_order)[0, 1]:.2f}")Output:
late share in data: 14.8%baseline accuracy: 0.852model accuracy: 0.868 precision recall f1-score support
on time 0.89 0.96 0.93 426 late 0.61 0.31 0.41 74
accuracy 0.87 500 macro avg 0.75 0.64 0.67 500weighted avg 0.85 0.87 0.85 500
P(late) for the new order: 0.71Three lessons sit in that output:
- The baseline matters. A “model” that always answers “on time” scores 85.2% accuracy, because only 14.8% of orders are late. The real model’s 86.8% looks good until you see how little it adds.
- Accuracy hides the failure. Recall for the late class is 0.31: the model misses about two of every three late orders, which is the case the business cares about.
- The pipeline prevents leakage.
make_pipelinefits the scaler on the training split only. Scaling the whole dataset before splitting would let test-set statistics leak into training and flatter the score.
The next steps would be to lower the decision threshold (trading precision for recall), add features, or try a tree-based model, and to judge each change by late-order recall rather than accuracy.
Choosing an evaluation metric
Section titled “Choosing an evaluation metric”| Task | Metrics | Use when |
|---|---|---|
| Classification | Precision, recall, F1, ROC AUC, PR AUC | Classes are imbalanced or errors have unequal cost (almost always) |
| Classification | Accuracy | Classes are balanced and errors cost the same |
| Regression | MAE, RMSE, R² | MAE for typical error in real units; RMSE when large errors hurt more |
| Ranking and retrieval | NDCG, MRR, recall@k | Order of results matters, as in search and RAG retrieval |
| Clustering | Silhouette score, Davies-Bouldin index | No labels exist; treat as a guide, not proof |
| Text generation | Task-specific graded test sets, human review, model-graded rubrics | Overlap scores such as BLEU and ROUGE correlate weakly with quality for open-ended output |
Pick the metric before training. Ask what a false positive and a false negative each cost: a missed fraud case and a blocked honest customer are different errors with different prices.
Deep learning and the transformer
Section titled “Deep learning and the transformer”Deep learning stacks layers of simple units so that early layers learn basic patterns and later layers combine them. The architecture follows the data:
| Architecture | Suited to |
|---|---|
| Convolutional networks (CNN) | Images and video |
| Recurrent networks (RNN, LSTM, GRU) | Sequences; largely replaced by transformers |
| Transformer | Text, code, and increasingly images and audio |
| Diffusion models | Image, audio and video generation |
| Generative adversarial networks (GAN) | Synthetic images and data |
The transformer, introduced in the 2017 paper “Attention Is All You Need”, replaced recurrence with self-attention: every token computes how much to attend to every other token, in parallel. That parallelism made training on very large datasets practical, and it is the base of nearly every current language model.
Deep learning needs far more data and compute than classical ML, and its decisions are harder to explain. On tabular business data (rows and columns), gradient-boosted trees are usually as good or better, and much cheaper.
How large language models fit in
Section titled “How large language models fit in”An LLM is a transformer pre-trained with self-supervision on a very large text corpus. Five terms carry most conversations about them:
- Token: the unit a model reads and writes, roughly a word fragment. Limits and prices are counted in tokens.
- Context window: the maximum number of tokens the model can consider at once, covering both your input and its output. As of November 2026 this ranges from a few thousand tokens on small local models to about one million on several hosted models; check the provider’s model page for the exact figure.
- Fine-tuning: further training of a pre-trained model on a smaller, task-specific dataset. Most teams never need it; prompting and retrieval come first.
- Retrieval-augmented generation (RAG): fetching relevant documents at request time and placing them in the prompt, so answers rest on your data.
- Hallucination: fluent output that is false. The model predicts plausible text; it does not look facts up unless you give it a tool or documents.
The AI abbreviations page expands the acronyms you will meet next to these terms. For most application teams, using an LLM is an integration problem, not a training problem. See How a prompt becomes a response for the request path and LLM model selection for choosing a model.
Common tools
Section titled “Common tools”Python is the default language. In practice that means scikit-learn (with XGBoost or LightGBM) for classical ML, PyTorch for deep learning, pandas or Polars for data, and a managed platform such as Amazon SageMaker, Azure Machine Learning or Google Gemini Enterprise Agent Platform (formerly Vertex AI) when a team needs hosted training and serving.
Common mistakes
Section titled “Common mistakes”- No baseline. Without a trivial comparison (most frequent class, last week’s value) you cannot tell whether the model learned anything.
- Data leakage. A feature that would not be known at prediction time, or preprocessing fitted on the full dataset, produces excellent test scores and a useless model. Split first, then fit everything inside a pipeline.
- Optimizing accuracy on imbalanced data. As the example shows, accuracy rewards ignoring the rare class.
- Tuning on the test set. Each time you adjust the model after looking at test results, the test set becomes less honest. Keep a separate validation split or use cross-validation.
- Random splits on time-ordered data. Predicting next month from a random sample that includes next month is leakage. Split by time.
- Ship and forget. Customer behavior, prices and upstream systems change. A model with no monitoring degrades silently (data drift).
- Reaching for an LLM first. A classifier over a few numeric columns does not need a language model; it will be slower, costlier and harder to test.
When machine learning is the wrong tool
Section titled “When machine learning is the wrong tool”- The rule is known and stable. Tax bands, validation rules and routing by ZIP code are code, not models.
- You have little or biased data. A model reproduces the patterns in its training data, including unfair ones.
- Errors are unacceptable and unexplainable decisions are not allowed. Some regulated decisions need a traceable rule or, at minimum, an interpretable model and human review.
- Nobody can own it after launch. A model is a product with monitoring, retraining and incident response, not a one-off deliverable.
- A simpler statistic answers the question. A moving average or a SQL query often does.
Interview questions
Section titled “Interview questions”1. What is the difference between AI, machine learning and deep learning? AI is the goal of software that behaves intelligently, by any method. Machine learning is the subset that learns behavior from data. Deep learning is the subset of ML that uses multi-layer neural networks and learns its own features from raw input. LLMs are deep learning models trained on text.
2. What is overfitting and how do you detect it? The model memorizes noise in the training data and fails to generalize. The sign is a large gap between training and validation scores. Remedies: more data, a simpler model, regularization, early stopping, and cross-validation to measure it reliably.
3. A fraud model reports 99% accuracy. Is it good? Not on that evidence. If 1% of transactions are fraud, predicting “not fraud” every time scores 99%. Ask for precision and recall on the fraud class, the PR curve, and the cost of each error type, then choose a threshold from those costs.
4. What is data leakage? Give an example. Information reaches training that will not exist at prediction time. Examples: a “refund issued” column when predicting complaints, scaling with statistics computed over the test rows, or the same customer appearing in both splits. The symptom is a test score that production never matches.
5. Senior scenario: a delivery-delay model performed well for six months and its precision has now halved. How do you investigate and what do you change? First confirm with labeled recent data that the drop is real and not a reporting change. Then compare recent feature distributions with the training set (data drift): a new carrier, a new warehouse or a seasonal peak changes inputs. Check for concept drift, where the same inputs now lead to different outcomes, and for upstream schema or unit changes. Short term, raise the threshold or fall back to a rule so the business is protected. Long term, retrain on recent data with a time-based split, add drift and performance monitors with alerts, schedule retraining, and keep a labeled sample flowing so quality is measured continuously rather than discovered by complaint.
6. When would you choose gradient-boosted trees over a neural network? For tabular data of small to medium size, where trees train quickly, need little preprocessing, handle mixed feature types and are easier to explain. Neural networks earn their cost on unstructured input such as images, audio and text, or at very large data scale.
Key takeaways
Section titled “Key takeaways”- AI contains ML, which contains deep learning, which contains LLMs.
- Supervised learning needs labels; self-supervised learning is what made LLMs possible.
- Always compare against a trivial baseline and evaluate on unseen data.
- Choose the metric from the cost of each error, not from habit.
- Leakage, imbalance and drift cause most production failures.
- Use rules when the logic is known, classical ML for tabular prediction, and LLMs for language tasks.
Related pages
Section titled “Related pages”- How a prompt becomes a response
- AI abbreviations
- LLM model selection
- AI models in 2026
- AI agents overview
- RAG vs agentic RAG
- Running LLMs locally
Official references
Section titled “Official references”- scikit-learn user guide: model evaluation (stable docs; sample verified with scikit-learn 1.9.1)
- scikit-learn user guide: common pitfalls and data leakage
- scikit-learn API:
Pipeline - Vaswani et al., “Attention Is All You Need” (2017)
- Google Machine Learning Crash Course
- Anthropic models overview (context window figures, checked November 2026)