Elsdc/Supervised2
0
1import streamlit as st
2import pandas as pd
3import plotly.express as px
4from sklearn.model_selection import train_test_split
5from sklearn.ensemble import RandomForestClassifier
6
7#Page config
8st.set_page_config(
9 page_title = 'Supervised Model 2 - Constantino',
10 layout = 'wide',
11 initial_sidebar_state='expanded'
12)
13
14#Title
15st.title("Iris Flower Species Prediction Using User Input Values via Slider ๐บ")
16
17padtop = '<div style="padding: 20px; "></div>' #added simple padding
18st.markdown(padtop, unsafe_allow_html=True)
19
20# Load dataset
21df = pd.read_csv('iris.csv')
22
23# Input widgets
24st.sidebar.subheader('Input features')
25sepal_length = st.sidebar.slider('Sepal length', 4.3, 7.9, 5.8)
26sepal_width = st.sidebar.slider('Sepal width', 2.0, 4.4, 3.1)
27petal_length = st.sidebar.slider('Petal length', 1.0, 6.9, 3.8)
28petal_width = st.sidebar.slider('Petal width', 0.1, 2.5, 1.2)
29
30# Separate to X and y
31X = df.drop('species', axis=1)
32y = df.species
33
34# Data splitting
35X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
36
37# Model building
38rf = RandomForestClassifier(max_depth=2, max_features=4, n_estimators=200, random_state=42)
39rf.fit(X_train, y_train)
40
41# Apply model to make predictions
42y_pred = rf.predict([[sepal_length, sepal_width, petal_length, petal_width]])
43
44# Brief dataset explanation
45st.write('The Iris dataset consists of four numerical features that describe the physical characteristics of iris flowers.')
46fig = px.scatter(df, x='petal_length', y='petal_width', color='species',
47 title='Scatter Plot of Petal Length vs. Petal Width')
48st.plotly_chart(fig)
49st.write('This charts shows how the petal length and petal width affect the classification of Iris species.')
50
51
52# Print input features
53st.markdown(padtop, unsafe_allow_html=True)
54st.subheader('Input features based on slider')
55input_feature = pd.DataFrame([[sepal_length, sepal_width, petal_length, petal_width]],
56 columns=['sepal_length', 'sepal_width', 'petal_length', 'petal_width'])
57st.write(input_feature)
58
59# Print prediction output
60st.subheader('Predicted Output ๐บ')
61st.metric('Species:', y_pred[0], border=True)