fit xgb regressor and sklearn logistic regression for a classificaiton problem and compare these two models using auc score in python

To fit the XGBRegressor and Sklearn Logistic Regression for a classification problem and compare these two models using AUC score in Python, you can follow the steps below:

First, import the necessary libraries:

main.py
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score
from xgboost import XGBRegressor
from sklearn.linear_model import LogisticRegression
219 chars
7 lines

Next, load your dataset and split it into training and testing sets:

main.py
# Load your dataset
data = pd.read_csv('your_dataset.csv')

# Separate the features and target variable
X = data.drop('target', axis=1)
y = data['target']

# 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)
297 chars
10 lines

Now, you can fit and compare the XGBRegressor and Logistic Regression models:

main.py
# Fit the XGBRegressor model
xgb_model = XGBRegressor()
xgb_model.fit(X_train, y_train)

# Make predictions using the XGBRegressor model
xgb_pred = xgb_model.predict(X_test)

# Calculate the AUC score for the XGBRegressor model
xgb_auc = roc_auc_score(y_test, xgb_pred)

# Fit the Logistic Regression model
logreg_model = LogisticRegression()
logreg_model.fit(X_train, y_train)

# Make predictions using the Logistic Regression model
logreg_pred = logreg_model.predict_proba(X_test)[:, 1]

# Calculate the AUC score for the Logistic Regression model
logreg_auc = roc_auc_score(y_test, logreg_pred)
598 chars
20 lines

Finally, print the AUC scores for both models to compare:

main.py
print("AUC score for XGBRegressor:", xgb_auc)
print("AUC score for Logistic Regression:", logreg_auc)
102 chars
3 lines

This will give you the AUC scores for both models, allowing you to compare their performance in your classification problem.

Note: Make sure to adjust the code based on your specific dataset and requirements.

gistlibby LogSnag