Tecxera Logo

How To Build Your First Machine Learning Model In Python: A Step-by-step Guide

Data Science Apr 27, 2025

How To Build Your First Machine Learning Model In Python: A Step-by-step Guide

Author

Ibrahim Oluwaseun

A passionate programmer.

Introduction

Machine learning is no longer just a buzzword, it’s transforming industries, from healthcare to finance, and even shaping the way we interact with technology every day. Whether it’s Netflix recommending your next favorite show or Siri understanding your voice commands, machine learning is at the heart of these innovations. But here’s the best part: you don’t need to be a tech genius to get started. With Python and a few powerful libraries, you can build your first machine learning model in no time!

In this step-by-step guide, you’ll learn how to build your very first machine learning model using Python. By the end of this tutorial, you’ll understand how to:

  • Preprocess data for machine learning.

  • Train a model using a popular algorithm.

  • Evaluate its performance and make predictions.

This guide is designed to be hands-on and beginner-friendly, so you’ll walk away with practical skills you can apply to real-world problems.

This tutorial is perfect for:

  • Beginners who are curious about machine learning but don’t know where to start.

  • Python enthusiasts looking to expand their skills into data science and AI.

  • Anyone who wants to build a strong foundation in machine learning.

If you know the basics of Python (like variables, loops, and functions), you’re ready to get in!

Tools Used:
We’ll be using some of the most popular Python libraries for machine learning:

  • Pandas: For data manipulation and analysis.

  • NumPy: For numerical operations.

  • Scikit-learn: For building and evaluating machine learning models.

These tools are beginner-friendly, widely used in the industry, and will make your journey into machine learning smooth and enjoyable.

Prerequisites

Before we start building your first machine learning model, let’s make sure you have everything you need to follow along smoothly. The best part? You don’t need to install anything on your computer! We’ll be using Google Colab, a free, cloud-based platform that lets you write and run Python code directly in your browser. Let’s get started!

1. Python Basics

To get the most out of this guide, you should have a basic understanding of Python programming. Familiarity with the following concepts will be helpful:

  • Variables and data types (e.g., integers, strings, lists).
  • Control structures (e.g., loops, conditionals).
  • Functions and how to define them.
  • Working with libraries using import.

If you’re new to Python, consider brushing up on these fundamentals with a beginner-friendly tutorial or course. A great place to start is the official Python documentation or free resources like Real Python.

2. Setting Up Google Colab

Google Colab is a free, cloud-based platform that requires no setup. It’s perfect for beginners because you can write and run Python code directly in your browser without installing anything on your computer. Here’s how to get started:

  1. Open your browser and go to Google Colab.
  2. Sign in with your Google account (or create one if you don’t have an account).
  3. Click on New Notebook to create a blank notebook.

That’s it! You’re now ready to start coding.

3. Installing Required Libraries

Google Colab comes with most of the libraries we’ll need pre-installed. However, if you need to install additional libraries, you can do so using the !pip install command. For this tutorial, we’ll use the following libraries:

  • Pandas: For data manipulation and analysis.
  • NumPy: For numerical computations.
  • Scikit-learn: For machine learning algorithms and tools.
  • Matplotlib or Seaborn: For data visualization (optional but recommended).

To install these libraries, simply run the following code in a Colab cell:

!pip install pandas numpy scikit-learn matplotlib seaborn

4. Dataset

For this tutorial, we’ll use the Iris dataset, a classic dataset in machine learning. It’s simple, well-structured, and perfect for beginners. The dataset contains 150 samples of iris flowers, with four features (sepal length, sepal width, petal length, and petal width) and a target variable (the species of iris).

The Iris dataset is included in Scikit-learn’s built-in datasets, so you don’t need to download it separately. However, if you’d like to use your own dataset, you can upload it to Google Colab by clicking the Upload button in the file explorer on the left-hand side.

