You can download this code by clicking the button below.
This code is now available for download.
This function uses the XGBoost library to predict heart disease. First, it splits the dataset into training and testing sets, then creates an XGBoost classifier, fits the model, and makes predictions on the test set. Finally, it calculates and returns the accuracy of the model.
Technology Stack : XGBoost, NumPy, Pandas, Scikit-learn
Code Type : Function
Code Difficulty : Intermediate
import xgboost as xgb
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
def predict_heart_disease(X, y):
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create an XGBoost classifier
xgb_clf = xgb.XGBClassifier(use_label_encoder=False, eval_metric='logloss')
# Fit the model
xgb_clf.fit(X_train, y_train)
# Predict the labels of the test set
y_pred = xgb_clf.predict(X_test)
# Calculate the accuracy of the model
accuracy = xgb_clf.score(X_test, y_test)
return accuracy