0Pricing
Python Academy · Lesson

Supervised Learning with Scikit-Learn

Build and evaluate supervised learning models such as linear regression and classification.

Supervised Learning with Scikit-Learn is a free Python Academy lesson on CoddyKit — lesson 2 of 6. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 6 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Supervised Learning with Scikit-Learn

Supervised learning involves training a model using labeled data to make predictions or classify data points. Scikit-learn is a popular Python library for implementing supervised learning models.

In this lesson, you’ll learn how to build and evaluate supervised learning models using Scikit-learn.

Supervised Learning with Scikit-Learn — illustration 1

2

What is Supervised Learning?

Supervised learning uses labeled data, where each input has a corresponding output. The goal is to learn a mapping from inputs to outputs.

Common tasks include:

  • Regression: Predicting continuous values, e.g., house prices.
  • Classification: Predicting categories, e.g., spam or not spam.

3

Setting Up Scikit-Learn

Install Scikit-learn using pip:

pip install scikit-learn

Once installed, you can import it and start building models.

4

Linear Regression

Linear regression is used to predict continuous values. Let’s build a simple linear regression model:

# Example: Linear Regression
from sklearn.linear_model import LinearRegression

# Sample data
X = [[1], [2], [3]]
y = [2, 4, 6]

model = LinearRegression()
model.fit(X, y)
print("Predicted value:", model.predict([[4]]))

5

Logistic Regression

Logistic regression is used for binary classification tasks, such as determining whether an email is spam or not.

# Example: Logistic Regression
from sklearn.linear_model import LogisticRegression

# Sample data
X = [[1], [2], [3]]
y = [0, 0, 1]

model = LogisticRegression()
model.fit(X, y)
print("Predicted class:", model.predict([[1.5]]))

6

Decision Trees

Decision trees are versatile models that can handle both regression and classification tasks. Let’s build a simple decision tree classifier:

# Example: Decision Tree Classifier
from sklearn.tree import DecisionTreeClassifier

# Sample data
X = [[1], [2], [3]]
y = [0, 1, 0]

model = DecisionTreeClassifier()
model.fit(X, y)
print("Predicted class:", model.predict([[2.5]]))

7

Evaluating Models

Scikit-learn provides metrics to evaluate model performance:

  • Mean Squared Error (MSE): For regression models.
  • Accuracy: For classification models.
# Example: Evaluating a model
from sklearn.metrics import mean_squared_error

# Predictions
y_true = [2, 4, 6]
y_pred = [2.1, 3.9, 5.8]
print("MSE:", mean_squared_error(y_true, y_pred))

8

Train-Test Split

Always split your dataset into training and testing sets to evaluate model performance on unseen data:

# Example: Train-Test Split
from sklearn.model_selection import train_test_split

X = [[1], [2], [3], [4]]
y = [0, 1, 0, 1]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
print("Training data:", X_train)

9

10

Common Mistakes in Supervised Learning

Here are some mistakes to avoid:

  • Not splitting data into training and testing sets.
  • Overfitting the model by using overly complex algorithms.
  • Using poorly labeled data for training.

11

What Did We Learn?

In this lesson, you learned:

  • How to build regression and classification models using Scikit-learn.
  • How to evaluate model performance using metrics like MSE and accuracy.
  • The importance of splitting data into training and testing sets.
  • Common mistakes to avoid in supervised learning tasks.

Great job! Let’s move to the next topic.

Supervised Learning with Scikit-Learn — illustration 11

Frequently asked questions

Is the “Supervised Learning with Scikit-Learn” lesson free?

Yes — the full text of “Supervised Learning with Scikit-Learn” is free to read here on the web, and the Python Academy course includes 6 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Supervised Learning with Scikit-Learn”?

Build and evaluate supervised learning models such as linear regression and classification. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 6, so you can start here or from the beginning and move at your own pace.

How long does the “Supervised Learning with Scikit-Learn” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to Machine Learning
  2. Supervised Learning with Scikit-Learn
  3. Unsupervised Learning
  4. Feature Engineering and Selection
  5. Introduction to Neural Networks
  6. Introduction to TensorFlow and Keras
← Back to Python Academy