If you’re curious about the dataset, you can find more details on the UCI Machine Learning Repository.

5. Organizing Your Project in Google Colab

Google Colab makes it easy to organize your project. Here’s how you can structure your work:

  1. Create a new notebook and name it something like my_first_ml_model.ipynb.
  2. Use the file explorer on the left-hand side to upload any external datasets (e.g., CSV files).
  3. Save your notebook to Google Drive for easy access later.

Here’s an example of how your Colab interface might look:

Google Colab Interface

Caption: The Google Colab interface with a notebook open and the file explorer visible.

With these prerequisites in place, you’re all set to start building your first machine learning model in Google Colab! Let’s move on to the step-by-step guide. 🚀

If you have any questions or run into issues, feel free to drop a comment below. Happy coding! 😊

Step-by-Step Guide

Now that you’re all set up with Google Colab and have the necessary libraries installed, it’s time to start building your first machine learning model! We’ll begin by importing the libraries we’ll use throughout this tutorial.


Step 1: Import Libraries

Before we can work with data or build a machine learning model, we need to import the necessary Python libraries. Each library has a specific purpose, and together they form the backbone of most machine learning projects. Here’s what we’ll use:

  1. Pandas: For data manipulation and analysis. It allows us to load, explore, and clean datasets efficiently.

  2. NumPy: For numerical operations. It’s essential for handling arrays and performing mathematical computations.

  3. Scikit-learn: For machine learning. It provides tools for preprocessing data, training models, and evaluating their performance.


Where to Put the Code

In Google Colab, you’ll write and run your code in code cells. Here’s how to get started:

  1. Open your Google Colab notebook (if you haven’t already, follow the steps in the Prerequisites section).

  2. Click on the first empty code cell (or add a new one by clicking + Code).

  3. Copy and paste the code below into the cell.

  4. Press Shift + Enter to run the code.


Code to Import Libraries

Here’s the code to import the necessary libraries:

# Importing the necessary libraries
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
 

Explanation of Each Library

  1. Pandas (pd):

    • Used for loading and manipulating datasets.

    • Provides data structures like DataFrames, which make it easy to work with tabular data.

  2. NumPy (np):

    • Used for numerical computations.

    • Essential for handling arrays and performing mathematical operations like matrix multiplication.

  3. Scikit-learn:

    • train_test_split: Splits the dataset into training and testing sets.

    • StandardScaler: Standardizes features by removing the mean and scaling to unit variance.

    • LogisticRegression: A simple and popular algorithm for classification tasks.

    • accuracy_score and confusion_matrix: Used to evaluate the performance of the model.


What Happens When You Run the Code?

When you run the code cell, Google Colab will import all the libraries and make them available for use in your notebook. You won’t see any output unless there’s an error (e.g., if a library isn’t installed). If everything runs successfully, you’re ready to move on to the next step!


Troubleshooting Tips

  • If you see an error like ModuleNotFoundError, it means a library isn’t installed. To fix this, run the following code in a new cell:

    !pip install pandas numpy scikit-learn
  • Make sure you’re running the code in a code cell (not a text cell) in Google Colab.


Step 2: Load the Dataset

Now that we’ve imported the necessary libraries, the next step is to load the dataset we’ll use to build our machine learning model. For this tutorial, we’ll use the Iris dataset, a classic dataset in machine learning. It’s simple, well-structured, and perfect for beginners. Let’s get started!

What is the Iris Dataset?

The Iris dataset contains 150 samples of iris flowers, with four features:

  • Sepal Length (in cm)
  • Sepal Width (in cm)
  • Petal Length (in cm)
  • Petal Width (in cm)

Each sample is labeled with one of three species of iris flowers:

  • Setosa
  • Versicolor
  • Virginica

Our goal is to build a model that can predict the species of an iris flower based on its measurements.

Where to Put the Code

