---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-9-a94a5e2a2c44> in <cell line: 22>()
20 # Initializing and training the XGBoost classifier
21 model = XGBClassifier(use_label_encoder=False, eval_metric=‘logloss’)
—> 22 model.fit(X_train, y_train)
23
24 # Predicting on the test set

15 frames
/usr/local/lib/python3.10/dist-packages/xgboost/data.py in _invalid_dataframe_dtype(data)
306 type_err = “DataFrame.dtypes for data must be int, float, bool or category.”
307 msg = f"“”{type_err} {_ENABLE_CAT_ERR} {err}“”"
–> 308 raise ValueError(msg)
309
310

ValueError: DataFrame.dtypes for data must be int, float, bool or category. When categorical type is supplied, The experimental DMatrix parameterenable_categorical must be set to True. Invalid columns:Email No.: object


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.metrics import classification_report, confusion_matrix, roc_curve, roc_auc_score
from xgboost import XGBClassifier

# Load your dataset
df = pd.read_csv('Emails(modified)reduced.csv')  # Replace 'your_dataset_path' with the actual path

# Assuming the last column is the target variable. Adjust column names as per your dataset.
X = df.iloc[:, :-1]  # Feature variables
y = df.iloc[:, -1]   # Target variable

# Splitting dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)

# Initializing and training the XGBoost classifier
model = XGBClassifier(use_label_encoder=False, eval_metric='logloss')
model.fit(X_train, y_train)

# Predicting on the test set
y_pred = model.predict(X_test)
y_score = model.predict_proba(X_test)[:, 1]  # Probability estimates for ROC curve

# Calculate accuracy, precision, and F1-score
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}, Precision: {precision:.2f}, Recall: {recall:.2f}, F1 Score: {f1:.2f}")
# Show classification report
print("Classification Report:")
print(classification_report(y_test, y_pred))

# Confusion matrix with plots
plt.figure(figsize=(6, 5))
cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.title('Confusion Matrix')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.show()

# ROC curve
fpr, tpr, _ = roc_curve(y_test, y_score)
roc_auc = roc_auc_score(y_test, y_score)
plt.figure()
plt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (area = {roc_auc:.2f})')
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle=":")
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.show()

# Visualize feature correlations
plt.figure(figsize=(10, 8))
corr = X_train.corr()
sns.heatmap(corr, cmap='coolwarm', annot=True)
plt.title('Feature Correlation Matrix')
plt.show()

# Density plot of feature values by class
spam_indices = y_train[y_train == 1].index
ham_indices = y_train[y_train == 0].index
for feature in X_train.columns[:5]:  # For demonstration, plot the first 5 features
    plt.figure(figsize=(8, 6))
    sns.kdeplot(X_train.loc[spam_indices, feature], label=f'Spam - {feature}')
    sns.kdeplot(X_train.loc[ham_indices, feature], label=f'Ham - {feature}')
    plt.title(f'Density Plot of {feature} by Class')
    plt.legend()
    plt.show()

# Actual vs. Predicted labels plot
plt.figure(figsize=(8, 6))
sns.countplot(x=y_test, color='blue', alpha=0.5, label='Actual')
sns.countplot(x=y_pred, color='red', alpha=0.5, label='Predicted') 
plt.title('Actual vs. Predicted Label Counts')
plt.legend()
plt.show()