Machine learning is a branch of artificial intelligence in which computer systems learn patterns from data and use those patterns to make predictions, classifications, recommendations, or decisions. Instead of defining every rule manually, developers train a model using examples and evaluate how well it performs on previously unseen data.

This tutorial explains how machine learning works, the main types of machine learning, common algorithms, the model-training process, practical examples, evaluation metrics, and the issues that should be checked before a model is used in a real application.

What exactly is machine learning?

Machine learning is the process of creating algorithms that improve their performance by learning from data. The algorithm examines examples, identifies relationships between input values and expected outcomes, and produces a mathematical model that can process new data.

For example, an email filtering model can learn from messages labelled as spam or not spam. During training, it identifies patterns associated with each class. When a new email arrives, the trained model estimates whether that message belongs to the spam category.

A machine learning system generally contains the following elements:

  • Data: Examples used to train, validate, and test the model.
  • Features: Measurable properties supplied to the learning algorithm.
  • Labels or targets: Expected outputs used in supervised learning.
  • Algorithm: The procedure used to identify patterns in the data.
  • Model: The learned representation produced by training.
  • Prediction: The output generated when the model receives new input.
  • Evaluation metric: A measurement used to determine how well the model performs.

How machine learning works from data to prediction

A typical machine learning workflow begins with a clearly defined problem and ends with a monitored model that processes new data. The exact steps vary, but the following sequence is common.

  1. Define the problem: Decide what the model should predict, classify, rank, or detect.
  2. Collect data: Gather representative examples from reliable sources.
  3. Prepare the data: Handle missing values, incorrect records, inconsistent formats, duplicates, and outliers where appropriate.
  4. Select features: Choose or create variables that contain useful information for the task.
  5. Split the data: Separate records into training, validation, and test sets.
  6. Choose an algorithm: Select a suitable learning method based on the task and data.
  7. Train the model: Allow the algorithm to learn parameters from the training data.
  8. Tune the model: Adjust hyperparameters using validation data or cross-validation.
  9. Evaluate performance: Measure results on data that was not used to fit the model.
  10. Deploy and monitor: Use the model in an application and watch for performance degradation, data drift, bias, and operational failures.

Types of machine learning

Machine learning methods are commonly grouped according to the kind of feedback available during training.

Supervised machine learning

Supervised learning uses labelled examples. Each training record contains input features and a known target value. The model learns a relationship between those inputs and outputs.

  • Classification: Predicts a category, such as spam or not spam.
  • Regression: Predicts a numerical value, such as a house price or delivery time.

Common supervised algorithms include linear regression, logistic regression, decision trees, random forests, support vector machines, nearest-neighbour methods, gradient-boosting algorithms, and neural networks.

Unsupervised machine learning

Unsupervised learning works with data that does not contain predefined target labels. The algorithm searches for structures, similarities, unusual records, or lower-dimensional representations.

  • Clustering: Groups similar records, such as customers with comparable behaviour.
  • Dimensionality reduction: Represents data with fewer variables while retaining useful information.
  • Anomaly detection: Identifies records that differ substantially from normal patterns.
  • Association analysis: Finds items or events that frequently occur together.

Semi-supervised machine learning

Semi-supervised learning combines a relatively small amount of labelled data with a larger collection of unlabelled data. It can be useful when obtaining raw examples is easy but assigning accurate labels is expensive or slow.

Reinforcement learning

Reinforcement learning trains an agent through interactions with an environment. The agent takes actions, observes the resulting state, and receives rewards or penalties. Its objective is to learn a policy that maximizes the expected cumulative reward.

Machine learning typeTraining signalTypical taskExample
Supervised learningLabelled examplesClassification or regressionPredicting whether a transaction is fraudulent
Unsupervised learningNo target labelsClustering or pattern discoveryGrouping customers by purchasing behaviour
Semi-supervised learningFew labelled and many unlabelled examplesClassification with limited labelsClassifying images when only some are annotated
Reinforcement learningRewards and penaltiesSequential decision-makingLearning a control strategy in a simulated environment

Common machine learning algorithms

AlgorithmPrimary useHow it works
Linear regressionNumerical predictionFits a linear relationship between input variables and a continuous target.
Logistic regressionClassificationEstimates the probability that a record belongs to a class.
Decision treeClassification and regressionDivides data through a sequence of feature-based decision rules.
Random forestClassification and regressionCombines predictions from multiple decision trees.
Gradient boostingClassification and regressionBuilds models sequentially so that later models reduce earlier errors.
Support vector machineClassification and regressionFinds a boundary that separates classes with a large margin.
K-nearest neighboursClassification and regressionPredicts from the outcomes of nearby training examples.
K-means clusteringClusteringAssigns records to clusters based on distance from cluster centres.
Principal component analysisDimensionality reductionTransforms correlated variables into a smaller number of components.
Neural networkComplex classification, regression, and representation learningUses connected layers of weighted transformations to learn patterns.