In Google Colab, you’ll write and run your code in code cells. Here’s how to get started:

  1. Open your Google Colab notebook (if you haven’t already, follow the steps in the Prerequisites section).
  2. Click on the first empty code cell (or add a new one by clicking + Code).
  3. Copy and paste the code below into the cell.
  4. Press Shift + Enter to run the code.

Code to Load the Dataset

Here’s the code to load the Iris dataset using Scikit-learn’s built-in datasets:

# Load the Iris datasetfrom sklearn.datasets import load_iris
import pandas as pd

# Load the dataset
iris = load_iris()

# Convert the dataset into a Pandas DataFrame for easier manipulation
df = pd.DataFrame(iris.data, columns=iris.feature_names)

# Add the target variable (species) to the DataFrame
df['species'] = iris.target

# Display the first 5 rows of the dataset
df.head()

Explanation of the Code

  1. load_iris(): This function loads the Iris dataset from Scikit-learn’s built-in datasets.
  2. pd.DataFrame(): Converts the dataset into a Pandas DataFrame, which is easier to work with.
  3. iris.feature_names: Adds the column names (sepal length, sepal width, petal length, petal width) to the DataFrame.
  4. iris.target: Adds the target variable (species) to the DataFrame.
  5. df.head(): Displays the first 5 rows of the dataset to give you a quick preview.

What Happens When You Run the Code?

When you run the code cell, Google Colab will:

  1. Load the Iris dataset.
  2. Convert it into a Pandas DataFrame.
  3. Display the first 5 rows of the dataset, so you can see what it looks like.

Here’s an example of what the output will look like:

sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) species
5.1 3.5 1.4 0.2 0
4.9 3.0 1.4 0.2 0
4.7 3.2 1.3 0.2 0
4.6 3.1 1.5 0.2 0
5.0 3.6 1.4 0.2 0

Troubleshooting Tips

  • If you see an error like ModuleNotFoundError, it means a library isn’t installed. To fix this, run the following code in a new cell:
    !pip install pandas scikit-learn
  • Make sure you’re running the code in a code cell (not a text cell) in Google Colab.

Now that we’ve loaded the dataset, we’re ready to move on to the next step: exploring the data. Let’s keep going! 🚀

Step 3: Explore the Data

Now that we’ve loaded the Iris dataset, it’s time to explore it! Exploratory Data Analysis (EDA) is a crucial step in any machine learning project. It helps us understand the structure, patterns, and potential issues in the dataset. Let’s break this down into three simple tasks:

  1. Check for Missing Values

  2. Summarize the Dataset

  3. Visualize the Data

1. Check for Missing Values

Missing values can cause errors when training a machine learning model. Let’s check if our dataset has any missing values. Run the following code in a new code cell:

# Check for missing values
df.isnull().sum()

Explanation:

  • df.isnull() checks for missing values in the DataFrame.

  • .sum() adds up the number of missing values in each column.

What Happens When You Run the Code?
If there are no missing values, you’ll see 0 for every column. If there are missing values, you’ll see the count of missing values for each column. For the Iris dataset, you should see all zeros because it’s a clean dataset.


2. Summarize the Dataset

Next, let’s get a quick summary of the dataset. This will help us understand the distribution of the data (e.g., mean, min, max values). Run the following code:

# Summarize the dataset
df.describe()

Explanation:

  • df.describe() provides summary statistics for numerical columns, including:

    • count: Number of non-missing values.

    • mean: Average value.

    • std: Standard deviation (measures how spread out the values are).

    • min: Minimum value.

    • max: Maximum value.

    • 25%, 50%, 75%: Percentiles (useful for understanding the distribution).

What Happens When You Run the Code?
You’ll see a table with summary statistics for each feature (sepal length, sepal width, petal length, petal width). This gives you a quick overview of the dataset.


3. Visualize the Data

Visualizing data is a great way to spot patterns and relationships between features. Let’s create a pair plot to visualize the relationships between all features. Run the following code:

