Spaces:
Sleeping
Sleeping
created app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
from sklearn.model_selection import train_test_split
|
5 |
+
from sklearn.neighbors import KNeighborsClassifier
|
6 |
+
|
7 |
+
def train_iris_model():
|
8 |
+
df = pd.read_csv("Iris.csv")
|
9 |
+
df['Species'] = df['Species'].map({'Iris-setosa': 0, 'Iris-virginica': 1, 'Iris-versicolor': 2})
|
10 |
+
del df["Id"]
|
11 |
+
X = df.loc[:, df.columns != 'Species']
|
12 |
+
y = df['Species']
|
13 |
+
X_train, _, y_train, _ = train_test_split(X, y, test_size=0.3, random_state=42)
|
14 |
+
|
15 |
+
model = KNeighborsClassifier()
|
16 |
+
model.fit(X_train, y_train)
|
17 |
+
|
18 |
+
return model
|
19 |
+
|
20 |
+
def predict_iris_species(model, input_data):
|
21 |
+
# Reshape the input data to (1, -1) to make it compatible with model.predict
|
22 |
+
input_data = np.array(input_data).reshape(1, -1)
|
23 |
+
|
24 |
+
# Make predictions using the trained model
|
25 |
+
prediction = model.predict(input_data)
|
26 |
+
|
27 |
+
return prediction
|
28 |
+
|
29 |
+
def main():
|
30 |
+
st.title("Iris Species Prediction App")
|
31 |
+
|
32 |
+
sepal_length = st.slider("Sepal Length", 0.0, 10.0, 5.0)
|
33 |
+
sepal_width = st.slider("Sepal Width", 0.0, 10.0, 5.0)
|
34 |
+
petal_length = st.slider("Petal Length", 0.0, 10.0, 5.0)
|
35 |
+
petal_width = st.slider("Petal Width", 0.0, 10.0, 5.0)
|
36 |
+
|
37 |
+
trained_model = train_iris_model()
|
38 |
+
input_values = [sepal_length, sepal_width, petal_length, petal_width]
|
39 |
+
prediction_result = predict_iris_species(trained_model, input_values)
|
40 |
+
|
41 |
+
st.write(f"Predicted Species: {prediction_result[0]}")
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
main()
|