sklearn.isotonic.isotonic_regression
sklearn.isotonic.isotonic_regression(y, *, sample_weight=None, y_min=None, y_max=None, increasing=True) [source]

Solve the isotonic regression model. Read more in the User Guide.  Parameters

yarray-like of shape (n_samples,)

The data.
sample_weightarray-like of shape (n_samples,), default=None

Weights on each point of the regression. If None, weight is set to 1 (equal weights).
y_minfloat, default=None

Lower bound on the lowest predicted value (the minimum value may still be higher). If not set, defaults to -inf.
y_maxfloat, default=None

Upper bound on the highest predicted value (the maximum may still be lower). If not set, defaults to +inf.
increasingbool, default=True

Whether to compute y_ is increasing (if set to True) or decreasing (if set to False)    Returns

y_list of floats

Isotonic fit of y.     References “Active set algorithms for isotonic regression; A unifying framework” by Michael J. Best and Nilotpal Chakravarti, section 3.


sklearn.linear_model.enet_path
sklearn.linear_model.enet_path(X, y, *, l1_ratio=0.5, eps=0.001, n_alphas=100, alphas=None, precompute='auto', Xy=None, copy_X=True, coef_init=None, verbose=False, return_n_iter=False, positive=False, check_input=True, **params) [source]

Compute elastic net path with coordinate descent. The elastic net optimization function varies for mono and multi-outputs. For mono-output tasks it is: 1 / (2 * n_samples) * ||y - Xw||^2_2
+ alpha * l1_ratio * ||w||_1
+ 0.5 * alpha * (1 - l1_ratio) * ||w||^2_2
 For multi-output tasks it is: (1 / (2 * n_samples)) * ||Y - XW||^Fro_2
+ alpha * l1_ratio * ||W||_21
+ 0.5 * alpha * (1 - l1_ratio) * ||W||_Fro^2
 Where: ||W||_21 = \sum_i \sqrt{\sum_j w_{ij}^2}
 i.e. the sum of norm of each row. Read more in the User Guide.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

Training data. Pass directly as Fortran-contiguous data to avoid unnecessary memory duplication. If y is mono-output then X can be sparse.
y{array-like, sparse matrix} of shape (n_samples,) or (n_samples, n_outputs)

Target values.
l1_ratiofloat, default=0.5

Number between 0 and 1 passed to elastic net (scaling between l1 and l2 penalties). l1_ratio=1 corresponds to the Lasso.
epsfloat, default=1e-3

Length of the path. eps=1e-3 means that alpha_min / alpha_max = 1e-3.
n_alphasint, default=100

Number of alphas along the regularization path.
alphasndarray, default=None

List of alphas where to compute the models. If None alphas are set automatically.
precompute‘auto’, bool or array-like of shape (n_features, n_features), default=’auto’

Whether to use a precomputed Gram matrix to speed up calculations. If set to 'auto' let us decide. The Gram matrix can also be passed as argument.
Xyarray-like of shape (n_features,) or (n_features, n_outputs), default=None

Xy = np.dot(X.T, y) that can be precomputed. It is useful only when the Gram matrix is precomputed.
copy_Xbool, default=True

If True, X will be copied; else, it may be overwritten.
coef_initndarray of shape (n_features, ), default=None

The initial values of the coefficients.
verbosebool or int, default=False

Amount of verbosity.
return_n_iterbool, default=False

Whether to return the number of iterations or not.
positivebool, default=False

If set to True, forces coefficients to be positive. (Only allowed when y.ndim == 1).
check_inputbool, default=True

If set to False, the input validation checks are skipped (including the Gram matrix when provided). It is assumed that they are handled by the caller.
**paramskwargs

Keyword arguments passed to the coordinate descent solver.    Returns

alphasndarray of shape (n_alphas,)

The alphas along the path where models are computed.
coefsndarray of shape (n_features, n_alphas) or (n_outputs, n_features, n_alphas)

Coefficients along the path.
dual_gapsndarray of shape (n_alphas,)

The dual gaps at the end of the optimization for each alpha.
n_iterslist of int

The number of iterations taken by the coordinate descent optimizer to reach the specified tolerance for each alpha. (Is returned when return_n_iter is set to True).      See also
MultiTaskElasticNet

MultiTaskElasticNetCV

ElasticNet

ElasticNetCV

  Notes For an example, see examples/linear_model/plot_lasso_coordinate_descent_path.py.
 Examples using sklearn.linear_model.enet_path

  Lasso and Elastic Net


sklearn.linear_model.lars_path
sklearn.linear_model.lars_path(X, y, Xy=None, *, Gram=None, max_iter=500, alpha_min=0, method='lar', copy_X=True, eps=2.220446049250313e-16, copy_Gram=True, verbose=0, return_path=True, return_n_iter=False, positive=False) [source]

Compute Least Angle Regression or Lasso path using LARS algorithm [1] The optimization objective for the case method=’lasso’ is: (1 / (2 * n_samples)) * ||y - Xw||^2_2 + alpha * ||w||_1
 in the case of method=’lars’, the objective function is only known in the form of an implicit equation (see discussion in [1]) Read more in the User Guide.  Parameters

XNone or array-like of shape (n_samples, n_features)

Input data. Note that if X is None then the Gram matrix must be specified, i.e., cannot be None or False.
yNone or array-like of shape (n_samples,)

Input targets.
Xyarray-like of shape (n_samples,) or (n_samples, n_targets), default=None

Xy = np.dot(X.T, y) that can be precomputed. It is useful only when the Gram matrix is precomputed.
GramNone, ‘auto’, array-like of shape (n_features, n_features), default=None

Precomputed Gram matrix (X’ * X), if 'auto', the Gram matrix is precomputed from the given X, if there are more samples than features.
max_iterint, default=500

Maximum number of iterations to perform, set to infinity for no limit.
alpha_minfloat, default=0

Minimum correlation along the path. It corresponds to the regularization parameter alpha parameter in the Lasso.
method{‘lar’, ‘lasso’}, default=’lar’

Specifies the returned model. Select 'lar' for Least Angle Regression, 'lasso' for the Lasso.
copy_Xbool, default=True

If False, X is overwritten.
epsfloat, default=np.finfo(float).eps

The machine-precision regularization in the computation of the Cholesky diagonal factors. Increase this for very ill-conditioned systems. Unlike the tol parameter in some iterative optimization-based algorithms, this parameter does not control the tolerance of the optimization.
copy_Grambool, default=True

If False, Gram is overwritten.
verboseint, default=0

Controls output verbosity.
return_pathbool, default=True

If return_path==True returns the entire path, else returns only the last point of the path.
return_n_iterbool, default=False

Whether to return the number of iterations.
positivebool, default=False

Restrict coefficients to be >= 0. This option is only allowed with method ‘lasso’. Note that the model coefficients will not converge to the ordinary-least-squares solution for small values of alpha. Only coefficients up to the smallest alpha value (alphas_[alphas_ > 0.].min() when fit_path=True) reached by the stepwise Lars-Lasso algorithm are typically in congruence with the solution of the coordinate descent lasso_path function.    Returns

alphasarray-like of shape (n_alphas + 1,)

Maximum of covariances (in absolute value) at each iteration. n_alphas is either max_iter, n_features or the number of nodes in the path with alpha >= alpha_min, whichever is smaller.
activearray-like of shape (n_alphas,)

Indices of active variables at the end of the path.
coefsarray-like of shape (n_features, n_alphas + 1)

Coefficients along the path
n_iterint

Number of iterations run. Returned only if return_n_iter is set to True.      See also
lars_path_gram

lasso_path

lasso_path_gram

LassoLars

Lars

LassoLarsCV

LarsCV

sklearn.decomposition.sparse_encode

  References
1
“Least Angle Regression”, Efron et al. http://statweb.stanford.edu/~tibs/ftp/lars.pdf
2
Wikipedia entry on the Least-angle regression
3
Wikipedia entry on the Lasso
 Examples using sklearn.linear_model.lars_path

  Lasso path using LARS


sklearn.linear_model.lars_path_gram
sklearn.linear_model.lars_path_gram(Xy, Gram, *, n_samples, max_iter=500, alpha_min=0, method='lar', copy_X=True, eps=2.220446049250313e-16, copy_Gram=True, verbose=0, return_path=True, return_n_iter=False, positive=False) [source]

lars_path in the sufficient stats mode [1] The optimization objective for the case method=’lasso’ is: (1 / (2 * n_samples)) * ||y - Xw||^2_2 + alpha * ||w||_1
 in the case of method=’lars’, the objective function is only known in the form of an implicit equation (see discussion in [1]) Read more in the User Guide.  Parameters

Xyarray-like of shape (n_samples,) or (n_samples, n_targets)

Xy = np.dot(X.T, y).
Gramarray-like of shape (n_features, n_features)

Gram = np.dot(X.T * X).
n_samplesint or float

Equivalent size of sample.
max_iterint, default=500

Maximum number of iterations to perform, set to infinity for no limit.
alpha_minfloat, default=0

Minimum correlation along the path. It corresponds to the regularization parameter alpha parameter in the Lasso.
method{‘lar’, ‘lasso’}, default=’lar’

Specifies the returned model. Select 'lar' for Least Angle Regression, 'lasso' for the Lasso.
copy_Xbool, default=True

If False, X is overwritten.
epsfloat, default=np.finfo(float).eps

The machine-precision regularization in the computation of the Cholesky diagonal factors. Increase this for very ill-conditioned systems. Unlike the tol parameter in some iterative optimization-based algorithms, this parameter does not control the tolerance of the optimization.
copy_Grambool, default=True

If False, Gram is overwritten.
verboseint, default=0

Controls output verbosity.
return_pathbool, default=True

If return_path==True returns the entire path, else returns only the last point of the path.
return_n_iterbool, default=False

Whether to return the number of iterations.
positivebool, default=False

Restrict coefficients to be >= 0. This option is only allowed with method ‘lasso’. Note that the model coefficients will not converge to the ordinary-least-squares solution for small values of alpha. Only coefficients up to the smallest alpha value (alphas_[alphas_ > 0.].min() when fit_path=True) reached by the stepwise Lars-Lasso algorithm are typically in congruence with the solution of the coordinate descent lasso_path function.    Returns

alphasarray-like of shape (n_alphas + 1,)

Maximum of covariances (in absolute value) at each iteration. n_alphas is either max_iter, n_features or the number of nodes in the path with alpha >= alpha_min, whichever is smaller.
activearray-like of shape (n_alphas,)

Indices of active variables at the end of the path.
coefsarray-like of shape (n_features, n_alphas + 1)

Coefficients along the path
n_iterint

Number of iterations run. Returned only if return_n_iter is set to True.      See also
lars_path

lasso_path

lasso_path_gram

LassoLars

Lars

LassoLarsCV

LarsCV

sklearn.decomposition.sparse_encode

  References
1
“Least Angle Regression”, Efron et al. http://statweb.stanford.edu/~tibs/ftp/lars.pdf
2
Wikipedia entry on the Least-angle regression
3
Wikipedia entry on the Lasso


sklearn.linear_model.lasso_path
sklearn.linear_model.lasso_path(X, y, *, eps=0.001, n_alphas=100, alphas=None, precompute='auto', Xy=None, copy_X=True, coef_init=None, verbose=False, return_n_iter=False, positive=False, **params) [source]

