Oops.. Currently, there are no active offers available. Please check back later!
Unlock New Skills – Dive into Our Curated Course Collection Today!

  • Login
Website Logo
  • Home
  • Projects
  • Courses
  • Contact
  • Blog
  • Client Services
  • Our Portfolio
Join Now

Course Category

  • Python
  • React Js
  • Django
    • Python Django Tutorial: Build a Comprehensive Student Management System | Python Projects & Django in Hindi
    • Master Django: Build a High-Performance Blog Website from Scratch 📝💻
  • Symfony
  • Laravel
  • Node Js
  • JavaScript
  • Bootstrap
  • Sylius
  • Wordpress
  • HTML5
  • CSS3
Learn More
Education Logo Images

At WebifyDev, we believe that great things happen when talented and motivated individuals come together.

  • example@gmail.com
  • (302) 555-0107
  • Home
  • Courses
  • About Us
  • Contact
  • Blog
  • Faqs
  • Privacy Policy
Enroll Now
Find With Us
Education Images
  • blog-image
    Dev Patel in Python
  • 29 Oct 2024

Building Your First Machine Learning Model with Python and scikit-learn

Get started with machine learning using Python! In this post, learn how to build a simple machine learning model using the scikit-learn library, from data preprocessing to prediction.

Building Your First Machine Learning Model with Python and scikit-learn
Build Machine Learning Models with Python – Unlock the Power of Data-Driven Predictions

Building Your First Machine Learning Model with Python and scikit-learn

Machine learning is transforming industries by allowing computers to learn from data and make predictions. Python, with its powerful scikit-learn library, offers a simple way to build machine learning models. In this guide, we’ll walk through the process of building your first model step-by-step.

1. Prerequisites

Before we begin, make sure you have the necessary libraries installed:

# Install scikit-learn and other dependencies
pip install scikit-learn pandas

2. Importing Libraries and Preparing the Data

We’ll use a sample dataset from scikit-learn for this example. Let’s import the necessary libraries and load the dataset.

# Importing libraries
import pandas as pd
from sklearn.datasets import load_iris

# Loading the dataset
data = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)
df['target'] = data.target

print(df.head())  # Display the first few rows

3. Splitting the Data into Training and Testing Sets

We need to divide the dataset into a training set and a testing set to evaluate the model’s performance.

# Import train_test_split function
from sklearn.model_selection import train_test_split

# Split the data
X = df.drop('target', axis=1)
y = df['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

4. Building a Machine Learning Model

We’ll use the k-Nearest Neighbors (k-NN) algorithm, a simple and popular classification method.

# Importing the k-NN classifier
from sklearn.neighbors import KNeighborsClassifier

# Initialize the model
model = KNeighborsClassifier(n_neighbors=3)

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

5. Making Predictions

Let’s use the trained model to make predictions on the test data.

# Make predictions on the test set
y_pred = model.predict(X_test)
print(y_pred)  # Display the predictions

6. Evaluating the Model

We can measure the performance of the model using accuracy score.

# Importing accuracy_score
from sklearn.metrics import accuracy_score

# Calculate the accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {accuracy * 100:.2f}%")

7. Conclusion

Congratulations! You’ve built your first machine learning model using Python and scikit-learn. With this basic knowledge, you can explore more advanced algorithms and datasets to further develop your skills in machine learning. Keep experimenting, and soon you'll be creating models that solve real-world problems!

Happy coding and learning!

Python Programming
blog-image
Dev
Author

👨‍💻 Dev Patel | Software Engineer 🚀 | Passionate about crafting efficient code, optimizing systems, and building user-friendly digital experiences! 💡

0 Comments

  • No comments yet. Be the first to comment!

Leave a Comment

Related Post

Similar Post

What is the Django Framework and Its Uses
What is the Django Framework and Its Uses
Read Article
Getting Started with Python Django: A Comprehensive Beginner's Guide
Getting Started with Python Django: A Comprehensive Beginner's Guide
Read Article
Python for Data Analysis: A Beginner’s Guide to Pandas and NumPy
Python for Data Analysis: A Beginner’s Guide to Pandas and NumPy
Read Article
WebifyDev Logo

At WebifyDev, we believe that great things happen when talented and motivated individuals come together.

Contact With Us
Useful Links
  • Home
  • My Account
  • Dashboard
  • Courses
  • Blog
  • Our Portfolio
  • Lucky Draw
Our Company
  • About
  • Contact Us
  • Client Services
  • Privacy Policy
  • Terms of Service
  • Cancellation & Refund Policy
  • Shipping Policy
  • Faqs
Get Contact
  • E-mail: webifydev.team@gmail.com
  • Address: Swarnim Dharti, Ahmedabad, Gujarat 382421

Copyright © 2025 WebifyDev. All Rights Reserved.

  • Privacy Policy
  • Login & Register