cover-img

How to use Support Vector Machine in Python with Scikit-learn in Streamlit

10 May, 2023

1

1

1

Introduction

Hey everyone, I am Omale Happiness Ojone and I am currently a Rotational Super Writer at Showwcase where I create content on tech and educate the community regarding python, data science and machine learning.

In this article I will show you how to use SVMs in python using scikit-learn library and integrate this model in streamlit for interactive data exploration.

Support Vector Machines (SVMs) are widely used in machine learning for classification and regression analysis. SVMs are known for their ability to handle high-dimensional data, non-linear data and their ability to generalize well to new data.

Prerequisites required

  1. Python installed on your system
  2. Jupyter notebook on your system
  3. Visual studio on your system

Let's get started

Installing Required Libraries

To get started, we need to install some libraries required to run our SVM model in python. First, we will install scikit-learn, a popular python library for machine learning. Open your terminal and run the following command:

pip install scikit-learn

Next, we will install Streamlit, an open-source Python library that makes it easy to build beautiful, interactive web apps for data science and machine learning. Open your terminal and run the following command:

pip install streamlit

Data Preparation

In this article, we will use the popular iris dataset, which is included in scikit-learn library. We will load the iris dataset and split it into training and testing datasets.

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

# Load the iris dataset
iris = load_iris()

# Split the dataset into training and testing datasets
X_train, X_test, y_train, y_test = train_test_split(
iris.data, iris.target, test_size=0.3, random_state=42)

Training SVM Model

Next, we will create an instance of the SVM model and train it on our training dataset. We will use the scikit-learn library to create an instance of the SVM model and fit it into our training data.

from sklearn.svm import SVC

# Create an instance of the SVM model
svm_model = SVC(kernel='linear')

# Fit the SVM model to the training data
svm_model.fit(X_train, y_train)

Evaluating SVM Model

After training our SVM model, we will evaluate its performance on the testing dataset. We will use the scikit-learn library to make predictions on our testing data and compute the accuracy of our model.

from sklearn.metrics import accuracy_score

# Make predictions on the testing data
y_pred = svm_model.predict(X_test)

# Compute the accuracy of the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")

Integrating SVM Model in Streamlit

Finally, we will integrate our SVM model into Streamlit. We will create a Streamlit app that allows us make predictions using our SVM model.

import streamlit as st
from my_utils import predict

st.set_page_config(page_title="Iris Deployment", page_icon=":car")

# start the user interface
st.title("Iris Deployment")

sepal_length = st.number_input("Sepal Length (cm)", min_value=0.0, step=0.1)
sepal_width = st.number_input("Sepal Width (cm)", min_value=0.0, step=0.1)
petal_length = st.number_input("Petal Length (cm)", min_value=0.0, step=0.1)
petal_width = st.number_input("Petal Width (cm)", min_value=0.0, step=0.1)

if st.button("Get Prediction", key="submit_button"):
   
    x_test = [sepal_length, sepal_width, petal_length, petal_width]
   
    prediction = predict(x_test=x_test)
   
    st.write(f"The model's prediction is {prediction}")

Let me explain in bits what the code does:

import streamlit as st
from my_utils import predict
  • Above, we import the streamlit library as st.

Streamlit is a python library used for building web applications with interactive user interface.

  • We also import a function called predict from a module called my_utils. It is used to make a prediction using a machine learning model.
st.set_page_config(page_title="Iris Deployment", page_icon=":car")
  • Setting the page title to "Iris Deployment" and the page icon to ":car" using the set_page_config method of the st object.
# start the user interface
st.title("Iris Deployment")
  • Adding a title "Iris Deployment" to the user interface using the title method of the st object.
sepal_length = st.number_input("Sepal Length (cm)", min_value=0.0, step=0.1)
sepal_width = st.number_input("Sepal Width (cm)", min_value=0.0, step=0.1)
petal_length = st.number_input("Petal Length (cm)", min_value=0.0, step=0.1)
petal_width = st.number_input("Petal Width (cm)", min_value=0.0, step=0.1)
  • Creating number input fields for the user to input values for sepal length, sepal width, petal length, and petal width using the number_input method of the st object. The minimum value for each input field is set to 0.0 and the step value is set to 0.1.
if st.button("Get Prediction", key="submit_button"):
   
    x_test = [sepal_length, sepal_width, petal_length, petal_width]
   
    prediction = predict(x_test=x_test)
   
    st.write(f"The model's prediction is {prediction}")
  • The if statement checks if the "Get Prediction" button has been clicked using the button method of the st object. If the button has been clicked, the code inside the if statement will be executed.
  • Inside the if statement, the values of sepal length, sepal width, petal length, and petal width are stored in a list called x_test.
  • The predict function is called with x_test as an argument, and the result is stored in a variable called prediction.
  • The write method of the st object is used to display the prediction to the user. The prediction is displayed as a string that includes the text "The model's prediction is" followed by the value of the prediction.

Output:

Let's test it with values and see the result

FAQs

  1. What is Support Vector Machine (SVM) algorithm?

Support Vector Machine (SVM) is a machine learning algorithm for classification and regression tasks. It is a supervised learning algorithm that can be used for both linear and non-linear data analysis.

  1. What is Scikit-learn?

Scikit-learn is a Python library for machine learning tasks such as classification, regression, and clustering. It provides a range of algorithms and tools for data analysis.

  1. What is Streamlit?

Streamlit is an open-source Python library for building interactive web applications for data science and machine learning tasks. It allows users to create and share data-driven apps in minutes.

  1. How can I install Streamlit?

You can install Streamlit library using pip command. Open your terminal or command prompt and run the following command:

pip install streamlit

Conclusion

Support Vector Machines (SVM) is a powerful classification algorithm used in machine learning. Using python and sci-kit learn library. We can implement SVM to build robust classification models that predict accurately. Streamlit provides a user-friendly interactive web app using simple python scripts. By combining these tools, developers can create powerful machine learning applications that are easy to use and understand. Following the steps outlined in this article, you can learn how to use SVM in python using sci-kit learn and deploy it in streamlit.

Thank you for reading.

1

1

1

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2025. Showcase Creators Inc. All rights reserved.