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 LinearClassifierMixin : sig ... end
module LinearDiscriminantAnalysis : sig ... end
module QuadraticDiscriminantAnalysis : sig ... end
module StandardScaler : sig ... end
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_classification_targets : [> `ArrayLike ] Np.Obj.t -> Py.Object.t

Ensure that target y is of a non-regression type.

Only the following target types (as defined in type_of_target) are allowed: 'binary', 'multiclass', 'multiclass-multioutput', 'multilabel-indicator', 'multilabel-sequences'

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

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 empirical_covariance : ?assume_centered:bool -> x:[> `ArrayLike ] Np.Obj.t -> unit -> [> `ArrayLike ] Np.Obj.t

Computes the Maximum likelihood covariance estimator

Parameters ---------- X : ndarray of shape (n_samples, n_features) Data from which to compute the covariance estimate

assume_centered : bool, default=False If True, data will not be centered before computation. Useful when working with data whose mean is almost, but not exactly zero. If False, data will be centered before computation.

Returns ------- covariance : ndarray of shape (n_features, n_features) Empirical covariance (Maximum Likelihood Estimator).

Examples -------- >>> from sklearn.covariance import empirical_covariance >>> X = [1,1,1],[1,1,1],[1,1,1], ... [0,0,0],[0,0,0],[0,0,0] >>> empirical_covariance(X) array([0.25, 0.25, 0.25], [0.25, 0.25, 0.25], [0.25, 0.25, 0.25])

val expit : ?out:Py.Object.t -> ?where:Py.Object.t -> x:[> `ArrayLike ] Np.Obj.t -> unit -> [> `ArrayLike ] Np.Obj.t

expit(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True, signature, extobj)

expit(x)

Expit (a.k.a. logistic sigmoid) ufunc for ndarrays.

The expit function, also known as the logistic sigmoid function, is defined as ``expit(x) = 1/(1+exp(-x))``. It is the inverse of the logit function.

Parameters ---------- x : ndarray The ndarray to apply expit to element-wise.

Returns ------- out : ndarray An ndarray of the same shape as x. Its entries are `expit` of the corresponding entry of x.

See Also -------- logit

Notes ----- As a ufunc expit takes a number of optional keyword arguments. For more information see `ufuncs <https://docs.scipy.org/doc/numpy/reference/ufuncs.html>`_

.. versionadded:: 0.10.0

Examples -------- >>> from scipy.special import expit, logit

>>> expit(-np.inf, -1.5, 0, 1.5, np.inf) array( 0. , 0.18242552, 0.5 , 0.81757448, 1. )

`logit` is the inverse of `expit`:

>>> logit(expit(-2.5, 0, 3.1, 5.0)) array(-2.5, 0. , 3.1, 5. )

Plot expit(x) for x in -6, 6:

>>> import matplotlib.pyplot as plt >>> x = np.linspace(-6, 6, 121) >>> y = expit(x) >>> plt.plot(x, y) >>> plt.grid() >>> plt.xlim(-6, 6) >>> plt.xlabel('x') >>> plt.title('expit(x)') >>> plt.show()

val ledoit_wolf : ?assume_centered:bool -> ?block_size:int -> x:[> `ArrayLike ] Np.Obj.t -> unit -> [> `ArrayLike ] Np.Obj.t * float

Estimates the shrunk Ledoit-Wolf covariance matrix.

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

Parameters ---------- X : array-like of shape (n_samples, n_features) Data from which to compute the covariance estimate

assume_centered : bool, default=False If True, data will not be centered before computation. Useful to work with data whose mean is significantly equal to zero but is not exactly zero. If False, data will be centered before computation.

block_size : int, default=1000 Size of the blocks into which the covariance matrix will be split. This is purely a memory optimization and does not affect results.

Returns ------- shrunk_cov : ndarray of shape (n_features, n_features) Shrunk covariance.

shrinkage : float Coefficient in the convex combination used for the computation of the shrunk estimate.

Notes ----- The regularized (shrunk) covariance is:

(1 - shrinkage) * cov + shrinkage * mu * np.identity(n_features)

where mu = trace(cov) / n_features

val shrunk_covariance : ?shrinkage:float -> emp_cov:[> `ArrayLike ] Np.Obj.t -> unit -> [> `ArrayLike ] Np.Obj.t

Calculates a covariance matrix shrunk on the diagonal

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

Parameters ---------- emp_cov : array-like of shape (n_features, n_features) Covariance matrix to be shrunk

shrinkage : float, default=0.1 Coefficient in the convex combination used for the computation of the shrunk estimate. Range is 0, 1.

Returns ------- shrunk_cov : ndarray of shape (n_features, n_features) Shrunk covariance.

Notes ----- The regularized (shrunk) covariance is given by:

(1 - shrinkage) * cov + shrinkage * mu * np.identity(n_features)

where mu = trace(cov) / n_features

val softmax : ?copy:bool -> x:[> `ArrayLike ] Np.Obj.t -> unit -> [> `ArrayLike ] Np.Obj.t

Calculate the softmax function.

The softmax function is calculated by np.exp(X) / np.sum(np.exp(X), axis=1)

This will cause overflow when large values are exponentiated. Hence the largest value in each row is subtracted from each data point to prevent this.

Parameters ---------- X : array-like of floats, shape (M, N) Argument to the logistic function

copy : bool, optional Copy X or not.

Returns ------- out : array, shape (M, N) Softmax function evaluated at every point in x

val unique_labels : Py.Object.t list -> [> `ArrayLike ] Np.Obj.t

Extract an ordered array of unique labels

We don't allow:

  • mix of multilabel and multiclass (single label) targets
  • mix of label indicator matrix and anything else, because there are no explicit labels)
  • mix of label indicator matrices of different sizes
  • mix of string and integer labels

At the moment, we also don't allow 'multiclass-multioutput' input type.

Parameters ---------- *ys : array-likes

Returns ------- out : numpy array of shape n_unique_labels An ordered array of unique labels.

Examples -------- >>> from sklearn.utils.multiclass import unique_labels >>> unique_labels(3, 5, 5, 5, 7, 7) array(3, 5, 7) >>> unique_labels(1, 2, 3, 4, 2, 2, 3, 4) array(1, 2, 3, 4) >>> unique_labels(1, 2, 10, 5, 11) array( 1, 2, 5, 10, 11)

OCaml

Innovation. Community. Security.