import streamlit as st from sklearn.datasets import load_iris from sklearn.tree import DecisionTreeClassifier, plot_tree from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt import pandas as pd # Load the Iris dataset iris = load_iris() X = iris.data y = iris.target feature_names = iris.feature_names target_names = iris.target_names # Train a Decision Tree model X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42) clf = DecisionTreeClassifier(max_depth=3, random_state=42) clf.fit(X_train, y_train) # Streamlit interface st.title("🌸 Iris Flower Predictor with Decision Tree") st.write("This app uses a Decision Tree Classifier to predict the type of Iris flower.") # Sidebar for user input st.sidebar.header("Input Features") sepal_length = st.sidebar.slider('Sepal length (cm)', 4.0, 8.0, 5.1) sepal_width = st.sidebar.slider('Sepal width (cm)', 2.0, 4.5, 3.5) petal_length = st.sidebar.slider('Petal length (cm)', 1.0, 7.0, 1.4) petal_width = st.sidebar.slider('Petal width (cm)', 0.1, 2.5, 0.2) # Make prediction input_data = [[sepal_length, sepal_width, petal_length, petal_width]] prediction = clf.predict(input_data)[0] predicted_class = target_names[prediction] st.subheader("🌼 Predicted Iris Species") st.success(f"The model predicts: **{predicted_class}**") # Show decision tree st.subheader("🧠 Decision Tree Visualization") fig, ax = plt.subplots(figsize=(12, 6)) plot_tree(clf, feature_names=feature_names, class_names=target_names, filled=True, rounded=True) st.pyplot(fig) # Show model accuracy accuracy = clf.score(X_test, y_test) st.subheader("📈 Model Accuracy") st.write(f"The model accuracy on the test set is **{accuracy:.2f}**")