{"spec_id":"pdp-basic","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\npdp-basic: Partial Dependence Plot\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-15\n\"\"\"\n\nimport importlib\nimport sys\nfrom pathlib import Path\n\n\n# Remove current directory from sys.path to avoid shadowing bokeh package\nsys.path = [p for p in sys.path if Path(p).resolve() != Path(__file__).resolve().parent]\n\n# Import bokeh module and its submodules\nbokeh_io = importlib.import_module(\"bokeh.io\")\nbokeh_models = importlib.import_module(\"bokeh.models\")\nbokeh_plotting = importlib.import_module(\"bokeh.plotting\")\n\nimport os\nimport time\n\nimport numpy as np\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\nfrom sklearn.datasets import make_friedman1\nfrom sklearn.ensemble import GradientBoostingRegressor\nfrom sklearn.inspection import partial_dependence\n\n\n# Use imported modules\noutput_file = bokeh_io.output_file\nsave = bokeh_io.save\nBand = bokeh_models.Band\nColumnDataSource = bokeh_models.ColumnDataSource\nSpan = bokeh_models.Span\nfigure = bokeh_plotting.figure\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\nBRAND = \"#009E73\"  # Okabe-Ito position 1\nACCENT = \"#C475FD\"  # Okabe-Ito position 2\n\n# Data - Train a model and compute partial dependence\nnp.random.seed(42)\n\n# Use Friedman #1 dataset which has known non-linear relationships\nX, y = make_friedman1(n_samples=500, n_features=5, noise=0.5, random_state=42)\n\n# Train a gradient boosting model\nmodel = GradientBoostingRegressor(n_estimators=100, max_depth=4, random_state=42)\nmodel.fit(X, y)\n\n# Compute partial dependence for feature 0 (has sin relationship)\nfeature_idx = 0\ngrid_resolution = 100\n\n# Compute partial dependence using sklearn\npdp_results = partial_dependence(model, X, features=[feature_idx], kind=\"both\", grid_resolution=grid_resolution)\n\n# Extract values\navg_predictions = pdp_results[\"average\"][0]\nindividual_predictions = pdp_results[\"individual\"][0]  # ICE lines\ngrid_values = pdp_results[\"grid_values\"][0]\n\n# Calculate confidence interval (percentiles of ICE lines)\nlower_bound = np.percentile(individual_predictions, 10, axis=0)\nupper_bound = np.percentile(individual_predictions, 90, axis=0)\n\n# Center partial dependence at zero for easier interpretation\ncenter_val = avg_predictions.mean()\navg_centered = avg_predictions - center_val\nlower_centered = lower_bound - center_val\nupper_centered = upper_bound - center_val\n\n# Get training data distribution for rug plot\nrug_x = X[:, feature_idx]\n\n# Create data source for main line and band\nsource = ColumnDataSource(data={\"x\": grid_values, \"y\": avg_centered, \"lower\": lower_centered, \"upper\": upper_centered})\n\n# Create data source for rug plot - position at bottom of plot area\ny_min = lower_centered.min() - 1.5\nrug_source = ColumnDataSource(data={\"x\": rug_x, \"y\": np.full_like(rug_x, y_min + 0.3)})\n\n# Plot\np = figure(\n    width=4800,\n    height=2700,\n    title=\"pdp-basic · bokeh · anyplot.ai\",\n    x_axis_label=\"Feature X₀ Value\",\n    y_axis_label=\"Partial Dependence (centered)\",\n)\n\n# Add confidence band\nband = Band(\n    base=\"x\",\n    lower=\"lower\",\n    upper=\"upper\",\n    source=source,\n    fill_color=BRAND,\n    fill_alpha=0.25,\n    line_color=BRAND,\n    line_alpha=0.4,\n)\np.add_layout(band)\n\n# Add horizontal line at y=0 for reference\nzero_line = Span(location=0, dimension=\"width\", line_color=INK_SOFT, line_width=3, line_dash=\"dashed\", line_alpha=0.6)\np.add_layout(zero_line)\n\n# Add invisible patch for confidence band legend entry\np.patch([], [], fill_color=BRAND, fill_alpha=0.25, line_color=BRAND, line_alpha=0.4, legend_label=\"80% CI\")\n\n# Add main PDP line\np.line(\"x\", \"y\", source=source, line_width=5, line_color=BRAND, legend_label=\"Average PD\")\n\n# Add rug plot for data distribution\np.scatter(\n    \"x\",\n    \"y\",\n    source=rug_source,\n    size=25,\n    color=ACCENT,\n    alpha=0.6,\n    line_width=3,\n    angle=1.5708,\n    marker=\"dash\",\n    legend_label=\"Data Distribution\",\n)\n\n# Style - Text sizing\np.title.text_font_size = \"28pt\"\np.title.text_color = INK\np.xaxis.axis_label_text_font_size = \"22pt\"\np.yaxis.axis_label_text_font_size = \"22pt\"\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\np.xaxis.major_label_text_font_size = \"18pt\"\np.yaxis.major_label_text_font_size = \"18pt\"\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\n\n# Axis styling\np.xaxis.axis_line_width = 3\np.yaxis.axis_line_width = 3\np.xaxis.axis_line_color = INK_SOFT\np.yaxis.axis_line_color = INK_SOFT\np.xaxis.major_tick_line_width = 3\np.yaxis.major_tick_line_width = 3\np.xaxis.major_tick_line_color = INK_SOFT\np.yaxis.major_tick_line_color = INK_SOFT\np.xaxis.minor_tick_line_width = 2\np.yaxis.minor_tick_line_width = 2\np.xaxis.minor_tick_line_color = INK_SOFT\np.yaxis.minor_tick_line_color = INK_SOFT\n\n# Grid styling\np.xgrid.grid_line_color = INK\np.ygrid.grid_line_color = INK\np.xgrid.grid_line_alpha = 0.10\np.ygrid.grid_line_alpha = 0.10\np.xgrid.grid_line_dash = \"dashed\"\np.ygrid.grid_line_dash = \"dashed\"\n\n# Legend styling\np.legend.location = \"bottom_right\"\np.legend.label_text_font_size = \"18pt\"\np.legend.label_text_color = INK_SOFT\np.legend.background_fill_color = ELEVATED_BG\np.legend.background_fill_alpha = 0.9\np.legend.border_line_color = INK_SOFT\np.legend.border_line_alpha = 0.5\np.legend.border_line_width = 2\np.legend.glyph_height = 50\np.legend.glyph_width = 50\np.legend.spacing = 20\np.legend.padding = 25\np.legend.margin = 40\n\n# Background and border\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = INK_SOFT\n\n# Save HTML\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with headless Chrome\nW, H = 4800, 2700\nopts = Options()\nfor arg in (\n    \"--headless=new\",\n    \"--no-sandbox\",\n    \"--disable-dev-shm-usage\",\n    \"--disable-gpu\",\n    f\"--window-size={W},{H}\",\n    \"--hide-scrollbars\",\n):\n    opts.add_argument(arg)\n\ndriver = webdriver.Chrome(options=opts)\ndriver.set_window_size(W, H)\ndriver.get(f\"file://{Path(f'plot-{THEME}.html').resolve()}\")\ntime.sleep(3)  # let bokeh's JS render the canvas\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}