# Import Seaborn for visualization
import seaborn as sns
import matplotlib.pyplot as plt

# Create a pair plot
sns.pairplot(df, hue='species')
plt.show()

Explanation:

  • sns.pairplot() creates a grid of scatter plots, showing relationships between all pairs of features.

  • The hue='species' parameter colors the points based on the species of iris, making it easier to see patterns.

What Happens When You Run the Code?

You’ll see a grid of scatter plots. Each plot shows the relationship between two features, with points colored by species. This helps you understand how the features differ across species.


Step 4: Preprocess the Data

Before we can train our machine learning model, we need to preprocess the data. This involves preparing the dataset in a way that makes it suitable for training. Let’s break this down into three steps:

  1. Handle Missing Values (if any)

  2. Encode Categorical Variables (if applicable)

  3. Split the Dataset into Training and Testing Sets

1. Handle Missing Values

If your dataset has missing values (which the Iris dataset doesn’t), you’ll need to handle them. Common approaches include:

  • Dropping rows with missing values: Use df.dropna().

  • Filling missing values: Use df.fillna() to fill missing values with a specific value (e.g., mean, median).

Since the Iris dataset has no missing values, we can skip this step. But here’s an example of how you’d handle missing values:

# Drop rows with missing values
df = df.dropna()

# OR fill missing values with the mean
df = df.fillna(df.mean())

2. Encode Categorical Variables

Categorical variables (like species in the Iris dataset) need to be converted into numerical values for machine learning models to process them. In the Iris dataset, the target variable (species) is already encoded as numbers (0, 1, 2), so we don’t need to do anything here. However, if your dataset has categorical variables, you can encode them using pd.get_dummies() or LabelEncoder.


3. Split the Dataset into Training and Testing Sets

To evaluate our model’s performance, we need to split the dataset into two parts:

  • Training set: Used to train the model.

  • Testing set: Used to evaluate the model.

Run the following code to split the dataset:

# Import train_test_split
from sklearn.model_selection import train_test_split

# Split the dataset
X = df.drop('species', axis=1)  # Features (all columns except 'species')
y = df['species']  # Target variable ('species')

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

Explanation:

  • X contains the features (sepal length, sepal width, petal length, petal width).

  • y contains the target variable (species).

  • train_test_split() splits the data into training and testing sets.

    • test_size=0.2 means 20% of the data will be used for testing, and 80% for training.

    • random_state=42 ensures the split is reproducible.

What Happens When You Run the Code?
The dataset is split into:

  • X_train and y_train: Training data (80% of the dataset).

  • X_test and y_test: Testing data (20% of the dataset).


What’s Next?

Now that we’ve explored and preprocessed the data, we’re ready to move on to Step 5: Choose a Machine Learning Algorithm. In the next step, we’ll select a simple algorithm (like Logistic Regression or Decision Trees) and start training our model. Stay tuned! 🚀


Step 5: Choose a Machine Learning Algorithm

Now that our data is preprocessed and ready, it’s time to choose a machine learning algorithm. For beginners, it’s best to start with a simple and interpretable algorithm. In this tutorial, we’ll use Logistic Regression. Here’s why:

Why Logistic Regression?

  1. Simple and Easy to Understand: Logistic Regression is a linear model that predicts the probability of a target variable. It’s great for classification tasks like predicting the species of iris flowers.

  2. Perfect for Beginners: It’s one of the first algorithms taught in machine learning courses because of its simplicity and effectiveness.

  3. Works Well with Small Datasets: Since the Iris dataset is small, Logistic Regression is a good fit.

If you’re curious, other beginner-friendly algorithms include:

  • Decision Trees: Easy to visualize and interpret.

  • K-Nearest Neighbors (KNN): Simple and intuitive.

But for now, let’s stick with Logistic Regression. In the future, you can experiment with other algorithms to see how they perform!


Step 6: Train the Model

