I made a dummy data to try out this code
import numpy as np
import pandas as pd
from xgboost import XGBClassifier
import shap
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
# Generate a sample dataset
np.random.seed(42)
# Create random features (100 samples, 5 features)
X = pd.DataFrame(np.random.randn(100, 5), columns=['feat1', 'feat2', 'feat3', 'feat4', 'feat5'])
# Create random labels for 3 classes
y = np.random.choice(['class_0', 'class_1', 'class_2'], size=100)
# Encode the labels for multiclass classification
label_encoder = LabelEncoder()
y_enc = label_encoder.fit_transform(y)
# Train-test split (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X, y_enc, test_size=0.2, random_state=42)
Your code is fine, I think there is probably something wrong with your x or y data which I cannot really check.