You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将XGBoost数据的部分依赖图从折线图改为散点图

How to Create SHAP-like Scatter Plots in R for XGBoost Models

Great question! The line plot from partial() shows the average partial dependence of your target on MSSubClass, but to get that scatter plot-style view of individual observations' relationships (like the SHAP plots you referenced), you have two solid approaches in R. Let's break them down:

First, a quick side note: In your original training code, you defined param.list but passed params to xgb.train—make sure to fix that to avoid errors (params = param.list).


Approach 1: Use pdp to Plot Individual Conditional Expectations (ICE) as Scatter Points

The partial() function can generate individual conditional expectation (ICE) values for each observation, which we can then plot as a scatter plot alongside the average partial dependence line.

# Load required packages
library(pdp)
library(xgboost)
library(ggplot2)

# Fix your model training code (correct param name)
param.list <- list(max_depth = 5, eta = 0.01, objective = "binary:logistic", eval_metric = "auc")
house.xgb <- xgb.train(
  params = param.list, 
  data = xgb.DMatrix(as.matrix(house[,1:3]), label = house$SalePrice, missing = NA), 
  nrounds = 500
)

# Compute ICE data instead of plotting directly
ice_data <- partial(
  house.xgb, 
  pred.var = "MSSubClass", 
  plot = FALSE, 
  train = house[,1:3], 
  ice = TRUE  # This enables individual observation values
)

# Plot as scatter plot with average PDP line
ggplot(ice_data, aes(x = MSSubClass, y = yhat)) +
  geom_point(alpha = 0.3, color = "#2c3e50")  # Scatter points for each observation
  geom_line(
    data = aggregate(yhat ~ MSSubClass, ice_data, mean),
    aes(y = yhat), 
    color = "#e74c3c", 
    size = 1.2
  ) +
  labs(
    x = "MSSubClass", 
    y = "Partial Prediction (SalePrice)", 
    title = "Individual & Average Partial Dependence"
  ) +
  theme_minimal()

This plot shows each observation's predicted value as MSSubClass changes, overlaid with the average trend (the original line plot you got).


Approach 2: Use the shap Package to Replicate Python-Style SHAP Scatter Plots

For an exact match to the SHAP scatter plots you referenced, use R's shap package to compute SHAP values (which measure each feature's contribution to individual predictions) and plot them against your feature.

# Install and load the shap package
install.packages("shap")
library(shap)

# Prepare training data matrix
X_train <- as.matrix(house[,1:3])

# Compute SHAP values for your model
shap_results <- shap.values(model = house.xgb, X = X_train)

# Create a data frame for plotting
shap_plot_df <- data.frame(
  MSSubClass = X_train[,"MSSubClass"],
  SHAP_Value = shap_results$shap_score[,"MSSubClass"],
  Actual_SalePrice = house$SalePrice
)

# Plot SHAP values vs MSSubClass (colored by actual sale price)
ggplot(shap_plot_df, aes(x = MSSubClass, y = SHAP_Value)) +
  geom_point(aes(color = Actual_SalePrice), alpha = 0.6) +
  scale_color_viridis_c(option = "plasma") +
  labs(
    x = "MSSubClass", 
    y = "SHAP Value (Contribution to Prediction)", 
    title = "SHAP Value vs MSSubClass",
    color = "Actual SalePrice"
  ) +
  theme_minimal()

This plot shows how each observation's MSSubClass value contributes to its predicted sale price (positive SHAP values increase the prediction, negative values decrease it), colored by the actual target variable—just like the Python SHAP example.


内容的提问来源于stack exchange,提问作者Kayla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:10:02