Machine learning classification example

Consider a small dataset used to predict whether a customer will renew a subscription. Each row represents a customer, while the target column records whether renewal occurred.

Usage hoursSupport ticketsMonths subscribedRenewed
42118Yes
863No
35212Yes
1254No

A supervised classification algorithm can learn from these labelled examples. After training, it receives the usage hours, ticket count, and subscription length of a new customer and returns a predicted class or renewal probability.

Train a simple machine learning model in Python

The following example trains a logistic regression classifier using a small in-memory dataset. It is intended to show the main steps: splitting data, fitting a model, making predictions, and measuring accuracy.

</>
Copy
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

# Features: usage hours, support tickets, months subscribed
X = [
    [42, 1, 18],
    [8, 6, 3],
    [35, 2, 12],
    [12, 5, 4],
    [50, 1, 24],
    [15, 4, 5],
    [30, 2, 10],
    [9, 7, 2]
]

# Labels: 1 means renewed, 0 means not renewed
y = [1, 0, 1, 0, 1, 0, 1, 0]

X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.25,
    random_state=42,
    stratify=y
)

model = LogisticRegression()
model.fit(X_train, y_train)

predictions = model.predict(X_test)
print("Predictions:", predictions)
print("Accuracy:", accuracy_score(y_test, predictions))

new_customer = [[28, 2, 11]]
renewal_probability = model.predict_proba(new_customer)[0][1]
print("Renewal probability:", renewal_probability)

Because this demonstration uses very little data, its result should not be treated as a reliable production model. A real project would require more representative records, data-quality checks, suitable validation, baseline comparisons, and monitoring.

Training, validation, and test data

Evaluating a model on the same data used for training can produce an overly optimistic result. Machine learning datasets are therefore separated according to purpose.

DatasetPurpose
Training setUsed by the algorithm to learn model parameters.
Validation setUsed to compare models, adjust hyperparameters, and make development decisions.
Test setUsed for a final estimate of performance on unseen data.

When data is limited, cross-validation can provide a more stable estimate. The training data is divided into multiple folds, and the model is trained and evaluated repeatedly using different folds.

Time-dependent data requires additional care. For forecasting or event prediction, future records must not be used to train a model that is evaluated on earlier records.

Machine learning features and labels

A feature is an input variable used by a model. A label or target is the expected outcome in supervised learning.

For a house-price model, features might include floor area, location, number of bedrooms, property age, and distance from transport. The target would be the sale price.

Feature preparation may include:

  • Converting categories into numerical representations
  • Scaling numerical values
  • Extracting date and time components
  • Combining related variables
  • Removing duplicate or irrelevant features
  • Handling missing values consistently
  • Transforming text, images, audio, or signals into model inputs

All transformations learned from data should be fitted only on the training set and then applied to validation, test, and production data. Fitting transformations on the complete dataset can introduce data leakage.

Machine learning model evaluation metrics

The correct metric depends on the prediction task and the cost of different errors.

Classification metrics

MetricWhat it measures
AccuracyThe proportion of all predictions that are correct.
PrecisionThe proportion of predicted positive cases that are actually positive.
RecallThe proportion of actual positive cases detected by the model.
F1-scoreThe harmonic mean of precision and recall.
ROC-AUCThe model’s ability to rank positive cases above negative cases across decision thresholds.
Log lossThe quality of predicted probabilities, with stronger penalties for confident incorrect predictions.

Accuracy alone can be misleading when classes are imbalanced. For example, a model may appear accurate by predicting the majority class for nearly every record while failing to identify the minority cases that matter.

Regression metrics

MetricWhat it measures
Mean absolute errorThe average absolute difference between predicted and actual values.
Mean squared errorThe average squared prediction error, which gives larger errors more weight.
Root mean squared errorThe square root of mean squared error, expressed in the target’s units.
R-squaredThe proportion of target variation explained by the model relative to a baseline.

Overfitting and underfitting in machine learning

Overfitting occurs when a model learns details and noise from the training data but performs poorly on new examples. It may produce excellent training results without learning patterns that generalize.

Underfitting occurs when a model is too simple, is trained inadequately, or does not receive suitable features. It performs poorly on both training and unseen data.

ConditionTraining performanceValidation performancePossible response
UnderfittingPoorPoorImprove features, use a more capable model, or adjust training.
Suitable fitGoodSimilar and acceptableConfirm on the test set and monitor after deployment.
OverfittingVery goodNoticeably worseUse regularization, simplify the model, gather more data, or improve validation.

Machine learning, artificial intelligence, and deep learning

Artificial intelligence, machine learning, and deep learning are related but not identical terms.

