package sklearn

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
val get_py : string -> Py.Object.t

Get an attribute of this module as a Py.Object.t. This is useful to pass a Python function to another function.

module BaseDiscreteNB : sig ... end
module BaseNB : sig ... end
module BernoulliNB : sig ... end
module CategoricalNB : sig ... end
module ComplementNB : sig ... end
module GaussianNB : sig ... end
module MultinomialNB : sig ... end
val abstractmethod : Py.Object.t -> Py.Object.t

A decorator indicating abstract methods.

Requires that the metaclass is ABCMeta or derived from it. A class that has a metaclass derived from ABCMeta cannot be instantiated unless all of its abstract methods are overridden. The abstract methods can be called using any of the normal 'super' call mechanisms. abstractmethod() may be used to declare abstract methods for properties and descriptors.

Usage:

class C(metaclass=ABCMeta): @abstractmethod def my_abstract_method(self, ...): ...

val binarize : ?threshold:float -> ?copy:bool -> x:[> `ArrayLike ] Np.Obj.t -> unit -> Py.Object.t

Boolean thresholding of array-like or scipy.sparse matrix

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

Parameters ---------- X : array-like, sparse matrix, shape n_samples, n_features The data to binarize, element by element. scipy.sparse matrices should be in CSR or CSC format to avoid an un-necessary copy.

threshold : float, optional (0.0 by default) Feature values below or equal to this are replaced by 0, above it by 1. Threshold may not be less than 0 for operations on sparse matrices.

copy : boolean, optional, default True set to False to perform inplace binarization and avoid a copy (if the input is already a numpy array or a scipy.sparse CSR / CSC matrix and if axis is 1).

See also -------- Binarizer: Performs binarization using the ``Transformer`` API (e.g. as part of a preprocessing :class:`sklearn.pipeline.Pipeline`).

val check_X_y : ?accept_sparse:[ `S of string | `StringList of string list | `Bool of bool ] -> ?accept_large_sparse:bool -> ?dtype: [ `S of string | `Dtype of Np.Dtype.t | `Dtypes of Np.Dtype.t list | `None ] -> ?order:[ `C | `F ] -> ?copy:bool -> ?force_all_finite:[ `Allow_nan | `Bool of bool ] -> ?ensure_2d:bool -> ?allow_nd:bool -> ?multi_output:bool -> ?ensure_min_samples:int -> ?ensure_min_features:int -> ?y_numeric:bool -> ?estimator:[> `BaseEstimator ] Np.Obj.t -> x:[> `ArrayLike ] Np.Obj.t -> y:[> `ArrayLike ] Np.Obj.t -> unit -> Py.Object.t * Py.Object.t

Input validation for standard estimators.

Checks X and y for consistent length, enforces X to be 2D and y 1D. By default, X is checked to be non-empty and containing only finite values. Standard input checks are also applied to y, such as checking that y does not have np.nan or np.inf targets. For multi-label y, set multi_output=True to allow 2D and sparse y. If the dtype of X is object, attempt converting to float, raising on failure.

Parameters ---------- X : nd-array, list or sparse matrix Input data.

y : nd-array, list or sparse matrix Labels.

accept_sparse : string, boolean or list of string (default=False) Strings representing allowed sparse matrix formats, such as 'csc', 'csr', etc. If the input is sparse but not in the allowed format, it will be converted to the first listed format. True allows the input to be any format. False means that a sparse matrix input will raise an error.

accept_large_sparse : bool (default=True) If a CSR, CSC, COO or BSR sparse matrix is supplied and accepted by accept_sparse, accept_large_sparse will cause it to be accepted only if its indices are stored with a 32-bit dtype.

.. versionadded:: 0.20

dtype : string, type, list of types or None (default='numeric') Data type of result. If None, the dtype of the input is preserved. If 'numeric', dtype is preserved unless array.dtype is object. If dtype is a list of types, conversion on the first type is only performed if the dtype of the input is not in the list.

order : 'F', 'C' or None (default=None) Whether an array will be forced to be fortran or c-style.

copy : boolean (default=False) Whether a forced copy will be triggered. If copy=False, a copy might be triggered by a conversion.

force_all_finite : boolean or 'allow-nan', (default=True) Whether to raise an error on np.inf, np.nan, pd.NA in X. This parameter does not influence whether y can have np.inf, np.nan, pd.NA values. The possibilities are:

  • True: Force all values of X to be finite.
  • False: accepts np.inf, np.nan, pd.NA in X.
  • 'allow-nan': accepts only np.nan or pd.NA values in X. Values cannot be infinite.

.. versionadded:: 0.20 ``force_all_finite`` accepts the string ``'allow-nan'``.

.. versionchanged:: 0.23 Accepts `pd.NA` and converts it into `np.nan`

ensure_2d : boolean (default=True) Whether to raise a value error if X is not 2D.

allow_nd : boolean (default=False) Whether to allow X.ndim > 2.

multi_output : boolean (default=False) Whether to allow 2D y (array or sparse matrix). If false, y will be validated as a vector. y cannot have np.nan or np.inf values if multi_output=True.

ensure_min_samples : int (default=1) Make sure that X has a minimum number of samples in its first axis (rows for a 2D array).

ensure_min_features : int (default=1) Make sure that the 2D array has some minimum number of features (columns). The default value of 1 rejects empty datasets. This check is only enforced when X has effectively 2 dimensions or is originally 1D and ``ensure_2d`` is True. Setting to 0 disables this check.

y_numeric : boolean (default=False) Whether to ensure that y has a numeric type. If dtype of y is object, it is converted to float64. Should only be used for regression algorithms.

estimator : str or estimator instance (default=None) If passed, include the name of the estimator in warning messages.

Returns ------- X_converted : object The converted and validated X.

y_converted : object The converted and validated y.

val check_array : ?accept_sparse:[ `S of string | `StringList of string list | `Bool of bool ] -> ?accept_large_sparse:bool -> ?dtype: [ `S of string | `Dtype of Np.Dtype.t | `Dtypes of Np.Dtype.t list | `None ] -> ?order:[ `C | `F ] -> ?copy:bool -> ?force_all_finite:[ `Allow_nan | `Bool of bool ] -> ?ensure_2d:bool -> ?allow_nd:bool -> ?ensure_min_samples:int -> ?ensure_min_features:int -> ?estimator:[> `BaseEstimator ] Np.Obj.t -> array:Py.Object.t -> unit -> Py.Object.t

Input validation on an array, list, sparse matrix or similar.

By default, the input is checked to be a non-empty 2D array containing only finite values. If the dtype of the array is object, attempt converting to float, raising on failure.

Parameters ---------- array : object Input object to check / convert.

accept_sparse : string, boolean or list/tuple of strings (default=False) Strings representing allowed sparse matrix formats, such as 'csc', 'csr', etc. If the input is sparse but not in the allowed format, it will be converted to the first listed format. True allows the input to be any format. False means that a sparse matrix input will raise an error.

accept_large_sparse : bool (default=True) If a CSR, CSC, COO or BSR sparse matrix is supplied and accepted by accept_sparse, accept_large_sparse=False will cause it to be accepted only if its indices are stored with a 32-bit dtype.

.. versionadded:: 0.20

dtype : string, type, list of types or None (default='numeric') Data type of result. If None, the dtype of the input is preserved. If 'numeric', dtype is preserved unless array.dtype is object. If dtype is a list of types, conversion on the first type is only performed if the dtype of the input is not in the list.

order : 'F', 'C' or None (default=None) Whether an array will be forced to be fortran or c-style. When order is None (default), then if copy=False, nothing is ensured about the memory layout of the output array; otherwise (copy=True) the memory layout of the returned array is kept as close as possible to the original array.

copy : boolean (default=False) Whether a forced copy will be triggered. If copy=False, a copy might be triggered by a conversion.

force_all_finite : boolean or 'allow-nan', (default=True) Whether to raise an error on np.inf, np.nan, pd.NA in array. The possibilities are:

  • True: Force all values of array to be finite.
  • False: accepts np.inf, np.nan, pd.NA in array.
  • 'allow-nan': accepts only np.nan and pd.NA values in array. Values cannot be infinite.

.. versionadded:: 0.20 ``force_all_finite`` accepts the string ``'allow-nan'``.

.. versionchanged:: 0.23 Accepts `pd.NA` and converts it into `np.nan`

ensure_2d : boolean (default=True) Whether to raise a value error if array is not 2D.

allow_nd : boolean (default=False) Whether to allow array.ndim > 2.

ensure_min_samples : int (default=1) Make sure that the array has a minimum number of samples in its first axis (rows for a 2D array). Setting to 0 disables this check.

ensure_min_features : int (default=1) Make sure that the 2D array has some minimum number of features (columns). The default value of 1 rejects empty datasets. This check is only enforced when the input data has effectively 2 dimensions or is originally 1D and ``ensure_2d`` is True. Setting to 0 disables this check.

estimator : str or estimator instance (default=None) If passed, include the name of the estimator in warning messages.

Returns ------- array_converted : object The converted and validated array.

val check_is_fitted : ?attributes: [ `S of string | `StringList of string list | `Arr of [> `ArrayLike ] Np.Obj.t ] -> ?msg:string -> ?all_or_any:[ `Callable of Py.Object.t | `PyObject of Py.Object.t ] -> estimator:[> `BaseEstimator ] Np.Obj.t -> unit -> Py.Object.t

Perform is_fitted validation for estimator.

Checks if the estimator is fitted by verifying the presence of fitted attributes (ending with a trailing underscore) and otherwise raises a NotFittedError with the given message.

This utility is meant to be used internally by estimators themselves, typically in their own predict / transform methods.

Parameters ---------- estimator : estimator instance. estimator instance for which the check is performed.

attributes : str, list or tuple of str, default=None Attribute name(s) given as string or a list/tuple of strings Eg.: ``'coef_', 'estimator_', ..., 'coef_'``

If `None`, `estimator` is considered fitted if there exist an attribute that ends with a underscore and does not start with double underscore.

msg : string The default error message is, 'This %(name)s instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.'

For custom messages if '%(name)s' is present in the message string, it is substituted for the estimator name.

Eg. : 'Estimator, %(name)s, must be fitted before sparsifying'.

all_or_any : callable, all, any, default all Specify whether all or any of the given attributes must exist.

Returns ------- None

Raises ------ NotFittedError If the attributes are not found.

val check_non_negative : x:[> `ArrayLike ] Np.Obj.t -> whom:string -> unit -> Py.Object.t

Check if there is any negative value in an array.

Parameters ---------- X : array-like or sparse matrix Input data.

whom : string Who passed X to this function.

val column_or_1d : ?warn:bool -> y:[> `ArrayLike ] Np.Obj.t -> unit -> [> `ArrayLike ] Np.Obj.t

Ravel column or 1d numpy array, else raises an error

Parameters ---------- y : array-like

warn : boolean, default False To control display of warnings.

Returns ------- y : array

val label_binarize : ?neg_label:int -> ?pos_label:int -> ?sparse_output:bool -> y:[> `ArrayLike ] Np.Obj.t -> classes:[> `ArrayLike ] Np.Obj.t -> unit -> [> `ArrayLike ] Np.Obj.t

Binarize labels in a one-vs-all fashion

Several regression and binary classification algorithms are available in scikit-learn. A simple way to extend these algorithms to the multi-class classification case is to use the so-called one-vs-all scheme.

This function makes it possible to compute this transformation for a fixed set of class labels known ahead of time.

Parameters ---------- y : array-like Sequence of integer labels or multilabel data to encode.

classes : array-like of shape n_classes Uniquely holds the label for each class.

neg_label : int (default: 0) Value with which negative labels must be encoded.

pos_label : int (default: 1) Value with which positive labels must be encoded.

sparse_output : boolean (default: False), Set to true if output binary array is desired in CSR sparse format

Returns ------- Y : numpy array or CSR matrix of shape n_samples, n_classes Shape will be n_samples, 1 for binary problems.

Examples -------- >>> from sklearn.preprocessing import label_binarize >>> label_binarize(1, 6, classes=1, 2, 4, 6) array([1, 0, 0, 0], [0, 0, 0, 1])

The class ordering is preserved:

>>> label_binarize(1, 6, classes=1, 6, 4, 2) array([1, 0, 0, 0], [0, 1, 0, 0])

Binary targets transform to a column vector

>>> label_binarize('yes', 'no', 'no', 'yes', classes='no', 'yes') array([1], [0], [0], [1])

See also -------- LabelBinarizer : class used to wrap the functionality of label_binarize and allow for fitting to classes independently of the transform operation

val logsumexp : ?axis:int list -> ?b:[> `ArrayLike ] Np.Obj.t -> ?keepdims:bool -> ?return_sign:bool -> a:[> `ArrayLike ] Np.Obj.t -> unit -> [> `ArrayLike ] Np.Obj.t * [> `ArrayLike ] Np.Obj.t

Compute the log of the sum of exponentials of input elements.

Parameters ---------- a : array_like Input array. axis : None or int or tuple of ints, optional Axis or axes over which the sum is taken. By default `axis` is None, and all elements are summed.

.. versionadded:: 0.11.0 keepdims : bool, optional If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original array.

.. versionadded:: 0.15.0 b : array-like, optional Scaling factor for exp(`a`) must be of the same shape as `a` or broadcastable to `a`. These values may be negative in order to implement subtraction.

.. versionadded:: 0.12.0 return_sign : bool, optional If this is set to True, the result will be a pair containing sign information; if False, results that are negative will be returned as NaN. Default is False (no sign information).

.. versionadded:: 0.16.0

Returns ------- res : ndarray The result, ``np.log(np.sum(np.exp(a)))`` calculated in a numerically more stable way. If `b` is given then ``np.log(np.sum(b*np.exp(a)))`` is returned. sgn : ndarray If return_sign is True, this will be an array of floating-point numbers matching res and +1, 0, or -1 depending on the sign of the result. If False, only one result is returned.

See Also -------- numpy.logaddexp, numpy.logaddexp2

Notes ----- NumPy has a logaddexp function which is very similar to `logsumexp`, but only handles two arguments. `logaddexp.reduce` is similar to this function, but may be less stable.

Examples -------- >>> from scipy.special import logsumexp >>> a = np.arange(10) >>> np.log(np.sum(np.exp(a))) 9.4586297444267107 >>> logsumexp(a) 9.4586297444267107

With weights

>>> a = np.arange(10) >>> b = np.arange(10, 0, -1) >>> logsumexp(a, b=b) 9.9170178533034665 >>> np.log(np.sum(b*np.exp(a))) 9.9170178533034647

Returning a sign flag

>>> logsumexp(1,2,b=1,-1,return_sign=True) (1.5413248546129181, -1.0)

Notice that `logsumexp` does not directly support masked arrays. To use it on a masked array, convert the mask into zero weights:

>>> a = np.ma.array(np.log(2), 2, np.log(3), ... mask=False, True, False) >>> b = (~a.mask).astype(int) >>> logsumexp(a.data, b=b), np.log(5) 1.6094379124341005, 1.6094379124341005

val safe_sparse_dot : ?dense_output:Py.Object.t -> a:[> `ArrayLike ] Np.Obj.t -> b:Py.Object.t -> unit -> [> `ArrayLike ] Np.Obj.t

Dot product that handle the sparse matrix case correctly

Parameters ---------- a : array or sparse matrix b : array or sparse matrix dense_output : boolean, (default=False) When False, ``a`` and ``b`` both being sparse will yield sparse output. When True, output will always be a dense array.

Returns ------- dot_product : array or sparse matrix sparse if ``a`` and ``b`` are sparse and ``dense_output=False``.

OCaml

Innovation. Community. Security.