如何将XGBoost数据的部分依赖图从折线图改为散点图
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

