Skip to main content

ML class trial

 

1. Simple Title

Building Your First Machine Learning Model

Example project: Predicting student exam scores using study hours


2. Learning Objectives

By the end of this 60-minute class, students will be able to:

  1. Explain machine learning in simple words.

  2. Understand how data is used to train a model.

  3. Describe a basic machine learning workflow.

  4. Build a simple prediction model using Python and scikit-learn.

  5. Test the model and understand its prediction.

  6. Identify common beginner mistakes in machine learning.


3. 60-Minute Lesson Flow

TimeActivity
0–5 minWelcome and topic introduction
5–12 minWhat is machine learning?
12–20 minReal-life example: predicting student exam scores
20–28 minUnderstanding the dataset
28–40 minStep-by-step model-building process
40–52 minPython code walkthrough using scikit-learn
52–57 minCommon beginner mistakes
57–60 minQuiz and final summary

4. Simple Explanation: What Is Machine Learning?

Machine learning is a way of teaching computers to learn from data.

Instead of giving the computer every rule manually, we give it examples. The computer studies those examples and learns a pattern.

For example, imagine we have data like this:

Study HoursExam Score
135
245
355
465
575

A machine learning model can look at this data and learn:

Students who study more hours usually score higher marks.

Then, if we ask:

What score might a student get if they study for 6 hours?

The model can make a prediction.

Machine learning is not magic. It is pattern learning from data.


5. Real-Life Example

Predicting Student Exam Scores

Suppose a teacher wants to estimate how students may perform based on how many hours they studied.

The teacher has past data:

StudentHours StudiedExam Score
A135
B240
C350
D460
E570
F680

Using this data, we can build a simple machine learning model.

The model will learn the relationship between:

Input: Hours studied
Output: Exam score

Then we can use it to predict the score of a new student.


6. Beginner-Friendly ML Project Idea

Project: Predict Exam Scores Based on Study Hours

This is a good first machine learning project because:

  1. The dataset is small and easy to understand.

  2. There is only one input column.

  3. The output is a number.

  4. The result is easy to visualize.

  5. It avoids heavy mathematics.

This type of problem is called a regression problem.

Regression means predicting a number.

Examples of regression:

ProblemPrediction
Predict house pricePrice in rupees or dollars
Predict exam scoreMarks out of 100
Predict temperatureDegrees Celsius
Predict salesNumber or amount of sales

7. Dataset Explanation

We will use a simple dataset with two columns:

Column NameMeaning
HoursNumber of hours a student studied
ScoreMarks scored by the student

Example:

HoursScore
1.035
2.045
3.050
4.060
5.070
6.075
7.085
8.090

Here:

Hours is the input feature.
Score is the target value.

In machine learning language:

Simple WordML Word
InputFeature
OutputTarget
Example rowData point
Learning from dataTraining
Guessing for new dataPrediction

8. Step-by-Step Model-Building Process

Step 1: Collect Data

We need examples from the past.

For this project, our data contains study hours and exam scores.


Step 2: Prepare the Data

We separate the data into:

X: input data
y: output data

Example:

X = Hours
y = Score

Step 3: Split the Data

We divide the data into two parts:

PartPurpose
Training dataUsed to teach the model
Testing dataUsed to check how well the model learned

This is like studying with practice questions and then testing yourself with new questions.


Step 4: Choose a Model

For this beginner project, we use Linear Regression.

Linear Regression tries to draw a straight line that best fits the data.

It is useful when one number increases or decreases along with another number.

Example:

As study hours increase, exam score usually increases.


Step 5: Train the Model

Training means the model studies the data and finds a pattern.

In code, this is usually done with:

model.fit(X_train, y_train)

Step 6: Make Predictions

After training, we can ask the model to predict results for new data.

Example:

model.predict([[6]])

This asks:

What score might a student get after studying 6 hours?


Step 7: Evaluate the Model

We check whether the model’s predictions are close to the actual answers.

For beginners, it is enough to compare:

Actual ScorePredicted Score
7577
9088

The predictions do not need to be perfect. They should be reasonably close.


9. Python Code Using scikit-learn

# Step 1: Import required libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error

# Step 2: Create a simple dataset
data = {
    "Hours": [1, 2, 3, 4, 5, 6, 7, 8, 9],
    "Score": [35, 40, 50, 60, 70, 75, 85, 90, 95]
}

df = pd.DataFrame(data)

# Step 3: Separate input and output
X = df[["Hours"]]   # Input feature
y = df["Score"]    # Target value

# Step 4: Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Step 5: Create the machine learning model
model = LinearRegression()

# Step 6: Train the model
model.fit(X_train, y_train)

# Step 7: Make predictions on test data
y_pred = model.predict(X_test)

# Step 8: Compare actual and predicted values
results = pd.DataFrame({
    "Actual Score": y_test,
    "Predicted Score": y_pred
})

print(results)

# Step 9: Check model error
error = mean_absolute_error(y_test, y_pred)
print("Mean Absolute Error:", error)

# Step 10: Predict score for a new student
new_hours = [[6.5]]
predicted_score = model.predict(new_hours)

print("Predicted score for 6.5 hours of study:", predicted_score[0])

10. Explanation of the Code

Import libraries

import pandas as pd

pandas helps us create and manage data tables.

from sklearn.model_selection import train_test_split

This helps us divide the data into training and testing parts.

from sklearn.linear_model import LinearRegression

This imports the Linear Regression model.

