File size: 3,607 Bytes
b4cf47a
 
 
 
 
31dce72
 
 
 
4db93dc
31dce72
b4cf47a
 
 
 
 
 
 
31dce72
 
 
1fa0f1f
31dce72
 
 
 
 
 
 
 
1fa0f1f
b4cf47a
 
 
 
 
 
 
 
 
 
 
1fa0f1f
 
 
 
 
 
b4cf47a
 
 
 
31dce72
 
 
 
 
 
66ea3b2
9f2ea64
 
 
 
b4cf47a
 
1fa0f1f
b4cf47a
d3e57c7
 
 
66ea3b2
 
 
 
 
 
 
 
b4cf47a
1fa0f1f
 
 
4db93dc
 
 
 
 
 
 
 
b4cf47a
31dce72
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier

def train_iris_model(algorithm):
    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)

    if algorithm == 'KNN':
        model = KNeighborsClassifier()
    elif algorithm == 'SVM':
        model = SVC(probability=True)
    elif algorithm == "logistic regression":
        model = LogisticRegression()
    elif algorithm == 'Random Forest':
        model = RandomForestClassifier()
    elif algorithm == 'Ada boost classifier':
        model = AdaBoostClassifier()
    elif algorithm == 'Decision tree':
        model = DecisionTreeClassifier()

    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)

    # Check if the model has a predict_proba method
    if hasattr(model, 'predict_proba'):
        confidence = model.predict_proba(input_data).max()
        return prediction, confidence
    else:
        return prediction, None

def main():
    st.title("Iris Species Prediction App")

    # Choose ML algorithm for training
    algorithm = st.sidebar.selectbox("Select ML Algorithm", ['KNN', 'SVM', 'logistic regression', 'Random Forest', 'Ada boost classifier', 'Decision tree'])

    # Train the model based on user's choice
    trained_model = train_iris_model(algorithm)

    st.sidebar.header("User Input")
    sepal_length = st.sidebar.slider("Sepal Length", 0.0, 10.0, 1.0)
    sepal_width = st.sidebar.slider("Sepal Width", 0.0, 10.0, 1.0)
    petal_length = st.sidebar.slider("Petal Length", 0.0, 10.0, 1.0)
    petal_width = st.sidebar.slider("Petal Width", 0.0, 10.0, 1.0)

    input_values = [sepal_length, sepal_width, petal_length, petal_width]
    prediction_result, confidence = 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}")

    if confidence is not None:
        st.info(f"Confidence of prediction: {confidence * 100:.2f}%")

    # 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()