ClassifierChain¶
A multi-output model that arranges classifiers into a chain.
This will create one model per output. The prediction of the first output will be used as a feature in the second model. The prediction for the second output will be used as a feature for the third model, etc. This "chain model" is therefore capable of capturing dependencies between outputs.
Parameters¶
-
model
Type → base.Classifier
A classifier model used for each label.
-
order
Type → list | None
Default →
None
A list with the targets order in which to construct the chain. If
None
then the order will be inferred from the order of the keys in the target.
Examples¶
from river import feature_selection
from river import linear_model
from river import metrics
from river import multioutput
from river import preprocessing
from river import stream
from sklearn import datasets
dataset = stream.iter_sklearn_dataset(
dataset=datasets.fetch_openml('yeast', version=4, parser='auto', as_frame=False),
shuffle=True,
seed=42
)
model = feature_selection.VarianceThreshold(threshold=0.01)
model |= preprocessing.StandardScaler()
model |= multioutput.ClassifierChain(
model=linear_model.LogisticRegression(),
order=list(range(14))
)
metric = metrics.multioutput.MicroAverage(metrics.Jaccard())
for x, y in dataset:
# Convert y values to booleans
y = {i: yi == 'TRUE' for i, yi in y.items()}
y_pred = model.predict_one(x)
metric = metric.update(y, y_pred)
model = model.learn_one(x, y)
metric
MicroAverage(Jaccard): 41.95%
Methods¶
learn_one
Update the model with a set of features x
and the labels y
.
Parameters
- x
- y
- kwargs
Returns
self
predict_one
Predict the labels of a set of features x
.
Parameters
- x — 'dict'
- kwargs
Returns
dict[FeatureName, bool]: The predicted labels.
predict_proba_one
Predict the probability of each label appearing given dictionary of features x
.
Parameters
- x
- kwargs
Returns
A dictionary that associates a probability which each label.