import pandas as pd
import shap
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from sklearn.utils.class_weight import compute_class_weight
from sklearn.metrics import accuracy_score, roc_auc_score
import numpy as np
# 读取特征和标签
X_scaled = np.load('X_features.npy')
y_resampled = np.load('y_labels.npy')
# 数据集划分
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_resampled, test_size=0.2, random_state=42)
# DNN模型构建
model = Sequential([
Dense(64, input_dim=X_train.shape[1], activation='relu'),
Dropout(0.3),
Dense(32, activation='relu'),
Dropout(0.3),
Dense(16, activation='relu'),
Dense(1, activation='sigmoid')
])
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# 计算类别的权重
class_weights = compute_class_weight('balanced', classes=np.unique(y_resampled), y=y_resampled)
class_weight_dict = dict(enumerate(class_weights))
# 训练模型
model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_test, y_test), class_weight=class_weight_dict)
# 评估模型
y_pred = model.predict(X_test)
y_pred_class = (y_pred > 0.5).astype(int)
# 打印评估指标
accuracy = accuracy_score(y_test, y_pred_class)
auc = roc_auc_score(y_test, y_pred)
print(f'DNN Accuracy: {accuracy:.4f}')
print(f'DNN AUC: {auc:.4f}')
# 使用 DeepExplainer 计算 SHAP 值,背景数据取前100个样本
explainer = shap.DeepExplainer(model, X_train[:100]) # 使用前100个样本作为背景数据
shap_values = explainer.shap_values(X_test[:100]) # 对前100个测试样本进行 SHAP 计算
# 打印shap_values的shape
print("shap_values[0].shape:", shap_values[0].shape) # 打印shap_values的shape
print("X_test[:100].shape:", X_test[:100].shape) # 打印X_test前100行的shape
shap_values_positive = shap_values[0] # 选择正类的 SHAP 值
X_test_df = pd.DataFrame(X_test[:100], columns=[
'hour', 'is_weekend', 'charging_time_interval', 'is_charging_location', 'soc_diff', 'soc_current', 'soc_mean_10h'
])
# 调用 SHAP 绘图函数
shap.summary_plot(shap_values[0], X_test_df, feature_names=X_test_df.columns)
想用SHAP绘图运行后总是报错:
AssertionError: The shape of the shap_values matrix does not match the shape of the provided data matrix.
打印出的shap_values和X_test矩阵的形状也不匹配:

我自己怎么调都没用,麻烦各位帮我看看!