Spaces:
Build error
Build error
File size: 3,303 Bytes
313793a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
import joblib
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
import streamlit as st
# Load your dataset
df = pd.read_csv("train.csv")
# Feature engineering
def feature_engineering(df):
df['hair_soul'] = df['hair_length'] * df['has_soul']
df['hair_bone'] = df['hair_length'] * df['bone_length']
# Function to predict ghost type
def ghost_classification(pipeline, input_df):
# Use the trained pipeline to make predictions on the input data
prediction = pipeline.predict(input_df)
# Return the predicted ghost type (the class label)
return prediction[0]
# Feature engineering on the dataset
feature_engineering(df)
# Define the preprocessor
preprocessor = ColumnTransformer(
transformers=[
("num", StandardScaler(), ['bone_length', 'rotting_flesh', 'hair_length', 'has_soul', 'hair_soul', 'hair_bone']),
("cat", OneHotEncoder(), ['color'])
]
)
# Split the data
X = df[['bone_length', 'rotting_flesh', 'hair_length', 'has_soul', 'color', 'hair_soul', 'hair_bone']]
y = df["type"]
# Define the model and pipeline
model = RandomForestClassifier()
pipeline = Pipeline(steps=[("preprocessor", preprocessor), ("classifier", model)])
# Train the model
pipeline.fit(X, y)
# Save the trained model
joblib.dump(pipeline, 'best_model.pkl')
print("Model retrained and saved as 'best_model.pkl'")
# Streamlit app logic
def main():
st.set_page_config(page_title="Ghost Prediction", page_icon="π»", layout="centered")
# Title with emoji
st.title("π» Ghost Prediction App")
st.markdown("### Predict the type of ghost based on its characteristics.")
st.markdown("---")
# Sidebar design with a neat header and spacing
st.sidebar.header("Input Parameters")
st.sidebar.markdown("Adjust the sliders and choose the options below to predict the ghost type.")
# Group the input sliders together with a more structured look
with st.sidebar:
bone_length = st.slider("Bone Length", 0.0, 1.0, step=0.01)
rotting_flesh = st.slider("Rotting Flesh", 0.0, 1.0, step=0.01)
hair_length = st.slider("Hair Length", 0.0, 1.0, step=0.01)
has_soul = st.slider("Has Soul", 0.0, 1.0, step=0.01)
color = st.selectbox('Color', df['color'].unique()) # Make sure 'color' is in df
# Create the input data frame for prediction
input_data = {
"bone_length": [bone_length],
"rotting_flesh": [rotting_flesh],
"hair_length": [hair_length],
"has_soul": [has_soul],
"color": [color]
}
# Feature engineering on the input data
input_df = pd.DataFrame(input_data)
feature_engineering(input_df)
# Prediction button with a clear label
if st.sidebar.button("Predict Ghost Type"):
ghost_type = ghost_classification(pipeline, input_df)
st.markdown("### Prediction Result")
st.write(f"**The predicted ghost type is:** {ghost_type}")
# Add some spacing and footer for visual appeal
st.markdown("---")
st.markdown("Made with π by Senasu Demir")
if __name__ == "__main__":
main()
|