Python Computer Science
Beginner
80 mins
Teacher/Student led
What you need:
Chromebook/Laptop/PC

AI and Machine Learning Basics

In this lesson, you'll uncover the essentials of Artificial Intelligence and Machine Learning. Follow step-by-step guidance to define AI, explore Machine Learning categories, set up a Python environment, and build models like linear regression for practical predictions.
Learning Goals Learning Outcomes Teacher Notes

Teacher Class Feed

Load previous activity

    1 - Introduction

    In this lesson, you'll explore the fundamentals of Artificial Intelligence (AI) and Machine Learning (ML), technologies that are transforming industries from healthcare to gaming.

    Through hands-on activities, you'll:

    1. Define AI and its types, considering real-world examples and contexts where it might be used.
    2. Examine Machine Learning as a subset of AI, including its main categories and how algorithms learn from data.
    3. Set up a Python project environment with necessary libraries for ML tasks.
    4. Implement a linear regression model to predict house prices based on features like size.
    5. Build a decision tree classifier to categorise video game characters by strength attributes.
    6. Extend these models in optional tasks, adding features and visualisations to deepen your understanding.
    You'll need VS Code with Python installed, and you'll install the scikit-learn library (provides tools for machine learning in Python) and the matplotlib library (library for creating visualisations in Python) during the lesson.

    2 - What is Artificial Intelligence (AI)?

    Artificial Intelligence (AI) refers to the capability of a machine or computer system to perform tasks that would normally require human intelligence. This includes activities like understanding natural language, recognising patterns, learning from experience, solving problems, and making decisions.

    AI can be divided into two main types:

    • Narrow AI: Designed for specific tasks, such as voice assistants that understand speech or recommendation systems on streaming services that suggest content based on your preferences.
    • General AI: A more advanced form that could perform any intellectual task a human can, though this is still largely theoretical and not yet achieved.

    AI powers many everyday technologies, from self-driving cars that navigate roads to chatbots that provide customer support. It leverages computing power to process vast amounts of data quickly, enabling solutions to complex problems that would be difficult or impossible for humans alone.

    Consider contexts where AI might be used: in healthcare for diagnosing diseases from scans, in finance for detecting fraud, or in education for personalised learning tools.

    AI doesn't 'think' like humans but simulates intelligence through algorithms and data.
    Take a moment to think of one example of AI in your daily life and note it down.

    3 - What is Machine Learning (ML)?

    Machine Learning (ML) is a subset of artificial intelligence (AI) that focuses on the development of algorithms and models that enable computers to learn from and make predictions or decisions based on data. Instead of being explicitly programmed with rules, ML systems improve their performance on a task through experience, by analysing patterns in data.

    ML can be categorised into three main types:

    • Supervised Learning: The model is trained on labelled data, where both inputs and desired outputs are provided. For example, teaching a system to classify emails as spam or not spam based on examples.
    • Unsupervised Learning: The model works with unlabelled data to find hidden patterns or groupings, such as clustering customers based on purchasing behaviour without predefined categories.
    • Reinforcement Learning: The model learns by interacting with an environment, receiving rewards or penalties for actions, like training a robot to navigate a maze.

    ML is used in various contexts: in recommendation engines (e.g., suggesting movies on Netflix), image recognition (e.g., identifying objects in photos), predictive maintenance in manufacturing, or even in healthcare for predicting patient outcomes from medical data. It leverages computing power to process large datasets, enabling solutions to problems that are too complex for traditional programming.

    ML algorithms 'learn' by adjusting internal parameters to minimise errors in predictions, often using techniques like gradient descent. Gradient descent is an optimisation algorithm that iteratively adjusts the model's parameters by calculating the gradient (slope) of the error function and moving in the direction that reduces the error, much like descending a hill to find the lowest point.
    Think of one example where ML could be applied in a real-world scenario, such as in social media or gaming, and note it down.

    4 - Set Up Your Project

    First, you need to create a new folder for this lesson's project and set up the environment.

    1. Open VS Code.
    2. Create a new folder on your computer called 'AIMLLesson'.
    3. Open this folder in VS Code (File > Open Folder).

    Next, create a virtual environment to manage your project's dependencies.

    1. Open the Command Palette: View > Command Palette.
    2. Type 'Python: Create Environment' and select it.
    3. Choose 'Venv'.
    4. Select your Python version.
    5. VS Code will create a '.venv' folder.

    Next, open a terminal in VS Code to install the required libraries.

    1. Go to View > Terminal.
    2. Run: pip install scikit-learn. This library provides tools for machine learning in Python.
    3. Then, run: pip install matplotlib. Matplotlib is a library for creating visualisations in Python, which we'll use to plot data and display graphs, such as showing regression lines in our ML demo.
    4. Wait for the installations to complete. 

    5 - Machine Learning - Linear Regression

    We'll create a basic linear regression model to predict house prices based on size – a supervised learning example.

    Linear regression is a fundamental machine learning algorithm used for predicting a continuous outcome variable based on one or more predictor variables. It assumes a linear relationship between the input features and the target variable. The goal is to find the line of best fit, which is determined by minimising the sum of the squared differences between the observed values and the values predicted by the line. In simple linear regression, like our example, there's one feature (house size) and one target (price), and the model learns the slope and intercept of the line that best represents the data.

    Create a file named ml_demo.py inside your AIMLLesson folder, and add the following complete code to it:

    import numpy as np  # NumPy is a library for numerical computing in Python
    from sklearn.linear_model import LinearRegression
    import matplotlib.pyplot as plt
    
    # Sample data: house sizes (in square meters) and prices (in thousands of euros)
    house_sizes = np.array([50, 100, 150, 200, 250, 300]).reshape(-1, 1)  # Features
    house_prices = np.array([150, 300, 450, 600, 750, 900])  # Labels
    
    # Create and train the model
    model = LinearRegression()
    model.fit(house_sizes, house_prices)
    
    # Predict price for a new house size (e.g., 180 sqm)
    new_size = np.array([[180]])
    predicted_price = model.predict(new_size)
    print("Predicted price for 180 sqm house:", predicted_price[0])
    
    # Visualise the data and regression line
    plt.scatter(house_sizes, house_prices, color='blue', label='Actual Prices')
    plt.plot(house_sizes, model.predict(house_sizes), color='red', label='Regression Line')
    plt.xlabel('House Size (sqm)')
    plt.ylabel('Price (thousands of euros)')
    plt.legend()
    plt.show()

    Run the code in VS Code terminal with python ml_demo.py. You should see a predicted price around 540 and a plot showing the data points and line. This demonstrates how ML algorithms learn from data to make predictions.

    Linear regression finds the best-fitting line through data points, minimising errors.


    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org 2017 - 2025. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more