Unit 13: Machine learning
I. Orientation — Learning from Data
Machine learning (ML) is a branch of artificial intelligence in which computer systems identify patterns in data and use those patterns to make predictions or decisions. Rather than receiving an explicit rule for every possible input, an ML system learns a mathematical relationship from examples.
Defining properties:
- Data-driven learning: An algorithm extracts useful regularities from observations such as images, measurements, text, or transaction records.
- Model: The learned relationship is represented by a model, such as a linear equation, decision tree, or neural network.
- Features: Each example is described by measurable input variables. For a house-price model, features might include floor area, age, and number of rooms.
- Target: In supervised learning, the target is the value or category to be predicted, such as a house’s price or an email’s spam status.
- Training: Model parameters are adjusted using a training dataset so that predictions increasingly agree with known examples.
- Inference: After training, the model processes unseen input and produces a prediction, classification, grouping, or action.
- Generalization: A useful model performs well on new data, not merely on the examples used during training.
- Evaluation: Numerical metrics—such as accuracy or mean squared error—measure performance on data excluded from training.
- Python ecosystem: Python supports ML through libraries including NumPy for numerical arrays, pandas for tabular data, Matplotlib for visualization, and scikit-learn for standard algorithms.
II. Introduction — Principles and Workflow
A. Introduction
Machine learning replaces or supplements manually written decision rules with models whose parameters are estimated from data.
-
Traditional programming: A programmer supplies data and explicit rules to produce answers.
TEXTdata + programmed rules → answers
A tax calculator, for example, directly implements published rates and conditions. -
Machine learning: During training, examples and known answers are supplied to a learning algorithm, which produces a model.
TEXTtraining data + known answers → learning algorithm → model new data + model → predicted answers
For spam detection, the algorithm learns associations between message features and labels rather than relying only on hand-written word lists. -
Dataset structure: A dataset commonly contains rows representing observations and columns representing variables.
- Feature matrix (X): An (n \times p) matrix, where (n) is the number of observations and (p) is the number of input features.
- Target vector (y): A vector of (n) known outcomes used in supervised learning.
- Example: If 500 houses are represented by area, age, and room count, then (X) has dimensions (500 \times 3), while (y) contains 500 prices.
-
Model parameters: Training determines internal values that control predictions. A simple linear regression model is:
[
\hat{y}=w_0+w_1x_1+w_2x_2+\cdots+w_px_p
]
Here, (\hat{y}) is the predicted output; (x_j) is feature (j); (w_j) is its learned weight; (w_0) is the intercept; and (p) is the number of features. -
Loss function: A loss quantifies prediction error so that training has an objective to minimize. Mean squared error for regression is:
[
\operatorname{MSE}=\frac{1}{n}\sum_{i=1}^{n}(y_i-\hat{y}_i)^2
]
Here, (n) is the number of evaluated observations, (y_i) is the actual value, and (\hat{y}_i) is the prediction. -
General workflow:
- Define the task: Specify the required output, such as predicting sales or grouping customers.
- Collect data: Obtain relevant, sufficiently representative observations.
- Prepare data: Handle missing values, remove errors, encode categories, and scale features when necessary.
- Split data: Reserve independent examples for validation or testing.
- Choose and train a model: Fit an algorithm appropriate to the task and data.
- Evaluate and refine: Measure performance, tune settings, and inspect errors.
- Deploy and monitor: Apply the model to new inputs and watch for declining performance.
-
Training and testing split: Separating data provides a more credible measure of generalization.
PYTHONfrom sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42 )
Xcontains features,ycontains targets,test_size=0.2reserves 20% for testing, andrandom_state=42makes the split reproducible. -
Underfitting: A model underfits when it is too simple to capture important structure. It performs poorly on both training and test data; for example, a straight line may inadequately represent a strongly curved relationship.
-
Overfitting: A model overfits when it learns training-specific noise. Training performance becomes very high while performance on unseen data remains substantially worse.
-
Hyperparameters: These are choices set before training, unlike parameters learned from data. Examples include a decision tree’s maximum depth and the number of neighbors used by a k-nearest-neighbors model.
-
Data quality and ethics: Model behavior reflects the data and objective used to create it.
- Bias: Historical or unrepresentative data can produce systematically unfair predictions.
- Privacy: Personal data must be collected, stored, and processed with appropriate authorization and safeguards.
- Interpretability: High-impact decisions may require explanations of which features influenced an output.
- Drift: Changes in populations or real-world conditions can make past training data less representative over time.
B. Applications and Limitations
Machine learning is valuable when patterns are too numerous, variable, or complex to express conveniently as fixed rules, but it does not automatically establish truth or causation.
- Common applications: Classification supports fraud and spam detection; regression forecasts prices or demand; clustering discovers customer segments; vision models identify objects; and language models process or generate text.
- Dependence on examples: Sparse, inaccurate, outdated, or biased training data generally leads to unreliable models.
- Correlation rather than causation: A model may exploit a statistical association without showing that one feature causes the outcome.
- Uncertainty: Predictions are estimates, not guarantees. A probability of
0.80represents model confidence under its assumptions, not certainty that an event will occur. - Operational constraints: Accuracy must be balanced against training cost, prediction speed, memory use, interpretability, privacy, and the consequences of errors.
- Human oversight: Medical, financial, legal, and safety-related outputs require domain controls and accountable human judgment.
III. Types of Machine Learning — Learning Signals and Objectives
A. Types of machine learning
Machine-learning approaches are primarily distinguished by the feedback available during training and the kind of output the system must learn.
-
Supervised learning
-
Learning signal: Every training example pairs features (X) with a known target (y). The model learns a mapping (f) such that:
[
\hat{y}=f(X)
]
Here, (X) is the input, (f) is the learned function, and (\hat{y}) is its predicted target. -
Classification: The target is a discrete class. Examples include
spamversusnot_spam, or one of several plant species. -
Regression: The target is a continuous numerical value, such as temperature, sales revenue, or journey time.
-
Algorithms: Common choices include linear regression, logistic regression, decision trees, random forests, support vector machines, and neural networks.
-
Evaluation: Classification may use accuracy, precision, recall, or F1-score; regression may use mean absolute error, mean squared error, or (R^2).
-
Python example:
PYTHONfrom sklearn.linear_model import LinearRegression model = LinearRegression() model.fit(X_train, y_train) predictions = model.predict(X_test)
fitestimates model parameters from labeled training data, whilepredictapplies the learned relationship to unseen features.
-
-
Unsupervised learning
- Learning signal: Training data contains features (X) but no supplied target (y). The algorithm searches for structure within the inputs.
- Clustering: Observations are divided into groups whose members are similar. K-means, for example, assigns each point to one of (k) clusters.
- Dimensionality reduction: A large feature set is represented using fewer variables while preserving important variation. Principal component analysis is a standard method.
- Association discovery: Frequent relationships among items can reveal patterns such as products often purchased together.
- Evaluation challenge: Because no authoritative labels may exist, usefulness can depend on measures such as cluster cohesion and on domain interpretation.
-
Semi-supervised learning
- Learning signal: Training combines a small labeled dataset with a larger unlabeled dataset.
- Purpose: It reduces the need for expensive manual labeling while exploiting the distribution of available inputs.
- Example: A small collection of medically reviewed scans can be combined with many unannotated scans, provided that assumptions about their similarity are justified.
- Limitation: Incorrect automatically generated labels can reinforce errors and reduce model quality.
-
Reinforcement learning
- Learning signal: An agent interacts with an environment, selects actions, and receives rewards or penalties rather than correct answers for every step.
- Core components:
- State (s): The current situation observed by the agent.
- Action (a): A choice available to the agent.
- Reward (r): Numerical feedback received after an action.
- Policy (\pi(a\mid s)): A strategy giving the probability of action (a) in state (s).
- Objective: The agent seeks to maximize cumulative future reward, not necessarily the immediate reward from one action.
- Exploration and exploitation: Exploration tests unfamiliar actions to gain information, while exploitation selects actions currently believed to be best.
- Applications: Reinforcement learning is used in game-playing, robotics, resource allocation, and sequential control.
- Limitation: Training may require many interactions, and a poorly designed reward can encourage unintended behavior.
B. Comparison, Selection, and Boundaries
Selecting a learning type depends on the available data, required output, and form of feedback.
- Supervised versus unsupervised: Supervised learning predicts predefined targets from labeled examples; unsupervised learning discovers structure without predefined answers.
- Semi-supervised compromise: It is appropriate when unlabeled data is plentiful but reliable labels are scarce or costly.
- Reinforcement distinction: It addresses sequences of decisions whose actions influence later states and rewards, unlike ordinary supervised prediction from fixed examples.
- Hybrid systems: A practical system may first use unsupervised clustering to create features and then train a supervised classifier, or use supervised learning to support a reinforcement-learning policy.
- Task-based selection: Known category labels indicate classification; known numerical targets indicate regression; unknown group structure indicates clustering; and repeated action with delayed feedback indicates reinforcement learning.
- No universally best method: Algorithm performance depends on dataset size, feature quality, assumptions, computational resources, evaluation criteria, and the real cost of different errors.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →