mojolearn.VotingClassifier#
- class mojolearn.VotingClassifier(estimators, *, voting='hard', weights=None, n_jobs=None, flatten_transform=True, verbose=False)#
Combine 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,)
Methods
fit(X, y[, sample_weight])Fit a clone of every active estimator on
Xandy.get_params([deep])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.
The GPU backend this estimator runs on.
- fit(X, y, sample_weight=None)#
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)#
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
- predict(X)#
Predict class labels by weighted vote (hard) or averaged probabilities (soft).
- Returns:
array of shape (n_samples,) – Labels from
classes_.
- transform(X)#
The members’ outputs side by side.
- Returns:
array, float64 – With
voting="soft", every member’s class probabilities, shape (n_samples, n_estimators * n_classes); withvoting="hard", every member’s predicted class code, shape (n_samples, n_estimators).
- numeric_mode_used()#
The numeric mode this estimator runs in.
- Returns:
str –
'identical'or'fast', read from the compiled binary the estimator uses, not from the argument it was given.
- set_params(**params)#
Set constructor parameters. The estimator is rebuilt through its constructor, so every constructor check runs again and any fitted state is dropped, as a new setting invalidates it.
- vendor_used()#
The GPU backend this estimator runs on.
- Returns:
str –
'metal','cuda'or'hip', read from the compiled binary the estimator uses.