From 721165f304dadb06fcbe8b2a18523a007dca7a6c Mon Sep 17 00:00:00 2001 From: Reid Johnson Date: Wed, 21 Feb 2024 21:05:21 -0800 Subject: [PATCH] Update examples --- docs/conf.py | 1 + docs/sphinxext/gallery.py | 2 +- .../tests/examples => examples}/__init__.py | 0 .../plot_quantile_conformalized.py | 0 .../plot_quantile_example.py | 0 .../plot_quantile_extrapolation.py | 4 ++-- .../plot_quantile_interpolation.py | 24 +++++++++---------- .../plot_quantile_intervals.py | 4 ++-- .../plot_quantile_multioutput.py | 0 .../plot_quantile_vs_standard.py | 8 +++---- .../plot_quantile_weighting.py | 0 11 files changed, 21 insertions(+), 22 deletions(-) rename {quantile_forest/tests/examples => examples}/__init__.py (100%) rename {quantile_forest/tests/examples => examples}/plot_quantile_conformalized.py (100%) rename {quantile_forest/tests/examples => examples}/plot_quantile_example.py (100%) rename {quantile_forest/tests/examples => examples}/plot_quantile_extrapolation.py (99%) rename {quantile_forest/tests/examples => examples}/plot_quantile_interpolation.py (100%) rename {quantile_forest/tests/examples => examples}/plot_quantile_intervals.py (98%) rename {quantile_forest/tests/examples => examples}/plot_quantile_multioutput.py (100%) rename {quantile_forest/tests/examples => examples}/plot_quantile_vs_standard.py (90%) rename {quantile_forest/tests/examples => examples}/plot_quantile_weighting.py (100%) diff --git a/docs/conf.py b/docs/conf.py index edeeb0b..c63a92d 100755 --- a/docs/conf.py +++ b/docs/conf.py @@ -16,6 +16,7 @@ from datetime import datetime sys.path.insert(0, os.path.abspath(".")) +sys.path.insert(0, os.path.abspath("..")) # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the diff --git a/docs/sphinxext/gallery.py b/docs/sphinxext/gallery.py index 884952f..856bc72 100644 --- a/docs/sphinxext/gallery.py +++ b/docs/sphinxext/gallery.py @@ -16,7 +16,7 @@ from docutils.statemachine import ViewList from sphinx.util.nodes import nested_parse_with_titles -from quantile_forest.tests.examples import iter_examples +from examples import iter_examples from .utils import create_generic_image, create_thumbnail, get_docstring_and_rest, prev_this_next diff --git a/quantile_forest/tests/examples/__init__.py b/examples/__init__.py similarity index 100% rename from quantile_forest/tests/examples/__init__.py rename to examples/__init__.py diff --git a/quantile_forest/tests/examples/plot_quantile_conformalized.py b/examples/plot_quantile_conformalized.py similarity index 100% rename from quantile_forest/tests/examples/plot_quantile_conformalized.py rename to examples/plot_quantile_conformalized.py diff --git a/quantile_forest/tests/examples/plot_quantile_example.py b/examples/plot_quantile_example.py similarity index 100% rename from quantile_forest/tests/examples/plot_quantile_example.py rename to examples/plot_quantile_example.py diff --git a/quantile_forest/tests/examples/plot_quantile_extrapolation.py b/examples/plot_quantile_extrapolation.py similarity index 99% rename from quantile_forest/tests/examples/plot_quantile_extrapolation.py rename to examples/plot_quantile_extrapolation.py index b5a664e..6f36704 100755 --- a/quantile_forest/tests/examples/plot_quantile_extrapolation.py +++ b/examples/plot_quantile_extrapolation.py @@ -40,7 +40,7 @@ def get_train_Xy(X, y, min_idx, max_idx): return X_train, y_train -def get_test_X(X): +def get_test_X(X, bounds): n_samples = len(X) X_test = np.atleast_2d(np.linspace(*bounds, n_samples)).T return X_test @@ -56,7 +56,7 @@ def get_test_X(X): # Based on the extrapolation bounds, get the training and test data. # Training data excludes extrapolated regions; test data includes them. X_train, y_train = get_train_Xy(X, y, extrap_min_idx, extrap_max_idx) -X_test = get_test_X(X) +X_test = get_test_X(X, bounds) qrf = RandomForestQuantileRegressor(max_samples_leaf=None, min_samples_leaf=10, random_state=0) qrf.fit(np.expand_dims(X_train, axis=-1), y_train) diff --git a/quantile_forest/tests/examples/plot_quantile_interpolation.py b/examples/plot_quantile_interpolation.py similarity index 100% rename from quantile_forest/tests/examples/plot_quantile_interpolation.py rename to examples/plot_quantile_interpolation.py index 07e760a..4b7588c 100755 --- a/quantile_forest/tests/examples/plot_quantile_interpolation.py +++ b/examples/plot_quantile_interpolation.py @@ -17,18 +17,6 @@ from quantile_forest import RandomForestQuantileRegressor -interpolations = { - "Linear": "#006aff", - "Lower": "#ffd237", - "Higher": "#0d4599", - "Midpoint": "#f2a619", - "Nearest": "#a6e5ff", -} - -# legend = {"Actual": "#000000"} | interpolations -legend = {"Actual": "#000000"} -legend.update(interpolations) - # Create toy dataset. X = np.array([[-1, -1], [-1, -1], [-1, -1], [1, 1], [1, 1]]) y = np.array([-2, -1, 0, 1, 2]) @@ -41,6 +29,18 @@ ) est.fit(X, y) +interpolations = { + "Linear": "#006aff", + "Lower": "#ffd237", + "Higher": "#0d4599", + "Midpoint": "#f2a619", + "Nearest": "#a6e5ff", +} + +# legend = {"Actual": "#000000"} | interpolations +legend = {"Actual": "#000000"} +legend.update(interpolations) + # Initialize data with actual values. data = { "method": ["Actual"] * len(y), diff --git a/quantile_forest/tests/examples/plot_quantile_intervals.py b/examples/plot_quantile_intervals.py similarity index 98% rename from quantile_forest/tests/examples/plot_quantile_intervals.py rename to examples/plot_quantile_intervals.py index 01d6b68..13a0603 100755 --- a/quantile_forest/tests/examples/plot_quantile_intervals.py +++ b/examples/plot_quantile_intervals.py @@ -72,7 +72,7 @@ def plot_calibration(df): "y_pred:Q", axis=alt.Axis(format="$,d"), scale=alt.Scale(domain=domain, nice=False), - title="Fitted Values (Conditional Mean)", + title="Fitted Values (conditional median)", ), y=alt.Y( "y_true:Q", @@ -152,7 +152,7 @@ def plot_intervals(df): y=alt.Y( "y_true:Q", axis=alt.Axis(format="$,d"), - title="Observed Values and Prediction Intervals", + title="Observed Values and Prediction Intervals (centered)", ), color=alt.value("#f2a619"), tooltip=tooltip, diff --git a/quantile_forest/tests/examples/plot_quantile_multioutput.py b/examples/plot_quantile_multioutput.py similarity index 100% rename from quantile_forest/tests/examples/plot_quantile_multioutput.py rename to examples/plot_quantile_multioutput.py diff --git a/quantile_forest/tests/examples/plot_quantile_vs_standard.py b/examples/plot_quantile_vs_standard.py similarity index 90% rename from quantile_forest/tests/examples/plot_quantile_vs_standard.py rename to examples/plot_quantile_vs_standard.py index 5b0cf0f..bc511e1 100755 --- a/quantile_forest/tests/examples/plot_quantile_vs_standard.py +++ b/examples/plot_quantile_vs_standard.py @@ -64,8 +64,6 @@ def plot_prediction_histograms(df, legend): .transform_calculate(calculate=f"round({alt.datum['rf']} * 10) / 10", as_="RF (Mean)") .transform_calculate(calculate=f"round({alt.datum['qrf']} * 10) / 10", as_="QRF (Median)") .transform_fold(["Actual", "RF (Mean)", "QRF (Median)"], as_=["label", "value"]) - .transform_joinaggregate(total="count(*)", groupby=["label"]) - .transform_calculate(pct="1 / datum.total") .mark_bar() .encode( x=alt.X( @@ -74,15 +72,15 @@ def plot_prediction_histograms(df, legend): labelAngle=0, labelExpr="datum.value % 0.5 == 0 ? datum.value : null", ), - title="Value", + title="Actual and Predicted Target Values", ), - y=alt.Y("sum(pct):Q", axis=alt.Axis(format=".0%", title="Percentage")), + y=alt.Y("count():Q", axis=alt.Axis(format=",d", title="Counts")), color=color, xOffset=alt.XOffset("label:N"), tooltip=[ alt.Tooltip("label:N", title="Label"), alt.Tooltip("value:O", title="Value (binned)"), - alt.Tooltip("sum(pct):Q", format=".0%", title="Percentage"), + alt.Tooltip("count():Q", format=",d", title="Counts"), ], ) .add_params(click) diff --git a/quantile_forest/tests/examples/plot_quantile_weighting.py b/examples/plot_quantile_weighting.py similarity index 100% rename from quantile_forest/tests/examples/plot_quantile_weighting.py rename to examples/plot_quantile_weighting.py