ChinmayBH's picture
created app.py
b4cf47a verified
raw
history blame
1.46 kB
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")
sepal_length = st.slider("Sepal Length", 0.0, 10.0, 5.0)
sepal_width = st.slider("Sepal Width", 0.0, 10.0, 5.0)
petal_length = st.slider("Petal Length", 0.0, 10.0, 5.0)
petal_width = st.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)
st.write(f"Predicted Species: {prediction_result[0]}")
if __name__ == "__main__":
main()