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 CalibratedClassifierCV : sig ... end
module IsotonicRegression : sig ... end
module LabelBinarizer : sig ... end
module LabelEncoder : sig ... end
module LinearSVC : sig ... end
val calibration_curve : ?normalize:bool -> ?n_bins:int -> ?strategy:[ `Uniform | `Quantile ] -> y_true:[> `ArrayLike ] Np.Obj.t -> y_prob:[> `ArrayLike ] Np.Obj.t -> unit -> [> `ArrayLike ] Np.Obj.t * [> `ArrayLike ] Np.Obj.t

Compute true and predicted probabilities for a calibration curve.

The method assumes the inputs come from a binary classifier, and discretize the 0, 1 interval into bins.

Calibration curves may also be referred to as reliability diagrams.

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

Parameters ---------- y_true : array-like of shape (n_samples,) True targets.

y_prob : array-like of shape (n_samples,) Probabilities of the positive class.

normalize : bool, default=False Whether y_prob needs to be normalized into the 0, 1 interval, i.e. is not a proper probability. If True, the smallest value in y_prob is linearly mapped onto 0 and the largest one onto 1.

n_bins : int, default=5 Number of bins to discretize the 0, 1 interval. A bigger number requires more data. Bins with no samples (i.e. without corresponding values in `y_prob`) will not be returned, thus the returned arrays may have less than `n_bins` values.

strategy : 'uniform', 'quantile', default='uniform' Strategy used to define the widths of the bins.

uniform The bins have identical widths. quantile The bins have the same number of samples and depend on `y_prob`.

Returns ------- prob_true : ndarray of shape (n_bins,) or smaller The proportion of samples whose class is the positive class, in each bin (fraction of positives).

prob_pred : ndarray of shape (n_bins,) or smaller The mean predicted probability in each bin.

References ---------- Alexandru Niculescu-Mizil and Rich Caruana (2005) Predicting Good Probabilities With Supervised Learning, in Proceedings of the 22nd International Conference on Machine Learning (ICML). See section 4 (Qualitative Analysis of Predictions).

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_consistent_length : Py.Object.t list -> Py.Object.t

Check that all arrays have consistent first dimensions.

Checks whether all objects in arrays have the same shape or length.

Parameters ---------- *arrays : list or tuple of input objects. Objects that will be checked for consistent length.

val check_cv : ?cv: [ `BaseCrossValidator of [> `BaseCrossValidator ] Np.Obj.t | `Arr of [> `ArrayLike ] Np.Obj.t | `I of int ] -> ?y:[> `ArrayLike ] Np.Obj.t -> ?classifier:bool -> unit -> [ `BaseCrossValidator | `Object ] Np.Obj.t

Input checker utility for building a cross-validator

Parameters ---------- cv : int, cross-validation generator or an iterable, default=None Determines the cross-validation splitting strategy. Possible inputs for cv are:

  • None, to use the default 5-fold cross validation,
  • integer, to specify the number of folds.
  • :term:`CV splitter`,
  • An iterable yielding (train, test) splits as arrays of indices.

For integer/None inputs, if classifier is True and ``y`` is either binary or multiclass, :class:`StratifiedKFold` is used. In all other cases, :class:`KFold` is used.

Refer :ref:`User Guide <cross_validation>` for the various cross-validation strategies that can be used here.

.. versionchanged:: 0.22 ``cv`` default value changed from 3-fold to 5-fold.

y : array-like, default=None The target variable for supervised learning problems.

classifier : bool, default=False Whether the task is a classification task, in which case stratified KFold will be used.

Returns ------- checked_cv : a cross-validator instance. The return value is a cross-validator which generates the train/test splits via the ``split`` method.

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 clone : ?safe:bool -> estimator:[> `BaseEstimator ] Np.Obj.t -> unit -> Py.Object.t

Constructs a new estimator with the same parameters.

Clone does a deep copy of the model in an estimator without actually copying attached data. It yields a new estimator with the same parameters that has not been fit on any data.

Parameters ---------- estimator :

st, tuple, set

}

of estimator objects or estimator object The estimator or group of estimators to be cloned.

safe : bool, default=True If safe is false, clone will fall back to a deep copy on objects that are not estimators.

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 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 fmin_bfgs : ?fprime:Py.Object.t -> ?args:Py.Object.t -> ?gtol:float -> ?norm:float -> ?epsilon:[ `Arr of [> `ArrayLike ] Np.Obj.t | `I of int ] -> ?maxiter:int -> ?full_output:bool -> ?disp:bool -> ?retall:bool -> ?callback:Py.Object.t -> f:Py.Object.t -> x0:[> `ArrayLike ] Np.Obj.t -> unit -> [> `ArrayLike ] Np.Obj.t * float * [> `ArrayLike ] Np.Obj.t * [> `ArrayLike ] Np.Obj.t * int * int * int * [> `ArrayLike ] Np.Obj.t

Minimize a function using the BFGS algorithm.

Parameters ---------- f : callable f(x,*args) Objective function to be minimized. x0 : ndarray Initial guess. fprime : callable f'(x,*args), optional Gradient of f. args : tuple, optional Extra arguments passed to f and fprime. gtol : float, optional Gradient norm must be less than gtol before successful termination. norm : float, optional Order of norm (Inf is max, -Inf is min) epsilon : int or ndarray, optional If fprime is approximated, use this value for the step size. callback : callable, optional An optional user-supplied function to call after each iteration. Called as callback(xk), where xk is the current parameter vector. maxiter : int, optional Maximum number of iterations to perform. full_output : bool, optional If True,return fopt, func_calls, grad_calls, and warnflag in addition to xopt. disp : bool, optional Print convergence message if True. retall : bool, optional Return a list of results at each iteration if True.

Returns ------- xopt : ndarray Parameters which minimize f, i.e., f(xopt) == fopt. fopt : float Minimum value. gopt : ndarray Value of gradient at minimum, f'(xopt), which should be near 0. Bopt : ndarray Value of 1/f''(xopt), i.e., the inverse Hessian matrix. func_calls : int Number of function_calls made. grad_calls : int Number of gradient calls made. warnflag : integer 1 : Maximum number of iterations exceeded. 2 : Gradient and/or function calls not changing. 3 : NaN result encountered. allvecs : list The value of xopt at each iteration. Only returned if retall is True.

See also -------- minimize: Interface to minimization algorithms for multivariate functions. See the 'BFGS' `method` in particular.

Notes ----- Optimize the function, f, whose gradient is given by fprime using the quasi-Newton method of Broyden, Fletcher, Goldfarb, and Shanno (BFGS)

References ---------- Wright, and Nocedal 'Numerical Optimization', 1999, p. 198.

val indexable : Py.Object.t list -> Py.Object.t

Make arrays indexable for cross-validation.

Checks consistent length, passes through None, and ensures that everything can be indexed by converting sparse matrices to csr and converting non-interable objects to arrays.

Parameters ---------- *iterables : lists, dataframes, arrays, sparse matrices List of objects to ensure sliceability.

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 signature : ?follow_wrapped:Py.Object.t -> obj:Py.Object.t -> unit -> Py.Object.t

Get a signature object for the passed callable.

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

xlogy(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True, signature, extobj)

xlogy(x, y)

Compute ``x*log(y)`` so that the result is 0 if ``x = 0``.

Parameters ---------- x : array_like Multiplier y : array_like Argument

Returns ------- z : array_like Computed x*log(y)

Notes -----

.. versionadded:: 0.13.0