TamerTokgoz's picture
Create app.py
fbc14a6 verified
import streamlit as st
import pandas as pd
import joblib
st.set_page_config(page_title="Insurance Predictor", layout="centered")
@st.cache_resource
def load_assets():
model = joblib.load("lgbm_model.pkl")
columns = joblib.load("model_columns.pkl")
return model, columns
model, model_columns = load_assets()
st.title("🏥 Sağlık Sigortası Fiyat Tahmini")
# Kullanıcı Girişleri
col1, col2 = st.columns(2)
with col1:
age = st.number_input("Yaş", 18, 100, 30)
bmi = st.number_input("BMI", 10.0, 60.0, 25.0)
children = st.number_input("Çocuk Sayısı", 0, 10, 0)
with col2:
sex = st.selectbox("Cinsiyet", ["male", "female"])
smoker = st.selectbox("Sigara", ["yes", "no"])
region = st.selectbox("Bölge", ["southeast", "southwest", "northwest", "northeast"])
if st.button("Tahmin Et"):
# 1. Temel DataFrame
input_df = pd.DataFrame([[age, bmi, children]], columns=['age', 'bmi', 'children'])
input_df['sex'] = 1 if sex == "male" else 0
input_df['smoker'] = 1 if smoker == "yes" else 0
# 2. Arka Planda Feature Engineering (Notebook'undaki Mantık)
# BMI_CAT
bmi_cat = "ideal"
if bmi < 18.5: bmi_cat = "underweight"
elif 25 <= bmi < 30: bmi_cat = "overweight"
elif bmi >= 30: bmi_cat = "obese"
# AGE_CAT
age_cat = "young"
if 35 < age <= 55: age_cat = "middle"
elif age > 55: age_cat = "old"
# Smoker_Obese Etkileşimi
input_df['is_smoker_obese'] = 1 if (smoker == "yes" and bmi >= 30) else 0
# 3. One-Hot Encoding Simülasyonu
for col in model_columns:
if col not in input_df.columns:
# Region, BMI_CAT ve AGE_CAT sütunlarını kontrol et
if f"region_{region}" == col or f"BMI_CAT_{bmi_cat}" == col or f"AGE_CAT_{age_cat}" == col:
input_df[col] = 1
else:
input_df[col] = 0
# Sütunları modelin beklediği sıraya diz
input_df = input_df[model_columns]
res = model.predict(input_df)[0]
st.success(f"Tahmini Yıllık Ücret: ${res:,.2f}")