Keras
medical
CardioAI / app.py
MaxJalo's picture
Create app.py
a717878 verified
raw
history blame
2.22 kB
# #1
import pandas as pd
import numpy as np
from datasets import load_dataset
from tensorflow import keras
from keras.layers import Dense, Dropout, BatchNormalization
from keras.optimizers import Adam
from keras.callbacks import EarlyStopping
from sklearn.model_selection import train_test_split
# #2
# Загрузка данных
heart = load_dataset("MaxJalo/CardioAI", split = 'train')
# #3
data = pd.DataFrame(heart,
columns=["age", "gender", "height", "weight", "ap_hi", "ap_lo", "cholesterol", "gluc", "smoke",
"alco", "active", 'cardio'])
# #4
X_for_train = data.drop(['cardio'], axis=1).values
X_min = np.min(X_for_train, axis=0)
X_max = np.max(X_for_train, axis=0)
X_normalized = (X_for_train - X_min) / (X_max - X_min)
y_normalized = data['cardio'].values
X_train, X_test, y_train, y_test = train_test_split(X_normalized, y_normalized, test_size=0.1, random_state=77)
print(X_train)
# #5
model = Sequential()
model.add(Dense(1, input_dim=X_train.shape[1], activation='linear', kernel_regularizer='l2'))
# model.add(Dense(16, activation='elu', kernel_regularizer='l2'))
# model.add(Dense(16, activation='elu', kernel_regularizer='l2'))
model.add(Dense(1, activation='linear'))
model.compile(optimizer='adam', loss='mse')
# #6
early_stopping = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)
history = model.fit(X_train, y_train, epochs=100, batch_size=50, validation_split=0.1, callbacks=[early_stopping],
verbose=1)
# #8
test_loss = model.evaluate(X_test, y_test)
print(f'Test loss (MSE): {test_loss}')
# #9
def webai(user_input):
user_input_clear = user_input
input_data = [user_input_clear]
input_data_scaled = (input_data - X_min) / (X_max - X_min)
print(input_data_scaled)
# Получаем предсказание от модели
predicted_result_scaled = model.predict(input_data_scaled)
print(predicted_result_scaled[0][0] * 100)
# 35 0 190 75 120 80 1 1 0 0 1
# 35 0 170 90 130 90 1 1 0 0 0
# 39 0 156 45 110 80 2 1 0 0 0
# 47 1 168 87 120 80 2 1 1 1 1
# 37 0 185 75 120 80 2 1 1 1 0
return f"{round(predicted_result_scaled[0][0] * 100, 2)}%"