| 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 |
|
|
| |
| iris = load_iris() |
| X = iris.data |
| y = iris.target |
| feature_names = iris.feature_names |
| target_names = iris.target_names |
|
|
| |
| 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) |
|
|
| |
| st.title("๐ธ Iris Flower Predictor with Decision Tree") |
| st.write("This app uses a Decision Tree Classifier to predict the type of Iris flower.") |
|
|
| |
| 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) |
|
|
| |
| 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}**") |
|
|
| |
| 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) |
|
|
| |
| accuracy = clf.score(X_test, y_test) |
| st.subheader("๐ Model Accuracy") |
| st.write(f"The model accuracy on the test set is **{accuracy:.2f}**") |