Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# #1
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
+
from tensorflow import keras
|
| 6 |
+
from keras.layers import Dense, Dropout, BatchNormalization
|
| 7 |
+
from keras.optimizers import Adam
|
| 8 |
+
from keras.callbacks import EarlyStopping
|
| 9 |
+
from sklearn.model_selection import train_test_split
|
| 10 |
+
|
| 11 |
+
# #2
|
| 12 |
+
# Загрузка данных
|
| 13 |
+
heart = load_dataset("MaxJalo/CardioAI", split = 'train')
|
| 14 |
+
|
| 15 |
+
# #3
|
| 16 |
+
data = pd.DataFrame(heart,
|
| 17 |
+
columns=["age", "gender", "height", "weight", "ap_hi", "ap_lo", "cholesterol", "gluc", "smoke",
|
| 18 |
+
"alco", "active", 'cardio'])
|
| 19 |
+
|
| 20 |
+
# #4
|
| 21 |
+
X_for_train = data.drop(['cardio'], axis=1).values
|
| 22 |
+
X_min = np.min(X_for_train, axis=0)
|
| 23 |
+
X_max = np.max(X_for_train, axis=0)
|
| 24 |
+
X_normalized = (X_for_train - X_min) / (X_max - X_min)
|
| 25 |
+
|
| 26 |
+
y_normalized = data['cardio'].values
|
| 27 |
+
|
| 28 |
+
X_train, X_test, y_train, y_test = train_test_split(X_normalized, y_normalized, test_size=0.1, random_state=77)
|
| 29 |
+
print(X_train)
|
| 30 |
+
|
| 31 |
+
# #5
|
| 32 |
+
model = Sequential()
|
| 33 |
+
model.add(Dense(1, input_dim=X_train.shape[1], activation='linear', kernel_regularizer='l2'))
|
| 34 |
+
# model.add(Dense(16, activation='elu', kernel_regularizer='l2'))
|
| 35 |
+
# model.add(Dense(16, activation='elu', kernel_regularizer='l2'))
|
| 36 |
+
model.add(Dense(1, activation='linear'))
|
| 37 |
+
|
| 38 |
+
model.compile(optimizer='adam', loss='mse')
|
| 39 |
+
|
| 40 |
+
# #6
|
| 41 |
+
early_stopping = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)
|
| 42 |
+
|
| 43 |
+
history = model.fit(X_train, y_train, epochs=100, batch_size=50, validation_split=0.1, callbacks=[early_stopping],
|
| 44 |
+
verbose=1)
|
| 45 |
+
|
| 46 |
+
# #8
|
| 47 |
+
test_loss = model.evaluate(X_test, y_test)
|
| 48 |
+
print(f'Test loss (MSE): {test_loss}')
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# #9
|
| 52 |
+
def webai(user_input):
|
| 53 |
+
user_input_clear = user_input
|
| 54 |
+
input_data = [user_input_clear]
|
| 55 |
+
input_data_scaled = (input_data - X_min) / (X_max - X_min)
|
| 56 |
+
print(input_data_scaled)
|
| 57 |
+
# Получаем предсказание от модели
|
| 58 |
+
predicted_result_scaled = model.predict(input_data_scaled)
|
| 59 |
+
print(predicted_result_scaled[0][0] * 100)
|
| 60 |
+
# 35 0 190 75 120 80 1 1 0 0 1
|
| 61 |
+
# 35 0 170 90 130 90 1 1 0 0 0
|
| 62 |
+
# 39 0 156 45 110 80 2 1 0 0 0
|
| 63 |
+
# 47 1 168 87 120 80 2 1 1 1 1
|
| 64 |
+
# 37 0 185 75 120 80 2 1 1 1 0
|
| 65 |
+
return f"{round(predicted_result_scaled[0][0] * 100, 2)}%"
|