mherlie commited on
Commit
3bf5901
·
1 Parent(s): cfc74ca

modified code

Browse files
Files changed (1) hide show
  1. app.py +52 -59
app.py CHANGED
@@ -9,67 +9,60 @@ import os
9
  # Fix protobuf compatibility issue
10
  os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
11
 
12
- # Sidebar Navigation
13
- st.sidebar.title("Iris Flower Classification")
14
- page = st.sidebar.radio("Navigation", ["Overview", "Dataset Upload", "Dataset Details", "Data Visualization", "Prediction"])
15
 
16
- df = None
17
- if page == "Dataset Upload":
18
- st.title("Upload Dataset")
19
- uploaded_file = st.file_uploader("Upload your Iris dataset (CSV format)", type=["csv"])
20
- if uploaded_file is not None:
21
- df = pd.read_csv(uploaded_file)
22
- df.rename(columns={'Species': 'species'}, inplace=True)
23
- st.success("Dataset successfully uploaded!")
24
 
25
- if df is not None:
26
- if page == "Overview":
27
- st.title("Overview")
28
- st.write("""
29
- This app uses machine learning to classify Iris flowers into three species:
30
- - Setosa
31
- - Versicolor
32
- - Virginica
33
-
34
- The model is trained using the **Random Forest Classifier**.
35
- """)
36
 
37
- elif page == "Dataset Details":
38
- st.title("Dataset Details")
39
- st.write("### Sample Data")
40
- st.dataframe(df.head())
41
- st.write("### Dataset Statistics")
42
- st.write(df.describe())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
- elif page == "Data Visualization":
45
- st.title("Data Visualization")
46
- st.write("### Pairplot of Features")
47
- fig = sns.pairplot(df, hue="species", diag_kind="kde")
48
- st.pyplot(fig)
49
-
50
- st.write("### Feature Correlation")
51
- fig, ax = plt.subplots()
52
- sns.heatmap(df.corr(), annot=True, cmap='coolwarm', ax=ax)
53
- st.pyplot(fig)
54
 
55
- elif page == "Prediction":
56
- st.title("Iris Flower Prediction")
57
-
58
- st.sidebar.header("Enter flower measurements:")
59
- sepal_length = st.sidebar.slider("Sepal Length (cm)", 4.0, 8.0, 5.0)
60
- sepal_width = st.sidebar.slider("Sepal Width (cm)", 2.0, 5.0, 3.0)
61
- petal_length = st.sidebar.slider("Petal Length (cm)", 1.0, 7.0, 4.0)
62
- petal_width = st.sidebar.slider("Petal Width (cm)", 0.1, 2.5, 1.0)
63
-
64
- X = df.iloc[:, 1:-1]
65
- y = df['species']
66
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
67
-
68
- model = RandomForestClassifier()
69
- model.fit(X_train, y_train)
70
-
71
- input_data = [[sepal_length, sepal_width, petal_length, petal_width]]
72
- prediction = model.predict(input_data)[0]
73
-
74
- st.write("### Prediction Result")
75
- st.success(f"The predicted species is: {prediction}")
 
9
  # Fix protobuf compatibility issue
10
  os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
11
 
12
+ # Load Dataset
13
+ df = pd.read_csv("Iris.csv")
14
+ df.rename(columns={'Species': 'species'}, inplace=True)
15
 
16
+ # Tabs Navigation
17
+ tabs = st.tabs(["Overview", "Dataset Details", "Data Visualization", "Prediction"])
 
 
 
 
 
 
18
 
19
+ with tabs[0]: # Overview
20
+ st.title("Overview")
21
+ st.write("""
22
+ This app uses machine learning to classify Iris flowers into three species:
23
+ - Setosa
24
+ - Versicolor
25
+ - Virginica
 
 
 
 
26
 
27
+ The model is trained using the **Random Forest Classifier**.
28
+ """)
29
+
30
+ with tabs[1]: # Dataset Details
31
+ st.title("Dataset Details")
32
+ st.write("### Sample Data")
33
+ st.dataframe(df.head())
34
+ st.write("### Dataset Statistics")
35
+ st.write(df.describe())
36
+
37
+ with tabs[2]: # Data Visualization
38
+ st.title("Data Visualization")
39
+ st.write("### Pairplot of Features")
40
+ fig = sns.pairplot(df, hue="species", diag_kind="kde")
41
+ st.pyplot(fig)
42
+
43
+ st.write("### Feature Correlation")
44
+ fig, ax = plt.subplots()
45
+ sns.heatmap(df.corr(), annot=True, cmap='coolwarm', ax=ax)
46
+ st.pyplot(fig)
47
+
48
+ with tabs[3]: # Prediction
49
+ st.title("Iris Flower Prediction")
50
+
51
+ st.sidebar.header("Enter flower measurements:")
52
+ sepal_length = st.sidebar.slider("Sepal Length (cm)", 4.0, 8.0, 5.0)
53
+ sepal_width = st.sidebar.slider("Sepal Width (cm)", 2.0, 5.0, 3.0)
54
+ petal_length = st.sidebar.slider("Petal Length (cm)", 1.0, 7.0, 4.0)
55
+ petal_width = st.sidebar.slider("Petal Width (cm)", 0.1, 2.5, 1.0)
56
+
57
+ X = df.iloc[:, 1:-1]
58
+ y = df['species']
59
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
60
+
61
+ model = RandomForestClassifier()
62
+ model.fit(X_train, y_train)
63
 
64
+ input_data = [[sepal_length, sepal_width, petal_length, petal_width]]
65
+ prediction = model.predict(input_data)[0]
 
 
 
 
 
 
 
 
66
 
67
+ st.write("### Prediction Result")
68
+ st.success(f"The predicted species is: {prediction}")