mojolearn.VotingClassifier#

class mojolearn.VotingClassifier(estimators, *, voting='hard', weights=None, n_jobs=None, flatten_transform=True, verbose=False)[source]#

Bases: _TreesWrapperBase

Combine classifiers by a weighted majority vote or averaged probabilities.

Follows scikit-learn’s VotingClassifier. fit clones every estimator and fits the clones on the label-encoded y; the estimators you pass are never fitted in place. voting="hard" predicts the weighted majority of the members’ predicted labels; voting="soft" predicts the argmax of the weighted average of their predict_proba. The members fit one after another.

Parameters:
  • estimators (list of (str, estimator)) – Named mojolearn classifiers. Names must be unique; an entry whose estimator is the string "drop" is skipped. An empty list, duplicate names, or every entry "drop" raise ValueError.

  • voting ({“hard”, “soft”}, default “hard”) – Majority vote on predicted labels, or argmax of the averaged probabilities (every member then needs predict_proba). Any other value raises ValueError.

  • weights (sequence of float, default None) – One weight per active estimator; None weighs them equally. A length that does not match the active estimators raises ValueError.

  • n_jobs (None) – Only None is accepted; any other value raises NotImplementedError.

  • flatten_transform (bool, default True) – Kept for scikit-learn compatibility; transform always returns a 2-D array.

  • verbose (bool, default False) – Only False is accepted; True raises NotImplementedError.

Variables:
  • estimators_ (list) – The fitted clones, in order (fitted on integer class codes).

  • named_estimators_ (dict) – The fitted clones by name.

  • classes_ (array) – The class labels seen in fit, sorted.

  • n_classes_ (int) – Number of classes.

  • n_features_in_ (int) – Number of features seen in fit.

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X = rng.random((1000, 8), dtype=np.float32)
>>> y = (X[:, 0] > 0.5).astype(np.int32)
>>> model = mojolearn.VotingClassifier(
...     [("rf", mojolearn.RandomForestClassifier()),
...      ("dt", mojolearn.DecisionTreeClassifier())], voting="soft").fit(X, y)
>>> model.predict(X[:5]).shape
(5,)
__init__(estimators, *, voting='hard', weights=None, n_jobs=None, flatten_transform=True, verbose=False)[source]#

Methods

fit(X, y[, sample_weight])

Fit a clone of every active estimator on X and y.

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Predict class labels by weighted vote (hard) or averaged probabilities (soft).

predict_proba(X)

Weighted average of the members' class probabilities.

set_params(**params)

Set constructor parameters.

transform(X)

The members' outputs side by side.

vendor_used()

The GPU backend this estimator runs on.

fit(X, y, sample_weight=None)[source]#

Fit a clone of every active estimator on X and y.

Parameters:
  • X (array of shape (n_samples, n_features)) – Training data, converted to float32.

  • y (array of shape (n_samples,)) – Class labels.

  • sample_weight (array of shape (n_samples,), default None) – Passed to every member’s fit when given.

Returns:

self

predict_proba(X)[source]#

Weighted average of the members’ class probabilities.

Only available with voting="soft"; with voting="hard" it raises AttributeError.

Returns:

array of shape (n_samples, n_classes), float64

predict(X)[source]#

Predict class labels by weighted vote (hard) or averaged probabilities (soft).

Returns:

array of shape (n_samples,) – Labels from classes_.

transform(X)[source]#

The members’ outputs side by side.

Returns:

array, float64 – With voting="soft", every member’s class probabilities, shape (n_samples, n_estimators * n_classes); with voting="hard", every member’s predicted class code, shape (n_samples, n_estimators).