Codingal > Coding for kids > Blogs > Machine Learning from Scratch: Training and Testing Models

Machine Learning from Scratch: Training and Testing Models

Aleena Martin on August 22, 2025

Introduction

 

Machine learning is a way for computers to learn from data and make decisions or predictions without being directly programmed to do so. It’s used in many areas, like recognizing faces in photos, suggesting products to buy, or even driving cars! In this guide, we’ll break down the basics of machine learning, explain how to train and test models, and show how to get started with machine learning from scratch, all in simple words.

What is Machine Learning?

Machine learning is like teaching a computer how to learn from data. Instead of telling the computer exactly what to do, we show it examples, and it learns patterns from them. The more examples (or data) it sees, the better it gets at understanding what to do.

For example, if you wanted a computer to recognize pictures of cats and dogs, you’d give it lots of pictures of cats and dogs, and it would learn to tell the difference based on patterns it finds, like the shape of ears or the length of the tail.

Key Concepts of Machine Learning

To understand how machine learning works, let’s go over a few key ideas:

  1. Data: This is the information you feed to the computer. It could be anything—pictures, numbers, or even text. The computer learns from this data.
  2. Model: The model is what the computer builds after it learns from the data. Think of it as a program or function that makes predictions based on the patterns it found in the data.
  3. Training: Training is the process where the computer looks at the data and learns from it. You give the computer a lot of examples, and it uses these to build its model.
  4. Testing: Once the model is trained, you need to test it. Testing involves giving the model new data it hasn’t seen before to see how well it can make predictions.
  5. Accuracy: This tells us how well the model is doing. A high accuracy means the model is making good predictions; a low accuracy means it needs more training or better data.

Steps to Build a Machine Learning Model

Now that we know the basic concepts, let’s go through the steps to build a machine learning model from scratch.

Step 1: Collect Data

The first step in any machine learning project is collecting data. The more data you have, the better your model will be. For example, if you’re trying to build a model to recognize cats and dogs, you’ll need to collect lots of images of both.

You could collect data from online sources, or you could create your own dataset by taking pictures or writing down numbers or text. The key is to have a variety of examples so the computer can learn well.

Step 2: Preprocess the Data

Before you can use the data, you need to preprocess it, which means cleaning it up so the computer can understand it. For example, if you have images, you might need to resize them so they are all the same size. If you have numbers or text, you might need to remove any extra information that doesn’t help.

This step makes sure the data is in a format the computer can easily learn from.

Step 3: Choose a Machine Learning Algorithm

An algorithm is like a recipe the computer follows to learn from the data. There are many different machine learning algorithms, each suited for different tasks. Here are a few common ones:

  • Linear Regression: This algorithm is used to predict numbers, like the price of a house based on its size.
  • Decision Trees: This algorithm helps the computer make decisions based on certain rules. For example, if you want to classify whether an email is spam or not, decision trees can be useful.
  • K-Nearest Neighbors (KNN): This algorithm compares new data to the existing data to find the closest match. It’s great for tasks like recognizing handwritten digits.

The algorithm you choose depends on the type of problem you’re trying to solve.

Step 4: Train the Model

Training the model is the most important step. During training, the algorithm looks at the data and tries to find patterns. For example, if you’re trying to classify images of cats and dogs, the computer will look at the features of each image, such as the shape of the ears, the length of the tail, and so on.

To train the model, you need to:

  • Split the data: You usually divide the data into two parts—a training set and a testing set. The training set is used to teach the model, and the testing set is used to see how well the model learned.
  • Feed the training data into the model: The computer uses this data to adjust its internal settings (called parameters) so that it can make better predictions.

Step 5: Test the Model

Once the model is trained, it’s time to test it using the testing set—the data that the model has not seen before. This helps you see how well the model performs on new data.

For example, if your model is trying to recognize pictures of cats and dogs, you would give it new pictures that it hasn’t seen during training. The model will try to predict whether each picture is a cat or a dog, and you can compare its predictions to the correct answers.

Step 6: Evaluate the Model

After testing the model, you’ll want to check how well it’s doing. This is where accuracy comes in. If the model is correct most of the time, it has high accuracy. If it’s wrong often, you might need to go back and adjust the model or get more data.

To evaluate your model, you can use various methods:

  • Confusion Matrix: This is a table that shows how many correct and incorrect predictions the model made.
  • Accuracy Score: This tells you the percentage of correct predictions the model made out of all the predictions.

If the model isn’t doing well, you may need to go back and tweak the algorithm or add more data for training.

Real-Life Examples of Machine Learning

Machine learning is used all around us in many ways, even if we don’t always notice it. Some common examples include:

  • Voice Assistants: Apps like Siri and Alexa use machine learning to understand your voice and respond to your questions.
  • Image Recognition: Social media platforms use machine learning to recognize faces in photos and tag them automatically.
  • Recommendation Systems: Platforms like Netflix or YouTube use machine learning to suggest videos or shows you might like based on your previous viewing habits.

Conclusion

Machine learning may sound complex, but at its core, it’s about teaching computers to learn from data. By training and testing models, we can create systems that recognize patterns, make decisions, and improve over time. Whether you’re building a simple project to classify images or working on more advanced tasks, understanding the basics of machine learning will open the door to many exciting possibilities. The key is to start with simple examples, learn from mistakes, and keep experimenting!

Share with your friends

Try a free class