Mykes commited on
Commit
b55f922
·
verified ·
1 Parent(s): e15a265

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -17
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 joblib
7
 
8
  # Загрузка сохраненной модели и других необходимых объектов
9
  @st.cache_resource
10
  def load_model():
11
  with open('model.pkl', 'rb') as file:
12
  model = pickle.load(file)
13
- scaler = joblib.load('scaler.joblib')
14
- feature_list = joblib.load('features.joblib')
15
- categorical_features = joblib.load('categorical_features.joblib')
16
- categorical_options = joblib.load('categorical_options.joblib')
17
- group_names = joblib.load('group_names.joblib')
18
- return model, scaler, feature_list, categorical_features, categorical_options, group_names
 
 
19
 
20
- model, scaler, feature_list, categorical_features, categorical_options, group_names = load_model()
 
 
 
 
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 feature_list:
37
- if feature in categorical_features: # список категориальных признаков
38
- options = categorical_options[feature] # словарь с возможными значениями для каждого признака
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 input_df_encoded.columns if col not in categorical_features]
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
- group_names = {0: 'контр', 1: 'топирамат', 2: 'леветирацетам'}
68
- st.success(f'Предсказанная группа: {group_names[prediction[0]]}')
 
 
 
 
 
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}')