narinsak unawong commited on
Commit
9a9cf94
·
verified ·
1 Parent(s): 533c469

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -56
app.py CHANGED
@@ -1,65 +1,44 @@
1
  import streamlit as st
2
- import pickle
3
  import pandas as pd
 
 
 
4
  from sklearn.pipeline import Pipeline
5
- import numpy as np
6
 
7
- # Load the model and encoders
8
  with open('model_penguin_706.pkl', 'rb') as file:
9
  model, species_encoder, island_encoder, sex_encoder = pickle.load(file)
10
 
11
- # Streamlit app layout
12
  st.title('Penguin Species Prediction')
13
 
14
- # Create user input fields
15
- st.sidebar.header('Input Features')
16
-
17
- # User inputs
18
- species = st.sidebar.selectbox('Species', species_encoder.classes_)
19
- island = st.sidebar.selectbox('Island', island_encoder.classes_)
20
- sex = st.sidebar.selectbox('Sex', sex_encoder.classes_)
21
-
22
- # Slider for numeric inputs
23
- bill_length_mm = st.sidebar.slider('Bill Length (mm)', 30.0, 60.0, 45.0)
24
- bill_depth_mm = st.sidebar.slider('Bill Depth (mm)', 10.0, 25.0, 18.0)
25
- flipper_length_mm = st.sidebar.slider('Flipper Length (mm)', 170.0, 240.0, 200.0)
26
- body_mass_g = st.sidebar.slider('Body Mass (g)', 2500.0, 6000.0, 4000.0)
27
-
28
- # Add missing columns with default values
29
- culmen_length_mm = 40.0 # Default value, update with realistic values if available
30
- culmen_depth_mm = 15.0 # Default value, update with realistic values if available
31
-
32
- # Prepare the input data (ensure columns are in the same order as expected by the model)
33
- input_data = pd.DataFrame({
34
- 'species': [species],
35
- 'island': [island],
36
- 'sex': [sex],
37
- 'bill_length_mm': [bill_length_mm],
38
- 'bill_depth_mm': [bill_depth_mm],
39
- 'flipper_length_mm': [flipper_length_mm],
40
- 'body_mass_g': [body_mass_g],
41
- 'culmen_length_mm': [culmen_length_mm],
42
- 'culmen_depth_mm': [culmen_depth_mm]
43
- })
44
-
45
- # Check for NaN values in the input data
46
- if input_data.isna().any().any():
47
- st.warning("Input data contains NaN values. Filling with default values.")
48
- input_data = input_data.fillna(0) # Replace NaN values with 0 or appropriate value
49
-
50
- # Apply encoding to categorical features (check column names here!)
51
- input_data['species'] = species_encoder.transform(input_data['species'])
52
- input_data['island'] = island_encoder.transform(input_data['island'])
53
- input_data['sex'] = sex_encoder.transform(input_data['sex'])
54
-
55
- # Ensure the columns are in the correct order
56
- if isinstance(model, Pipeline):
57
- preprocessor = model.named_steps.get('preprocessor') # Replace with actual step name if different
58
- if preprocessor:
59
- input_data = preprocessor.transform(input_data) # Apply any necessary transformations
60
-
61
- # Make prediction
62
- prediction = model.predict(input_data)
63
-
64
- # Show the result
65
- st.write(f'Predicted Species: {species_encoder.inverse_transform(prediction)}')
 
1
  import streamlit as st
 
2
  import pandas as pd
3
+ import pickle
4
+ from sklearn.preprocessing import LabelEncoder, StandardScaler, OneHotEncoder
5
+ from sklearn.compose import ColumnTransformer
6
  from sklearn.pipeline import Pipeline
7
+ from sklearn.neighbors import KNeighborsClassifier
8
 
9
+ # Load the saved model and encoders
10
  with open('model_penguin_706.pkl', 'rb') as file:
11
  model, species_encoder, island_encoder, sex_encoder = pickle.load(file)
12
 
13
+ # Create the Streamlit app
14
  st.title('Penguin Species Prediction')
15
 
16
+ # Input fields for user data
17
+ island = st.selectbox('Island', ['Torgersen', 'Biscoe', 'Dream'])
18
+ culmen_length_mm = st.number_input('Culmen Length (mm)', min_value=0.0)
19
+ culmen_depth_mm = st.number_input('Culmen Depth (mm)', min_value=0.0)
20
+ flipper_length_mm = st.number_input('Flipper Length (mm)', min_value=0.0)
21
+ body_mass_g = st.number_input('Body Mass (g)', min_value=0.0)
22
+ sex = st.selectbox('Sex', ['MALE', 'FEMALE'])
23
+
24
+
25
+ # Create a button to trigger prediction
26
+ if st.button('Predict Species'):
27
+ # Create a DataFrame from user inputs
28
+ x_new = pd.DataFrame({
29
+ 'island': [island],
30
+ 'culmen_length_mm': [culmen_length_mm],
31
+ 'culmen_depth_mm': [culmen_depth_mm],
32
+ 'flipper_length_mm': [flipper_length_mm],
33
+ 'body_mass_g': [body_mass_g],
34
+ 'sex': [sex]
35
+ })
36
+
37
+ # Make the prediction
38
+ y_pred_new = model.predict(x_new)
39
+
40
+ # Inverse transform the prediction
41
+ result = species_encoder.inverse_transform(y_pred_new)
42
+
43
+ # Display the prediction
44
+ st.write('Predicted Species:', result[0])