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