Spaces:
Sleeping
Sleeping
File size: 12,267 Bytes
de2bb03 b55f922 b5f54c4 de2bb03 b5f54c4 de2bb03 30364ee b55f922 de2bb03 b5f54c4 b55f922 dfdcea4 b55f922 323e84a de2bb03 b5f54c4 de2bb03 b5f54c4 de2bb03 b5f54c4 de2bb03 b5f54c4 de2bb03 b5f54c4 de2bb03 b5f54c4 323e84a b5f54c4 de2bb03 b5f54c4 de2bb03 b5f54c4 de2bb03 b5f54c4 dfdcea4 b5f54c4 de2bb03 b5f54c4 de2bb03 b5f54c4 323e84a b5f54c4 |
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
import streamlit as st
import pandas as pd
import numpy as np
import pickle
import json
import plotly.graph_objects as go
import plotly.express as px
# Настройка страницы
st.set_page_config(
page_title="Классификатор пациентов",
layout="wide",
initial_sidebar_state="expanded",
menu_items={
'About': "Классификатор пациентов для определения группы лечения"
}
)
# Применяем кастомный CSS
st.markdown("""
<style>
.stNumberInput input {
width: 150px;
}
.stSelectbox select {
width: 150px;
}
.main .block-container {
padding-top: 2rem;
padding-bottom: 2rem;
}
div[data-testid="stSidebarNav"] {
padding-top: 0rem;
}
.sidebar .sidebar-content {
width: 300px;
}
.metric-card {
background-color: #f0f2f6;
border-radius: 0.5rem;
padding: 1rem;
margin: 0.5rem 0;
}
.small-font {
font-size: 0.8rem;
}
</style>
""", unsafe_allow_html=True)
# Загрузка модели и данных
@st.cache_resource
def load_model():
with open('model.pkl', 'rb') as file:
model = pickle.load(file)
with open('scaler.pkl', 'rb') as file:
scaler = pickle.load(file)
with open('data.json', 'r', encoding='utf-8') as f:
data = json.load(f)
return model, scaler, data
def create_gauge_chart(value, title):
fig = go.Figure(go.Indicator(
mode = "gauge+number",
value = value * 100,
domain = {'x': [0, 1], 'y': [0, 1]},
title = {'text': title},
gauge = {
'axis': {'range': [None, 100]},
'bar': {'color': "darkblue"},
'steps': [
{'range': [0, 33], 'color': "lightgray"},
{'range': [33, 66], 'color': "gray"},
{'range': [66, 100], 'color': "darkgray"}
],
'threshold': {
'line': {'color': "red", 'width': 4},
'thickness': 0.75,
'value': value * 100
}
}
))
fig.update_layout(height=200)
return fig
model, scaler, data = load_model()
feature_list = data['features']
categorical_features = data['categorical_features']
numeric_features = data['numeric_features']
categorical_options = data['categorical_options']
group_names = data['group_names']
numeric_defaults = data['numeric_defaults']
categorical_defaults = data['categorical_defaults']
# Основной заголовок
st.title('Классификатор пациентов')
# Описание в основной части
with st.container():
st.markdown("""
### О системе
Данная система помогает классифицировать пациентов по группам на основе введенных параметров.
#### Как использовать:
1. Заполните все поля в форме слева
2. Нажмите кнопку "Выполнить классификацию"
3. Получите результат с вероятностями принадлежности к каждой группе
#### Группы классификации:
- Контрольная группа
- Группа топирамата
- Группа леветирацетама
""")
# Боковая панель для ввода данных
with st.sidebar:
st.header('Ввод данных')
# Создаем вкладки для разных типов параметров
tab1, tab2 = st.tabs(["📊 Числовые", "📋 Категориальные"])
input_data = {}
with tab1:
# Числовые признаки
for i in range(0, len(numeric_features), 2):
col1, col2 = st.columns(2)
with col1:
if i < len(numeric_features):
feature = numeric_features[i]
default_value = float(numeric_defaults[feature])
input_data[feature] = st.number_input(
f'{feature}',
value=default_value,
format="%.2f",
help=f"Среднее: {default_value:.2f}"
)
with col2:
if i + 1 < len(numeric_features):
feature = numeric_features[i + 1]
default_value = float(numeric_defaults[feature])
input_data[feature] = st.number_input(
f'{feature}',
value=default_value,
format="%.2f",
help=f"Среднее: {default_value:.2f}"
)
with tab2:
# Категориальные признаки
for i in range(0, len(categorical_features), 2):
col1, col2 = st.columns(2)
with col1:
if i < len(categorical_features):
feature = categorical_features[i]
options = categorical_options[feature]
default_idx = options.index(categorical_defaults[feature]) if categorical_defaults[feature] in options else 0
input_data[feature] = st.selectbox(
f'{feature}',
options,
index=default_idx,
help=f"Типичное: {categorical_defaults[feature]}"
)
with col2:
if i + 1 < len(categorical_features):
feature = categorical_features[i + 1]
options = categorical_options[feature]
default_idx = options.index(categorical_defaults[feature]) if categorical_defaults[feature] in options else 0
input_data[feature] = st.selectbox(
f'{feature}',
options,
index=default_idx,
help=f"Типичное: {categorical_defaults[feature]}"
)
# Кнопка классификации
if st.button('Выполнить классификацию', use_container_width=True):
with st.spinner('Выполняется классификация...'):
# Преобразование входных данных
input_df = pd.DataFrame([input_data])
# One-hot encoding для категориальных признаков
input_df_encoded = pd.get_dummies(input_df, columns=categorical_features)
# Масштабирование числовых признаков
if numeric_features:
input_df_encoded[numeric_features] = scaler.transform(input_df_encoded[numeric_features])
# Убедитесь, что все необходимые столбцы присутствуют
for col in feature_list:
if col not in input_df_encoded.columns:
input_df_encoded[col] = 0
X_pred = input_df_encoded[feature_list]
prediction = model.predict(X_pred)
probabilities = model.predict_proba(X_pred)[0]
# Сохраняем результаты в session state
st.session_state['prediction'] = prediction[0]
st.session_state['probabilities'] = probabilities
st.session_state['input_data'] = input_df
# Отображение результатов в основной части
if 'prediction' in st.session_state:
st.header('Результаты классификации')
# Создаем контейнер для основных метрик
with st.container():
col1, col2, col3 = st.columns(3)
with col1:
st.markdown("""
<div class="metric-card">
<h3>Предсказанная группа</h3>
<h2 style="color: darkblue;">{}</h2>
</div>
""".format(group_names[str(st.session_state['prediction'])]), unsafe_allow_html=True)
with col2:
max_prob = max(st.session_state['probabilities'])
st.markdown("""
<div class="metric-card">
<h3>Уверенность модели</h3>
<h2 style="color: darkblue;">{:.1%}</h2>
</div>
""".format(max_prob), unsafe_allow_html=True)
with col3:
second_best_prob = sorted(st.session_state['probabilities'])[-2]
st.markdown("""
<div class="metric-card">
<h3>Вторая вероятная группа</h3>
<h2 style="color: darkblue;">{:.1%}</h2>
</div>
""".format(second_best_prob), unsafe_allow_html=True)
# Визуализация вероятностей
st.subheader('Распределение вероятностей по группам')
# График вероятностей
prob_df = pd.DataFrame({
'Группа': [group_names[str(i)] for i in range(len(group_names))],
'Вероятность': st.session_state['probabilities']
})
fig = px.bar(prob_df,
x='Группа',
y='Вероятность',
color='Вероятность',
color_continuous_scale='Blues',
text=prob_df['Вероятность'].apply(lambda x: f'{x:.1%}'))
fig.update_layout(
height=400,
yaxis_range=[0, 1],
yaxis_tickformat='.0%',
showlegend=False
)
st.plotly_chart(fig, use_container_width=True)
# Детальная информация
with st.expander("Детальная информация"):
tab1, tab2 = st.tabs(["📊 Введенные данные", "ℹ️ Пояснение"])
with tab1:
# Отображаем введенные данные в виде двух таблиц
col1, col2 = st.columns(2)
with col1:
st.subheader("Числовые параметры")
numeric_data = st.session_state['input_data'][numeric_features]
st.dataframe(numeric_data.T.style.format("{:.2f}"))
with col2:
st.subheader("Категориальные параметры")
categorical_data = st.session_state['input_data'][categorical_features]
st.dataframe(categorical_data.T)
with tab2:
st.markdown("""
### Как интерпретировать результаты:
- **Предсказанная группа** - основная рекомендованная группа для пациента
- **Уверенность модели** - вероятность принадлежности к предсказанной группе
- **Вторая вероятная группа** - вероятность принадлежности ко второй наиболее вероятной группе
### Примечания:
- Чем выше уверенность модели, тем надежнее предсказание
- При уверенности ниже 50% рекомендуется дополнительное обследование
- Результаты модели носят рекомендательный характер
""")
# Добавляем footer
st.markdown("""
---
<div class="small-font">
© 2023 Классификатор пациентов | Версия 1.0
</div>
""", unsafe_allow_html=True) |