Some estimators can be trained incrementally – without seeing the entire
dataset at once. Scikit-Learn provdes the
partial_fit API to stream batches
of data to an estimator that can be fit in batches.
Normally, if you pass a Dask Array to an estimator expecting a NumPy array, the Dask Array will be converted to a single, large NumPy array. On a single machine, you’ll likely run out of RAM and crash the program. On a distributed cluster, all the workers will send their data to a single machine and crash it.
dask_ml.wrappers.Incremental provides a bridge between Dask and
Scikit-Learn estimators supporting the
partial_fit API. You wrap the
underlying estimator in
Incremental. Dask-ML will sequentially pass each
block of a Dask Array to the underlying estimator’s
dask_ml.wrappers.Incremetnal currently does not work well with
hyper-parameter optimization like
If you need to do hyper-parameter optimization on larger-than-memory datasets,
Incremental Hyperparameter Optimization for an introduction.
||Metaestimator for feeding Dask Arrays to an estimator blockwise.|
dask_ml.wrappers.Incremental is a meta-estimator (an estimator that
takes another estimator) that bridges scikit-learn estimators expecting
NumPy arrays, and users with large Dask Arrays.
Each block of a Dask Array is fed to the underlying estiamtor’s
partial_fit method. The training is entirely sequential, so you won’t
notice massive training time speedups from parallelism. In a distributed
environment, you should notice some speedup from avoiding extra IO, and the
fact that models are typically much smaller than data, and so faster to move
In : from dask_ml.datasets import make_classification In : from dask_ml.wrappers import Incremental In : from sklearn.linear_model import SGDClassifier In : X, y = make_classification(chunks=25) In : X Out: dask.array<normal, shape=(100, 20), dtype=float64, chunksize=(25, 20)> In : estimator = SGDClassifier(random_state=10, max_iter=100) In : clf = Incremental(estimator) In : clf.fit(X, y, classes=[0, 1]) Out: Incremental(estimator=SGDClassifier(alpha=0.0001, average=False, class_weight=None, early_stopping=False, epsilon=0.1, eta0=0.0, fit_intercept=True, l1_ratio=0.15, learning_rate='optimal', loss='hinge', max_iter=100, n_iter_no_change=5, n_jobs=None, penalty='l2', power_t=0.5, random_state=10, shuffle=True, tol=0.001, validation_fraction=0.1, verbose=0, warm_start=False), random_state=None, scoring=None, shuffle_blocks=True)
In this example, we make a (small) random Dask Array. It has 100 samples, broken in the 4 blocks of 25 samples each. The chunking is only along the first axis (the samples). There is no chunking along the features.
You instantiate the underlying estimator as usual. It really is just a
scikit-learn compatible estimator, and will be trained normally via its
Notice that we call the regular
.fit method, not
training. Dask-ML takes care of passing each block to the underlying estimator
sklearn.linear_model.SGDClassifier.partial_fit(), we need to
classes argument to
fit. In general, any argument that is
required for the underlying estimators
parital_fit becomes required for
Take care with the behavior of
Incremental.score(). Most estimators
inherit the default scoring methods of R2 score for regressors and accuracy
score for classifiers. For these estimators, we automatically use Dask-ML’s
scoring methods, which are able to operate on Dask arrays.
If your underlying estimator uses a different scoring method, you’ll need
to ensure that the scoring method is able to operate on Dask arrays. You
can also explicitly pass
scoring= to pass a dask-aware scorer.
We can get the accuracy score on our dataset.
In : clf.score(X, y) Out: 0.62
All of the attributes learned durning training, like
coef_, are available
In : clf.coef_ Out: array([[-5.39119936e+00, -1.56759488e+01, -6.00861788e+01, 2.71268360e+01, -4.48292193e-01, 7.57325153e+00, -6.46139441e-01, 2.54349231e+01, 1.82530763e+01, -2.85841416e+01, 3.30877008e+01, 1.01778158e+01, 7.86040548e+00, 2.58093228e+00, -1.26888220e+01, 4.33806194e-02, 2.67592298e+01, 6.55430170e-01, 4.41006808e+00, -5.67343854e+01]])
If necessary, the actual estimator trained is available as
In : clf.estimator_ Out: SGDClassifier(alpha=0.0001, average=False, class_weight=None, early_stopping=False, epsilon=0.1, eta0=0.0, fit_intercept=True, l1_ratio=0.15, learning_rate='optimal', loss='hinge', max_iter=100, n_iter_no_change=5, n_jobs=None, penalty='l2', power_t=0.5, random_state=10, shuffle=True, tol=0.001, validation_fraction=0.1, verbose=0, warm_start=False)