Compute Lasso path with coordinate descent The Lasso optimization function varies for mono and multi-outputs. For mono-output tasks it is: (1 / (2 * n_samples)) * ||y - Xw||^2_2 + alpha * ||w||_1
 For multi-output tasks it is: (1 / (2 * n_samples)) * ||Y - XW||^2_Fro + alpha * ||W||_21
 Where: ||W||_21 = \sum_i \sqrt{\sum_j w_{ij}^2}
 i.e. the sum of norm of each row. Read more in the User Guide.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

Training data. Pass directly as Fortran-contiguous data to avoid unnecessary memory duplication. If y is mono-output then X can be sparse.
y{array-like, sparse matrix} of shape (n_samples,) or (n_samples, n_outputs)

Target values
epsfloat, default=1e-3

Length of the path. eps=1e-3 means that alpha_min / alpha_max = 1e-3
n_alphasint, default=100

Number of alphas along the regularization path
alphasndarray, default=None

List of alphas where to compute the models. If None alphas are set automatically
precompute‘auto’, bool or array-like of shape (n_features, n_features), default=’auto’

Whether to use a precomputed Gram matrix to speed up calculations. If set to 'auto' let us decide. The Gram matrix can also be passed as argument.
Xyarray-like of shape (n_features,) or (n_features, n_outputs), default=None

Xy = np.dot(X.T, y) that can be precomputed. It is useful only when the Gram matrix is precomputed.
copy_Xbool, default=True

If True, X will be copied; else, it may be overwritten.
coef_initndarray of shape (n_features, ), default=None

The initial values of the coefficients.
verbosebool or int, default=False

Amount of verbosity.
return_n_iterbool, default=False

whether to return the number of iterations or not.
positivebool, default=False

If set to True, forces coefficients to be positive. (Only allowed when y.ndim == 1).
**paramskwargs

keyword arguments passed to the coordinate descent solver.    Returns

alphasndarray of shape (n_alphas,)

The alphas along the path where models are computed.
coefsndarray of shape (n_features, n_alphas) or (n_outputs, n_features, n_alphas)

Coefficients along the path.
dual_gapsndarray of shape (n_alphas,)

The dual gaps at the end of the optimization for each alpha.
n_iterslist of int

The number of iterations taken by the coordinate descent optimizer to reach the specified tolerance for each alpha.      See also
lars_path

Lasso

LassoLars

LassoCV

LassoLarsCV

sklearn.decomposition.sparse_encode

  Notes For an example, see examples/linear_model/plot_lasso_coordinate_descent_path.py. To avoid unnecessary memory duplication the X argument of the fit method should be directly passed as a Fortran-contiguous numpy array. Note that in certain cases, the Lars solver may be significantly faster to implement this functionality. In particular, linear interpolation can be used to retrieve model coefficients between the values output by lars_path Examples Comparing lasso_path and lars_path with interpolation: >>> X = np.array([[1, 2, 3.1], [2.3, 5.4, 4.3]]).T
>>> y = np.array([1, 2, 3.1])
>>> # Use lasso_path to compute a coefficient path
>>> _, coef_path, _ = lasso_path(X, y, alphas=[5., 1., .5])
>>> print(coef_path)
[[0.         0.         0.46874778]
 [0.2159048  0.4425765  0.23689075]]
 >>> # Now use lars_path and 1D linear interpolation to compute the
>>> # same path
>>> from sklearn.linear_model import lars_path
>>> alphas, active, coef_path_lars = lars_path(X, y, method='lasso')
>>> from scipy import interpolate
>>> coef_path_continuous = interpolate.interp1d(alphas[::-1],
...                                             coef_path_lars[:, ::-1])
>>> print(coef_path_continuous([5., 1., .5]))
[[0.         0.         0.46915237]
 [0.2159048  0.4425765  0.23668876]]

 Examples using sklearn.linear_model.lasso_path

  Lasso and Elastic Net


sklearn.linear_model.orthogonal_mp
sklearn.linear_model.orthogonal_mp(X, y, *, n_nonzero_coefs=None, tol=None, precompute=False, copy_X=True, return_path=False, return_n_iter=False) [source]

Orthogonal Matching Pursuit (OMP). Solves n_targets Orthogonal Matching Pursuit problems. An instance of the problem has the form: When parametrized by the number of non-zero coefficients using n_nonzero_coefs: argmin ||y - Xgamma||^2 subject to ||gamma||_0 <= n_{nonzero coefs} When parametrized by error using the parameter tol: argmin ||gamma||_0 subject to ||y - Xgamma||^2 <= tol Read more in the User Guide.  Parameters

Xndarray of shape (n_samples, n_features)

Input data. Columns are assumed to have unit norm.
yndarray of shape (n_samples,) or (n_samples, n_targets)

Input targets.
n_nonzero_coefsint, default=None

Desired number of non-zero entries in the solution. If None (by default) this value is set to 10% of n_features.
tolfloat, default=None

Maximum norm of the residual. If not None, overrides n_nonzero_coefs.
precompute‘auto’ or bool, default=False

Whether to perform precomputations. Improves performance when n_targets or n_samples is very large.
copy_Xbool, default=True

Whether the design matrix X must be copied by the algorithm. A false value is only helpful if X is already Fortran-ordered, otherwise a copy is made anyway.
return_pathbool, default=False

Whether to return every value of the nonzero coefficients along the forward path. Useful for cross-validation.
return_n_iterbool, default=False

Whether or not to return the number of iterations.    Returns

coefndarray of shape (n_features,) or (n_features, n_targets)

Coefficients of the OMP solution. If return_path=True, this contains the whole coefficient path. In this case its shape is (n_features, n_features) or (n_features, n_targets, n_features) and iterating over the last axis yields coefficients in increasing order of active features.
n_itersarray-like or int

Number of active features across every target. Returned only if return_n_iter is set to True.      See also
OrthogonalMatchingPursuit

orthogonal_mp_gram

lars_path