from sklearn.metrics import mean_absolute_error

This helps us measure how far the predictions are from the actual values.


Create the dataset

data = {
    "Hours": [1, 2, 3, 4, 5, 6, 7, 8, 9],
    "Score": [35, 40, 50, 60, 70, 75, 85, 90, 95]
}

This is our small example dataset.

Each row means:

A student studied for a certain number of hours and got a certain exam score.


Convert data into a table

df = pd.DataFrame(data)

This converts the dictionary into a table.


Separate input and output

X = df[["Hours"]]
y = df["Score"]

X contains the input.

y contains the answer we want to predict.

Important point:

df[["Hours"]]

has double square brackets because scikit-learn expects input in table form.


Split the data

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

This means:

  • 80% of data is used for training.

  • 20% of data is used for testing.

  • random_state=42 helps us get the same split every time.


Create the model

model = LinearRegression()

This creates an empty Linear Regression model.

At this point, it has not learned anything yet.


Train the model

model.fit(X_train, y_train)

This is where learning happens.

The model studies the relationship between study hours and exam scores.


Make predictions

y_pred = model.predict(X_test)

The model predicts scores for the test data.


Compare results

results = pd.DataFrame({
    "Actual Score": y_test,
    "Predicted Score": y_pred
})

This creates a table comparing actual and predicted scores.


Measure the error

error = mean_absolute_error(y_test, y_pred)

Mean Absolute Error tells us, on average, how far the prediction is from the real answer.

For example, if the error is 3, it means:

On average, the model is off by about 3 marks.


Predict for a new student

new_hours = [[6.5]]
predicted_score = model.predict(new_hours)

This predicts the exam score for a student who studied for 6.5 hours.


11. Common Mistakes Beginners Make

Mistake 1: Thinking machine learning is always perfect

Machine learning gives predictions, not guarantees.

A student who studies 8 hours may still score less because of stress, poor sleep, illness, or exam difficulty.


Mistake 2: Using very little data

Our example uses a tiny dataset for learning purposes.

In real life, we need more data to build better models.


Mistake 3: Not separating training and testing data

If we test the model on the same data used for training, we may get misleading results.

It is like giving students the same questions in practice and final exam.


Mistake 4: Ignoring data quality

Bad data leads to bad predictions.

For example:

HoursScore
295

This may be possible, but if it was entered by mistake, it can confuse the model.


Mistake 5: Thinking one column explains everything

Exam scores depend on many things, not only study hours.

Other useful columns could be:

  • Attendance

  • Previous scores

  • Sleep hours

  • Practice tests completed

  • Difficulty level of exam

  • Health and stress level


Mistake 6: Copying code without understanding the flow

Beginners should focus on the process:

  1. Data

  2. Input and output

  3. Train-test split

  4. Model

  5. Training

  6. Prediction

  7. Evaluation

The exact code becomes easier once the process is clear.


12. Short Quiz with Answers

Question 1

What is machine learning?

A. A way to manually write every rule
B. A way for computers to learn patterns from data
C. A type of computer hardware
D. A replacement for all human thinking

Answer: B


Question 2

In our project, what is the input feature?

A. Student name
B. Exam score
C. Study hours
D. School name

Answer: C


Question 3

In our project, what is the target value?

A. Study hours
B. Exam score
C. Python code
D. Model name

Answer: B


Question 4

Why do we split data into training and testing sets?

A. To make the code longer
B. To check how the model performs on new data
C. To delete some data
D. To avoid using Python

Answer: B


Question 5

Which model did we use in this lesson?

A. Linear Regression
B. Decision Tree
C. Neural Network
D. Chatbot

Answer: A


Question 6

What does model.fit() do?

A. Deletes the dataset
B. Trains the model
C. Prints the result
D. Changes Python version

Answer: B


Question 7

What does model.predict() do?

A. Trains the model
B. Makes predictions using the trained model
C. Splits the data
D. Creates a spreadsheet

Answer: B


Question 8

Can a machine learning prediction be wrong?

A. Yes
B. No

Answer: A


13. Final Summary

In this lesson, we built our first machine learning model using a simple and practical example: predicting student exam scores from study hours.

We learned that machine learning means teaching computers to find patterns in data. We used past examples of study hours and scores, trained a Linear Regression model, tested it, and used it to make a new prediction.

The most important idea is not the code alone. The most important idea is the workflow:

  1. Collect data.

  2. Understand the input and output.

  3. Split the data.

  4. Choose a model.

  5. Train the model.

  6. Make predictions.

  7. Check the results.

Machine learning becomes easier when we start small, understand each step, and practice with simple projects.

A good next step is to try the same process with a house price prediction dataset using features like house size, number of bedrooms, and location.

Comments

Popular posts from this blog

ML trial 2

Building Your First Machine Learning Model Topic: Predicting Student Exam Scores Using Machine Learning Target Audience Complete beginners who are new to machine learning and Python-based model building. Duration 60 minutes Main Objective Students will understand the basic idea of machine learning and build a simple model that predicts student exam scores based on study hours. Learning Objectives Explain machine learning in simple words. Understand the difference between input data and output data. Describe the basic steps used to build a machine learning model. Use a small dataset to train a simple prediction model. Run beginner-friendly Python code using scikit-learn. Understand how a model makes predictions. Identify common mistakes beginners should avoid. 60-Minute Lesson Flow Time Section Teacher Activity Student Activity ...