Spaces:
Sleeping
Sleeping
import streamlit as st | |
import numpy as np | |
import pandas as pd | |
from sklearn.model_selection import train_test_split | |
from sklearn.neighbors import KNeighborsClassifier | |
def train_iris_model(): | |
df = pd.read_csv("Iris.csv") | |
df['Species'] = df['Species'].map({'Iris-setosa': 0, 'Iris-virginica': 1, 'Iris-versicolor': 2}) | |
del df["Id"] | |
X = df.loc[:, df.columns != 'Species'] | |
y = df['Species'] | |
X_train, _, y_train, _ = train_test_split(X, y, test_size=0.3, random_state=42) | |
model = KNeighborsClassifier() | |
model.fit(X_train, y_train) | |
return model | |
def predict_iris_species(model, input_data): | |
# Reshape the input data to (1, -1) to make it compatible with model.predict | |
input_data = np.array(input_data).reshape(1, -1) | |
# Make predictions using the trained model | |
prediction = model.predict(input_data) | |
return prediction | |
def main(): | |
st.title("Iris Species Prediction App") | |
st.sidebar.header("User Input") | |
sepal_length = st.sidebar.slider("Sepal Length", 0.0, 10.0, 5.0) | |
sepal_width = st.sidebar.slider("Sepal Width", 0.0, 10.0, 5.0) | |
petal_length = st.sidebar.slider("Petal Length", 0.0, 10.0, 5.0) | |
petal_width = st.sidebar.slider("Petal Width", 0.0, 10.0, 5.0) | |
trained_model = train_iris_model() | |
input_values = [sepal_length, sepal_width, petal_length, petal_width] | |
prediction_result = predict_iris_species(trained_model, input_values) | |
species_mapping = {0: 'Iris-setosa', 1: 'Iris-virginica', 2: 'Iris-versicolor'} | |
predicted_species = species_mapping.get(prediction_result[0], 'Unknown') | |
st.subheader("User Input Values:") | |
st.write(f"- Sepal Length: {sepal_length}") | |
st.write(f"- Sepal Width: {sepal_width}") | |
st.write(f"- Petal Length: {petal_length}") | |
st.write(f"- Petal Width: {petal_width}") | |
st.subheader("Prediction:") | |
st.success(f"Predicted Species: {predicted_species}") | |
# Display relevant images based on prediction | |
if predicted_species == 'Iris-setosa': | |
st.image('setosa_image.jpg', caption='Iris-setosa', use_column_width=True) | |
elif predicted_species == 'Iris-virginica': | |
st.image('virginica_image.jpg', caption='Iris-virginica', use_column_width=True) | |
elif predicted_species == 'Iris-versicolor': | |
st.image('versicolor_image.jpg', caption='Iris-versicolor', use_column_width=True) | |
if __name__ == "__main__": | |
main() |