How Machine Learning Works: Data, Training, Models, Inference and Predictions
In the previous article, we explored what Artificial Intelligence is and how Machine Learning, Deep Learning, Generative AI, and Large Language Models fit into the AI ecosystem.
Now let's go one level deeper.
Have you ever wondered how a machine actually learns?
How does a computer look at thousands of examples, identify patterns, and eventually make predictions about something it has never seen before?
For example:
- How does Netflix recommend movies?
- How does a bank identify potentially fraudulent transactions?
- How can an e-commerce platform predict customer behavior?
- How can a system estimate tomorrow's product demand?
Behind many of these applications is a technology called Machine Learning (ML).
In this article, we'll understand how Machine Learning works, from collecting data to training a model and using it to make predictions.
1. What Is Machine Learning?
Machine Learning is a subset of Artificial Intelligence that allows computer systems to learn patterns from data and use those patterns to make predictions or decisions.
In traditional programming, developers explicitly write rules that determine how a system behaves.
In Machine Learning, developers provide data and select an appropriate learning algorithm. The algorithm uses that data to learn patterns by adjusting the model's internal parameters.
Let's understand the difference.
Traditional programming
Imagine you want to build a system that determines whether a customer qualifies for a discount.
You might write rules such as:
- If the customer has spent more than $500, apply a 10% discount.
- If the customer is a premium member, apply a 15% discount.
The system follows the rules you explicitly define.
Machine Learning
Now imagine you want to predict whether a customer will purchase a product.
Instead of manually defining every possible customer behavior, you provide historical customer data and purchase outcomes.
A Machine Learning model learns patterns from this information and uses them to estimate whether a new customer is likely to purchase.
The key difference: Traditional programming follows explicitly written rules, while Machine Learning uses patterns learned from data to make predictions.
2. Understanding the Machine Learning Lifecycle
Machine Learning is not simply about giving data to an algorithm and expecting intelligent results.
It involves several steps.
A typical Machine Learning lifecycle looks like this:
- Data collection.
- Data preparation.
- Feature selection and engineering.
- Model selection.
- Model training.
- Model evaluation.
- Model deployment.
- Inference and monitoring.
Let's understand each step using one practical example.
Example: Predicting House Prices
Imagine a real estate company wants to build a system that estimates property prices.
The company has historical information about thousands of properties.
It wants to use this information to train a model that can estimate the price of a new property.
3. Step One: Data Collection
Data is the foundation of Machine Learning.
A model learns patterns from the information provided during training. Therefore, the quality and relevance of the data have a significant impact on the results.
For our house price prediction system, we might collect:
- Property size.
- Number of bedrooms.
- Number of bathrooms.
- Location.
- Property age.
- Distance from the city center.
- Historical selling price.
A simplified dataset might look like this:
| Size (sq ft) | Bedrooms | Location | Price |
|---|---|---|---|
| 1,000 | 2 | City Center | $150,000 |
| 1,500 | 3 | Suburb | $180,000 |
| 2,000 | 4 | City Center | $300,000 |
| 1,200 | 2 | Suburb | $140,000 |
| 2,500 | 4 | City Center | $380,000 |
Each row represents one property.
The information used to make a prediction is called input data, while the known property price is the target value.
Important: A larger dataset does not automatically mean a better model. Data quality, relevance, representativeness, and accuracy matter.
4. Step Two: Data Preparation
Raw data is rarely ready for Machine Learning.
It may contain:
- Missing values.
- Duplicate records.
- Incorrect information.
- Inconsistent formats.
- Outliers.
- Irrelevant information.
Before training a model, we need to prepare the data.
For example:
Imagine one property has a size of 1,500 square feet, but its bedroom count is missing.
We need to decide how to handle that missing value.
Possible approaches include:
- Removing incomplete records when appropriate.
- Filling missing values using a suitable statistical method.
- Investigating the original data source.
- Using a model or method designed to handle missing information.
We may also need to convert categorical information, such as property location, into a numerical representation that the selected algorithm can process.
Another important step is splitting the dataset into training and testing portions.
Training data vs testing data
Training data: The data used by the learning algorithm to fit the model.
Testing data: Separate data used to evaluate how well the trained model performs on examples it did not use during training.
For example, we might use 80% of our dataset for training and 20% for testing.
This is a common approach, not a fixed rule for every project.
We must also avoid data leakage, where information from the test set unintentionally influences training or preprocessing.
Key takeaway: Good data preparation helps a model learn meaningful patterns and allows us to evaluate it more honestly.
5. Step Three: Understanding Features and Labels
Before training a model, we need to understand two important terms: features and labels.
What are features?
Features are the input variables that a model uses to make predictions.
For our house price example, features include:
- Property size.
- Number of bedrooms.
- Number of bathrooms.
- Location.
- Property age.
What is a label?
A label is the target value the model is expected to learn to predict in a supervised learning task.
In our example, the property selling price is the label.
Think about it this way:
Features = Information provided to the model.
Label = The answer the model is trying to predict.
Choosing relevant features is important because irrelevant or misleading information can reduce model performance.
The process of creating or transforming useful input variables is called feature engineering.
6. Step Four: Choosing a Machine Learning Model
Now we have prepared our data.
The next step is choosing an appropriate Machine Learning algorithm.
An algorithm is the learning procedure used to fit a model to data.
A model is the result of that learning process, including its learned parameters.
Different algorithms are suitable for different problems.
Some common examples include:
| Algorithm | Common use |
|---|---|
| Linear Regression | Predicting numerical values |
| Logistic Regression | Classification problems |
| Decision Trees | Classification and regression |
| Random Forest | Classification and regression |
| K-Means | Grouping similar data points |
| Neural Networks | Learning complex patterns |
For our house price prediction problem, Linear Regression could be a reasonable starting point.
Why?
Because we want to estimate a numerical value: the price of a property.
However, the best algorithm depends on the dataset, assumptions, complexity, and performance requirements.
There is no single algorithm that works best for every problem.
7. Step Five: Model Training
This is where the actual learning process happens.
During training, the algorithm uses the training data to adjust the model's internal parameters so that its predictions become more accurate according to a chosen objective.
Let's simplify this with our house price example.
Suppose the model receives information about a property:
- Size: 1,500 square feet.
- Bedrooms: 3.
- Location: Suburb.
Initially, the model may produce an inaccurate price estimate.
The training process compares the model's prediction with the actual price in the training data.
It calculates an error using a selected loss function.
The learning algorithm then adjusts the model's parameters to reduce that error.
This process repeats across the training data.
Eventually, the model learns patterns that help it make better predictions.
What is a loss function?
A loss function measures how far a model's prediction is from the expected answer.
For a simple regression problem, Mean Squared Error (MSE) is one commonly used loss function.
It calculates the average squared difference between predicted and actual values.
A lower training loss can indicate that the model is fitting the training data better, but it does not automatically mean that the model will perform well on new data.
What are model parameters?
Parameters are internal values learned by a model during training.
For example, a Linear Regression model learns coefficients that determine how its input features contribute to its predictions.
The model uses these learned parameters when making predictions.
Key takeaway: Model training is the process of learning useful parameters from data by optimizing a chosen objective.
8. Step Six: Model Evaluation
After training, we need to determine whether the model performs well.
A model that performs well on training data may still perform poorly on unseen data.
This problem is known as overfitting.
What is overfitting?
Overfitting happens when a model learns the training data too closely, including noise or accidental patterns.
As a result, it performs well on training examples but struggles with new data.
Another problem is underfitting.
What is underfitting?
Underfitting occurs when a model is too simple or has not learned enough from the data to capture the underlying patterns.
It performs poorly even on the training data.
Common evaluation metrics
Different problems require different evaluation metrics.
| Metric | Common application |
|---|---|
| Mean Absolute Error (MAE) | Regression |
| Mean Squared Error (MSE) | Regression |
| Root Mean Squared Error (RMSE) | Regression |
| Accuracy | Classification |
| Precision | Classification |
| Recall | Classification |
| F1-score | Classification |
For our house price prediction model, we could use MAE.
For example, if the model's MAE is $10,000, its predictions differ from actual prices by an average absolute amount of $10,000 on the evaluated dataset.
This is a simplified interpretation, and we should always consider the distribution of errors and the business context.
Important: Evaluation should measure performance on data that was not used to fit the model.
9. Step Seven: Model Deployment
Once we are satisfied with the model's performance, we may want to make it available to users or applications.
This is called model deployment.
For example, a real estate company could integrate the trained model into its website.
A user enters:
- Property size.
- Number of bedrooms.
- Location.
- Property age.
The website sends this information to the deployed model.
The model returns an estimated property price.
In a real system, the model may be exposed through an API or another serving mechanism.
This is where Machine Learning connects with traditional software development.
We need to consider:
- API design.
- Input validation.
- Model serving.
- Application integration.
- Latency.
- Security.
- Infrastructure.
- Monitoring.
A trained model sitting on a computer is not automatically a useful business solution. It needs a reliable way to interact with the application and its users.
10. Step Eight: What Is Inference?
Inference is the process of using a trained model to generate predictions or outputs from new input data.
Let's continue our house price example.
We have already trained and deployed our model.
Now a user enters information about a new property:
- Size: 1,800 square feet.
- Bedrooms: 3.
- Location: Suburb.
- Property age: 5 years.
The trained model processes these inputs using its learned parameters.
It might return an estimated property price of $220,000.
That prediction process is called inference.
Training vs inference
This is one of the most important distinctions in Machine Learning.
| Training | Inference |
|---|---|
| The model learns from data | The trained model generates outputs |
| Adjusts model parameters | Uses learned parameters |
| Usually requires training data and optimization | Requires new input data |
| Often computationally expensive | Depends on the model and serving setup |
Simple explanation:
Training is when a model learns.
Inference is when a trained model is used.
We'll revisit this distinction when we study Large Language Models.
11. The Complete Machine Learning Workflow
Let's put everything together.
Imagine a company wants to predict house prices.
The workflow would look like this:
- Collect historical property data.
- Clean and prepare the data.
- Identify relevant features and target labels.
- Select a suitable algorithm.
- Train the model using training data.
- Evaluate it using unseen data.
- Deploy the trained model.
- Send new property information to the model.
- Generate a prediction through inference.
- Monitor performance and update the model when necessary.
This is the basic Machine Learning lifecycle.
Real-world projects may involve additional stages, such as hyperparameter tuning, cross-validation, model versioning, data validation, and continuous monitoring.
12. Where Does This Fit Into Modern AI?
You might be wondering:
If Machine Learning works like this, how do ChatGPT, Claude, and Gemini work?
They are based on much more sophisticated Machine Learning and Deep Learning techniques.
Modern LLMs use neural network architectures, particularly Transformers, and are trained through specialized processes involving large-scale data and computation.
Their training and deployment pipelines are more complex than our simple house price example.
However, some fundamental ideas remain relevant:
- Data is used during training.
- Model parameters are learned.
- Training objectives guide optimization.
- Models are evaluated.
- Trained models generate outputs through inference.
Understanding these fundamentals will make it much easier to understand how LLMs work in the upcoming articles.
13. Practical Exercise: Test Your Understanding
Let's test what you've learned.
Questions
- What is the difference between traditional programming and Machine Learning?
- What is the difference between training data and testing data?
- What are features and labels?
- What happens during model training?
- What is a loss function?
- What is overfitting?
- What is the difference between training and inference?
- Why does a trained model need deployment before a business application can use it?
Practical challenge
Imagine an online store wants to predict whether a customer will purchase a product.
Identify:
- Three possible input features.
- The target label.
- What historical data the business needs.
- Which type of Machine Learning problem this is.
- How you would evaluate the model.
- How the store could use the prediction.
Try answering these questions without copying the article.
The goal is to understand the workflow, not memorize definitions.
What Should You Be Able to Do After Day 2?
By completing this lesson, you should be able to:
- Explain the Machine Learning lifecycle.
- Understand how data is used to train models.
- Differentiate features and labels.
- Explain training, testing, and inference.
- Understand loss functions and evaluation metrics.
- Identify overfitting and underfitting.
- Describe how a trained model can be deployed inside a software application.
What's Next? — Day 3
Deep Learning Explained: Neural Networks, Layers, Weights, Biases and Backpropagation
We've now understood how Machine Learning models learn from data.
In the next article, we'll explore what happens inside neural networks.
We'll learn how neurons, layers, weights, biases, activation functions, and backpropagation work together to help a Deep Learning model learn complex patterns.
Learning AI is a process of understanding how the pieces connect. Before working with advanced AI models, we need to understand the fundamentals that make them possible.
This is Day 2 of my AI from Scratch series. Let's keep learning, one concept at a time.
About the author
- I'm an AI Solutions Consultant focused on helping businesses identify challenges and turn them into practical AI solutions. I work with AI agents, workflow automation, API integrations, and solution architecture to build smarter, more efficient business systems.
Comments
Join the discussionNo comments yet. Be the first to comment.