Machine Learning: Fitting Useful Patterns From Data
📑 On this page
- A concrete example: detecting spam
- Traditional programming and machine learning
- Supervised learning
- Unsupervised learning
- Reinforcement learning
- Features represent inputs
- Parameters are learned
- Training, validation, and test data
- Generalization
- Overfitting and underfitting
- Metrics express tradeoffs
- Correlation and proxy variables
- Distribution shift and drift
- Feedback loops
- When not to use machine learning
- Knowledge check
- The one idea to remember
Traditional programs often contain rules written directly by developers:
IF sender is blocked
THEN mark message as spamThat works for clear cases, but no developer can list every wording, sender pattern, and behavior used in spam.
Machine learning fits a model from data so it can make predictions, classifications, or decisions on new inputs.
People design the task and learning process. The model learns adjustable patterns within those choices.
A concrete example: detecting spam
A training dataset contains emails labeled:
- Spam
- Not spam
The learning algorithm adjusts a model so its predictions match the labels as often as possible.
It may learn statistical associations involving:
- Words and phrases
- Sender behavior
- Link patterns
- Message formatting
- Previous reports
For a new email, the trained model produces a spam score. The service applies a threshold and other rules to choose an action.
The model has not memorized a complete definition of deception. It learned patterns from examples.
Traditional programming and machine learning
A simplified comparison is:
Traditional:
rules + input -> output
Machine learning training:
examples + desired outcomes -> fitted model
Inference:
fitted model + new input -> predicted outputThe comparison is useful but incomplete.
ML systems still contain ordinary code for data collection, validation, features, serving, permissions, user interfaces, and monitoring.
Explicit rules may also wrap the model, such as never allowing a predicted recommendation to bypass a legal restriction.
Supervised learning
Supervised learning uses examples paired with target outputs.
Tasks include:
- Classification: Choose a category, such as spam or legitimate.
- Regression: Predict a numeric value, such as delivery time.
Labels may come from:
- Human reviewers
- Historical outcomes
- Sensors
- Existing business processes
- Another system
Labels can be noisy or biased. Historical loan approval is not the same as true creditworthiness, especially if past decisions denied some groups the chance to demonstrate repayment.
The target must represent the intended problem.
Unsupervised learning
Unsupervised learning works without a direct target label for each example.
It can:
- Group similar records
- Reduce dimensions
- Find unusual patterns
- Learn representations
A retailer might cluster customers by purchase behavior, but the clusters do not arrive with objective meanings. People interpret and name them.
An unusual transaction is not automatically fraudulent. Anomaly detection identifies difference, which may come from a new legitimate behavior or a data error.
Unsupervised results need validation in their real context.
Reinforcement learning
In reinforcement learning, an agent takes actions in an environment and receives rewards or penalties.
The goal is to learn a policy that maximizes expected cumulative reward.
Examples include:
- Game play
- Robot control
- Resource allocation
- Some recommendation strategies
Reward design is critical.
If a cleaning robot receives reward only for collecting dirt, it might learn to create dirt or avoid finishing. The system optimizes the specified signal, not the designer's unstated intention.
Features represent inputs
A feature is an input value used by a model.
For delivery prediction, features might include:
- Distance
- Time of day
- Weather
- Restaurant preparation history
- Current traffic
Modern neural networks can learn internal representations from raw text, audio, or pixels, but input choice and preprocessing still matter.
A feature can leak future information. Using the actual delivery completion timestamp to predict delivery time would produce excellent training results and an unusable deployed model.
Parameters are learned
A model has adjustable values called parameters.
Training changes parameters to reduce a loss function, which measures disagreement between predictions and desired outcomes.
A simple linear model may learn one weight per feature. A neural network can contain millions or billions of parameters.
Parameter count alone does not measure usefulness, truth, or intelligence.
Architecture, data, objective, training process, and deployment conditions all contribute.
Training, validation, and test data
Data is commonly divided into:
- Training set: Used to fit parameters
- Validation set: Used to choose settings and compare models
- Test set: Used for a final independent estimate
If the test set influences repeated design decisions, it becomes another validation set and loses independence.
Splits should reflect deployment. Randomly splitting records may leak nearly identical examples or future information across sets.
For time-dependent prediction, train on earlier data and test on later data.
Generalization
The goal is not merely to perform well on examples already seen. It is to generalize to new relevant inputs.
A model may memorize training data and fail when:
- Wording changes
- Lighting differs
- New users arrive
- Economic conditions shift
- Attackers adapt
Evaluation should represent the variation expected after deployment.
No finite test proves performance on every future input, so monitoring and fallback behavior remain necessary.
Overfitting and underfitting
Overfitting occurs when a model learns training-specific noise or detail and performs poorly on new data.
Underfitting occurs when a model is too simple or insufficiently trained to capture useful patterns.
Ways to reduce overfitting include:
- More representative data
- Regularization
- Simpler models
- Early stopping
- Data augmentation
- Cross-validation
The best model is not the one with the lowest training error. It is the one that performs acceptably on the real task.
Metrics express tradeoffs
For classification:
- Precision: Of predicted positives, how many were correct?
- Recall: Of actual positives, how many were found?
- Accuracy: What fraction of all predictions were correct?
If only 1 in 10,000 transactions is fraud, predicting "not fraud" always gives high accuracy and no value.
Metrics should reflect impact. Missing fraud and blocking legitimate payments have different costs.
Evaluate by important subgroups and error types, not only one aggregate number.
Correlation and proxy variables
A model can use a feature correlated with a protected characteristic even when the characteristic itself is removed.
Postal code may act as a proxy for income, ethnicity, or historical segregation.
Removing one column does not automatically remove unfair influence.
Fairness analysis needs social and domain context, outcome measurement, and consideration of who bears false positives and false negatives.
Technical metrics help, but cannot decide values alone.
Distribution shift and drift
The data encountered after deployment may differ from training data.
Causes include:
- Seasonal behavior
- New products
- Policy changes
- Economic events
- Sensor replacement
- User adaptation
- Adversarial behavior
Data drift means input patterns change. Concept drift means the relationship between inputs and desired output changes.
Monitor distributions, performance, feedback quality, and business outcomes. Retraining on new data is useful only if the new labels and process remain trustworthy.
Feedback loops
Model decisions can change the data later observed.
A recommendation system shows some content more often, generating more clicks for that content. A predictive-policing system sends officers to selected locations, producing more recorded incidents there.
The resulting data is not a neutral picture of the world; it is partly produced by the model's earlier actions.
Experiments, exploration, causal analysis, and careful policy review help identify feedback loops.
When not to use machine learning
Prefer explicit logic when:
- The rule is known and stable.
- Exact correctness is required.
- Data is too limited or unreliable.
- A simple calculation solves the problem.
- The cost of errors is unacceptable without explanation.
An ML model adds data pipelines, evaluation, monitoring, retraining, and uncertain outputs.
Use it when learned patterns offer value that simpler approaches cannot deliver responsibly.
Knowledge check
- How does supervised learning differ from unsupervised learning?
- Why can historical labels encode bias?
- What is generalization?
- Why can accuracy be misleading for rare events?
- How can a model create a feedback loop in its future training data?
The one idea to remember
Machine learning fits patterns from data, but people define the data, target, objective, metrics, and deployment. A useful model must generalize to real conditions and be monitored as those conditions change.