Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,28 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
4 |
-
from sklearn.preprocessing import StandardScaler
|
5 |
import pickle
|
6 |
-
import
|
7 |
|
8 |
# Загрузка сохраненной модели и других необходимых объектов
|
9 |
@st.cache_resource
|
10 |
def load_model():
|
11 |
with open('model.pkl', 'rb') as file:
|
12 |
model = pickle.load(file)
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
19 |
|
20 |
-
model, scaler,
|
|
|
|
|
|
|
|
|
21 |
|
22 |
st.title('Классификатор пациентов')
|
23 |
|
@@ -28,15 +33,14 @@ def get_user_input():
|
|
28 |
# Числовые признаки
|
29 |
st.subheader('Числовые параметры')
|
30 |
for feature in feature_list:
|
31 |
-
if feature not in categorical_features:
|
32 |
input_data[feature] = st.number_input(f'{feature}', value=0.0)
|
33 |
|
34 |
# Категориальные признаки
|
35 |
st.subheader('Категориальные параметры')
|
36 |
-
for feature in
|
37 |
-
|
38 |
-
|
39 |
-
input_data[feature] = st.selectbox(f'{feature}', options)
|
40 |
|
41 |
return input_data
|
42 |
|
@@ -57,12 +61,17 @@ if st.button('Выполнить классификацию'):
|
|
57 |
input_df_encoded[col] = 0
|
58 |
|
59 |
# Масштабирование числовых признаков
|
60 |
-
numeric_cols = [col for col in
|
61 |
input_df_encoded[numeric_cols] = scaler.transform(input_df_encoded[numeric_cols])
|
62 |
|
63 |
# Получение предсказания
|
64 |
prediction = model.predict(input_df_encoded[feature_list])
|
|
|
65 |
|
66 |
# Отображение результата
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
|
|
4 |
import pickle
|
5 |
+
import json
|
6 |
|
7 |
# Загрузка сохраненной модели и других необходимых объектов
|
8 |
@st.cache_resource
|
9 |
def load_model():
|
10 |
with open('model.pkl', 'rb') as file:
|
11 |
model = pickle.load(file)
|
12 |
+
|
13 |
+
with open('scaler.pkl', 'rb') as file:
|
14 |
+
scaler = pickle.load(file)
|
15 |
+
|
16 |
+
with open('data.json', 'r', encoding='utf-8') as f:
|
17 |
+
data = json.load(f)
|
18 |
+
|
19 |
+
return model, scaler, data
|
20 |
|
21 |
+
model, scaler, data = load_model()
|
22 |
+
feature_list = data['features']
|
23 |
+
categorical_features = data['categorical_features']
|
24 |
+
categorical_options = data['categorical_options']
|
25 |
+
group_names = data['group_names']
|
26 |
|
27 |
st.title('Классификатор пациентов')
|
28 |
|
|
|
33 |
# Числовые признаки
|
34 |
st.subheader('Числовые параметры')
|
35 |
for feature in feature_list:
|
36 |
+
if feature not in categorical_features:
|
37 |
input_data[feature] = st.number_input(f'{feature}', value=0.0)
|
38 |
|
39 |
# Категориальные признаки
|
40 |
st.subheader('Категориальные параметры')
|
41 |
+
for feature in categorical_features:
|
42 |
+
options = categorical_options[feature]
|
43 |
+
input_data[feature] = st.selectbox(f'{feature}', options)
|
|
|
44 |
|
45 |
return input_data
|
46 |
|
|
|
61 |
input_df_encoded[col] = 0
|
62 |
|
63 |
# Масштабирование числовых признаков
|
64 |
+
numeric_cols = [col for col in feature_list if col not in categorical_features]
|
65 |
input_df_encoded[numeric_cols] = scaler.transform(input_df_encoded[numeric_cols])
|
66 |
|
67 |
# Получение предсказания
|
68 |
prediction = model.predict(input_df_encoded[feature_list])
|
69 |
+
probabilities = model.predict_proba(input_df_encoded[feature_list])[0]
|
70 |
|
71 |
# Отображение результата
|
72 |
+
st.success(f'Предсказанная группа: {group_names[str(prediction[0])]}')
|
73 |
+
|
74 |
+
# Вывод вероятностей
|
75 |
+
st.write('Вероятности для каждой группы:')
|
76 |
+
for i, prob in enumerate(probabilities):
|
77 |
+
st.write(f'{group_names[str(i)]}: {prob:.2f}')
|