mojolearn.VotingClassifier#
- class mojolearn.VotingClassifier(estimators, *, voting='hard', weights=None, n_jobs=None, flatten_transform=True, verbose=False)[source]#
Bases:
_TreesWrapperBaseCombine classifiers by a weighted majority vote or averaged probabilities.
Follows scikit-learn’s
VotingClassifier.fitclones every estimator and fits the clones on the label-encodedy; 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 theirpredict_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"raiseValueError.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 raisesValueError.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;
transformalways returns a 2-D array.verbose (bool, default False) – Only False is accepted; True raises
NotImplementedError.
- Variables:
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
Xandy.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).
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
Xandy.- 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
fitwhen given.
- Returns:
self
- predict_proba(X)[source]#
Weighted average of the members’ class probabilities.
Only available with
voting="soft"; withvoting="hard"it raisesAttributeError.- Returns:
array of shape (n_samples, n_classes), float64