Py学习  »  机器学习算法

Nature子刊使用的机器学习可解释方法,教你复现

GISer last • 2 月前 • 120 次点击  

引言

  作为地学领域工作者,一个可靠的机器学习模型往往代表着一篇高水平论文的发表。但是构建机器学习模型时,参数的选取过程往往令人头大,每次增减输入参数之后都需要重新训练一遍模型观察模型输出的准确性。这一过程消耗了大量的时间。本文将通过分析特征如何影响模型表现,提供一种方法来解释各个输入变量如何影响模型性能,以及如何选取关键输入参数。

  这里所使用的方法为Python中基于博弈论的SHAP分析方法。SHAP方法是解释性AI(XAI)领域的一个重大进步。最近发表在nature communications上的一篇重量级文章《Explainable artificial intelligence model to predict acute critical illness from electronic health records》正是使用了这种方法。简而言之,它都说明了每个输入变量对每个最终估算结果值的促进或者抑制作用。关于SHAP方法的具体信息您可以在https://shap.readthedocs.io/en/latest/找到,本文中也会对使用到的一些基础方法进行解释。

  首先我们简单的构建一个训练模型,这个模型可以是您已经构建完成的模型:

#生成随机数据
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

X, y = make_classification(n_samples=1000, n_features=20, n_informative=2, n_redundant=10, n_classes=2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

#训练神经网络模型
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
model = Sequential([
    Dense(128, activation='relu', input_shape=(X_train.shape[1],)),
    Dense(64, activation='relu'),
    Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=10, batch_size=32, validation_split=0.2)

  在这之后我们训练得到了一个神经网络模型,现在可以使用 SHAP的 DeepExplainer 来解释模型的预测。

import shap

# 选取一些测试背景数据
background = X_train[:100]
explainer = shap.DeepExplainer(model, background)

# 利用背景数据解释预测
shap_values = explainer.shap_values(X_test[:10])

结果可视化

  计算SHAP值(shap_values)后,就可以评估模型在测试集上的性能并解释结果。我们可以通过SHAP图来对最终结果进行详细的分析。这里小编给出分析以及绘图过程的完整代码。

# Evaluate the model
loss, accuracy = model.evaluate(X_test, y_test, verbose=0)
print(f"Test Loss: {loss}\nTest Accuracy: {accuracy}")

# Plot summary of SHAP values
# Ensure this matches the structure of your shap_values
correct_shap_values = shap_values[1]

print("Shape of SHAP values:", np.array(correct_shap_values).shape)
print("Shape of features:", X_test[:10].shape)

shap_values_output = explainer.shap_values(X_test[:10])
# For binary classification or single-output models, shap_values_output should be a list with one or two elements
print(type(shap_values_output))
print([np.array(values).shape for values in shap_values_output])

# Assuming shap_values_output correctly contains your SHAP values for plotting
correct_shap_values = shap_values_output[0]  # Adjust based on your model's specifics

# Convert list of arrays to a single array
reshaped_shap_values = np.concatenate([np.array(vals).reshape(1-1for vals in shap_values_output], axis=0)

# Now reshaped_shap_values should have the shape (10, 20), matching X_test[:10]
print("Reshaped SHAP values shape:", reshaped_shap_values.shape)

# Attempt to plot with the reshaped SHAP values
shap.summary_plot(reshaped_shap_values, X_test[:10], feature_names=[f'Feature {i}' for i in range(X.shape[1])])

运行如上代码,您会获得一张类似这种的散点图:

  图中红色特征使预测值更大(类似正相关),蓝色使预测值变小,紫色邻近均值。而颜色区域宽度越大,说明该特征的对最终结果影响越大。这里可以看到第9个输入参数对最终结果有着最大的影响。另外,比如输入参数15,大多数的点弥漫在SHAP = 0,说明它对大部分结果都没啥影响,只对小部分结果有影响。

  本文通过一个完整的示例说明了如何生成合成数据集、训练深度学习模型以及使用 SHAP 的 DeepExplainer 来解释模型的预测。并提供了如何利用评估指标和绘图对模型性能进行定量评估,也提供了对每个特征对模型预测的影响的定性理解,希望对您有所帮助。

  "我们在深度学习错综复杂的迷宫中航行,借助 SHAP 的的光芒,一次一个特征地照亮理解的道路。这就是我们揭开神秘面纱的旅程,以确保机器学习模型不仅仅是黑匣子,而是一本打开的书,我们可以阅读、理解和信任它们的故事。"


Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/178966
 
120 次点击