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.
- Define the problem: Decide what the model should predict, classify, rank, or detect.
- Collect data: Gather representative examples from reliable sources.
- Prepare the data: Handle missing values, incorrect records, inconsistent formats, duplicates, and outliers where appropriate.
- Select features: Choose or create variables that contain useful information for the task.
- Split the data: Separate records into training, validation, and test sets.
- Choose an algorithm: Select a suitable learning method based on the task and data.
- Train the model: Allow the algorithm to learn parameters from the training data.
- Tune the model: Adjust hyperparameters using validation data or cross-validation.
- Evaluate performance: Measure results on data that was not used to fit the model.
- 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 type | Training signal | Typical task | Example |
|---|---|---|---|
| Supervised learning | Labelled examples | Classification or regression | Predicting whether a transaction is fraudulent |
| Unsupervised learning | No target labels | Clustering or pattern discovery | Grouping customers by purchasing behaviour |
| Semi-supervised learning | Few labelled and many unlabelled examples | Classification with limited labels | Classifying images when only some are annotated |
| Reinforcement learning | Rewards and penalties | Sequential decision-making | Learning a control strategy in a simulated environment |
Common machine learning algorithms
| Algorithm | Primary use | How it works |
|---|---|---|
| Linear regression | Numerical prediction | Fits a linear relationship between input variables and a continuous target. |
| Logistic regression | Classification | Estimates the probability that a record belongs to a class. |
| Decision tree | Classification and regression | Divides data through a sequence of feature-based decision rules. |
| Random forest | Classification and regression | Combines predictions from multiple decision trees. |
| Gradient boosting | Classification and regression | Builds models sequentially so that later models reduce earlier errors. |
| Support vector machine | Classification and regression | Finds a boundary that separates classes with a large margin. |
| K-nearest neighbours | Classification and regression | Predicts from the outcomes of nearby training examples. |
| K-means clustering | Clustering | Assigns records to clusters based on distance from cluster centres. |
| Principal component analysis | Dimensionality reduction | Transforms correlated variables into a smaller number of components. |
| Neural network | Complex classification, regression, and representation learning | Uses 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 hours | Support tickets | Months subscribed | Renewed |
|---|---|---|---|
| 42 | 1 | 18 | Yes |
| 8 | 6 | 3 | No |
| 35 | 2 | 12 | Yes |
| 12 | 5 | 4 | No |
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.
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.
| Dataset | Purpose |
|---|---|
| Training set | Used by the algorithm to learn model parameters. |
| Validation set | Used to compare models, adjust hyperparameters, and make development decisions. |
| Test set | Used 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
| Metric | What it measures |
|---|---|
| Accuracy | The proportion of all predictions that are correct. |
| Precision | The proportion of predicted positive cases that are actually positive. |
| Recall | The proportion of actual positive cases detected by the model. |
| F1-score | The harmonic mean of precision and recall. |
| ROC-AUC | The model’s ability to rank positive cases above negative cases across decision thresholds. |
| Log loss | The 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
| Metric | What it measures |
|---|---|
| Mean absolute error | The average absolute difference between predicted and actual values. |
| Mean squared error | The average squared prediction error, which gives larger errors more weight. |
| Root mean squared error | The square root of mean squared error, expressed in the target’s units. |
| R-squared | The 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.
| Condition | Training performance | Validation performance | Possible response |
|---|---|---|---|
| Underfitting | Poor | Poor | Improve features, use a more capable model, or adjust training. |
| Suitable fit | Good | Similar and acceptable | Confirm on the test set and monitor after deployment. |
| Overfitting | Very good | Noticeably worse | Use 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.
| Term | Description |
|---|---|
| Artificial intelligence | The broader field concerned with systems that perform tasks associated with intelligent behaviour. |
| Machine learning | A subset of artificial intelligence that learns patterns from data. |
| Deep learning | A 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.
TutorialKart.com