Building an Iris Flower Classification App with Streamlit and Random Forest
In this project, we dive into creating a **machine learning web app** using **Streamlit** and the **Random Forest Classifier** to predict the species of an Iris flower. Streamlit is a Python library that makes it super easy to build interactive web applications without needing a lot of front-end development skills. We combined that with the power of machine learning to deliver a functional, real-time classification tool.
### Overview of the Project
The **Iris dataset**, one of the most famous in the world of machine learning, contains three types of Iris flowers:
- Iris Setosa
- Iris Versicolor
- Iris Virginica
Each flower is described by four features:
1. **Sepal length**
2. **Sepal width**
3. **Petal length**
4. **Petal width**
Using these features, we built an app that allows users to input the values of these features through a simple web interface and predict which type of Iris flower it is.
### Key Technologies
1. **Streamlit**: This library simplifies the creation of interactive web apps in Python. It handles UI elements like sliders, buttons, and dynamic text with ease, making it a perfect choice for displaying data science projects.
2. **Scikit-learn**: A go-to library for machine learning in Python. We used its Random Forest Classifier to train a model that accurately predicts flower species based on input features.
3. **Iris Dataset**: A popular dataset used for classification tasks, which consists of 150 observations and 3 flower species, each described by 4 numerical features.
### How It Works
1. **Input Features via Sliders**: The user selects values for the flower’s sepal length, sepal width, petal length, and petal width using sliders in the app.
2. **Prediction**: After selecting these values, the app runs a prediction using the trained **Random Forest** model, outputting the predicted flower species.
3. **Real-time Interaction**: Users can change the slider values, and the app will immediately update with a new prediction, providing instant feedback.
### Why This Project is Useful
This project is a practical demonstration of how machine learning models can be integrated into real-world applications. By using **Streamlit**, we enable users to interact directly with the model without writing code. This app shows how quickly and easily we can build a functional machine learning app to solve classification problems.
### Next Steps
- **Model Improvement**: Tuning the model for better accuracy.
- **Deployment**: Hosting the app on platforms like **Heroku** or **Streamlit Cloud** so anyone can access it.
- **Additional Features**: Expanding the app with more datasets, prediction models, or visualization features.
If you’re looking to learn how machine learning models can be deployed in web applications, this project is a perfect hands-on example. It combines the best of **data science** and **front-end development**, making it an ideal showcase of your Python skills!
Here is the code of this project:
import streamlit as st
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
def load_data():
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['species'] = iris.target
return df, iris.target_names
df, target_names = load_data()
model = RandomForestClassifier()
model.fit(df.iloc[:, :-1], df['species'])
st.title("Input Features")
# Correct column names as they are in the dataset
sepal_length = st.slider("Sepal length (cm)", float(df['sepal length (cm)'].min()), float(df['sepal length (cm)'].max()))
sepal_width = st.slider("Sepal width (cm)", float(df['sepal width (cm)'].min()), float(df['sepal width (cm)'].max()))
petal_length = st.slider("Petal length (cm)", float(df['petal length (cm)'].min()), float(df['petal length (cm)'].max()))
petal_width = st.slider("Petal width (cm)", float(df['petal width (cm)'].min()), float(df['petal width (cm)'].max()))
input_data = [[sepal_length, sepal_width, petal_length, petal_width]]
# Prediction
prediction = model.predict(input_data)
predicted_species = target_names[prediction[0]]
st.write("Prediction")
st.write(f"The predicted species is: {predicted_species}")
Comments
Post a Comment