|
|
|
"""Untitled19.ipynb |
|
|
|
Automatically generated by Colab. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/14fK8TvV3AakdmLkH1MHkYcDeFVpENGGs |
|
""" |
|
|
|
!pip install datasets |
|
|
|
!pip install huggingface_hub |
|
|
|
!huggingface-cli login |
|
|
|
from huggingface_hub import notebook_login |
|
|
|
notebook_login() |
|
|
|
!pip install tensorflow |
|
|
|
import numpy as np |
|
import pandas as pd |
|
from sklearn.model_selection import train_test_split |
|
from sklearn.metrics import classification_report |
|
import tensorflow as tf |
|
from huggingface_hub import upload_folder |
|
import os |
|
import shap |
|
|
|
|
|
data = pd.read_csv("/content/cardio_train.csv", sep=';') |
|
|
|
|
|
data = data.rename(columns={'cardio': 'target'}) |
|
|
|
|
|
X = data.drop(columns='target') |
|
y = data['target'] |
|
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) |
|
|
|
|
|
model = tf.keras.models.Sequential() |
|
model.add(tf.keras.layers.Dense(16, activation='relu', input_shape=(X_train.shape[1],))) |
|
model.add(tf.keras.layers.Dense(8, activation='relu')) |
|
model.add(tf.keras.layers.Dense(1, activation='sigmoid')) |
|
|
|
|
|
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) |
|
|
|
|
|
model.fit(X_train, y_train, epochs=50, batch_size=32, validation_split=0.2, verbose=1) |
|
|
|
|
|
loss, accuracy = model.evaluate(X_test, y_test, verbose=0) |
|
print(f"Accuracy of Keras model: {accuracy:.4f}") |
|
|
|
|
|
y_pred = (model.predict(X_test) > 0.5).astype("int32") |
|
|
|
|
|
report = classification_report(y_test, y_pred) |
|
print("\nClassification Report:\n", report) |
|
|
|
|
|
model.save("Cardiovascular-Disease-Detection.keras") |
|
|
|
|
|
folder_path = "apipyo/Cardiovascular_Disease" |
|
|
|
|
|
os.makedirs(folder_path, exist_ok=True) |
|
|
|
|
|
os.rename("Cardiovascular-Disease-Detection.keras", os.path.join(folder_path, "Cardiovascular-Disease-Detection.keras")) |
|
|
|
import numpy as np |
|
import pandas as pd |
|
from sklearn.model_selection import train_test_split |
|
from sklearn.ensemble import GradientBoostingClassifier |
|
from sklearn.metrics import accuracy_score, classification_report |
|
|
|
|
|
import tensorflow as tf |
|
from tensorflow.keras.models import Sequential |
|
from tensorflow.keras.layers import Dense, Flatten |
|
from tensorflow.keras.datasets import mnist |
|
|
|
data = pd.read_csv("/content/cardio_train.csv", sep = ";") |
|
data = data.rename(columns = {'cardio':'target'}) |
|
data.head() |
|
|
|
!pip install sweetviz |
|
import sweetviz as sv |
|
report = sv.analyze(data) |
|
|
|
|
|
with open('Cardiac_Data_Analysis.html', 'r') as file: |
|
report_html = file.read() |
|
|
|
|
|
with open('Cardiac_Data_Analysis.bin', 'wb') as file: |
|
file.write(report_html.encode('utf-8')) |
|
|
|
import torch |
|
|
|
|
|
with open('Cardiac_Data_Analysis.html', 'r', encoding='utf-8') as file: |
|
report_html = file.read() |
|
|
|
|
|
with open('Cardiac_Data_Analysis.bin', 'wb') as file: |
|
file.write(report_html.encode('utf-8')) |
|
|
|
|
|
html_tensor = torch.tensor([len(report_html)]) |
|
|
|
|
|
torch.save(html_tensor, 'Cardiac_Data_Analysis.pth') |
|
|
|
print("Files saved successfully: Cardiac_Data_Analysis.bin and Cardiac_Data_Analysis.pth") |
|
|
|
X = data.drop(columns = 'target',axis = 1) |
|
Y = data['target'] |
|
|
|
X = data.drop(columns = 'target',axis = 1) |
|
Y = data['target'] |
|
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=42) |
|
|
|
|
|
|
|
gb_classifier = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42) |
|
|
|
|
|
gb_classifier.fit(X_train, y_train) |
|
|
|
|
|
y_pred = gb_classifier.predict(X_test) |
|
|
|
|
|
accuracy = accuracy_score(y_test, y_pred) |
|
report = classification_report(y_test, y_pred) |
|
|
|
print("Accuracy:", accuracy) |
|
print("\nClassification Report:\n", report) |
|
|
|
input_data = (15,22530,1,169,80.0,120,80,1,1,0,0,1) |
|
idata = np.asarray(input_data) |
|
idata_reshaped = idata.reshape(1,-1) |
|
|
|
model = gb_classifier |
|
|
|
prediction = model.predict(idata_reshaped) |
|
|
|
print(prediction) |
|
|
|
if(prediction[0]==1): |
|
{print("This person has heart desease")} |
|
else: |
|
print("This person is safe") |
|
|
|
|
|
importances = model.feature_importances_ |
|
features = X.columns |
|
for feature, importance in zip(features, importances): |
|
print(f'{feature}: {importance:.4f}') |
|
|
|
import joblib |
|
import numpy as np |
|
import torch |
|
|
|
|
|
|
|
joblib.dump(gb_classifier, 'model.pkl') |
|
|
|
|
|
predictions_tensor = torch.tensor(y_pred) |
|
|
|
|
|
torch.save(predictions_tensor, 'predictions.pth') |
|
|
|
print("Model saved as model.pkl and predictions saved as predictions.pth") |
|
|
|
import joblib |
|
import numpy as np |
|
import pickle |
|
|
|
|
|
|
|
|
|
joblib.dump(gb_classifier, 'model.bin') |
|
|
|
|
|
predictions_array = np.array(y_pred) |
|
|
|
|
|
np.save('predictions.bin', predictions_array) |
|
|
|
print("Model saved as model.bin and predictions saved as predictions.bin") |