Spaces:
Sleeping
Sleeping
narinsak unawong commited on
Update app.py
Browse files
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 |
-
|
| 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
|
| 12 |
st.title('Penguin Species Prediction')
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
st.
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
sex = st.
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
'
|
| 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])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|