Using DJINN with scikit-learn

DJINN_Regressor and DJINN_Classifier are scikit-learn estimators, so they work anywhere a scikit-learn model does. Using DJINN with scikit-learn runs everything on this page.

Settings and clone

Every setting is a constructor argument, so get_params(), set_params() and sklearn.base.clone() work as usual. clone copies the settings, not the trained networks.

from sklearn.base import clone
from djinn import DJINN_Regressor

model = DJINN_Regressor(n_trees=3, max_tree_depth=6, random_state=0)
bigger = clone(model).set_params(n_trees=5)

Inputs

X can be a NumPy array, a list or a pandas DataFrame. Column names from a DataFrame are kept in feature_names_in_, and scikit-learn warns if you predict on data with different columns.

Pipelines

DJINN scales each input to [0, 1] using the training minimum and maximum, so it doesn’t need standardized inputs. It does need inputs without extreme outliers. A single huge value squashes the rest of that feature into a small range, and a test point far outside the training range gets extrapolated in a straight line by the ReLU networks, which can give wild predictions. A QuantileTransformer in front of DJINN fixes both. Text or categorical columns also need encoding:

from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, QuantileTransformer

prep = ColumnTransformer(
    [
        ("region", OneHotEncoder(), ["region"]),
        ("numeric", QuantileTransformer(n_quantiles=500), numeric_columns),
    ]
)
pipe = Pipeline([("prep", prep), ("djinn", DJINN_Regressor(n_trees=3))])
pipe.fit(X_train, y_train)

On the California housing data in Using DJINN with scikit-learn, one row with an average household size over 1,000 drops DJINN’s R² on its cross-validation fold below zero. With the quantile transform, DJINN beats a random forest on every fold. DJINN_Regressor(clip_inputs=True) is a simpler fix for the second problem, since it stops the model from extrapolating, but it doesn’t help with the first.

Saving

Fitted models and pipelines can be saved with pickle or joblib. As with any pickle, load them with the same versions of DJINN, scikit-learn and PyTorch.

save() and djinn.load() save only the DJINN model, as PyTorch checkpoints and a JSON file. Use them to keep a model long term or to keep training it later with continue_training().

Estimator checks

The test suite runs scikit-learn’s parametrize_with_checks() on both models. A few checks are expected to fail:

  • check_fit2d_1feature, check_estimators_pickle, check_pipeline_consistency, and check_classifiers_classes for the classifier, fit on a handful of samples or on data that one split separates. The trees only grow to depth 1, which is too shallow to build a network from. Pickling and pipelines are tested separately on realistic data.

  • check_methods_subset_invariance compares predictions made in batches with predictions made one row at a time. The networks run in float32, so these can differ by about 1e-7, which is more than the check allows.