Spaces:
Running
Running
Commit
·
0de191c
1
Parent(s):
f3cadeb
add
Browse files- app.py +99 -30
- models/healthcare_model.pkl +0 -3
- models/sample.txt +0 -0
- requirements.txt +0 -5
app.py
CHANGED
@@ -3,7 +3,15 @@
|
|
3 |
import streamlit as st
|
4 |
import pandas as pd
|
5 |
import joblib
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# 모델 로드
|
9 |
@st.cache_resource
|
@@ -11,38 +19,99 @@ def load_model():
|
|
11 |
model = joblib.load('models/healthcare_model.pkl')
|
12 |
return model
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
def main():
|
15 |
-
st.title('의료 보험 청구액 예측 서비스')
|
16 |
-
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
# 예측 버튼
|
28 |
-
if st.sidebar.button('예측하기'):
|
29 |
-
# 입력 데이터 구성
|
30 |
-
input_data = {
|
31 |
-
'Age': age,
|
32 |
-
'Gender': gender,
|
33 |
-
'BMI': bmi,
|
34 |
-
'Region': region,
|
35 |
-
'Smoker': smoker,
|
36 |
-
'NumVisits': num_visits
|
37 |
-
}
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
42 |
|
43 |
-
#
|
44 |
-
st.
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
def predict_insurance_claim(data, pipeline):
|
48 |
new_df = pd.DataFrame([data])
|
|
|
3 |
import streamlit as st
|
4 |
import pandas as pd
|
5 |
import joblib
|
6 |
+
|
7 |
+
import plotly.express as px
|
8 |
+
import numpy as np
|
9 |
+
|
10 |
+
# 데이터 로드
|
11 |
+
@st.cache_data
|
12 |
+
def load_data():
|
13 |
+
df = pd.read_csv('data/healthcare.csv', index_col=0)
|
14 |
+
return df
|
15 |
|
16 |
# 모델 로드
|
17 |
@st.cache_resource
|
|
|
19 |
model = joblib.load('models/healthcare_model.pkl')
|
20 |
return model
|
21 |
|
22 |
+
def create_charts(df):
|
23 |
+
st.header('📊 데이터 분석')
|
24 |
+
|
25 |
+
# 1. 흡연 여부에 따른 보험 청구액 분포
|
26 |
+
st.subheader('흡연 여부에 따른 보험 청구액 분포')
|
27 |
+
fig = px.box(df, x='Smoker', y='InsuranceClaim', color='Smoker')
|
28 |
+
st.plotly_chart(fig)
|
29 |
+
|
30 |
+
# 2. 나이와 보험 청구액의 관계
|
31 |
+
st.subheader('나이와 보험 청구액의 관계')
|
32 |
+
fig = px.scatter(df, x='Age', y='InsuranceClaim', color='Gender',
|
33 |
+
title='나이별 보험 청구액 분포',
|
34 |
+
labels={'InsuranceClaim': '보험 청구액', 'Age': '나이'})
|
35 |
+
st.plotly_chart(fig)
|
36 |
+
|
37 |
+
# 3. BMI 구간별 평균 보험 청구액
|
38 |
+
st.subheader('BMI 구간별 평균 보험 청구액')
|
39 |
+
df['BMI_Category'] = pd.cut(df['BMI'],
|
40 |
+
bins=[0, 18.5, 25, 30, 100],
|
41 |
+
labels=['저체중', '정상', '과체중', '비만'])
|
42 |
+
bmi_avg = df.groupby('BMI_Category')['InsuranceClaim'].mean().reset_index()
|
43 |
+
fig = px.bar(bmi_avg, x='BMI_Category', y='InsuranceClaim',
|
44 |
+
title='BMI 구간별 평균 보험 청구액',
|
45 |
+
labels={'InsuranceClaim': '평균 보험 청구액', 'BMI_Category': 'BMI 구간'})
|
46 |
+
st.plotly_chart(fig)
|
47 |
+
|
48 |
+
# 4. 지역별 평균 보험 청구액
|
49 |
+
st.subheader('지역별 평균 보험 청구액')
|
50 |
+
region_avg = df.groupby('Region')['InsuranceClaim'].mean().reset_index()
|
51 |
+
fig = px.pie(region_avg, values='InsuranceClaim', names='Region',
|
52 |
+
title='지역별 평균 보험 청구액 비율')
|
53 |
+
st.plotly_chart(fig)
|
54 |
+
|
55 |
+
# 5. 주요 통계 지표
|
56 |
+
st.subheader('💡 주요 통계 지표')
|
57 |
+
col1, col2, col3 = st.columns(3)
|
58 |
+
with col1:
|
59 |
+
st.metric("평균 보험 청구액", f"${df['InsuranceClaim'].mean():,.2f}")
|
60 |
+
with col2:
|
61 |
+
st.metric("최대 보험 청구액", f"${df['InsuranceClaim'].max():,.2f}")
|
62 |
+
with col3:
|
63 |
+
st.metric("최소 보험 청구액", f"${df['InsuranceClaim'].min():,.2f}")
|
64 |
+
|
65 |
def main():
|
66 |
+
st.title('🏥 의료 보험 청구액 예측 서비스')
|
67 |
+
|
68 |
+
# 데이터 로드
|
69 |
+
df = load_data()
|
70 |
+
|
71 |
+
# 탭 생성
|
72 |
+
tab1, tab2 = st.tabs(["예측하기", "데이터 분석"])
|
73 |
+
|
74 |
+
with tab1:
|
75 |
+
# 사이드바에 입력 폼 생성
|
76 |
+
st.sidebar.header('환자 정보 입력')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
+
age = st.sidebar.number_input('나이', min_value=0, max_value=100, value=30)
|
79 |
+
gender = st.sidebar.selectbox('성별', ['Male', 'Female'])
|
80 |
+
bmi = st.sidebar.number_input('BMI', min_value=10.0, max_value=50.0, value=25.0)
|
81 |
+
region = st.sidebar.selectbox('지역', ['North', 'South', 'East', 'West'])
|
82 |
+
smoker = st.sidebar.selectbox('흡연 여부', ['Yes', 'No'])
|
83 |
+
num_visits = st.sidebar.number_input('방문 횟수', min_value=0, max_value=20, value=5)
|
84 |
|
85 |
+
# 예측 버튼
|
86 |
+
if st.sidebar.button('예측하기'):
|
87 |
+
# 입력 데이터 구성
|
88 |
+
input_data = {
|
89 |
+
'Age': age,
|
90 |
+
'Gender': gender,
|
91 |
+
'BMI': bmi,
|
92 |
+
'Region': region,
|
93 |
+
'Smoker': smoker,
|
94 |
+
'NumVisits': num_visits
|
95 |
+
}
|
96 |
+
|
97 |
+
# 모델 로드 및 예측
|
98 |
+
model = load_model()
|
99 |
+
prediction = predict_insurance_claim(input_data, model)
|
100 |
+
|
101 |
+
# 결과 표시
|
102 |
+
st.header('예측 결과')
|
103 |
+
st.write(f'예상 보험 청구액: ${prediction:,.2f}')
|
104 |
+
|
105 |
+
# 예측 결과 설명
|
106 |
+
st.info("""
|
107 |
+
💡 예측 결과 설명:
|
108 |
+
- 이 예측은 환자의 기본 정보를 바탕으로 계산되었습니다.
|
109 |
+
- 실제 청구액은 구체적인 진료 내용에 따라 달라질 수 있습니다.
|
110 |
+
- 이 예측은 참고용으로만 사용해주세요.
|
111 |
+
""")
|
112 |
+
|
113 |
+
with tab2:
|
114 |
+
create_charts(df)
|
115 |
|
116 |
def predict_insurance_claim(data, pipeline):
|
117 |
new_df = pd.DataFrame([data])
|
models/healthcare_model.pkl
DELETED
@@ -1,3 +0,0 @@
|
|
1 |
-
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:a2cc7436c6162d93b08225a39337c511b12374b3618a3d21dd2332c30043a979
|
3 |
-
size 4362
|
|
|
|
|
|
|
|
models/sample.txt
DELETED
File without changes
|
requirements.txt
DELETED
@@ -1,5 +0,0 @@
|
|
1 |
-
streamlit
|
2 |
-
scikit-learn
|
3 |
-
pandas
|
4 |
-
joblib
|
5 |
-
numpy
|
|
|
|
|
|
|
|
|
|
|
|