Mathew K Analytics

Lesson 58 · Python for Data Science

3 - Regression and Classification in Python

In this lesson, we will explore two key ideas in machine learning: regression and classification. You will see simple examples and build up to a small…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb
 

Welcome to Regression and Classification in Python!#

In this lesson, we will explore two key ideas in machine learning: regression and classification.

You will see simple examples and build up to a small hands-on project.

Do not worry if you are new! We will go step by step.

What are Regression and Classification?#

  • Regression helps us predict numbers, like a house price or temperature.
  • Classification helps us sort things into categories, like if an email is spam or not.

We will start with the basics.

# Let's start with a simple regression: Predicting kitten weight from age.
ages = [1, 2, 3, 4, 5]  # months
weights = [300, 400, 500, 600, 700]  # grams
# Let's look at our data side by side.
for i in range(len(ages)):
    print("Kitten age:", ages[i], "months - Weight:", weights[i], "grams")
    
Kitten age: 1 months - Weight: 300 grams
Kitten age: 2 months - Weight: 400 grams
Kitten age: 3 months - Weight: 500 grams
Kitten age: 4 months - Weight: 600 grams
Kitten age: 5 months - Weight: 700 grams
# Now, let's make a simple prediction.
def predict_weight(age):
    # We notice weight increases by about 100g per month.
    return 200 + age * 100

predicted = predict_weight(6)
print("Predicted weight for 6 month kitten:", predicted, "grams")
Predicted weight for 6 month kitten: 800 grams
# You can try it yourself!
user_age = int(input("Enter kitten's age in months: "))
print("Guessing the kitten's weight:", predict_weight(user_age), "grams")
Guessing the kitten's weight: 900 grams
 
# If you want to adjust the rule, change the formula in predict_weight.
# For example, what if kittens gain 120g per month?
def predict_weight_v2(age):
    return 180 + age * 120

print("6 months, new formula:", predict_weight_v2(6), "grams")
6 months, new formula: 900 grams

Quick Recap on Regression#

  • Regression is about finding a rule to predict numbers.
  • It is like finding the line, curve, or rule that connects dots.
  • We used a simple example and wrote code to predict the next value.

Lets try a new kind of problem: classification.

# Let's switch to classification: Is a fruit an apple or an orange?
colors = ['red', 'green', 'orange', 'red', 'orange']
labels = ['apple', 'apple', 'orange', 'apple', 'orange']
# Let's create a simple rule to guess: If it is orange, call it orange, otherwise apple.
def classify_fruit(color):
    if color == 'orange':
        return 'orange'
    else:
        return 'apple'
    
for color in colors:
    print("If the fruit is", color, "I guess:", classify_fruit(color))
    
If the fruit is red I guess: apple
If the fruit is green I guess: apple
If the fruit is orange I guess: orange
If the fruit is red I guess: apple
If the fruit is orange I guess: orange
# Let's try a fruit of your choice!
your_color = input("Enter a fruit color (red, green, orange): ")
print("I guess:", classify_fruit(your_color))
I guess: apple
 
# What happens if your input is not a color in our list?
mystery_color = input("Guess a mystery fruit color: ")
result = classify_fruit(mystery_color)
print("With mystery color, I guess:", result)
With mystery color, I guess: apple
 
# Let's handle unknown colors in our classifier.
def classify_fruit_safe(color):
    if color == 'orange':
        return 'orange'
    elif color == 'red' or color == 'green':
        return 'apple'
    else:
        return 'unknown'
    
print("Test blue:", classify_fruit_safe('blue'))
Test blue: unknown

Built-in Tools: Using scikit-learn#

Python's scikit-learn library has many tools for regression and classification. We will use one now to show how easy real-world machine learning can be.

# Let's use scikit-learn for simple regression.
from sklearn.linear_model import LinearRegression
import numpy as np

X = np.array(ages).reshape(-1, 1)
y = np.array(weights)

model = LinearRegression()
model.fit(X, y)

print("Predicting the weight for a 7-month-old kitten:", model.predict([[7]])[0], "grams")
Predicting the weight for a 7-month-old kitten: 900.0 grams
# Let's use scikit-learn for simple classification.
from sklearn.tree import DecisionTreeClassifier

color_map = {'red':0, 'green':1, 'orange':2}
Xc = np.array([color_map[c] for c in colors]).reshape(-1,1)
yc = np.array([0 if l == 'apple' else 1 for l in labels])

clf = DecisionTreeClassifier()
clf.fit(Xc, yc)

test_color = color_map['orange']
predicted_label = clf.predict([[test_color]])[0]
if predicted_label == 1:
    print("Classifier predicts: orange")
else:
    print("Classifier predicts: apple")
    
Classifier predicts: orange

Quick Mini-Project: Predict Exam Grades#

Lets put it all together! We have student study hours and grades. We will predict a grade from hours, then classify pass/fail.

# Here are some study data: hours vs. grades.
hours = [2, 4, 6, 8, 10]
grades = [55, 65, 78, 88, 96]
# Regression to guess a grade from study hours.
from sklearn.linear_model import LinearRegression
import numpy as np

Xh = np.array(hours).reshape(-1, 1)
yh = np.array(grades)

grade_model = LinearRegression()
grade_model.fit(Xh, yh)

new_hours = int(input("How many hours did you study? "))
predicted_grade = grade_model.predict([[new_hours]])[0]
print("Predicted exam grade:", round(predicted_grade, 1))
Predicted exam grade: 81.6
 
# Classify: Pass if grade 60 or higher, else Fail.
def pass_fail(grade):
    if grade >= 60:
        return 'PASS'
    else:
        return 'FAIL'
    
print("Prediction:", pass_fail(predicted_grade))
Prediction: PASS

Common Errors and Safety Tips#

  • Double-check matching lists: Your features and labels should be the same length.
  • Make sure to check for unknown cases in classifiers.
  • Watch out for typos and missing parentheses!

It is okay to see errors. They help you learn.

# List length mismatch: What happens if lists are different lengths?
bad_ages = [1,2,3]
bad_weights = [300,400]
try:
    for i in range(len(bad_ages)):
        print(bad_ages[i], bad_weights[i])
except IndexError:
    print("Oops! The lists do not match in length.")
    
1 300
2 400
Oops! The lists do not match in length.

Tips and Tricks#

  • Use functions to keep code tidy.
  • Try scikit-learn for bigger problems.
  • Print out steps to see what is happening.
  • Test with surprises to see how safe your code is.

Challenge Exercise#

See if you can:

  • Make a predictor for your own data, like daily steps or pets' ages.
  • Write a classifier for favorite foods or school subjects.

Try first with simple rules, then use scikit-learn!

Recap: What Have We Learned?#

  • Regression = predicting numbers.
  • Classification = sorting things into groups.
  • Code can use rules you make or learn from data.

Practice makes perfect, so keep going!

Thank You for Watching!#

If you found this lesson helpful:

  • Like the video
  • Comment with your favorite example
  • Subscribe for more
  • Share it with your friends

See you next time!

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.