Machine Learning For Kids: Your First Machine Learning Program



Machine learning for kids: Welcome, young coders, to your first exciting adventure in the world of Machine Learning! It might sound complicated, but don’t worry, we’re going to break it down into bite-sized pieces. Machine Learning is all about teaching computers how to learn and make decisions from data, just like we humans do from our experiences.

Who Is This For?

Grade: 6th to 10th

This tutorial is designed for students from grades 6th to 10th who have some basic knowledge of Python, including variables, data types, loops, functions, and have a general interest in data and problem-solving. So, if you’re keen on exploring how to make a computer learn from data, you’re in the right place!

What Will We Learn?

In this tutorial, we will learn how to use a Python library called Scikit-learn to create a basic Machine Learning model. We’ll use a simple and famous dataset called the “Iris” dataset, which includes different measurements of Iris flowers of three different species.

Understanding Your First Machine Learning Program

In Machine Learning, our goal is to create a model – this is a kind of computer program that learns from data. After the model is trained with existing data (known as the training data), we can use it to make predictions from new, unseen data.

Scikit-learn is a popular Python library for Machine Learning. It has lots of tools that make it easy to create and train these models.

Let’s take a look at a simple Machine Learning program:

# Import necessary libraries
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn import tree
from sklearn.metrics import accuracy_score

# Load iris dataset
iris = datasets.load_iris()

# Split the dataset into train and test data
X_train, X_test, y_train, y_test = train_test_split(,, test_size=0.5, random_state=42)

# Initialize our decision tree object
classification_tree = tree.DecisionTreeClassifier()

# Train the model using the training sets
classification_tree =, y_train)

# Predict the response for test dataset
y_pred = classification_tree.predict(X_test)

# Print the accuracy
print("Accuracy:", accuracy_score(y_test, y_pred))

Let’s break down this code into simpler steps:

We start by importing the necessary libraries. We will be using datasets from sklearn to load the iris dataset, train_test_split to split our dataset into training and testing sets, tree to use a Decision Tree model, and accuracy_score to measure how well our model did.

We load the iris dataset. The data (measurements) are stored in, and what we want to predict (the species of the flowers) is stored in

We split the dataset into two parts: a training set and a test set. We will use the training set to train our model, and the test set to test how well the model has learned.

We create a Decision Tree Classifier. This is a type of model that makes decisions based on the data it’s given. You can think of it as a series of yes/no questions leading to a final decision.

We use the fit method to train our model using the training data.

After the model is trained, we use the predict method to predict the species of the flowers in the test set.

Finally, we use the accuracy_score function to find out how often our model was correct.

This simple program marks your first step into the world of Machine Learning! Remember, this is just the beginning. There’s so much more to explore and learn, so keep on coding and stay curious!


Tutorial 1 – Installing Python
Tutorial 2 – Your First Program in Python
Tutorial 3 – Python Variables
Tutorial 4 – Python Data Types
Tutorial 5 – Python Conditionals
Tutorial 6 – Python Loops
Tutorial 7 – Python Functions
Tutorial 8 – Advanced Python Functions
Tutorial 9 – Starter Machine Learning Python Program
Tutorial 10 – Your First Machine Learning Program!

Explore more with this book.

Python for Kids, 2nd Edition: A Playful Introduction to Programming
Buy Now
We earn a commission if you make a purchase, at no additional cost to you.
02/18/2024 05:01 am GMT


Briggs, Jason R. Python for Kids: A Playful Introduction To Programming. No Starch Press, 2012.