SlidingWindowClassifier#
- class skactiveml.classifier.SlidingWindowClassifier(estimator, classes=None, missing_label=nan, cost_matrix=None, window_size=None, only_labeled=False, random_state=None)[source]#
Bases:
SkactivemlClassifier,MetaEstimatorMixinSliding Window Classifier
Implementation of a wrapper class for SkactivemlClassifier such that the number of training samples can be limited to the latest window_size samples. Furthermore, saves X, y and sample_weight, enabling the use of a partial_fit for any classifier.
- Parameters:
- estimatorskactiveml.base.SkactivemlClassifier
The classifier to be wrapped. If this classifier already implements a partial_fit, this method will be overwritten by this wrapper using the sliding window approach.
- classesarray-like of shape (n_classes,), default=None
Holds the label for each class. If None, the classes declared by estimator are used, or else determined during the fit.
- missing_labelscalar or string or np.nan or None, default=np.nan
Value to represent a missing label.
- cost_matrixarray-like of shape (n_classes, n_classes)
Cost matrix with cost_matrix[i,j] indicating cost of predicting class classes[j] for a sample of class classes[i]. Can be only set, if classes is not none. If both the wrapper and estimator specify a cost matrix, their costs must agree for each pair of class labels, even if their declared class orders differ.
- window_sizeint, default=None,
Value to represent the estimator sliding window size for X, y and sample weight. If None the window is unrestricted in its size.
- only_labeledbool, default=False
If True, unlabeled samples are discarded.
- random_stateint or RandomState instance or None, default=None
Determines random number for predict method. Pass an int for reproducible results across multiple method calls.
Notes
Attributes this wrapper does not hold itself are read from the wrapped estimator, e.g., classes_. The fitted attributes it holds itself, e.g., target_spec_ and the sliding window in X_train_, are never read from the estimator.
When a window mixes weighted and unweighted batches, samples from an unweighted batch have unit weight. Their weights are retained until the corresponding samples leave the window or a new fit replaces it.
Methods
fit(X, y[, sample_weight])Fit the model using X as training data and y as class labels.
partial_fit(X, y[, sample_weight])Add X and y to the sliding window and refit the estimator on the samples in the window.
predict(X, **predict_kwargs)Return class label predictions for the input data X.
predict_freq(X, **predict_freq_kwargs)Return class frequency estimates for the test samples X.
predict_proba(X, **predict_proba_kwargs)Return probability estimates for the input data X.
score(X, y[, sample_weight])Return the mean accuracy on the given test data and labels.
Get metadata routing of this object.
get_params([deep])Get parameters for this estimator.
set_fit_request(*[, sample_weight])Configure whether metadata should be requested to be passed to the
fitmethod.set_params(**params)Set the parameters of this estimator.
set_partial_fit_request(*[, sample_weight])Configure whether metadata should be requested to be passed to the
partial_fitmethod.set_score_request(*[, sample_weight])Configure whether metadata should be requested to be passed to the
scoremethod.
- SlidingWindowClassifier.fit(X, y, sample_weight=None, **fit_kwargs)[source]#
Fit the model using X as training data and y as class labels.
- Parameters:
- Xarray-like of shape (n_samples, …)
The feature matrix representing the samples.
- yarray-like of shape (n_samples,)
It contains the class labels of the training samples. Missing labels are represented by the attribute self.missing_label_.
- sample_weightarray-like of shape (n_samples,), default=None
It contains the weights of the training samples’ class labels.
- fit_kwargsdict-like
Further parameters as input to the fit method of the estimator.
- Returns:
- self: SlidingWindowClassifier,
The SlidingWindowClassifier is fitted on the training data.
- SlidingWindowClassifier.partial_fit(X, y, sample_weight=None, **fit_kwargs)[source]#
Add X and y to the sliding window and refit the estimator on the samples in the window.
- Parameters:
- Xarray-like of shape (n_samples, …)
The feature matrix representing the samples.
- yarray-like of shape (n_samples,)
It contains the class labels of the training samples. Missing labels are represented by the attribute self.missing_label_.
- sample_weightarray-like of shape (n_samples,), default=None
It contains the weights of the training samples’ class labels.
- fit_kwargsdict-like
Further parameters as input to the fit method of the estimator.
- Returns:
- selfSlidingWindowClassifier,
The SlidingWindowClassifier is fitted on the training data.
- SlidingWindowClassifier.predict(X, **predict_kwargs)[source]#
Return class label predictions for the input data X.
- Parameters:
- Xarray-like of shape (n_samples, …)
Input samples.
- predict_kwargsdict-like
Further parameters as input to the predict method of the estimator.
- Returns:
- y_prednumpy.ndarray shape (n_samples,)
Predicted class labels of the input samples.
- SlidingWindowClassifier.predict_freq(X, **predict_freq_kwargs)[source]#
Return class frequency estimates for the test samples X.
- Parameters:
- Xarray-like of shape (n_samples, …)
Test samples whose class frequencies are to be estimated.
- Returns:
- Fnumpy.ndarray of shape (n_samples, classes)
The class frequency estimates of the test samples X. Classes are ordered according to the attribute self.classes_.
- SlidingWindowClassifier.predict_proba(X, **predict_proba_kwargs)[source]#
Return probability estimates for the input data X.
- Parameters:
- Xarray-like of shape (n_samples, …)
Input samples.
- predict_proba_kwargsdict-like
Further parameters as input to the predict_proba method of the estimator.
- Returns:
- Pnumpy.ndarray shape (n_samples, classes)
The class probabilities of the input samples X. Classes are ordered according to the attribute self.classes_.
- SlidingWindowClassifier.score(X, y, sample_weight=None)#
Return the mean accuracy on the given test data and labels.
- Parameters:
- Xarray-like of shape (n_samples, …)
Test samples.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs)
True class labels of the test samples X.
- sample_weightarray-like of shape (n_samples,), default=None
Sample weights of the test sample X.
- Returns:
- scorefloat
Mean accuracy of self.predict(X) regarding y. For multi-label classification, this is the subset accuracy, i.e., a sample counts as correct only if all of its labels are predicted correctly.
- SlidingWindowClassifier.get_metadata_routing()#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequestencapsulating routing information.
- SlidingWindowClassifier.get_params(deep=True)#
Get parameters for this estimator.
- Parameters:
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
- paramsdict
Parameter names mapped to their values.
- SlidingWindowClassifier.set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') SlidingWindowClassifier#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter infit.
- Returns:
- selfobject
The updated object.
- SlidingWindowClassifier.set_params(**params)#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline). The latter have parameters of the form<component>__<parameter>so that it’s possible to update each component of a nested object.- Parameters:
- **paramsdict
Estimator parameters.
- Returns:
- selfestimator instance
Estimator instance.
- SlidingWindowClassifier.set_partial_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') SlidingWindowClassifier#
Configure whether metadata should be requested to be passed to the
partial_fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topartial_fitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topartial_fit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter inpartial_fit.
- Returns:
- selfobject
The updated object.
- SlidingWindowClassifier.set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') SlidingWindowClassifier#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter inscore.
- Returns:
- selfobject
The updated object.