package sklearn

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
type tag = [
  1. | `VotingClassifier
]
type t = [ `BaseEstimator | `ClassifierMixin | `MetaEstimatorMixin | `Object | `TransformerMixin | `VotingClassifier ] Obj.t
val of_pyobject : Py.Object.t -> t
val to_pyobject : [> tag ] Obj.t -> Py.Object.t
val as_classifier : t -> [ `ClassifierMixin ] Obj.t
val as_transformer : t -> [ `TransformerMixin ] Obj.t
val as_estimator : t -> [ `BaseEstimator ] Obj.t
val as_meta_estimator : t -> [ `MetaEstimatorMixin ] Obj.t
val create : ?voting:[ `Hard | `Soft ] -> ?weights:[> `ArrayLike ] Np.Obj.t -> ?n_jobs:int -> ?flatten_transform:bool -> estimators:(string * [> `BaseEstimator ] Np.Obj.t) list -> unit -> t

Soft Voting/Majority Rule classifier for unfitted estimators.

.. versionadded:: 0.17

Read more in the :ref:`User Guide <voting_classifier>`.

Parameters ---------- estimators : list of (str, estimator) tuples Invoking the ``fit`` method on the ``VotingClassifier`` will fit clones of those original estimators that will be stored in the class attribute ``self.estimators_``. An estimator can be set to ``'drop'`` using ``set_params``.

.. deprecated:: 0.22 Using ``None`` to drop an estimator is deprecated in 0.22 and support will be dropped in 0.24. Use the string ``'drop'`` instead.

voting : str, 'hard', 'soft' (default='hard') If 'hard', uses predicted class labels for majority rule voting. Else if 'soft', predicts the class label based on the argmax of the sums of the predicted probabilities, which is recommended for an ensemble of well-calibrated classifiers.

weights : array-like, shape (n_classifiers,), optional (default=`None`) Sequence of weights (`float` or `int`) to weight the occurrences of predicted class labels (`hard` voting) or class probabilities before averaging (`soft` voting). Uses uniform weights if `None`.

n_jobs : int or None, optional (default=None) The number of jobs to run in parallel for ``fit``. ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context. ``-1`` means using all processors. See :term:`Glossary <n_jobs>` for more details.

flatten_transform : bool, optional (default=True) Affects shape of transform output only when voting='soft' If voting='soft' and flatten_transform=True, transform method returns matrix with shape (n_samples, n_classifiers * n_classes). If flatten_transform=False, it returns (n_classifiers, n_samples, n_classes).

Attributes ---------- estimators_ : list of classifiers The collection of fitted sub-estimators as defined in ``estimators`` that are not 'drop'.

named_estimators_ : Bunch object, a dictionary with attribute access Attribute to access any fitted sub-estimators by name.

.. versionadded:: 0.20

classes_ : array-like, shape (n_predictions,) The classes labels.

See Also -------- VotingRegressor: Prediction voting regressor.

Examples -------- >>> import numpy as np >>> from sklearn.linear_model import LogisticRegression >>> from sklearn.naive_bayes import GaussianNB >>> from sklearn.ensemble import RandomForestClassifier, VotingClassifier >>> clf1 = LogisticRegression(multi_class='multinomial', random_state=1) >>> clf2 = RandomForestClassifier(n_estimators=50, random_state=1) >>> clf3 = GaussianNB() >>> X = np.array([-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]) >>> y = np.array(1, 1, 1, 2, 2, 2) >>> eclf1 = VotingClassifier(estimators= ... ('lr', clf1), ('rf', clf2), ('gnb', clf3), voting='hard') >>> eclf1 = eclf1.fit(X, y) >>> print(eclf1.predict(X)) 1 1 1 2 2 2 >>> np.array_equal(eclf1.named_estimators_.lr.predict(X), ... eclf1.named_estimators_'lr'.predict(X)) True >>> eclf2 = VotingClassifier(estimators= ... ('lr', clf1), ('rf', clf2), ('gnb', clf3), ... voting='soft') >>> eclf2 = eclf2.fit(X, y) >>> print(eclf2.predict(X)) 1 1 1 2 2 2 >>> eclf3 = VotingClassifier(estimators= ... ('lr', clf1), ('rf', clf2), ('gnb', clf3), ... voting='soft', weights=2,1,1, ... flatten_transform=True) >>> eclf3 = eclf3.fit(X, y) >>> print(eclf3.predict(X)) 1 1 1 2 2 2 >>> print(eclf3.transform(X).shape) (6, 6)

val fit : ?sample_weight:[> `ArrayLike ] Np.Obj.t -> x:[> `ArrayLike ] Np.Obj.t -> y:[> `ArrayLike ] Np.Obj.t -> [> tag ] Obj.t -> t

Fit the estimators.

Parameters ---------- X : array-like, sparse matrix, shape (n_samples, n_features) Training vectors, where n_samples is the number of samples and n_features is the number of features.

y : array-like, shape (n_samples,) Target values.

sample_weight : array-like, shape (n_samples,) or None Sample weights. If None, then samples are equally weighted. Note that this is supported only if all underlying estimators support sample weights.

Returns ------- self : object

val fit_transform : ?y:[> `ArrayLike ] Np.Obj.t -> ?fit_params:(string * Py.Object.t) list -> x:[> `ArrayLike ] Np.Obj.t -> [> tag ] Obj.t -> [> `ArrayLike ] Np.Obj.t

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Parameters ---------- X : numpy array of shape n_samples, n_features Training set.

y : numpy array of shape n_samples Target values.

**fit_params : dict Additional fit parameters.

Returns ------- X_new : numpy array of shape n_samples, n_features_new Transformed array.

val get_params : ?deep:bool -> [> tag ] Obj.t -> Py.Object.t

Get the parameters of an estimator from the ensemble.

Parameters ---------- deep : bool Setting it to True gets the various classifiers and the parameters of the classifiers as well.

val predict : x:[> `ArrayLike ] Np.Obj.t -> [> tag ] Obj.t -> [> `ArrayLike ] Np.Obj.t

Predict class labels for X.

Parameters ---------- X : array-like, sparse matrix, shape (n_samples, n_features) The input samples.

Returns ------- maj : array-like, shape (n_samples,) Predicted class labels.

val score : ?sample_weight:[> `ArrayLike ] Np.Obj.t -> x:[> `ArrayLike ] Np.Obj.t -> y:[> `ArrayLike ] Np.Obj.t -> [> tag ] Obj.t -> float

Return the mean accuracy on the given test data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Parameters ---------- X : array-like of shape (n_samples, n_features) Test samples.

y : array-like of shape (n_samples,) or (n_samples, n_outputs) True labels for X.

sample_weight : array-like of shape (n_samples,), default=None Sample weights.

Returns ------- score : float Mean accuracy of self.predict(X) wrt. y.

val set_params : ?params:(string * Py.Object.t) list -> [> tag ] Obj.t -> t

Set the parameters of an estimator from the ensemble.

Valid parameter keys can be listed with `get_params()`.

Parameters ---------- **params : keyword arguments Specific parameters using e.g. `set_params(parameter_name=new_value)`. In addition, to setting the parameters of the stacking estimator, the individual estimator of the stacking estimators can also be set, or can be removed by setting them to 'drop'.

val transform : x:[> `ArrayLike ] Np.Obj.t -> [> tag ] Obj.t -> [> `ArrayLike ] Np.Obj.t

Return class labels or probabilities for X for each estimator.

Parameters ---------- X : array-like, sparse matrix, shape (n_samples, n_features) Training vectors, where n_samples is the number of samples and n_features is the number of features.

Returns ------- probabilities_or_labels If `voting='soft'` and `flatten_transform=True`: returns array-like of shape (n_classifiers, n_samples * n_classes), being class probabilities calculated by each classifier. If `voting='soft' and `flatten_transform=False`: array-like of shape (n_classifiers, n_samples, n_classes) If `voting='hard'`: array-like of shape (n_samples, n_classifiers), being class labels predicted by each classifier.

val estimators_ : t -> Py.Object.t

Attribute estimators_: get value or raise Not_found if None.

val estimators_opt : t -> Py.Object.t option

Attribute estimators_: get value as an option.

val named_estimators_ : t -> Dict.t

Attribute named_estimators_: get value or raise Not_found if None.

val named_estimators_opt : t -> Dict.t option

Attribute named_estimators_: get value as an option.

val classes_ : t -> [> `ArrayLike ] Np.Obj.t

Attribute classes_: get value or raise Not_found if None.

val classes_opt : t -> [> `ArrayLike ] Np.Obj.t option

Attribute classes_: get value as an option.

val to_string : t -> string

Print the object to a human-readable representation.

val show : t -> string

Print the object to a human-readable representation.

val pp : Format.formatter -> t -> unit

Pretty-print the object to a formatter.

OCaml

Innovation. Community. Security.