|
|
|
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 |
|
|
|
|
|
|
|
heart = load_dataset("MaxJalo/CardioAI", split = 'train') |
|
|
|
|
|
data = pd.DataFrame(heart, |
|
columns=["age", "gender", "height", "weight", "ap_hi", "ap_lo", "cholesterol", "gluc", "smoke", |
|
"alco", "active", 'cardio']) |
|
|
|
|
|
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) |
|
|
|
|
|
model = Sequential() |
|
model.add(Dense(1, input_dim=X_train.shape[1], activation='linear', kernel_regularizer='l2')) |
|
|
|
|
|
model.add(Dense(1, activation='linear')) |
|
|
|
model.compile(optimizer='adam', loss='mse') |
|
|
|
|
|
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) |
|
|
|
|
|
test_loss = model.evaluate(X_test, y_test) |
|
print(f'Test loss (MSE): {test_loss}') |
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
|
|
return f"{round(predicted_result_scaled[0][0] * 100, 2)}%" |