TermDescription
Artificial intelligenceThe broader field concerned with systems that perform tasks associated with intelligent behaviour.
Machine learningA subset of artificial intelligence that learns patterns from data.
Deep learningA subset of machine learning based on neural networks with multiple computational layers.

Not every artificial intelligence system uses machine learning, and not every machine learning problem requires deep learning. Traditional statistical and tree-based models may be more suitable when datasets are small, interpretability is important, or computation is limited.

Real-world machine learning examples

  • Email filtering: Classifying messages as spam, suspicious, or legitimate.
  • Recommendation systems: Ranking products, articles, videos, or music based on user and item information.
  • Demand forecasting: Estimating future product, staffing, or energy requirements.
  • Fraud detection: Identifying transactions that differ from established patterns.
  • Predictive maintenance: Estimating equipment failure risk from sensor and maintenance data.
  • Document classification: Sorting text into topics, departments, or processing queues.
  • Image analysis: Detecting, classifying, or segmenting objects in images.
  • Speech processing: Converting speech to text or identifying spoken commands.
  • Customer retention: Estimating which customers may stop using a service.
  • Quality control: Detecting defects from measurements, images, or production signals.

Limitations and risks of machine learning

A machine learning model is not automatically reliable because it achieves a high metric on one dataset. Its behaviour depends on the quality, relevance, and representativeness of the data as well as the way the problem is defined.

  • Biased data: Historical or unrepresentative data can produce unfair or unreliable outcomes.
  • Data leakage: Information unavailable at prediction time may accidentally enter the training process.
  • Concept drift: Relationships between inputs and outcomes may change after deployment.
  • Data drift: Production inputs may differ from the data used for training.
  • Incorrect metrics: A model may optimize a measurement that does not reflect the real business or safety objective.
  • Lack of interpretability: Some models are difficult to explain or audit.
  • Privacy concerns: Training data may contain personal, confidential, or regulated information.
  • Security risks: Models and data pipelines can be affected by malicious inputs, unauthorized access, or model extraction.
  • Automation bias: Users may trust model outputs without adequate human review.
  • Maintenance requirements: Models require monitoring, retraining, version control, and rollback procedures.

Machine learning project checklist

  • Define the prediction target and intended decision before selecting an algorithm.
  • Confirm that the available data represents the population and conditions where the model will operate.
  • Establish a simple baseline before testing complex models.
  • Prevent duplicate entities and future information from crossing dataset splits.
  • Choose evaluation metrics based on the consequences of false positives and false negatives.
  • Compare model performance with an existing rule, process, or statistical method.
  • Test performance across relevant user groups, locations, products, devices, and time periods.
  • Document data sources, transformations, labels, assumptions, limitations, and model versions.
  • Set thresholds and human-review rules based on the application.
  • Monitor production inputs, predictions, errors, latency, and outcome quality.
  • Provide a fallback process when the model or its dependencies are unavailable.
  • Define when the model must be retrained, reviewed, replaced, or retired.

Machine learning frequently asked questions

What exactly is machine learning?

Machine learning is a method of building computer systems that learn patterns from data. A trained model uses those learned patterns to classify information, predict values, rank options, detect unusual events, or support decisions.

What are the main types of machine learning?

The main types are supervised learning, unsupervised learning, semi-supervised learning, and reinforcement learning. They differ mainly in the kind of feedback available to the algorithm during training.

What is the difference between an algorithm and a machine learning model?

An algorithm is the learning procedure used to identify patterns. A model is the trained result produced after that algorithm processes a particular dataset.

Does machine learning always require large datasets?

No. The amount of data required depends on the task, feature quality, model complexity, noise level, and accuracy requirements. Some structured-data problems can be addressed with modest datasets, while image, audio, language, and highly variable tasks may require substantially more data.

How is a machine learning model tested?

A model is tested using data that was not used to fit or tune it. Its predictions are compared with known outcomes using task-appropriate metrics, and its performance should also be checked across relevant conditions and subgroups.

Machine learning tutorial editorial QA checklist

  • Confirm that machine learning is described as a subset of artificial intelligence rather than as a synonym for all AI.
  • Verify that supervised, unsupervised, semi-supervised, and reinforcement learning are distinguished correctly.
  • Check that classification predicts categories and regression predicts numerical values.
  • Ensure that training, validation, and test data have separate purposes.
  • Confirm that examples do not imply that a small demonstration dataset is production-ready.
  • Verify that model evaluation uses metrics appropriate for class balance and error costs.
  • Check that data leakage, overfitting, bias, and production drift are addressed.
  • Ensure that feature transformations are fitted on training data rather than on the complete dataset.
  • Confirm that deployed models include monitoring, fallback, retraining, and version-control considerations.
  • Review all real-world examples to ensure that machine learning is presented as one part of a wider operational process.