Training a machine learning model is the process of teaching the algorithm to recognize patterns in the data. Let’s break this down into three simple steps:

  1. Initialize the Model

  2. Fit the Model to the Training Data

  3. Understand What “Training” Means

1. Initialize the Model

First, we need to create an instance of the Logistic Regression model. Run the following code in a new code cell:

# Import Logistic Regression
from sklearn.linear_model import LogisticRegression

# Initialize the model
model = LogisticRegression()

Explanation:

  • LogisticRegression() creates an instance of the Logistic Regression model.

  • This model will be trained on our dataset to predict the species of iris flowers.


2. Fit the Model to the Training Data

Next, we’ll train the model using the training data (X_train and y_train). Run the following code:

# Train the model
model.fit(X_train, y_train)
 

Explanation:

  • model.fit() trains the model on the training data.

  • X_train contains the features (sepal length, sepal width, petal length, petal width).

  • y_train contains the target variable (species).

What Happens When You Run the Code?
The model learns the relationship between the features (X_train) and the target variable (y_train). This process is called training.


3. What Does “Training” Mean?

Training a model is like teaching a child to recognize different types of flowers. Here’s how it works:

  1. Input: You show the child examples of flowers (features like sepal length, petal width) and tell them what species each flower is (target variable).

  2. Learning: The child starts to notice patterns, like “flowers with long petals are usually Virginica.”

  3. Output: After enough examples, the child can predict the species of a new flower based on its features.

In machine learning:

  • The model is the child.

  • The training data (X_train and y_train) is the set of examples.

  • The training process (model.fit()) is the learning phase.

Once the model is trained, it can make predictions on new, unseen data.


What’s Next?

Now that our model is trained, we’re ready to move on to Step 7: Make Predictions. In the next step, we’ll use the trained model to predict the species of iris flowers in the test dataset. Exciting, right? Let’s keep it going! 🚀


Step 7: Make Predictions

Now that our model is trained, it’s time to see how well it performs on new, unseen data. We’ll use the test dataset (X_test) to make predictions and compare them with the actual values (y_test). Let’s get started!

1. Use the Trained Model to Predict

Run the following code in a new code cell to make predictions:

# Make predictions on the test dataset
y_pred = model.predict(X_test)

# Display the first 5 predictions
print("Predictions:", y_pred[:5])

Explanation:

  • model.predict(X_test) uses the trained model to predict the species of iris flowers in the test dataset.

  • y_pred contains the predicted species for each sample in X_test.

  • print(y_pred[:5]) shows the first 5 predictions to give you a quick preview.

What Happens When You Run the Code?
You’ll see an array of predicted species (e.g., [0, 1, 2, 1, 0]). These numbers correspond to the species labels:

  • 0: Setosa

  • 1: Versicolor

  • 2: Virginica


2. Compare Predictions with Actual Values

To see how accurate the predictions are, let’s compare them with the actual values (y_test). Run the following code:

# Display the first 5 actual values
print("Actual values:", y_test[:5].values)

What Happens When You Run the Code?
You’ll see an array of actual species labels (e.g., [0, 1, 2, 1, 0]). Compare this with the predictions to see if they match.


Step 8: Evaluate the Model

Making predictions is exciting, but how do we know if our model is good? That’s where evaluation metrics come in. Let’s calculate and interpret some common metrics for classification tasks.

1. Accuracy

Accuracy is the most straightforward metric. It tells us the percentage of correct predictions. Run the following code to calculate accuracy:

# Import accuracy_score
from sklearn.metrics import accuracy_score

# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

Explanation:

  • accuracy_score(y_test, y_pred) compares the actual values (y_test) with the predicted values (y_pred).

  • The result is a number between 0 and 1, where 1 means 100% accuracy.

What Happens When You Run the Code?
You’ll see the accuracy of your model (e.g., 0.9667). This means the model correctly predicted 96.67% of the test samples.


2. Confusion Matrix