sklearn.decomposition.sparse_encode

  Notes Orthogonal matching pursuit was introduced in S. Mallat, Z. Zhang, Matching pursuits with time-frequency dictionaries, IEEE Transactions on Signal Processing, Vol. 41, No. 12. (December 1993), pp. 3397-3415. (http://blanche.polytechnique.fr/~mallat/papiers/MallatPursuit93.pdf) This implementation is based on Rubinstein, R., Zibulevsky, M. and Elad, M., Efficient Implementation of the K-SVD Algorithm using Batch Orthogonal Matching Pursuit Technical Report - CS Technion, April 2008. https://www.cs.technion.ac.il/~ronrubin/Publications/KSVD-OMP-v2.pdf


sklearn.linear_model.orthogonal_mp_gram
sklearn.linear_model.orthogonal_mp_gram(Gram, Xy, *, n_nonzero_coefs=None, tol=None, norms_squared=None, copy_Gram=True, copy_Xy=True, return_path=False, return_n_iter=False) [source]

Gram Orthogonal Matching Pursuit (OMP). Solves n_targets Orthogonal Matching Pursuit problems using only the Gram matrix X.T * X and the product X.T * y. Read more in the User Guide.  Parameters

Gramndarray of shape (n_features, n_features)

Gram matrix of the input data: X.T * X.
Xyndarray of shape (n_features,) or (n_features, n_targets)

Input targets multiplied by X: X.T * y.
n_nonzero_coefsint, default=None

Desired number of non-zero entries in the solution. If None (by default) this value is set to 10% of n_features.
tolfloat, default=None

Maximum norm of the residual. If not None, overrides n_nonzero_coefs.
norms_squaredarray-like of shape (n_targets,), default=None

Squared L2 norms of the lines of y. Required if tol is not None.
copy_Grambool, default=True

Whether the gram matrix must be copied by the algorithm. A false value is only helpful if it is already Fortran-ordered, otherwise a copy is made anyway.
copy_Xybool, default=True

Whether the covariance vector Xy must be copied by the algorithm. If False, it may be overwritten.
return_pathbool, default=False

Whether to return every value of the nonzero coefficients along the forward path. Useful for cross-validation.
return_n_iterbool, default=False

Whether or not to return the number of iterations.    Returns

coefndarray of shape (n_features,) or (n_features, n_targets)

Coefficients of the OMP solution. If return_path=True, this contains the whole coefficient path. In this case its shape is (n_features, n_features) or (n_features, n_targets, n_features) and iterating over the last axis yields coefficients in increasing order of active features.
n_itersarray-like or int

Number of active features across every target. Returned only if return_n_iter is set to True.      See also
OrthogonalMatchingPursuit

orthogonal_mp

lars_path

sklearn.decomposition.sparse_encode

  Notes Orthogonal matching pursuit was introduced in G. Mallat, Z. Zhang, Matching pursuits with time-frequency dictionaries, IEEE Transactions on Signal Processing, Vol. 41, No. 12. (December 1993), pp. 3397-3415. (http://blanche.polytechnique.fr/~mallat/papiers/MallatPursuit93.pdf) This implementation is based on Rubinstein, R., Zibulevsky, M. and Elad, M., Efficient Implementation of the K-SVD Algorithm using Batch Orthogonal Matching Pursuit Technical Report - CS Technion, April 2008. https://www.cs.technion.ac.il/~ronrubin/Publications/KSVD-OMP-v2.pdf


sklearn.linear_model.PassiveAggressiveRegressor
sklearn.linear_model.PassiveAggressiveRegressor(*, C=1.0, fit_intercept=True, max_iter=1000, tol=0.001, early_stopping=False, validation_fraction=0.1, n_iter_no_change=5, shuffle=True, verbose=0, loss='epsilon_insensitive', epsilon=0.1, random_state=None, warm_start=False, average=False) [source]

Passive Aggressive Regressor Read more in the User Guide.  Parameters

Cfloat, default=1.0

Maximum step size (regularization). Defaults to 1.0.
fit_interceptbool, default=True

Whether the intercept should be estimated or not. If False, the data is assumed to be already centered. Defaults to True.
max_iterint, default=1000

The maximum number of passes over the training data (aka epochs). It only impacts the behavior in the fit method, and not the partial_fit method.  New in version 0.19.
tolfloat or None, default=1e-3

The stopping criterion. If it is not None, the iterations will stop when (loss > previous_loss - tol).  New in version 0.19.
early_stoppingbool, default=False

Whether to use early stopping to terminate training when validation. score is not improving. If set to True, it will automatically set aside a fraction of training data as validation and terminate training when validation score is not improving by at least tol for n_iter_no_change consecutive epochs.  New in version 0.20.
validation_fractionfloat, default=0.1

The proportion of training data to set aside as validation set for early stopping. Must be between 0 and 1. Only used if early_stopping is True.  New in version 0.20.
n_iter_no_changeint, default=5

Number of iterations with no improvement to wait before early stopping.  New in version 0.20.
shufflebool, default=True

Whether or not the training data should be shuffled after each epoch.
verboseinteger, default=0

The verbosity level
lossstring, default=”epsilon_insensitive”

The loss function to be used: epsilon_insensitive: equivalent to PA-I in the reference paper. squared_epsilon_insensitive: equivalent to PA-II in the reference paper.
epsilonfloat, default=DEFAULT_EPSILON

If the difference between the current prediction and the correct label is below this threshold, the model is not updated.
random_stateint, RandomState instance, default=None

Used to shuffle the training data, when shuffle is set to True. Pass an int for reproducible output across multiple function calls. See Glossary.
warm_startbool, default=False

When set to True, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. See the Glossary. Repeatedly calling fit or partial_fit when warm_start is True can result in a different solution than when calling fit a single time because of the way the data is shuffled.
averagebool or int, default=False

When set to True, computes the averaged SGD weights and stores the result in the coef_ attribute. If set to an int greater than 1, averaging will begin once the total number of samples seen reaches average. So average=10 will begin averaging after seeing 10 samples.  New in version 0.19: parameter average to use weights averaging in SGD     Attributes

coef_array, shape = [1, n_features] if n_classes == 2 else [n_classes, n_features]

Weights assigned to the features.
intercept_array, shape = [1] if n_classes == 2 else [n_classes]

Constants in decision function.
n_iter_int

The actual number of iterations to reach the stopping criterion.
t_int

Number of weight updates performed during training. Same as (n_iter_ * n_samples).      See also
SGDRegressor

  References Online Passive-Aggressive Algorithms <http://jmlr.csail.mit.edu/papers/volume7/crammer06a/crammer06a.pdf> K. Crammer, O. Dekel, J. Keshat, S. Shalev-Shwartz, Y. Singer - JMLR (2006) Examples >>> from sklearn.linear_model import PassiveAggressiveRegressor
>>> from sklearn.datasets import make_regression
 >>> X, y = make_regression(n_features=4, random_state=0)
>>> regr = PassiveAggressiveRegressor(max_iter=100, random_state=0,
... tol=1e-3)
>>> regr.fit(X, y)
PassiveAggressiveRegressor(max_iter=100, random_state=0)
>>> print(regr.coef_)
[20.48736655 34.18818427 67.59122734 87.94731329]
>>> print(regr.intercept_)
[-0.02306214]
>>> print(regr.predict([[0, 0, 0, 0]]))
[-0.02306214]


sklearn.linear_model.ridge_regression
sklearn.linear_model.ridge_regression(X, y, alpha, *, sample_weight=None, solver='auto', max_iter=None, tol=0.001, verbose=0, random_state=None, return_n_iter=False, return_intercept=False, check_input=True) [source]

Solve the ridge equation by the method of normal equations. Read more in the User Guide.  Parameters

X{ndarray, sparse matrix, LinearOperator} of shape (n_samples, n_features)

Training data
yndarray of shape (n_samples,) or (n_samples, n_targets)

Target values
alphafloat or array-like of shape (n_targets,)

Regularization strength; must be a positive float. Regularization improves the conditioning of the problem and reduces the variance of the estimates. Larger values specify stronger regularization. Alpha corresponds to 1 / (2C) in other linear models such as LogisticRegression or LinearSVC. If an array is passed, penalties are assumed to be specific to the targets. Hence they must correspond in number.
sample_weightfloat or array-like of shape (n_samples,), default=None

Individual weights for each sample. If given a float, every sample will have the same weight. If sample_weight is not None and solver=’auto’, the solver will be set to ‘cholesky’.  New in version 0.17.
solver{‘auto’, ‘svd’, ‘cholesky’, ‘lsqr’, ‘sparse_cg’, ‘sag’, ‘saga’}, default=’auto’

Solver to use in the computational routines:  ‘auto’ chooses the solver automatically based on the type of data. ‘svd’ uses a Singular Value Decomposition of X to compute the Ridge coefficients. More stable for singular matrices than ‘cholesky’. ‘cholesky’ uses the standard scipy.linalg.solve function to obtain a closed-form solution via a Cholesky decomposition of dot(X.T, X) ‘sparse_cg’ uses the conjugate gradient solver as found in scipy.sparse.linalg.cg. As an iterative algorithm, this solver is more appropriate than ‘cholesky’ for large-scale data (possibility to set tol and max_iter). ‘lsqr’ uses the dedicated regularized least-squares routine scipy.sparse.linalg.lsqr. It is the fastest and uses an iterative procedure. ‘sag’ uses a Stochastic Average Gradient descent, and ‘saga’ uses its improved, unbiased version named SAGA. Both methods also use an iterative procedure, and are often faster than other solvers when both n_samples and n_features are large. Note that ‘sag’ and ‘saga’ fast convergence is only guaranteed on features with approximately the same scale. You can preprocess the data with a scaler from sklearn.preprocessing.  All last five solvers support both dense and sparse data. However, only ‘sag’ and ‘sparse_cg’ supports sparse input when fit_intercept is True.  New in version 0.17: Stochastic Average Gradient descent solver.   New in version 0.19: SAGA solver.
max_iterint, default=None

Maximum number of iterations for conjugate gradient solver. For the ‘sparse_cg’ and ‘lsqr’ solvers, the default value is determined by scipy.sparse.linalg. For ‘sag’ and saga solver, the default value is 1000.
tolfloat, default=1e-3

Precision of the solution.
verboseint, default=0

Verbosity level. Setting verbose > 0 will display additional information depending on the solver used.
random_stateint, RandomState instance, default=None

Used when solver == ‘sag’ or ‘saga’ to shuffle the data. See Glossary for details.
return_n_iterbool, default=False

If True, the method also returns n_iter, the actual number of iteration performed by the solver.  New in version 0.17.
return_interceptbool, default=False

If True and if X is sparse, the method also returns the intercept, and the solver is automatically changed to ‘sag’. This is only a temporary fix for fitting the intercept with sparse data. For dense data, use sklearn.linear_model._preprocess_data before your regression.  New in version 0.17.
check_inputbool, default=True

If False, the input arrays X and y will not be checked.  New in version 0.21.     Returns

coefndarray of shape (n_features,) or (n_targets, n_features)

Weight vector(s).
n_iterint, optional

The actual number of iteration performed by the solver. Only returned if return_n_iter is True.
interceptfloat or ndarray of shape (n_targets,)

The intercept of the model. Only returned if return_intercept is True and if X is a scipy sparse array.     Notes This function won’t compute the intercept.


sklearn.manifold.locally_linear_embedding
sklearn.manifold.locally_linear_embedding(X, *, n_neighbors, n_components, reg=0.001, eigen_solver='auto', tol=1e-06, max_iter=100, method='standard', hessian_tol=0.0001, modified_tol=1e-12, random_state=None, n_jobs=None) [source]

Perform a Locally Linear Embedding analysis on the data. Read more in the User Guide.  Parameters

X{array-like, NearestNeighbors}

Sample data, shape = (n_samples, n_features), in the form of a numpy array or a NearestNeighbors object.
n_neighborsint

number of neighbors to consider for each point.
n_componentsint

number of coordinates for the manifold.
regfloat, default=1e-3

regularization constant, multiplies the trace of the local covariance matrix of the distances.
eigen_solver{‘auto’, ‘arpack’, ‘dense’}, default=’auto’

auto : algorithm will attempt to choose the best method for input data
arpackuse arnoldi iteration in shift-invert mode.

For this method, M may be a dense matrix, sparse matrix, or general linear operator. Warning: ARPACK can be unstable for some problems. It is best to try several random seeds in order to check results.
denseuse standard dense matrix operations for the eigenvalue

decomposition. For this method, M must be an array or matrix type. This method should be avoided for large problems.
tolfloat, default=1e-6

Tolerance for ‘arpack’ method Not used if eigen_solver==’dense’.
max_iterint, default=100

maximum number of iterations for the arpack solver.
method{‘standard’, ‘hessian’, ‘modified’, ‘ltsa’}, default=’standard’

standarduse the standard locally linear embedding algorithm.

see reference [1]
hessianuse the Hessian eigenmap method. This method requires

n_neighbors > n_components * (1 + (n_components + 1) / 2. see reference [2]
modifieduse the modified locally linear embedding algorithm.

see reference [3]
ltsause local tangent space alignment algorithm

see reference [4]
hessian_tolfloat, default=1e-4

Tolerance for Hessian eigenmapping method. Only used if method == ‘hessian’
modified_tolfloat, default=1e-12

Tolerance for modified LLE method. Only used if method == ‘modified’
random_stateint, RandomState instance, default=None

Determines the random number generator when solver == ‘arpack’. Pass an int for reproducible results across multiple function calls. See :term: Glossary <random_state>.
n_jobsint or None, default=None

The number of parallel jobs to run for neighbors search. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.    Returns

Yarray-like, shape [n_samples, n_components]

Embedding vectors.
squared_errorfloat

Reconstruction error for the embedding vectors. Equivalent to norm(Y - W Y, 'fro')**2, where W are the reconstruction weights.     References
1
Roweis, S. & Saul, L. Nonlinear dimensionality reduction by locally linear embedding. Science 290:2323 (2000).
2
Donoho, D. & Grimes, C. Hessian eigenmaps: Locally linear embedding techniques for high-dimensional data. Proc Natl Acad Sci U S A. 100:5591 (2003).
3
Zhang, Z. & Wang, J. MLLE: Modified Locally Linear Embedding Using Multiple Weights. http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.70.382
4
Zhang, Z. & Zha, H. Principal manifolds and nonlinear dimensionality reduction via tangent space alignment. Journal of Shanghai Univ. 8:406 (2004)
 Examples using sklearn.manifold.locally_linear_embedding

  Swiss Roll reduction with LLE


sklearn.manifold.smacof
sklearn.manifold.smacof(dissimilarities, *, metric=True, n_components=2, init=None, n_init=8, n_jobs=None, max_iter=300, verbose=0, eps=0.001, random_state=None, return_n_iter=False) [source]

Computes multidimensional scaling using the SMACOF algorithm. The SMACOF (Scaling by MAjorizing a COmplicated Function) algorithm is a multidimensional scaling algorithm which minimizes an objective function (the stress) using a majorization technique. Stress majorization, also known as the Guttman Transform, guarantees a monotone convergence of stress, and is more powerful than traditional techniques such as gradient descent. The SMACOF algorithm for metric MDS can summarized by the following steps:  Set an initial start configuration, randomly or not. Compute the stress Compute the Guttman Transform Iterate 2 and 3 until convergence.  The nonmetric algorithm adds a monotonic regression step before computing the stress.  Parameters

dissimilaritiesndarray of shape (n_samples, n_samples)

Pairwise dissimilarities between the points. Must be symmetric.
metricbool, default=True

Compute metric or nonmetric SMACOF algorithm.
n_componentsint, default=2

Number of dimensions in which to immerse the dissimilarities. If an init array is provided, this option is overridden and the shape of init is used to determine the dimensionality of the embedding space.
initndarray of shape (n_samples, n_components), default=None

Starting configuration of the embedding to initialize the algorithm. By default, the algorithm is initialized with a randomly chosen array.
n_initint, default=8

Number of times the SMACOF algorithm will be run with different initializations. The final results will be the best output of the runs, determined by the run with the smallest final stress. If init is provided, this option is overridden and a single run is performed.
n_jobsint, default=None

The number of jobs to use for the computation. If multiple initializations are used (n_init), each run of the algorithm is computed in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
max_iterint, default=300

Maximum number of iterations of the SMACOF algorithm for a single run.
verboseint, default=0

Level of verbosity.
epsfloat, default=1e-3

Relative tolerance with respect to stress at which to declare convergence.
random_stateint, RandomState instance or None, default=None

Determines the random number generator used to initialize the centers. Pass an int for reproducible results across multiple function calls. See :term: Glossary <random_state>.
return_n_iterbool, default=False

Whether or not to return the number of iterations.    Returns

Xndarray of shape (n_samples, n_components)

Coordinates of the points in a n_components-space.
stressfloat

The final value of the stress (sum of squared distance of the disparities and the distances for all constrained points).
n_iterint

The number of iterations corresponding to the best stress. Returned only if return_n_iter is set to True.     Notes “Modern Multidimensional Scaling - Theory and Applications” Borg, I.; Groenen P. Springer Series in Statistics (1997) “Nonmetric multidimensional scaling: a numerical method” Kruskal, J. Psychometrika, 29 (1964) “Multidimensional scaling by optimizing goodness of fit to a nonmetric hypothesis” Kruskal, J. Psychometrika, 29, (1964)


sklearn.manifold.spectral_embedding
sklearn.manifold.spectral_embedding(adjacency, *, n_components=8, eigen_solver=None, random_state=None, eigen_tol=0.0, norm_laplacian=True, drop_first=True) [source]

Project the sample on the first eigenvectors of the graph Laplacian. The adjacency matrix is used to compute a normalized graph Laplacian whose spectrum (especially the eigenvectors associated to the smallest eigenvalues) has an interpretation in terms of minimal number of cuts necessary to split the graph into comparably sized components. This embedding can also ‘work’ even if the adjacency variable is not strictly the adjacency matrix of a graph but more generally an affinity or similarity matrix between samples (for instance the heat kernel of a euclidean distance matrix or a k-NN matrix). However care must taken to always make the affinity matrix symmetric so that the eigenvector decomposition works as expected. Note : Laplacian Eigenmaps is the actual algorithm implemented here. Read more in the User Guide.  Parameters

adjacency{array-like, sparse graph} of shape (n_samples, n_samples)

The adjacency matrix of the graph to embed.
n_componentsint, default=8

The dimension of the projection subspace.
eigen_solver{‘arpack’, ‘lobpcg’, ‘amg’}, default=None

The eigenvalue decomposition strategy to use. AMG requires pyamg to be installed. It can be faster on very large, sparse problems, but may also lead to instabilities. If None, then 'arpack' is used.
random_stateint, RandomState instance or None, default=None

Determines the random number generator used for the initialization of the lobpcg eigenvectors decomposition when solver == ‘amg’. Pass an int for reproducible results across multiple function calls. See :term: Glossary <random_state>.
eigen_tolfloat, default=0.0

Stopping criterion for eigendecomposition of the Laplacian matrix when using arpack eigen_solver.
norm_laplacianbool, default=True

If True, then compute normalized Laplacian.
drop_firstbool, default=True

Whether to drop the first eigenvector. For spectral embedding, this should be True as the first eigenvector should be constant vector for connected graph, but for spectral clustering, this should be kept as False to retain the first eigenvector.    Returns

embeddingndarray of shape (n_samples, n_components)

The reduced samples.     Notes Spectral Embedding (Laplacian Eigenmaps) is most useful when the graph has one connected component. If there graph has many components, the first few eigenvectors will simply uncover the connected components of the graph. References  https://en.wikipedia.org/wiki/LOBPCG Toward the Optimal Preconditioned Eigensolver: Locally Optimal Block Preconditioned Conjugate Gradient Method Andrew V. Knyazev https://doi.org/10.1137%2FS1064827500366124


sklearn.manifold.trustworthiness
sklearn.manifold.trustworthiness(X, X_embedded, *, n_neighbors=5, metric='euclidean') [source]

Expresses to what extent the local structure is retained. The trustworthiness is within [0, 1]. It is defined as  \[T(k) = 1 - \frac{2}{nk (2n - 3k - 1)} \sum^n_{i=1} \sum_{j \in \mathcal{N}_{i}^{k}} \max(0, (r(i, j) - k))\] where for each sample i, \(\mathcal{N}_{i}^{k}\) are its k nearest neighbors in the output space, and every sample j is its \(r(i, j)\)-th nearest neighbor in the input space. In other words, any unexpected nearest neighbors in the output space are penalised in proportion to their rank in the input space.  “Neighborhood Preservation in Nonlinear Projection Methods: An Experimental Study” J. Venna, S. Kaski “Learning a Parametric Embedding by Preserving Local Structure” L.J.P. van der Maaten   Parameters

Xndarray of shape (n_samples, n_features) or (n_samples, n_samples)

If the metric is ‘precomputed’ X must be a square distance matrix. Otherwise it contains a sample per row.
X_embeddedndarray of shape (n_samples, n_components)

Embedding of the training data in low-dimensional space.
n_neighborsint, default=5

Number of neighbors k that will be considered.
metricstr or callable, default=’euclidean’

Which metric to use for computing pairwise distances between samples from the original input space. If metric is ‘precomputed’, X must be a matrix of pairwise distances or squared distances. Otherwise, see the documentation of argument metric in sklearn.pairwise.pairwise_distances for a list of available metrics.  New in version 0.20.     Returns

trustworthinessfloat

Trustworthiness of the low-dimensional embedding.


sklearn.metrics.accuracy_score
sklearn.metrics.accuracy_score(y_true, y_pred, *, normalize=True, sample_weight=None) [source]

Accuracy classification score. In multilabel classification, this function computes subset accuracy: the set of labels predicted for a sample must exactly match the corresponding set of labels in y_true. Read more in the User Guide.  Parameters

y_true1d array-like, or label indicator array / sparse matrix

Ground truth (correct) labels.
y_pred1d array-like, or label indicator array / sparse matrix

Predicted labels, as returned by a classifier.
normalizebool, default=True

If False, return the number of correctly classified samples. Otherwise, return the fraction of correctly classified samples.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

If normalize == True, return the fraction of correctly classified samples (float), else returns the number of correctly classified samples (int). The best performance is 1 with normalize == True and the number of samples with normalize == False.      See also
jaccard_score, hamming_loss, zero_one_loss

  Notes In binary and multiclass classification, this function is equal to the jaccard_score function. Examples >>> from sklearn.metrics import accuracy_score
>>> y_pred = [0, 2, 1, 3]
>>> y_true = [0, 1, 2, 3]
>>> accuracy_score(y_true, y_pred)
0.5
>>> accuracy_score(y_true, y_pred, normalize=False)
2
 In the multilabel case with binary label indicators: >>> import numpy as np
>>> accuracy_score(np.array([[0, 1], [1, 1]]), np.ones((2, 2)))
0.5

 Examples using sklearn.metrics.accuracy_score

  Plot classification probability

  Multi-class AdaBoosted Decision Trees

  Probabilistic predictions with Gaussian process classification (GPC)

  Demonstration of multi-metric evaluation on cross_val_score and GridSearchCV

  Importance of Feature Scaling

  Effect of varying threshold for self-training

  Classification of text documents using sparse features


sklearn.metrics.adjusted_mutual_info_score
sklearn.metrics.adjusted_mutual_info_score(labels_true, labels_pred, *, average_method='arithmetic') [source]

Adjusted Mutual Information between two clusterings. Adjusted Mutual Information (AMI) is an adjustment of the Mutual Information (MI) score to account for chance. It accounts for the fact that the MI is generally higher for two clusterings with a larger number of clusters, regardless of whether there is actually more information shared. For two clusterings \(U\) and \(V\), the AMI is given as: AMI(U, V) = [MI(U, V) - E(MI(U, V))] / [avg(H(U), H(V)) - E(MI(U, V))]
 This metric is independent of the absolute values of the labels: a permutation of the class or cluster label values won’t change the score value in any way. This metric is furthermore symmetric: switching label_true with label_pred will return the same score value. This can be useful to measure the agreement of two independent label assignments strategies on the same dataset when the real ground truth is not known. Be mindful that this function is an order of magnitude slower than other metrics, such as the Adjusted Rand Index. Read more in the User Guide.  Parameters

labels_trueint array, shape = [n_samples]

A clustering of the data into disjoint subsets.
labels_predint array-like of shape (n_samples,)

A clustering of the data into disjoint subsets.
average_methodstr, default=’arithmetic’

How to compute the normalizer in the denominator. Possible options are ‘min’, ‘geometric’, ‘arithmetic’, and ‘max’.  New in version 0.20.   Changed in version 0.22: The default value of average_method changed from ‘max’ to ‘arithmetic’.     Returns
 ami: float (upperlimited by 1.0)

The AMI returns a value of 1 when the two partitions are identical (ie perfectly matched). Random partitions (independent labellings) have an expected AMI around 0 on average hence can be negative.      See also
adjusted_rand_score

Adjusted Rand Index.
mutual_info_score

Mutual Information (not adjusted for chance).    References
1
Vinh, Epps, and Bailey, (2010). Information Theoretic Measures for Clusterings Comparison: Variants, Properties, Normalization and Correction for Chance, JMLR
2
Wikipedia entry for the Adjusted Mutual Information   Examples Perfect labelings are both homogeneous and complete, hence have score 1.0: >>> from sklearn.metrics.cluster import adjusted_mutual_info_score
>>> adjusted_mutual_info_score([0, 0, 1, 1], [0, 0, 1, 1])
...
1.0
>>> adjusted_mutual_info_score([0, 0, 1, 1], [1, 1, 0, 0])
...
1.0
 If classes members are completely split across different clusters, the assignment is totally in-complete, hence the AMI is null: >>> adjusted_mutual_info_score([0, 0, 0, 0], [0, 1, 2, 3])
...
0.0

 Examples using sklearn.metrics.adjusted_mutual_info_score

  Demo of affinity propagation clustering algorithm

  Demo of DBSCAN clustering algorithm

  Adjustment for chance in clustering performance evaluation

  A demo of K-Means clustering on the handwritten digits data


sklearn.metrics.adjusted_rand_score
sklearn.metrics.adjusted_rand_score(labels_true, labels_pred) [source]

Rand index adjusted for chance. The Rand Index computes a similarity measure between two clusterings by considering all pairs of samples and counting pairs that are assigned in the same or different clusters in the predicted and true clusterings. The raw RI score is then “adjusted for chance” into the ARI score using the following scheme: ARI = (RI - Expected_RI) / (max(RI) - Expected_RI)
 The adjusted Rand index is thus ensured to have a value close to 0.0 for random labeling independently of the number of clusters and samples and exactly 1.0 when the clusterings are identical (up to a permutation). ARI is a symmetric measure: adjusted_rand_score(a, b) == adjusted_rand_score(b, a)
 Read more in the User Guide.  Parameters

labels_trueint array, shape = [n_samples]

Ground truth class labels to be used as a reference
labels_predarray-like of shape (n_samples,)

Cluster labels to evaluate    Returns

ARIfloat

Similarity score between -1.0 and 1.0. Random labelings have an ARI close to 0.0. 1.0 stands for perfect match.      See also
adjusted_mutual_info_score

Adjusted Mutual Information.    References
Hubert1985
L. Hubert and P. Arabie, Comparing Partitions, Journal of Classification 1985 https://link.springer.com/article/10.1007%2FBF01908075
Steinley2004
D. Steinley, Properties of the Hubert-Arabie adjusted Rand index, Psychological Methods 2004
wk
https://en.wikipedia.org/wiki/Rand_index#Adjusted_Rand_index   Examples Perfectly matching labelings have a score of 1 even >>> from sklearn.metrics.cluster import adjusted_rand_score
>>> adjusted_rand_score([0, 0, 1, 1], [0, 0, 1, 1])
1.0
>>> adjusted_rand_score([0, 0, 1, 1], [1, 1, 0, 0])
1.0
 Labelings that assign all classes members to the same clusters are complete but may not always be pure, hence penalized: >>> adjusted_rand_score([0, 0, 1, 2], [0, 0, 1, 1])
0.57...
 ARI is symmetric, so labelings that have pure clusters with members coming from the same classes but unnecessary splits are penalized: >>> adjusted_rand_score([0, 0, 1, 1], [0, 0, 1, 2])
0.57...
 If classes members are completely split across different clusters, the assignment is totally incomplete, hence the ARI is very low: >>> adjusted_rand_score([0, 0, 0, 0], [0, 1, 2, 3])
0.0

 Examples using sklearn.metrics.adjusted_rand_score

  Demo of affinity propagation clustering algorithm

  Demo of DBSCAN clustering algorithm

  Adjustment for chance in clustering performance evaluation

  A demo of K-Means clustering on the handwritten digits data

  Clustering text documents using k-means


sklearn.metrics.auc
sklearn.metrics.auc(x, y) [source]

Compute Area Under the Curve (AUC) using the trapezoidal rule. This is a general function, given points on a curve. For computing the area under the ROC-curve, see roc_auc_score. For an alternative way to summarize a precision-recall curve, see average_precision_score.  Parameters

xndarray of shape (n,)

x coordinates. These must be either monotonic increasing or monotonic decreasing.
yndarray of shape, (n,)

y coordinates.    Returns

aucfloat
    See also
roc_auc_score

Compute the area under the ROC curve.
average_precision_score

Compute average precision from prediction scores.
precision_recall_curve

Compute precision-recall pairs for different probability thresholds.    Examples >>> import numpy as np
>>> from sklearn import metrics
>>> y = np.array([1, 1, 2, 2])
>>> pred = np.array([0.1, 0.4, 0.35, 0.8])
>>> fpr, tpr, thresholds = metrics.roc_curve(y, pred, pos_label=2)
>>> metrics.auc(fpr, tpr)
0.75

 Examples using sklearn.metrics.auc

  Species distribution modeling

  Poisson regression and non-normal loss

  Tweedie regression on insurance claims

  Receiver Operating Characteristic (ROC) with cross validation

  Receiver Operating Characteristic (ROC)

  Precision-Recall


sklearn.metrics.average_precision_score
sklearn.metrics.average_precision_score(y_true, y_score, *, average='macro', pos_label=1, sample_weight=None) [source]

Compute average precision (AP) from prediction scores. AP summarizes a precision-recall curve as the weighted mean of precisions achieved at each threshold, with the increase in recall from the previous threshold used as the weight:  \[\text{AP} = \sum_n (R_n - R_{n-1}) P_n\] where \(P_n\) and \(R_n\) are the precision and recall at the nth threshold [1]. This implementation is not interpolated and is different from computing the area under the precision-recall curve with the trapezoidal rule, which uses linear interpolation and can be too optimistic. Note: this implementation is restricted to the binary classification task or multilabel classification task. Read more in the User Guide.  Parameters

y_truendarray of shape (n_samples,) or (n_samples, n_classes)

True binary labels or binary label indicators.
y_scorendarray of shape (n_samples,) or (n_samples, n_classes)

Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by decision_function on some classifiers).
average{‘micro’, ‘samples’, ‘weighted’, ‘macro’} or None, default=’macro’

If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data:
'micro':

Calculate metrics globally by considering each element of the label indicator matrix as a label.
'macro':

Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.
'weighted':

Calculate metrics for each label, and find their average, weighted by support (the number of true instances for each label).
'samples':

Calculate metrics for each instance, and find their average.   Will be ignored when y_true is binary.
pos_labelint or str, default=1

The label of the positive class. Only applied to binary y_true. For multilabel-indicator y_true, pos_label is fixed to 1.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

average_precisionfloat
    See also
roc_auc_score

Compute the area under the ROC curve.
precision_recall_curve

Compute precision-recall pairs for different probability thresholds.    Notes  Changed in version 0.19: Instead of linearly interpolating between operating points, precisions are weighted by the change in recall since the last operating point.  References
1
Wikipedia entry for the Average precision   Examples >>> import numpy as np
>>> from sklearn.metrics import average_precision_score
>>> y_true = np.array([0, 0, 1, 1])
>>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
>>> average_precision_score(y_true, y_scores)
0.83...

 Examples using sklearn.metrics.average_precision_score

  Precision-Recall


sklearn.metrics.balanced_accuracy_score
sklearn.metrics.balanced_accuracy_score(y_true, y_pred, *, sample_weight=None, adjusted=False) [source]

Compute the balanced accuracy. The balanced accuracy in binary and multiclass classification problems to deal with imbalanced datasets. It is defined as the average of recall obtained on each class. The best value is 1 and the worst value is 0 when adjusted=False. Read more in the User Guide.  New in version 0.20.   Parameters

y_true1d array-like

Ground truth (correct) target values.
y_pred1d array-like

Estimated targets as returned by a classifier.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
adjustedbool, default=False

When true, the result is adjusted for chance, so that random performance would score 0, and perfect performance scores 1.    Returns

balanced_accuracyfloat
    See also
recall_score, roc_auc_score

  Notes Some literature promotes alternative definitions of balanced accuracy. Our definition is equivalent to accuracy_score with class-balanced sample weights, and shares desirable properties with the binary case. See the User Guide. References
1
Brodersen, K.H.; Ong, C.S.; Stephan, K.E.; Buhmann, J.M. (2010). The balanced accuracy and its posterior distribution. Proceedings of the 20th International Conference on Pattern Recognition, 3121-24.
2
John. D. Kelleher, Brian Mac Namee, Aoife D’Arcy, (2015). Fundamentals of Machine Learning for Predictive Data Analytics: Algorithms, Worked Examples, and Case Studies.   Examples >>> from sklearn.metrics import balanced_accuracy_score
>>> y_true = [0, 1, 0, 0, 1, 0]
>>> y_pred = [0, 1, 0, 0, 0, 1]
>>> balanced_accuracy_score(y_true, y_pred)
0.625


sklearn.metrics.brier_score_loss
sklearn.metrics.brier_score_loss(y_true, y_prob, *, sample_weight=None, pos_label=None) [source]

Compute the Brier score loss. The smaller the Brier score loss, the better, hence the naming with “loss”. The Brier score measures the mean squared difference between the predicted probability and the actual outcome. The Brier score always takes on a value between zero and one, since this is the largest possible difference between a predicted probability (which must be between zero and one) and the actual outcome (which can take on values of only 0 and 1). It can be decomposed is the sum of refinement loss and calibration loss. The Brier score is appropriate for binary and categorical outcomes that can be structured as true or false, but is inappropriate for ordinal variables which can take on three or more values (this is because the Brier score assumes that all possible outcomes are equivalently “distant” from one another). Which label is considered to be the positive label is controlled via the parameter pos_label, which defaults to the greater label unless y_true is all 0 or all -1, in which case pos_label defaults to 1. Read more in the User Guide.  Parameters

y_truearray of shape (n_samples,)

True targets.
y_probarray of shape (n_samples,)

Probabilities of the positive class.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
pos_labelint or str, default=None

Label of the positive class. pos_label will be infered in the following manner:  if y_true in {-1, 1} or {0, 1}, pos_label defaults to 1; else if y_true contains string, an error will be raised and pos_label should be explicitely specified; otherwise, pos_label defaults to the greater label, i.e. np.unique(y_true)[-1].     Returns

scorefloat

Brier score loss.     References
1
Wikipedia entry for the Brier score.   Examples >>> import numpy as np
>>> from sklearn.metrics import brier_score_loss
>>> y_true = np.array([0, 1, 1, 0])
>>> y_true_categorical = np.array(["spam", "ham", "ham", "spam"])
>>> y_prob = np.array([0.1, 0.9, 0.8, 0.3])
>>> brier_score_loss(y_true, y_prob)
0.037...
>>> brier_score_loss(y_true, 1-y_prob, pos_label=0)
0.037...
>>> brier_score_loss(y_true_categorical, y_prob, pos_label="ham")
0.037...
>>> brier_score_loss(y_true, np.array(y_prob) > 0.5)
0.0

 Examples using sklearn.metrics.brier_score_loss

  Probability Calibration curves

  Probability calibration of classifiers


sklearn.metrics.calinski_harabasz_score
sklearn.metrics.calinski_harabasz_score(X, labels) [source]

Compute the Calinski and Harabasz score. It is also known as the Variance Ratio Criterion. The score is defined as ratio between the within-cluster dispersion and the between-cluster dispersion. Read more in the User Guide.  Parameters

Xarray-like of shape (n_samples, n_features)

A list of n_features-dimensional data points. Each row corresponds to a single data point.
labelsarray-like of shape (n_samples,)

Predicted labels for each sample.    Returns

scorefloat

The resulting Calinski-Harabasz score.     References
1
T. Calinski and J. Harabasz, 1974. “A dendrite method for cluster analysis”. Communications in Statistics


sklearn.metrics.check_scoring
sklearn.metrics.check_scoring(estimator, scoring=None, *, allow_none=False) [source]

Determine scorer from user options. A TypeError will be thrown if the estimator cannot be scored.  Parameters

estimatorestimator object implementing ‘fit’

The object to use to fit the data.
scoringstr or callable, default=None

A string (see model evaluation documentation) or a scorer callable object / function with signature scorer(estimator, X, y).
allow_nonebool, default=False

If no scoring is specified and the estimator has no score function, we can either return None or raise an exception.    Returns

scoringcallable

A scorer callable object / function with signature scorer(estimator, X, y).


sklearn.metrics.classification_report
sklearn.metrics.classification_report(y_true, y_pred, *, labels=None, target_names=None, sample_weight=None, digits=2, output_dict=False, zero_division='warn') [source]

Build a text report showing the main classification metrics. Read more in the User Guide.  Parameters

y_true1d array-like, or label indicator array / sparse matrix

Ground truth (correct) target values.
y_pred1d array-like, or label indicator array / sparse matrix

Estimated targets as returned by a classifier.
labelsarray-like of shape (n_labels,), default=None

Optional list of label indices to include in the report.
target_nameslist of str of shape (n_labels,), default=None

Optional display names matching the labels (same order).
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
digitsint, default=2

Number of digits for formatting output floating point values. When output_dict is True, this will be ignored and the returned values will not be rounded.
output_dictbool, default=False

If True, return output as dict.  New in version 0.20.
zero_division“warn”, 0 or 1, default=”warn”

Sets the value to return when there is a zero division. If set to “warn”, this acts as 0, but warnings are also raised.    Returns

reportstring / dict

Text summary of the precision, recall, F1 score for each class. Dictionary returned if output_dict is True. Dictionary has the following structure: {'label 1': {'precision':0.5,
             'recall':1.0,
             'f1-score':0.67,
             'support':1},
 'label 2': { ... },
  ...
}
 The reported averages include macro average (averaging the unweighted mean per label), weighted average (averaging the support-weighted mean per label), and sample average (only for multilabel classification). Micro average (averaging the total true positives, false negatives and false positives) is only shown for multi-label or multi-class with a subset of classes, because it corresponds to accuracy otherwise and would be the same for all metrics. See also precision_recall_fscore_support for more details on averages. Note that in binary classification, recall of the positive class is also known as “sensitivity”; recall of the negative class is “specificity”.      See also
precision_recall_fscore_support, confusion_matrix

multilabel_confusion_matrix

  Examples >>> from sklearn.metrics import classification_report
>>> y_true = [0, 1, 2, 2, 2]
>>> y_pred = [0, 0, 2, 2, 1]
>>> target_names = ['class 0', 'class 1', 'class 2']
>>> print(classification_report(y_true, y_pred, target_names=target_names))
              precision    recall  f1-score   support

     class 0       0.50      1.00      0.67         1
     class 1       0.00      0.00      0.00         1
     class 2       1.00      0.67      0.80         3

    accuracy                           0.60         5
   macro avg       0.50      0.56      0.49         5
weighted avg       0.70      0.60      0.61         5

>>> y_pred = [1, 1, 0]
>>> y_true = [1, 1, 1]
>>> print(classification_report(y_true, y_pred, labels=[1, 2, 3]))
              precision    recall  f1-score   support

           1       1.00      0.67      0.80         3
           2       0.00      0.00      0.00         0
           3       0.00      0.00      0.00         0

   micro avg       1.00      0.67      0.80         3
   macro avg       0.33      0.22      0.27         3
weighted avg       1.00      0.67      0.80         3

 Examples using sklearn.metrics.classification_report

  Recognizing hand-written digits

  Faces recognition example using eigenfaces and SVMs

  Pipeline Anova SVM

  Parameter estimation using grid search with cross-validation

  Restricted Boltzmann Machine features for digit classification

  Column Transformer with Heterogeneous Data Sources

  Label Propagation digits: Demonstrating performance

  Label Propagation digits active learning

  Classification of text documents using sparse features


sklearn.metrics.cluster.contingency_matrix
sklearn.metrics.cluster.contingency_matrix(labels_true, labels_pred, *, eps=None, sparse=False, dtype=<class 'numpy.int64'>) [source]

Build a contingency matrix describing the relationship between labels.  Parameters

labels_trueint array, shape = [n_samples]

Ground truth class labels to be used as a reference.
labels_predarray-like of shape (n_samples,)

Cluster labels to evaluate.
epsfloat, default=None

If a float, that value is added to all values in the contingency matrix. This helps to stop NaN propagation. If None, nothing is adjusted.
sparsebool, default=False

If True, return a sparse CSR continency matrix. If eps is not None and sparse is True will raise ValueError.  New in version 0.18.
dtypenumeric type, default=np.int64

Output dtype. Ignored if eps is not None.  New in version 0.24.     Returns

contingency{array-like, sparse}, shape=[n_classes_true, n_classes_pred]

Matrix \(C\) such that \(C_{i, j}\) is the number of samples in true class \(i\) and in predicted class \(j\). If eps is None, the dtype of this array will be integer unless set otherwise with the dtype argument. If eps is given, the dtype will be float. Will be a sklearn.sparse.csr_matrix if sparse=True.


sklearn.metrics.cluster.pair_confusion_matrix
sklearn.metrics.cluster.pair_confusion_matrix(labels_true, labels_pred) [source]

Pair confusion matrix arising from two clusterings. The pair confusion matrix \(C\) computes a 2 by 2 similarity matrix between two clusterings by considering all pairs of samples and counting pairs that are assigned into the same or into different clusters under the true and predicted clusterings. Considering a pair of samples that is clustered together a positive pair, then as in binary classification the count of true negatives is \(C_{00}\), false negatives is \(C_{10}\), true positives is \(C_{11}\) and false positives is \(C_{01}\). Read more in the User Guide.  Parameters

labels_truearray-like of shape (n_samples,), dtype=integral

Ground truth class labels to be used as a reference.
labels_predarray-like of shape (n_samples,), dtype=integral

Cluster labels to evaluate.    Returns

Cndarray of shape (2, 2), dtype=np.int64

The contingency matrix.      See also
rand_score

Rand Score
adjusted_rand_score

Adjusted Rand Score
adjusted_mutual_info_score

Adjusted Mutual Information    References Examples Perfectly matching labelings have all non-zero entries on the diagonal regardless of actual label values: >>> from sklearn.metrics.cluster import pair_confusion_matrix
>>> pair_confusion_matrix([0, 0, 1, 1], [1, 1, 0, 0])
array([[8, 0],
       [0, 4]]...
 Labelings that assign all classes members to the same clusters are complete but may be not always pure, hence penalized, and have some off-diagonal non-zero entries: >>> pair_confusion_matrix([0, 0, 1, 2], [0, 0, 1, 1])
array([[8, 2],
       [0, 2]]...
 Note that the matrix is not symmetric.


sklearn.metrics.cohen_kappa_score
sklearn.metrics.cohen_kappa_score(y1, y2, *, labels=None, weights=None, sample_weight=None) [source]

Cohen’s kappa: a statistic that measures inter-annotator agreement. This function computes Cohen’s kappa [1], a score that expresses the level of agreement between two annotators on a classification problem. It is defined as  \[\kappa = (p_o - p_e) / (1 - p_e)\] where \(p_o\) is the empirical probability of agreement on the label assigned to any sample (the observed agreement ratio), and \(p_e\) is the expected agreement when both annotators assign labels randomly. \(p_e\) is estimated using a per-annotator empirical prior over the class labels [2]. Read more in the User Guide.  Parameters

y1array of shape (n_samples,)

Labels assigned by the first annotator.
y2array of shape (n_samples,)

Labels assigned by the second annotator. The kappa statistic is symmetric, so swapping y1 and y2 doesn’t change the value.
labelsarray-like of shape (n_classes,), default=None

List of labels to index the matrix. This may be used to select a subset of labels. If None, all labels that appear at least once in y1 or y2 are used.
weights{‘linear’, ‘quadratic’}, default=None

Weighting type to calculate the score. None means no weighted; “linear” means linear weighted; “quadratic” means quadratic weighted.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

kappafloat

The kappa statistic, which is a number between -1 and 1. The maximum value means complete agreement; zero or lower means chance agreement.     References
1
J. Cohen (1960). “A coefficient of agreement for nominal scales”. Educational and Psychological Measurement 20(1):37-46. doi:10.1177/001316446002000104.
2
R. Artstein and M. Poesio (2008). “Inter-coder agreement for computational linguistics”. Computational Linguistics 34(4):555-596.
3
Wikipedia entry for the Cohen’s kappa.


sklearn.metrics.completeness_score
sklearn.metrics.completeness_score(labels_true, labels_pred) [source]

Completeness metric of a cluster labeling given a ground truth. A clustering result satisfies completeness if all the data points that are members of a given class are elements of the same cluster. This metric is independent of the absolute values of the labels: a permutation of the class or cluster label values won’t change the score value in any way. This metric is not symmetric: switching label_true with label_pred will return the homogeneity_score which will be different in general. Read more in the User Guide.  Parameters

labels_trueint array, shape = [n_samples]

ground truth class labels to be used as a reference
labels_predarray-like of shape (n_samples,)

cluster labels to evaluate    Returns

completenessfloat

score between 0.0 and 1.0. 1.0 stands for perfectly complete labeling      See also
homogeneity_score

v_measure_score

  References
1
Andrew Rosenberg and Julia Hirschberg, 2007. V-Measure: A conditional entropy-based external cluster evaluation measure   Examples Perfect labelings are complete: >>> from sklearn.metrics.cluster import completeness_score
>>> completeness_score([0, 0, 1, 1], [1, 1, 0, 0])
1.0
 Non-perfect labelings that assign all classes members to the same clusters are still complete: >>> print(completeness_score([0, 0, 1, 1], [0, 0, 0, 0]))
1.0
>>> print(completeness_score([0, 1, 2, 3], [0, 0, 1, 1]))
0.999...
 If classes members are split across different clusters, the assignment cannot be complete: >>> print(completeness_score([0, 0, 1, 1], [0, 1, 0, 1]))
0.0
>>> print(completeness_score([0, 0, 0, 0], [0, 1, 2, 3]))
0.0

 Examples using sklearn.metrics.completeness_score

  Release Highlights for scikit-learn 0.23

  Demo of affinity propagation clustering algorithm

  Demo of DBSCAN clustering algorithm

  A demo of K-Means clustering on the handwritten digits data

  Clustering text documents using k-means


sklearn.metrics.confusion_matrix
sklearn.metrics.confusion_matrix(y_true, y_pred, *, labels=None, sample_weight=None, normalize=None) [source]

Compute confusion matrix to evaluate the accuracy of a classification. By definition a confusion matrix \(C\) is such that \(C_{i, j}\) is equal to the number of observations known to be in group \(i\) and predicted to be in group \(j\). Thus in binary classification, the count of true negatives is \(C_{0,0}\), false negatives is \(C_{1,0}\), true positives is \(C_{1,1}\) and false positives is \(C_{0,1}\). Read more in the User Guide.  Parameters

y_truearray-like of shape (n_samples,)

Ground truth (correct) target values.
y_predarray-like of shape (n_samples,)

Estimated targets as returned by a classifier.
labelsarray-like of shape (n_classes), default=None

List of labels to index the matrix. This may be used to reorder or select a subset of labels. If None is given, those that appear at least once in y_true or y_pred are used in sorted order.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.  New in version 0.18.
normalize{‘true’, ‘pred’, ‘all’}, default=None

Normalizes confusion matrix over the true (rows), predicted (columns) conditions or all the population. If None, confusion matrix will not be normalized.    Returns

Cndarray of shape (n_classes, n_classes)

Confusion matrix whose i-th row and j-th column entry indicates the number of samples with true label being i-th class and predicted label being j-th class.      See also
plot_confusion_matrix

Plot Confusion Matrix.
ConfusionMatrixDisplay

Confusion Matrix visualization.    References
1
Wikipedia entry for the Confusion matrix (Wikipedia and other references may use a different convention for axes).   Examples >>> from sklearn.metrics import confusion_matrix
>>> y_true = [2, 0, 2, 2, 0, 1]
>>> y_pred = [0, 0, 2, 2, 0, 2]
>>> confusion_matrix(y_true, y_pred)
array([[2, 0, 0],
       [0, 0, 1],
       [1, 0, 2]])
 >>> y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
>>> y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
>>> confusion_matrix(y_true, y_pred, labels=["ant", "bird", "cat"])
array([[2, 0, 0],
       [0, 0, 1],
       [1, 0, 2]])
 In the binary case, we can extract true positives, etc as follows: >>> tn, fp, fn, tp = confusion_matrix([0, 1, 0, 1], [1, 1, 1, 0]).ravel()
>>> (tn, fp, fn, tp)
(0, 2, 1, 1)

 Examples using sklearn.metrics.confusion_matrix

  Faces recognition example using eigenfaces and SVMs

  Visualizations with Display Objects

  Label Propagation digits: Demonstrating performance

  Label Propagation digits active learning

  Classification of text documents using sparse features


sklearn.metrics.consensus_score
sklearn.metrics.consensus_score(a, b, *, similarity='jaccard') [source]

The similarity of two sets of biclusters. Similarity between individual biclusters is computed. Then the best matching between sets is found using the Hungarian algorithm. The final score is the sum of similarities divided by the size of the larger set. Read more in the User Guide.  Parameters

a(rows, columns)

Tuple of row and column indicators for a set of biclusters.
b(rows, columns)

Another set of biclusters like a.
similarity‘jaccard’ or callable, default=’jaccard’

May be the string “jaccard” to use the Jaccard coefficient, or any function that takes four arguments, each of which is a 1d indicator vector: (a_rows, a_columns, b_rows, b_columns).     References  Hochreiter, Bodenhofer, et. al., 2010. FABIA: factor analysis for bicluster acquisition.
 Examples using sklearn.metrics.consensus_score

  A demo of the Spectral Co-Clustering algorithm

  A demo of the Spectral Biclustering algorithm


sklearn.metrics.coverage_error
sklearn.metrics.coverage_error(y_true, y_score, *, sample_weight=None) [source]

Coverage error measure. Compute how far we need to go through the ranked scores to cover all true labels. The best value is equal to the average number of labels in y_true per sample. Ties in y_scores are broken by giving maximal rank that would have been assigned to all tied values. Note: Our implementation’s score is 1 greater than the one given in Tsoumakas et al., 2010. This extends it to handle the degenerate case in which an instance has 0 true labels. Read more in the User Guide.  Parameters

y_truendarray of shape (n_samples, n_labels)

True binary labels in binary indicator format.
y_scorendarray of shape (n_samples, n_labels)

Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers).
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

coverage_errorfloat
   References
1
Tsoumakas, G., Katakis, I., & Vlahavas, I. (2010). Mining multi-label data. In Data mining and knowledge discovery handbook (pp. 667-685). Springer US.


sklearn.metrics.davies_bouldin_score
sklearn.metrics.davies_bouldin_score(X, labels) [source]

Computes the Davies-Bouldin score. The score is defined as the average similarity measure of each cluster with its most similar cluster, where similarity is the ratio of within-cluster distances to between-cluster distances. Thus, clusters which are farther apart and less dispersed will result in a better score. The minimum score is zero, with lower values indicating better clustering. Read more in the User Guide.  New in version 0.20.   Parameters

Xarray-like of shape (n_samples, n_features)

A list of n_features-dimensional data points. Each row corresponds to a single data point.
labelsarray-like of shape (n_samples,)

Predicted labels for each sample.    Returns
 score: float

The resulting Davies-Bouldin score.     References
1
Davies, David L.; Bouldin, Donald W. (1979). “A Cluster Separation Measure”. IEEE Transactions on Pattern Analysis and Machine Intelligence. PAMI-1 (2): 224-227


sklearn.metrics.dcg_score
sklearn.metrics.dcg_score(y_true, y_score, *, k=None, log_base=2, sample_weight=None, ignore_ties=False) [source]

Compute Discounted Cumulative Gain. Sum the true scores ranked in the order induced by the predicted scores, after applying a logarithmic discount. This ranking metric yields a high value if true labels are ranked high by y_score. Usually the Normalized Discounted Cumulative Gain (NDCG, computed by ndcg_score) is preferred.  Parameters

y_truendarray of shape (n_samples, n_labels)

True targets of multilabel classification, or true scores of entities to be ranked.
y_scorendarray of shape (n_samples, n_labels)

Target scores, can either be probability estimates, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers).
kint, default=None

Only consider the highest k scores in the ranking. If None, use all outputs.
log_basefloat, default=2

Base of the logarithm used for the discount. A low value means a sharper discount (top results are more important).
sample_weightndarray of shape (n_samples,), default=None

Sample weights. If None, all samples are given the same weight.
ignore_tiesbool, default=False

Assume that there are no ties in y_score (which is likely to be the case if y_score is continuous) for efficiency gains.    Returns

discounted_cumulative_gainfloat

The averaged sample DCG scores.      See also
ndcg_score

The Discounted Cumulative Gain divided by the Ideal Discounted Cumulative Gain (the DCG obtained for a perfect ranking), in order to have a score between 0 and 1.    References Wikipedia entry for Discounted Cumulative Gain. Jarvelin, K., & Kekalainen, J. (2002). Cumulated gain-based evaluation of IR techniques. ACM Transactions on Information Systems (TOIS), 20(4), 422-446. Wang, Y., Wang, L., Li, Y., He, D., Chen, W., & Liu, T. Y. (2013, May). A theoretical analysis of NDCG ranking measures. In Proceedings of the 26th Annual Conference on Learning Theory (COLT 2013). McSherry, F., & Najork, M. (2008, March). Computing information retrieval performance measures efficiently in the presence of tied scores. In European conference on information retrieval (pp. 414-421). Springer, Berlin, Heidelberg. Examples >>> from sklearn.metrics import dcg_score
>>> # we have groud-truth relevance of some answers to a query:
>>> true_relevance = np.asarray([[10, 0, 0, 1, 5]])
>>> # we predict scores for the answers
>>> scores = np.asarray([[.1, .2, .3, 4, 70]])
>>> dcg_score(true_relevance, scores)
9.49...
>>> # we can set k to truncate the sum; only top k answers contribute
>>> dcg_score(true_relevance, scores, k=2)
5.63...
>>> # now we have some ties in our prediction
>>> scores = np.asarray([[1, 0, 0, 0, 1]])
>>> # by default ties are averaged, so here we get the average true
>>> # relevance of our top predictions: (10 + 5) / 2 = 7.5
>>> dcg_score(true_relevance, scores, k=1)
7.5
>>> # we can choose to ignore ties for faster results, but only
>>> # if we know there aren't ties in our scores, otherwise we get
>>> # wrong results:
>>> dcg_score(true_relevance,
...           scores, k=1, ignore_ties=True)
5.0


sklearn.metrics.det_curve
sklearn.metrics.det_curve(y_true, y_score, pos_label=None, sample_weight=None) [source]

Compute error rates for different probability thresholds.  Note This metric is used for evaluation of ranking and error tradeoffs of a binary classification task.  Read more in the User Guide.  New in version 0.24.   Parameters

y_truendarray of shape (n_samples,)

True binary labels. If labels are not either {-1, 1} or {0, 1}, then pos_label should be explicitly given.
y_scorendarray of shape of (n_samples,)

Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers).
pos_labelint or str, default=None

The label of the positive class. When pos_label=None, if y_true is in {-1, 1} or {0, 1}, pos_label is set to 1, otherwise an error will be raised.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

fprndarray of shape (n_thresholds,)

False positive rate (FPR) such that element i is the false positive rate of predictions with score >= thresholds[i]. This is occasionally referred to as false acceptance propability or fall-out.
fnrndarray of shape (n_thresholds,)

False negative rate (FNR) such that element i is the false negative rate of predictions with score >= thresholds[i]. This is occasionally referred to as false rejection or miss rate.
thresholdsndarray of shape (n_thresholds,)

Decreasing score values.      See also
plot_det_curve

Plot detection error tradeoff (DET) curve.
DetCurveDisplay

DET curve visualization.
roc_curve

Compute Receiver operating characteristic (ROC) curve.
precision_recall_curve

Compute precision-recall curve.    Examples >>> import numpy as np
>>> from sklearn.metrics import det_curve
>>> y_true = np.array([0, 0, 1, 1])
>>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
>>> fpr, fnr, thresholds = det_curve(y_true, y_scores)
>>> fpr
array([0.5, 0.5, 0. ])
>>> fnr
array([0. , 0.5, 0.5])
>>> thresholds
array([0.35, 0.4 , 0.8 ])

 Examples using sklearn.metrics.det_curve

  Detection error tradeoff (DET) curve


sklearn.metrics.explained_variance_score
sklearn.metrics.explained_variance_score(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average') [source]

Explained variance regression score function. Best possible score is 1.0, lower values are worse. Read more in the User Guide.  Parameters

y_truearray-like of shape (n_samples,) or (n_samples, n_outputs)

Ground truth (correct) target values.
y_predarray-like of shape (n_samples,) or (n_samples, n_outputs)

Estimated target values.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
multioutput{‘raw_values’, ‘uniform_average’, ‘variance_weighted’} or array-like of shape (n_outputs,), default=’uniform_average’

Defines aggregating of multiple output scores. Array-like value defines weights used to average scores.  ‘raw_values’ :

Returns a full set of scores in case of multioutput input.  ‘uniform_average’ :

Scores of all outputs are averaged with uniform weight.  ‘variance_weighted’ :

Scores of all outputs are averaged, weighted by the variances of each individual output.      Returns

scorefloat or ndarray of floats

The explained variance or ndarray if ‘multioutput’ is ‘raw_values’.     Notes This is not a symmetric function. Examples >>> from sklearn.metrics import explained_variance_score
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> explained_variance_score(y_true, y_pred)
0.957...
>>> y_true = [[0.5, 1], [-1, 1], [7, -6]]
>>> y_pred = [[0, 2], [-1, 2], [8, -5]]
>>> explained_variance_score(y_true, y_pred, multioutput='uniform_average')
0.983...


sklearn.metrics.f1_score
sklearn.metrics.f1_score(y_true, y_pred, *, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn') [source]

Compute the F1 score, also known as balanced F-score or F-measure. The F1 score can be interpreted as a weighted average of the precision and recall, where an F1 score reaches its best value at 1 and worst score at 0. The relative contribution of precision and recall to the F1 score are equal. The formula for the F1 score is: F1 = 2 * (precision * recall) / (precision + recall)
 In the multi-class and multi-label case, this is the average of the F1 score of each class with weighting depending on the average parameter. Read more in the User Guide.  Parameters

y_true1d array-like, or label indicator array / sparse matrix

Ground truth (correct) target values.
y_pred1d array-like, or label indicator array / sparse matrix

Estimated targets as returned by a classifier.
labelsarray-like, default=None

The set of labels to include when average != 'binary', and their order if average is None. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order.  Changed in version 0.17: Parameter labels improved for multiclass problem.
pos_labelstr or int, default=1

The class to report if average='binary' and the data is binary. If the data are multiclass or multilabel, this will be ignored; setting labels=[pos_label] and average != 'binary' will report scores for that label only.
average{‘micro’, ‘macro’, ‘samples’,’weighted’, ‘binary’} or None, default=’binary’

This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data:
'binary':

Only report results for the class specified by pos_label. This is applicable only if targets (y_{true,pred}) are binary.
'micro':

Calculate metrics globally by counting the total true positives, false negatives and false positives.
'macro':

Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.
'weighted':

Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters ‘macro’ to account for label imbalance; it can result in an F-score that is not between precision and recall.
'samples':

Calculate metrics for each instance, and find their average (only meaningful for multilabel classification where this differs from accuracy_score).
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
zero_division“warn”, 0 or 1, default=”warn”

Sets the value to return when there is a zero division, i.e. when all predictions and labels are negative. If set to “warn”, this acts as 0, but warnings are also raised.    Returns

f1_scorefloat or array of float, shape = [n_unique_labels]

F1 score of the positive class in binary classification or weighted average of the F1 scores of each class for the multiclass task.      See also
fbeta_score, precision_recall_fscore_support, jaccard_score

multilabel_confusion_matrix

  Notes When true positive + false positive == 0, precision is undefined. When true positive + false negative == 0, recall is undefined. In such cases, by default the metric will be set to 0, as will f-score, and UndefinedMetricWarning will be raised. This behavior can be modified with zero_division. References
1
Wikipedia entry for the F1-score.   Examples >>> from sklearn.metrics import f1_score
>>> y_true = [0, 1, 2, 0, 1, 2]
>>> y_pred = [0, 2, 1, 0, 0, 1]
>>> f1_score(y_true, y_pred, average='macro')
0.26...
>>> f1_score(y_true, y_pred, average='micro')
0.33...
>>> f1_score(y_true, y_pred, average='weighted')
0.26...
>>> f1_score(y_true, y_pred, average=None)
array([0.8, 0. , 0. ])
>>> y_true = [0, 0, 0, 0, 0, 0]
>>> y_pred = [0, 0, 0, 0, 0, 0]
>>> f1_score(y_true, y_pred, zero_division=1)
1.0...

 Examples using sklearn.metrics.f1_score

  Probability Calibration curves

  Precision-Recall

  Semi-supervised Classification on a Text Dataset


sklearn.metrics.fbeta_score
sklearn.metrics.fbeta_score(y_true, y_pred, *, beta, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn') [source]

Compute the F-beta score. The F-beta score is the weighted harmonic mean of precision and recall, reaching its optimal value at 1 and its worst value at 0. The beta parameter determines the weight of recall in the combined score. beta < 1 lends more weight to precision, while beta > 1 favors recall (beta -> 0 considers only precision, beta -> +inf only recall). Read more in the User Guide.  Parameters

y_true1d array-like, or label indicator array / sparse matrix

Ground truth (correct) target values.
y_pred1d array-like, or label indicator array / sparse matrix

Estimated targets as returned by a classifier.
betafloat

Determines the weight of recall in the combined score.
labelsarray-like, default=None

The set of labels to include when average != 'binary', and their order if average is None. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order.  Changed in version 0.17: Parameter labels improved for multiclass problem.
pos_labelstr or int, default=1

The class to report if average='binary' and the data is binary. If the data are multiclass or multilabel, this will be ignored; setting labels=[pos_label] and average != 'binary' will report scores for that label only.
average{‘micro’, ‘macro’, ‘samples’, ‘weighted’, ‘binary’} or None default=’binary’

This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data:
'binary':

Only report results for the class specified by pos_label. This is applicable only if targets (y_{true,pred}) are binary.
'micro':

Calculate metrics globally by counting the total true positives, false negatives and false positives.
'macro':

Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.
'weighted':

Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters ‘macro’ to account for label imbalance; it can result in an F-score that is not between precision and recall.
'samples':

Calculate metrics for each instance, and find their average (only meaningful for multilabel classification where this differs from accuracy_score).
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
zero_division“warn”, 0 or 1, default=”warn”

Sets the value to return when there is a zero division, i.e. when all predictions and labels are negative. If set to “warn”, this acts as 0, but warnings are also raised.    Returns

fbeta_scorefloat (if average is not None) or array of float, shape = [n_unique_labels]

F-beta score of the positive class in binary classification or weighted average of the F-beta score of each class for the multiclass task.      See also
precision_recall_fscore_support, multilabel_confusion_matrix

  Notes When true positive + false positive == 0 or true positive + false negative == 0, f-score returns 0 and raises UndefinedMetricWarning. This behavior can be modified with zero_division. References
1
R. Baeza-Yates and B. Ribeiro-Neto (2011). Modern Information Retrieval. Addison Wesley, pp. 327-328.
2
Wikipedia entry for the F1-score.   Examples >>> from sklearn.metrics import fbeta_score
>>> y_true = [0, 1, 2, 0, 1, 2]
>>> y_pred = [0, 2, 1, 0, 0, 1]
>>> fbeta_score(y_true, y_pred, average='macro', beta=0.5)
0.23...
>>> fbeta_score(y_true, y_pred, average='micro', beta=0.5)
0.33...
>>> fbeta_score(y_true, y_pred, average='weighted', beta=0.5)
0.23...
>>> fbeta_score(y_true, y_pred, average=None, beta=0.5)
array([0.71..., 0.        , 0.        ])


sklearn.metrics.fowlkes_mallows_score
sklearn.metrics.fowlkes_mallows_score(labels_true, labels_pred, *, sparse=False) [source]

Measure the similarity of two clusterings of a set of points.  New in version 0.18.  The Fowlkes-Mallows index (FMI) is defined as the geometric mean between of the precision and recall: FMI = TP / sqrt((TP + FP) * (TP + FN))
 Where TP is the number of True Positive (i.e. the number of pair of points that belongs in the same clusters in both labels_true and labels_pred), FP is the number of False Positive (i.e. the number of pair of points that belongs in the same clusters in labels_true and not in labels_pred) and FN is the number of False Negative (i.e the number of pair of points that belongs in the same clusters in labels_pred and not in labels_True). The score ranges from 0 to 1. A high value indicates a good similarity between two clusters. Read more in the User Guide.  Parameters

labels_trueint array, shape = (n_samples,)

A clustering of the data into disjoint subsets.
labels_predarray, shape = (n_samples, )

A clustering of the data into disjoint subsets.
sparsebool, default=False

Compute contingency matrix internally with sparse matrix.    Returns

scorefloat

The resulting Fowlkes-Mallows score.     References
1
E. B. Fowkles and C. L. Mallows, 1983. “A method for comparing two hierarchical clusterings”. Journal of the American Statistical Association
2
Wikipedia entry for the Fowlkes-Mallows Index   Examples Perfect labelings are both homogeneous and complete, hence have score 1.0: >>> from sklearn.metrics.cluster import fowlkes_mallows_score
>>> fowlkes_mallows_score([0, 0, 1, 1], [0, 0, 1, 1])
1.0
>>> fowlkes_mallows_score([0, 0, 1, 1], [1, 1, 0, 0])
1.0
 If classes members are completely split across different clusters, the assignment is totally random, hence the FMI is null: >>> fowlkes_mallows_score([0, 0, 0, 0], [0, 1, 2, 3])
0.0


sklearn.metrics.get_scorer
sklearn.metrics.get_scorer(scoring) [source]

Get a scorer from string. Read more in the User Guide.  Parameters

scoringstr or callable

Scoring method as string. If callable it is returned as is.    Returns

scorercallable

The scorer.


sklearn.metrics.hamming_loss
sklearn.metrics.hamming_loss(y_true, y_pred, *, sample_weight=None) [source]

Compute the average Hamming loss. The Hamming loss is the fraction of labels that are incorrectly predicted. Read more in the User Guide.  Parameters

y_true1d array-like, or label indicator array / sparse matrix

Ground truth (correct) labels.
y_pred1d array-like, or label indicator array / sparse matrix

Predicted labels, as returned by a classifier.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.  New in version 0.18.     Returns

lossfloat or int

Return the average Hamming loss between element of y_true and y_pred.      See also
accuracy_score, jaccard_score, zero_one_loss

  Notes In multiclass classification, the Hamming loss corresponds to the Hamming distance between y_true and y_pred which is equivalent to the subset zero_one_loss function, when normalize parameter is set to True. In multilabel classification, the Hamming loss is different from the subset zero-one loss. The zero-one loss considers the entire set of labels for a given sample incorrect if it does not entirely match the true set of labels. Hamming loss is more forgiving in that it penalizes only the individual labels. The Hamming loss is upperbounded by the subset zero-one loss, when normalize parameter is set to True. It is always between 0 and 1, lower being better. References
1
Grigorios Tsoumakas, Ioannis Katakis. Multi-Label Classification: An Overview. International Journal of Data Warehousing & Mining, 3(3), 1-13, July-September 2007.
2
Wikipedia entry on the Hamming distance.   Examples >>> from sklearn.metrics import hamming_loss
>>> y_pred = [1, 2, 3, 4]
>>> y_true = [2, 2, 3, 4]
>>> hamming_loss(y_true, y_pred)
0.25
 In the multilabel case with binary label indicators: >>> import numpy as np
>>> hamming_loss(np.array([[0, 1], [1, 1]]), np.zeros((2, 2)))
0.75

 Examples using sklearn.metrics.hamming_loss

  Model Complexity Influence


sklearn.metrics.hinge_loss
sklearn.metrics.hinge_loss(y_true, pred_decision, *, labels=None, sample_weight=None) [source]

Average hinge loss (non-regularized). In binary class case, assuming labels in y_true are encoded with +1 and -1, when a prediction mistake is made, margin = y_true * pred_decision is always negative (since the signs disagree), implying 1 - margin is always greater than 1. The cumulated hinge loss is therefore an upper bound of the number of mistakes made by the classifier. In multiclass case, the function expects that either all the labels are included in y_true or an optional labels argument is provided which contains all the labels. The multilabel margin is calculated according to Crammer-Singer’s method. As in the binary case, the cumulated hinge loss is an upper bound of the number of mistakes made by the classifier. Read more in the User Guide.  Parameters

y_truearray of shape (n_samples,)

True target, consisting of integers of two values. The positive label must be greater than the negative label.
pred_decisionarray of shape (n_samples,) or (n_samples, n_classes)

Predicted decisions, as output by decision_function (floats).
labelsarray-like, default=None

Contains all the labels for the problem. Used in multiclass hinge loss.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

lossfloat
   References
1
Wikipedia entry on the Hinge loss.
2
Koby Crammer, Yoram Singer. On the Algorithmic Implementation of Multiclass Kernel-based Vector Machines. Journal of Machine Learning Research 2, (2001), 265-292.
3
L1 AND L2 Regularization for Multiclass Hinge Loss Models by Robert C. Moore, John DeNero.   Examples >>> from sklearn import svm
>>> from sklearn.metrics import hinge_loss
>>> X = [[0], [1]]
>>> y = [-1, 1]
>>> est = svm.LinearSVC(random_state=0)
>>> est.fit(X, y)
LinearSVC(random_state=0)
>>> pred_decision = est.decision_function([[-2], [3], [0.5]])
>>> pred_decision
array([-2.18...,  2.36...,  0.09...])
>>> hinge_loss([-1, 1, 1], pred_decision)
0.30...
 In the multiclass case: >>> import numpy as np
>>> X = np.array([[0], [1], [2], [3]])
>>> Y = np.array([0, 1, 2, 3])
>>> labels = np.array([0, 1, 2, 3])
>>> est = svm.LinearSVC()
>>> est.fit(X, Y)
LinearSVC()
>>> pred_decision = est.decision_function([[-1], [2], [3]])
>>> y_true = [0, 2, 3]
>>> hinge_loss(y_true, pred_decision, labels=labels)
0.56...


sklearn.metrics.homogeneity_completeness_v_measure
sklearn.metrics.homogeneity_completeness_v_measure(labels_true, labels_pred, *, beta=1.0) [source]

Compute the homogeneity and completeness and V-Measure scores at once. Those metrics are based on normalized conditional entropy measures of the clustering labeling to evaluate given the knowledge of a Ground Truth class labels of the same samples. A clustering result satisfies homogeneity if all of its clusters contain only data points which are members of a single class. A clustering result satisfies completeness if all the data points that are members of a given class are elements of the same cluster. Both scores have positive values between 0.0 and 1.0, larger values being desirable. Those 3 metrics are independent of the absolute values of the labels: a permutation of the class or cluster label values won’t change the score values in any way. V-Measure is furthermore symmetric: swapping labels_true and label_pred will give the same score. This does not hold for homogeneity and completeness. V-Measure is identical to normalized_mutual_info_score with the arithmetic averaging method. Read more in the User Guide.  Parameters

labels_trueint array, shape = [n_samples]

ground truth class labels to be used as a reference
labels_predarray-like of shape (n_samples,)

cluster labels to evaluate
betafloat, default=1.0

Ratio of weight attributed to homogeneity vs completeness. If beta is greater than 1, completeness is weighted more strongly in the calculation. If beta is less than 1, homogeneity is weighted more strongly.    Returns

homogeneityfloat

score between 0.0 and 1.0. 1.0 stands for perfectly homogeneous labeling
completenessfloat

score between 0.0 and 1.0. 1.0 stands for perfectly complete labeling
v_measurefloat

harmonic mean of the first two      See also
homogeneity_score

completeness_score

v_measure_score