A confusion matrix provides a detailed breakdown of the predictions. It shows how many predictions were correct and where the model made mistakes. Run the following code:

# Import confusion_matrix
from sklearn.metrics import confusion_matrix

# Calculate confusion matrix
conf_matrix = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:\n", conf_matrix)

Explanation:

  • The confusion matrix is a 3x3 grid (since there are 3 species).

  • The rows represent the actual species, and the columns represent the predicted species.

  • The diagonal values show correct predictions, while off-diagonal values show misclassifications.

What Happens When You Run the Code?
You’ll see a matrix like this:

[[10  0  0]
 [ 0  9  1]
 [ 0  0 10]]

This means:

  • All Setosa (0) samples were correctly predicted.

  • 9 Versicolor (1) samples were correctly predicted, but 1 was misclassified as Virginica (2).

  • All Virginica (2) samples were correctly predicted.


Step 9: Improve the Model (Optional)

If your model’s accuracy isn’t as high as you’d like, don’t worry! There are several ways to improve it. Here are a few beginner-friendly techniques:

1. Hyperparameter Tuning

Hyperparameters are settings for the model (e.g., the regularization strength in Logistic Regression). You can use tools like GridSearchCV to find the best hyperparameters.

2. Feature Engineering

Create new features or transform existing ones to help the model learn better. For example, you could combine sepal length and width into a single feature.

3. Try a Different Algorithm

If Logistic Regression doesn’t perform well, try other algorithms like Decision Trees, Random Forests, or K-Nearest Neighbors.


Conclusion

Congratulations! You’ve successfully built, trained, and evaluated your first machine learning model. In the next steps, we’ll recap what you’ve learned and provide additional resources to continue your machine learning journey.

Whether you’re a complete beginner or someone brushing up on the basics, this is a huge step toward mastering machine learning. Now it’s your turn to take the next steps:

Share Your Experience

Did you find this tutorial helpful? Do you have questions or suggestions? Drop a comment below and share your experience! Your feedback helps us create better content for you.

Subscribe for More Tutorials

If you enjoyed this guide, don’t miss out on future tutorials! Subscribe to our newsletter to get the latest updates on machine learning, Python, and data science. Whether you’re a beginner or an expert, we’ve got something for everyone.

Explore Related Posts

Ready to dive deeper? Check out these related posts:

Keep learning, keep experimenting, and most importantly, have fun with machine learning! 🚀


FAQs

Here are answers to some common questions beginners have when starting their machine learning journey:

1. What if I don’t have a dataset?

No worries! You can use publicly available datasets from platforms like:

2. How do I choose the right algorithm?

Choosing the right algorithm depends on:

  • The type of problem: Is it classification, regression, or clustering?

  • The size of the dataset: Some algorithms work better with small datasets, while others require large amounts of data.

  • The complexity of the problem: Start with simple algorithms (like Logistic Regression or Decision Trees) and gradually move to more complex ones.

3. What’s the difference between training and testing data?

  • Training data: Used to teach the model. The model learns patterns from this data.

  • Testing data: Used to evaluate the model. It’s unseen data that tests how well the model generalizes to new inputs.

4. Can I use this tutorial for other datasets?

Absolutely! The steps in this tutorial are universal. You can apply them to any dataset by:

  1. Loading your dataset.

  2. Preprocessing the data.

  3. Choosing an algorithm.

  4. Training and evaluating the model.

5. What’s the best way to improve my model’s accuracy?

Here are a few tips:

  • Feature Engineering: Create new features or transform existing ones.

  • Hyperparameter Tuning: Experiment with different settings for your model.

  • Try Different Algorithms: Some algorithms work better for specific types of data.


Comments

Leave a Comment

Oluwaseun Ibrahim

3 months ago

Vey Explanatory

just me

3 months ago

Yh

Stay Updated

Subscribe to our newsletter for the latest updates, insights, and more!