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

Compute the recall. The recall is the ratio tp / (tp + fn) where tp is the number of true positives and fn the number of false negatives. The recall is intuitively the ability of the classifier to find all the positive samples. The best value is 1 and the worst value is 0. 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’} 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. If set to “warn”, this acts as 0, but warnings are also raised.    Returns

recallfloat (if average is not None) or array of float of shape

(n_unique_labels,) Recall of the positive class in binary classification or weighted average of the recall of each class for the multiclass task.      See also
precision_recall_fscore_support, balanced_accuracy_score

multilabel_confusion_matrix

  Notes When true positive + false negative == 0, recall returns 0 and raises UndefinedMetricWarning. This behavior can be modified with zero_division. Examples >>> from sklearn.metrics import recall_score
>>> y_true = [0, 1, 2, 0, 1, 2]
>>> y_pred = [0, 2, 1, 0, 0, 1]
>>> recall_score(y_true, y_pred, average='macro')
0.33...
>>> recall_score(y_true, y_pred, average='micro')
0.33...
>>> recall_score(y_true, y_pred, average='weighted')
0.33...
>>> recall_score(y_true, y_pred, average=None)
array([1., 0., 0.])
>>> y_true = [0, 0, 0, 0, 0, 0]
>>> recall_score(y_true, y_pred, average=None)
array([0.5, 0. , 0. ])
>>> recall_score(y_true, y_pred, average=None, zero_division=1)
array([0.5, 1. , 1. ])

 Examples using sklearn.metrics.recall_score

  Probability Calibration curves

  Precision-Recall


sklearn.metrics.roc_auc_score
sklearn.metrics.roc_auc_score(y_true, y_score, *, average='macro', sample_weight=None, max_fpr=None, multi_class='raise', labels=None) [source]

Compute Area Under the Receiver Operating Characteristic Curve (ROC AUC) from prediction scores. Note: this implementation can be used with binary, multiclass and multilabel classification, but some restrictions apply (see Parameters). Read more in the User Guide.  Parameters

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

True labels or binary label indicators. The binary and multiclass cases expect labels with shape (n_samples,) while the multilabel case expects binary label indicators with shape (n_samples, n_classes).
y_scorearray-like of shape (n_samples,) or (n_samples, n_classes)

Target scores.  In the binary case, it corresponds to an array of shape (n_samples,). Both probability estimates and non-thresholded decision values can be provided. The probability estimates correspond to the probability of the class with the greater label, i.e. estimator.classes_[1] and thus estimator.predict_proba(X, y)[:, 1]. The decision values corresponds to the output of estimator.decision_function(X, y). See more information in the User guide; In the multiclass case, it corresponds to an array of shape (n_samples, n_classes) of probability estimates provided by the predict_proba method. The probability estimates must sum to 1 across the possible classes. In addition, the order of the class scores must correspond to the order of labels, if provided, or else to the numerical or lexicographical order of the labels in y_true. See more information in the User guide; In the multilabel case, it corresponds to an array of shape (n_samples, n_classes). Probability estimates are provided by the predict_proba method and the non-thresholded decision values by the decision_function method. The probability estimates correspond to the probability of the class with the greater label for each output of the classifier. See more information in the User guide.
average{‘micro’, ‘macro’, ‘samples’, ‘weighted’} or None, default=’macro’

If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data: Note: multiclass ROC AUC currently only handles the ‘macro’ and ‘weighted’ averages.
'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.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.
max_fprfloat > 0 and <= 1, default=None

If not None, the standardized partial AUC [2] over the range [0, max_fpr] is returned. For the multiclass case, max_fpr, should be either equal to None or 1.0 as AUC ROC partial computation currently is not supported for multiclass.
multi_class{‘raise’, ‘ovr’, ‘ovo’}, default=’raise’

Only used for multiclass targets. Determines the type of configuration to use. The default value raises an error, so either 'ovr' or 'ovo' must be passed explicitly.
'ovr':

Stands for One-vs-rest. Computes the AUC of each class against the rest [3] [4]. This treats the multiclass case in the same way as the multilabel case. Sensitive to class imbalance even when average == 'macro', because class imbalance affects the composition of each of the ‘rest’ groupings.
'ovo':

Stands for One-vs-one. Computes the average AUC of all possible pairwise combinations of classes [5]. Insensitive to class imbalance when average == 'macro'.
labelsarray-like of shape (n_classes,), default=None

Only used for multiclass targets. List of labels that index the classes in y_score. If None, the numerical or lexicographical order of the labels in y_true is used.    Returns

aucfloat
    See also
average_precision_score

Area under the precision-recall curve.
roc_curve

Compute Receiver operating characteristic (ROC) curve.
plot_roc_curve

Plot Receiver operating characteristic (ROC) curve.    References
1
Wikipedia entry for the Receiver operating characteristic
2
Analyzing a portion of the ROC curve. McClish, 1989
3
Provost, F., Domingos, P. (2000). Well-trained PETs: Improving probability estimation trees (Section 6.2), CeDER Working Paper #IS-00-04, Stern School of Business, New York University.
4
Fawcett, T. (2006). An introduction to ROC analysis. Pattern Recognition Letters, 27(8), 861-874.
5
Hand, D.J., Till, R.J. (2001). A Simple Generalisation of the Area Under the ROC Curve for Multiple Class Classification Problems. Machine Learning, 45(2), 171-186.   Examples Binary case: >>> from sklearn.datasets import load_breast_cancer
>>> from sklearn.linear_model import LogisticRegression
>>> from sklearn.metrics import roc_auc_score
>>> X, y = load_breast_cancer(return_X_y=True)
>>> clf = LogisticRegression(solver="liblinear", random_state=0).fit(X, y)
>>> roc_auc_score(y, clf.predict_proba(X)[:, 1])
0.99...
>>> roc_auc_score(y, clf.decision_function(X))
0.99...
 Multiclass case: >>> from sklearn.datasets import load_iris
>>> X, y = load_iris(return_X_y=True)
>>> clf = LogisticRegression(solver="liblinear").fit(X, y)
>>> roc_auc_score(y, clf.predict_proba(X), multi_class='ovr')
0.99...
 Multilabel case: >>> from sklearn.datasets import make_multilabel_classification
>>> from sklearn.multioutput import MultiOutputClassifier
>>> X, y = make_multilabel_classification(random_state=0)
>>> clf = MultiOutputClassifier(clf).fit(X, y)
>>> # get a list of n_output containing probability arrays of shape
>>> # (n_samples, n_classes)
>>> y_pred = clf.predict_proba(X)
>>> # extract the positive columns for each output
>>> y_pred = np.transpose([pred[:, 1] for pred in y_pred])
>>> roc_auc_score(y, y_pred, average=None)
array([0.82..., 0.86..., 0.94..., 0.85... , 0.94...])
>>> from sklearn.linear_model import RidgeClassifierCV
>>> clf = RidgeClassifierCV().fit(X, y)
>>> roc_auc_score(y, clf.decision_function(X), average=None)
array([0.81..., 0.84... , 0.93..., 0.87..., 0.94...])

 Examples using sklearn.metrics.roc_auc_score

  Release Highlights for scikit-learn 0.22

  Receiver Operating Characteristic (ROC) with cross validation

  Receiver Operating Characteristic (ROC)

  Statistical comparison of models using grid search


sklearn.metrics.roc_curve
sklearn.metrics.roc_curve(y_true, y_score, *, pos_label=None, sample_weight=None, drop_intermediate=True) [source]

Compute Receiver operating characteristic (ROC). Note: this implementation is restricted to the binary classification task. Read more in the User Guide.  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 (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.
drop_intermediatebool, default=True

Whether to drop some suboptimal thresholds which would not appear on a plotted ROC curve. This is useful in order to create lighter ROC curves.  New in version 0.17: parameter drop_intermediate.     Returns

fprndarray of shape (>2,)

Increasing false positive rates such that element i is the false positive rate of predictions with score >= thresholds[i].
tprndarray of shape (>2,)

Increasing true positive rates such that element i is the true positive rate of predictions with score >= thresholds[i].
thresholdsndarray of shape = (n_thresholds,)

Decreasing thresholds on the decision function used to compute fpr and tpr. thresholds[0] represents no instances being predicted and is arbitrarily set to max(y_score) + 1.      See also
plot_roc_curve

Plot Receiver operating characteristic (ROC) curve.
RocCurveDisplay

ROC Curve visualization.
det_curve

Compute error rates for different probability thresholds.
roc_auc_score

Compute the area under the ROC curve.    Notes Since the thresholds are sorted from low to high values, they are reversed upon returning them to ensure they correspond to both fpr and tpr, which are sorted in reversed order during their calculation. References
1
Wikipedia entry for the Receiver operating characteristic
2
Fawcett T. An introduction to ROC analysis[J]. Pattern Recognition Letters, 2006, 27(8):861-874.   Examples >>> import numpy as np
>>> from sklearn import metrics
>>> y = np.array([1, 1, 2, 2])
>>> scores = np.array([0.1, 0.4, 0.35, 0.8])
>>> fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=2)
>>> fpr
array([0. , 0. , 0.5, 0.5, 1. ])
>>> tpr
array([0. , 0.5, 0.5, 1. , 1. ])
>>> thresholds
array([1.8 , 0.8 , 0.4 , 0.35, 0.1 ])

 Examples using sklearn.metrics.roc_curve

  Feature transformations with ensembles of trees

  Species distribution modeling

  Visualizations with Display Objects

  Detection error tradeoff (DET) curve

  Receiver Operating Characteristic (ROC)


sklearn.metrics.silhouette_samples
sklearn.metrics.silhouette_samples(X, labels, *, metric='euclidean', **kwds) [source]

Compute the Silhouette Coefficient for each sample. The Silhouette Coefficient is a measure of how well samples are clustered with samples that are similar to themselves. Clustering models with a high Silhouette Coefficient are said to be dense, where samples in the same cluster are similar to each other, and well separated, where samples in different clusters are not very similar to each other. The Silhouette Coefficient is calculated using the mean intra-cluster distance (a) and the mean nearest-cluster distance (b) for each sample. The Silhouette Coefficient for a sample is (b - a) / max(a,
b). Note that Silhouette Coefficient is only defined if number of labels is 2 <= n_labels <= n_samples - 1. This function returns the Silhouette Coefficient for each sample. The best value is 1 and the worst value is -1. Values near 0 indicate overlapping clusters. Read more in the User Guide.  Parameters

Xarray-like of shape (n_samples_a, n_samples_a) if metric == “precomputed” or (n_samples_a, n_features) otherwise

An array of pairwise distances between samples, or a feature array.
labelsarray-like of shape (n_samples,)

Label values for each sample.
metricstr or callable, default=’euclidean’

The metric to use when calculating distance between instances in a feature array. If metric is a string, it must be one of the options allowed by sklearn.metrics.pairwise.pairwise_distances. If X is the distance array itself, use “precomputed” as the metric. Precomputed distance matrices must have 0 along the diagonal.
`**kwds`optional keyword parameters

Any further parameters are passed directly to the distance function. If using a scipy.spatial.distance metric, the parameters are still metric dependent. See the scipy docs for usage examples.    Returns

silhouettearray-like of shape (n_samples,)

Silhouette Coefficients for each sample.     References
1
Peter J. Rousseeuw (1987). “Silhouettes: a Graphical Aid to the Interpretation and Validation of Cluster Analysis”. Computational and Applied Mathematics 20: 53-65.
2
Wikipedia entry on the Silhouette Coefficient
 Examples using sklearn.metrics.silhouette_samples

  Selecting the number of clusters with silhouette analysis on KMeans clustering


sklearn.metrics.silhouette_score
sklearn.metrics.silhouette_score(X, labels, *, metric='euclidean', sample_size=None, random_state=None, **kwds) [source]

Compute the mean Silhouette Coefficient of all samples. The Silhouette Coefficient is calculated using the mean intra-cluster distance (a) and the mean nearest-cluster distance (b) for each sample. The Silhouette Coefficient for a sample is (b - a) / max(a,
b). To clarify, b is the distance between a sample and the nearest cluster that the sample is not a part of. Note that Silhouette Coefficient is only defined if number of labels is 2 <= n_labels <= n_samples - 1. This function returns the mean Silhouette Coefficient over all samples. To obtain the values for each sample, use silhouette_samples. The best value is 1 and the worst value is -1. Values near 0 indicate overlapping clusters. Negative values generally indicate that a sample has been assigned to the wrong cluster, as a different cluster is more similar. Read more in the User Guide.  Parameters

Xarray-like of shape (n_samples_a, n_samples_a) if metric == “precomputed” or (n_samples_a, n_features) otherwise

An array of pairwise distances between samples, or a feature array.
labelsarray-like of shape (n_samples,)

Predicted labels for each sample.
metricstr or callable, default=’euclidean’

The metric to use when calculating distance between instances in a feature array. If metric is a string, it must be one of the options allowed by metrics.pairwise.pairwise_distances. If X is the distance array itself, use metric="precomputed".
sample_sizeint, default=None

The size of the sample to use when computing the Silhouette Coefficient on a random subset of the data. If sample_size is None, no sampling is used.
random_stateint, RandomState instance or None, default=None

Determines random number generation for selecting a subset of samples. Used when sample_size is not None. Pass an int for reproducible results across multiple function calls. See Glossary.
**kwdsoptional keyword parameters

Any further parameters are passed directly to the distance function. If using a scipy.spatial.distance metric, the parameters are still metric dependent. See the scipy docs for usage examples.    Returns

silhouettefloat

Mean Silhouette Coefficient for all samples.     References
1
Peter J. Rousseeuw (1987). “Silhouettes: a Graphical Aid to the Interpretation and Validation of Cluster Analysis”. Computational and Applied Mathematics 20: 53-65.
2
Wikipedia entry on the Silhouette Coefficient
 Examples using sklearn.metrics.silhouette_score

  Demo of affinity propagation clustering algorithm

  Demo of DBSCAN clustering algorithm

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

  Selecting the number of clusters with silhouette analysis on KMeans clustering

  Clustering text documents using k-means


sklearn.metrics.top_k_accuracy_score
sklearn.metrics.top_k_accuracy_score(y_true, y_score, *, k=2, normalize=True, sample_weight=None, labels=None) [source]

Top-k Accuracy classification score. This metric computes the number of times where the correct label is among the top k labels predicted (ranked by predicted scores). Note that the multilabel case isn’t covered here. Read more in the User Guide  Parameters

y_truearray-like of shape (n_samples,)

True labels.
y_scorearray-like of shape (n_samples,) or (n_samples, n_classes)

Target scores. These can be either probability estimates or non-thresholded decision values (as returned by decision_function on some classifiers). The binary case expects scores with shape (n_samples,) while the multiclass case expects scores with shape (n_samples, n_classes). In the nulticlass case, the order of the class scores must correspond to the order of labels, if provided, or else to the numerical or lexicographical order of the labels in y_true.
kint, default=2

Number of most likely outcomes considered to find the correct label.
normalizebool, default=True

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

Sample weights. If None, all samples are given the same weight.
labelsarray-like of shape (n_classes,), default=None

Multiclass only. List of labels that index the classes in y_score. If None, the numerical or lexicographical order of the labels in y_true is used.    Returns

scorefloat

The top-k accuracy score. The best performance is 1 with normalize == True and the number of samples with normalize == False.      See also
accuracy_score

  Notes In cases where two or more labels are assigned equal predicted scores, the labels with the highest indices will be chosen first. This might impact the result if the correct label falls after the threshold because of that. Examples >>> import numpy as np
>>> from sklearn.metrics import top_k_accuracy_score
>>> y_true = np.array([0, 1, 2, 2])
>>> y_score = np.array([[0.5, 0.2, 0.2],  # 0 is in top 2
...                     [0.3, 0.4, 0.2],  # 1 is in top 2
...                     [0.2, 0.4, 0.3],  # 2 is in top 2
...                     [0.7, 0.2, 0.1]]) # 2 isn't in top 2
>>> top_k_accuracy_score(y_true, y_score, k=2)
0.75
>>> # Not normalizing gives the number of "correctly" classified samples
>>> top_k_accuracy_score(y_true, y_score, k=2, normalize=False)
3


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

V-measure cluster labeling given a ground truth. This score is identical to normalized_mutual_info_score with the 'arithmetic' option for averaging. The V-measure is the harmonic mean between homogeneity and completeness: v = (1 + beta) * homogeneity * completeness
     / (beta * homogeneity + completeness)
 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. 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

v_measurefloat

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

completeness_score

normalized_mutual_info_score

  References
1
Andrew Rosenberg and Julia Hirschberg, 2007. V-Measure: A conditional entropy-based external cluster evaluation measure   Examples Perfect labelings are both homogeneous and complete, hence have score 1.0: >>> from sklearn.metrics.cluster import v_measure_score
>>> v_measure_score([0, 0, 1, 1], [0, 0, 1, 1])
1.0
>>> v_measure_score([0, 0, 1, 1], [1, 1, 0, 0])
1.0
 Labelings that assign all classes members to the same clusters are complete be not homogeneous, hence penalized: >>> print("%.6f" % v_measure_score([0, 0, 1, 2], [0, 0, 1, 1]))
0.8...
>>> print("%.6f" % v_measure_score([0, 1, 2, 3], [0, 0, 1, 1]))
0.66...
 Labelings that have pure clusters with members coming from the same classes are homogeneous but un-necessary splits harms completeness and thus penalize V-measure as well: >>> print("%.6f" % v_measure_score([0, 0, 1, 1], [0, 0, 1, 2]))
0.8...
>>> print("%.6f" % v_measure_score([0, 0, 1, 1], [0, 1, 2, 3]))
0.66...
 If classes members are completely split across different clusters, the assignment is totally incomplete, hence the V-Measure is null: >>> print("%.6f" % v_measure_score([0, 0, 0, 0], [0, 1, 2, 3]))
0.0...
 Clusters that include samples from totally different classes totally destroy the homogeneity of the labeling, hence: >>> print("%.6f" % v_measure_score([0, 0, 1, 1], [0, 0, 0, 0]))
0.0...

 Examples using sklearn.metrics.v_measure_score

  Biclustering documents with the Spectral Co-clustering algorithm

  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.zero_one_loss
sklearn.metrics.zero_one_loss(y_true, y_pred, *, normalize=True, sample_weight=None) [source]

Zero-one classification loss. If normalize is True, return the fraction of misclassifications (float), else it returns the number of misclassifications (int). The best performance is 0. 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 misclassifications. Otherwise, return the fraction of misclassifications.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

lossfloat or int,

If normalize == True, return the fraction of misclassifications (float), else it returns the number of misclassifications (int).      See also
accuracy_score, hamming_loss, jaccard_score

  Notes In multilabel classification, the zero_one_loss function corresponds to the subset zero-one loss: for each sample, the entire set of labels must be correctly predicted, otherwise the loss for that sample is equal to one. Examples >>> from sklearn.metrics import zero_one_loss
>>> y_pred = [1, 2, 3, 4]
>>> y_true = [2, 2, 3, 4]
>>> zero_one_loss(y_true, y_pred)
0.25
>>> zero_one_loss(y_true, y_pred, normalize=False)
1
 In the multilabel case with binary label indicators: >>> import numpy as np
>>> zero_one_loss(np.array([[0, 1], [1, 1]]), np.ones((2, 2)))
0.5

 Examples using sklearn.metrics.zero_one_loss

  Discrete versus Real AdaBoost


sklearn.model_selection.check_cv
sklearn.model_selection.check_cv(cv=5, y=None, *, classifier=False) [source]

Input checker utility for building a cross-validator  Parameters

cvint, cross-validation generator or an iterable, default=None

Determines the cross-validation splitting strategy. Possible inputs for cv are: - None, to use the default 5-fold cross validation, - integer, to specify the number of folds. - CV splitter, - An iterable yielding (train, test) splits as arrays of indices. For integer/None inputs, if classifier is True and y is either binary or multiclass, StratifiedKFold is used. In all other cases, KFold is used. Refer User Guide for the various cross-validation strategies that can be used here.  Changed in version 0.22: cv default value changed from 3-fold to 5-fold.
yarray-like, default=None

The target variable for supervised learning problems.
classifierbool, default=False

Whether the task is a classification task, in which case stratified KFold will be used.    Returns

checked_cva cross-validator instance.

The return value is a cross-validator which generates the train/test splits via the split method.


sklearn.model_selection.cross_validate
sklearn.model_selection.cross_validate(estimator, X, y=None, *, groups=None, scoring=None, cv=None, n_jobs=None, verbose=0, fit_params=None, pre_dispatch='2*n_jobs', return_train_score=False, return_estimator=False, error_score=nan) [source]

Evaluate metric(s) by cross-validation and also record fit/score times. Read more in the User Guide.  Parameters

estimatorestimator object implementing ‘fit’

The object to use to fit the data.
Xarray-like of shape (n_samples, n_features)

The data to fit. Can be for example a list, or an array.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

The target variable to try to predict in the case of supervised learning.
groupsarray-like of shape (n_samples,), default=None

Group labels for the samples used while splitting the dataset into train/test set. Only used in conjunction with a “Group” cv instance (e.g., GroupKFold).
scoringstr, callable, list, tuple, or dict, default=None

Strategy to evaluate the performance of the cross-validated model on the test set. If scoring represents a single score, one can use:  a single string (see The scoring parameter: defining model evaluation rules); a callable (see Defining your scoring strategy from metric functions) that returns a single value.  If scoring reprents multiple scores, one can use:  a list or tuple of unique strings; a callable returning a dictionary where the keys are the metric names and the values are the metric scores; a dictionary with metric names as keys and callables a values.  See Specifying multiple metrics for evaluation for an example.
cvint, cross-validation generator or an iterable, default=None

Determines the cross-validation splitting strategy. Possible inputs for cv are:  None, to use the default 5-fold cross validation, int, to specify the number of folds in a (Stratified)KFold,
CV splitter, An iterable yielding (train, test) splits as arrays of indices.  For int/None inputs, if the estimator is a classifier and y is either binary or multiclass, StratifiedKFold is used. In all other cases, KFold is used. Refer User Guide for the various cross-validation strategies that can be used here.  Changed in version 0.22: cv default value if None changed from 3-fold to 5-fold.
n_jobsint, default=None

Number of jobs to run in parallel. Training the estimator and computing the score are parallelized over the cross-validation splits. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
verboseint, default=0

The verbosity level.
fit_paramsdict, default=None

Parameters to pass to the fit method of the estimator.
pre_dispatchint or str, default=’2*n_jobs’

Controls the number of jobs that get dispatched during parallel execution. Reducing this number can be useful to avoid an explosion of memory consumption when more jobs get dispatched than CPUs can process. This parameter can be:  None, in which case all the jobs are immediately created and spawned. Use this for lightweight and fast-running jobs, to avoid delays due to on-demand spawning of the jobs An int, giving the exact number of total jobs that are spawned A str, giving an expression as a function of n_jobs, as in ‘2*n_jobs’
return_train_scorebool, default=False

Whether to include train scores. Computing training scores is used to get insights on how different parameter settings impact the overfitting/underfitting trade-off. However computing the scores on the training set can be computationally expensive and is not strictly required to select the parameters that yield the best generalization performance.  New in version 0.19.   Changed in version 0.21: Default value was changed from True to False
return_estimatorbool, default=False

Whether to return the estimators fitted on each split.  New in version 0.20.
error_score‘raise’ or numeric, default=np.nan

Value to assign to the score if an error occurs in estimator fitting. If set to ‘raise’, the error is raised. If a numeric value is given, FitFailedWarning is raised.  New in version 0.20.     Returns

scoresdict of float arrays of shape (n_splits,)

Array of scores of the estimator for each run of the cross validation. A dict of arrays containing the score/time arrays for each scorer is returned. The possible keys for this dict are:
test_score

The score array for test scores on each cv split. Suffix _score in test_score changes to a specific metric like test_r2 or test_auc if there are multiple scoring metrics in the scoring parameter.
train_score

The score array for train scores on each cv split. Suffix _score in train_score changes to a specific metric like train_r2 or train_auc if there are multiple scoring metrics in the scoring parameter. This is available only if return_train_score parameter is True.
fit_time

The time for fitting the estimator on the train set for each cv split.
score_time

The time for scoring the estimator on the test set for each cv split. (Note time for scoring on the train set is not included even if return_train_score is set to True
estimator

The estimator objects for each cv split. This is available only if return_estimator parameter is set to True.        See also
cross_val_score

Run cross-validation for single metric evaluation.
cross_val_predict

Get predictions from each split of cross-validation for diagnostic purposes.
sklearn.metrics.make_scorer

Make a scorer from a performance metric or loss function.    Examples >>> from sklearn import datasets, linear_model
>>> from sklearn.model_selection import cross_validate
>>> from sklearn.metrics import make_scorer
>>> from sklearn.metrics import confusion_matrix
>>> from sklearn.svm import LinearSVC
>>> diabetes = datasets.load_diabetes()
>>> X = diabetes.data[:150]
>>> y = diabetes.target[:150]
>>> lasso = linear_model.Lasso()
 Single metric evaluation using cross_validate >>> cv_results = cross_validate(lasso, X, y, cv=3)
>>> sorted(cv_results.keys())
['fit_time', 'score_time', 'test_score']
>>> cv_results['test_score']
array([0.33150734, 0.08022311, 0.03531764])
 Multiple metric evaluation using cross_validate (please refer the scoring parameter doc for more information) >>> scores = cross_validate(lasso, X, y, cv=3,
...                         scoring=('r2', 'neg_mean_squared_error'),
...                         return_train_score=True)
>>> print(scores['test_neg_mean_squared_error'])
[-3635.5... -3573.3... -6114.7...]
>>> print(scores['train_r2'])
[0.28010158 0.39088426 0.22784852]

 Examples using sklearn.model_selection.cross_validate

  Categorical Feature Support in Gradient Boosting

  Combine predictors using stacking

  Common pitfalls in interpretation of coefficients of linear models


sklearn.model_selection.cross_val_predict
sklearn.model_selection.cross_val_predict(estimator, X, y=None, *, groups=None, cv=None, n_jobs=None, verbose=0, fit_params=None, pre_dispatch='2*n_jobs', method='predict') [source]

Generate cross-validated estimates for each input data point The data is split according to the cv parameter. Each sample belongs to exactly one test set, and its prediction is computed with an estimator fitted on the corresponding training set. Passing these predictions into an evaluation metric may not be a valid way to measure generalization performance. Results can differ from cross_validate and cross_val_score unless all tests sets have equal size and the metric decomposes over samples. Read more in the User Guide.  Parameters

estimatorestimator object implementing ‘fit’ and ‘predict’

The object to use to fit the data.
Xarray-like of shape (n_samples, n_features)

The data to fit. Can be, for example a list, or an array at least 2d.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

The target variable to try to predict in the case of supervised learning.
groupsarray-like of shape (n_samples,), default=None

Group labels for the samples used while splitting the dataset into train/test set. Only used in conjunction with a “Group” cv instance (e.g., GroupKFold).
cvint, cross-validation generator or an iterable, default=None

Determines the cross-validation splitting strategy. Possible inputs for cv are:  None, to use the default 5-fold cross validation, int, to specify the number of folds in a (Stratified)KFold,
CV splitter, An iterable yielding (train, test) splits as arrays of indices.  For int/None inputs, if the estimator is a classifier and y is either binary or multiclass, StratifiedKFold is used. In all other cases, KFold is used. Refer User Guide for the various cross-validation strategies that can be used here.  Changed in version 0.22: cv default value if None changed from 3-fold to 5-fold.
n_jobsint, default=None

Number of jobs to run in parallel. Training the estimator and predicting are parallelized over the cross-validation splits. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
verboseint, default=0

The verbosity level.
fit_paramsdict, defualt=None

Parameters to pass to the fit method of the estimator.
pre_dispatchint or str, default=’2*n_jobs’

Controls the number of jobs that get dispatched during parallel execution. Reducing this number can be useful to avoid an explosion of memory consumption when more jobs get dispatched than CPUs can process. This parameter can be:  None, in which case all the jobs are immediately created and spawned. Use this for lightweight and fast-running jobs, to avoid delays due to on-demand spawning of the jobs An int, giving the exact number of total jobs that are spawned A str, giving an expression as a function of n_jobs, as in ‘2*n_jobs’
method{‘predict’, ‘predict_proba’, ‘predict_log_proba’, ‘decision_function’}, default=’predict’

The method to be invoked by estimator.    Returns

predictionsndarray

This is the result of calling method. Shape:  When method is ‘predict’ and in special case where method is ‘decision_function’ and the target is binary: (n_samples,) When method is one of {‘predict_proba’, ‘predict_log_proba’, ‘decision_function’} (unless special case above): (n_samples, n_classes) If estimator is multioutput, an extra dimension ‘n_outputs’ is added to the end of each shape above.       See also
cross_val_score

Calculate score for each CV split.
cross_validate

Calculate one or more scores and timings for each CV split.    Notes In the case that one or more classes are absent in a training portion, a default score needs to be assigned to all instances for that class if method produces columns per class, as in {‘decision_function’, ‘predict_proba’, ‘predict_log_proba’}. For predict_proba this value is 0. In order to ensure finite output, we approximate negative infinity by the minimum finite float value for the dtype in other cases. Examples >>> from sklearn import datasets, linear_model
>>> from sklearn.model_selection import cross_val_predict
>>> diabetes = datasets.load_diabetes()
>>> X = diabetes.data[:150]
>>> y = diabetes.target[:150]
>>> lasso = linear_model.Lasso()
>>> y_pred = cross_val_predict(lasso, X, y, cv=3)

 Examples using sklearn.model_selection.cross_val_predict

  Combine predictors using stacking

  Plotting Cross-Validated Predictions


sklearn.model_selection.cross_val_score
sklearn.model_selection.cross_val_score(estimator, X, y=None, *, groups=None, scoring=None, cv=None, n_jobs=None, verbose=0, fit_params=None, pre_dispatch='2*n_jobs', error_score=nan) [source]

Evaluate a score by cross-validation Read more in the User Guide.  Parameters

estimatorestimator object implementing ‘fit’

The object to use to fit the data.
Xarray-like of shape (n_samples, n_features)

The data to fit. Can be for example a list, or an array.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

The target variable to try to predict in the case of supervised learning.
groupsarray-like of shape (n_samples,), default=None

Group labels for the samples used while splitting the dataset into train/test set. Only used in conjunction with a “Group” cv instance (e.g., GroupKFold).
scoringstr or callable, default=None

A str (see model evaluation documentation) or a scorer callable object / function with signature scorer(estimator, X, y) which should return only a single value. Similar to cross_validate but only a single metric is permitted. If None, the estimator’s default scorer (if available) is used.
cvint, cross-validation generator or an iterable, default=None

Determines the cross-validation splitting strategy. Possible inputs for cv are:  None, to use the default 5-fold cross validation, int, to specify the number of folds in a (Stratified)KFold,
CV splitter, An iterable yielding (train, test) splits as arrays of indices.  For int/None inputs, if the estimator is a classifier and y is either binary or multiclass, StratifiedKFold is used. In all other cases, KFold is used. Refer User Guide for the various cross-validation strategies that can be used here.  Changed in version 0.22: cv default value if None changed from 3-fold to 5-fold.
n_jobsint, default=None

Number of jobs to run in parallel. Training the estimator and computing the score are parallelized over the cross-validation splits. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
verboseint, default=0

The verbosity level.
fit_paramsdict, default=None

Parameters to pass to the fit method of the estimator.
pre_dispatchint or str, default=’2*n_jobs’

Controls the number of jobs that get dispatched during parallel execution. Reducing this number can be useful to avoid an explosion of memory consumption when more jobs get dispatched than CPUs can process. This parameter can be:  None, in which case all the jobs are immediately created and spawned. Use this for lightweight and fast-running jobs, to avoid delays due to on-demand spawning of the jobs An int, giving the exact number of total jobs that are spawned A str, giving an expression as a function of n_jobs, as in ‘2*n_jobs’
error_score‘raise’ or numeric, default=np.nan

Value to assign to the score if an error occurs in estimator fitting. If set to ‘raise’, the error is raised. If a numeric value is given, FitFailedWarning is raised.  New in version 0.20.     Returns

scoresndarray of float of shape=(len(list(cv)),)

Array of scores of the estimator for each run of the cross validation.      See also
cross_validate

To run cross-validation on multiple metrics and also to return train scores, fit times and score times.
cross_val_predict

Get predictions from each split of cross-validation for diagnostic purposes.
sklearn.metrics.make_scorer

Make a scorer from a performance metric or loss function.    Examples >>> from sklearn import datasets, linear_model
>>> from sklearn.model_selection import cross_val_score
>>> diabetes = datasets.load_diabetes()
>>> X = diabetes.data[:150]
>>> y = diabetes.target[:150]
>>> lasso = linear_model.Lasso()
>>> print(cross_val_score(lasso, X, y, cv=3))
[0.33150734 0.08022311 0.03531764]

 Examples using sklearn.model_selection.cross_val_score

  Model selection with Probabilistic PCA and Factor Analysis (FA)

  Imputing missing values with variants of IterativeImputer

  Imputing missing values before building an estimator

  Underfitting vs. Overfitting

  Receiver Operating Characteristic (ROC) with cross validation

  Nested versus non-nested cross-validation

  SVM-Anova: SVM with univariate feature selection

  Cross-validation on Digits Dataset Exercise


sklearn.model_selection.learning_curve
sklearn.model_selection.learning_curve(estimator, X, y, *, groups=None, train_sizes=array([0.1, 0.33, 0.55, 0.78, 1.0]), cv=None, scoring=None, exploit_incremental_learning=False, n_jobs=None, pre_dispatch='all', verbose=0, shuffle=False, random_state=None, error_score=nan, return_times=False, fit_params=None) [source]

Learning curve. Determines cross-validated training and test scores for different training set sizes. A cross-validation generator splits the whole dataset k times in training and test data. Subsets of the training set with varying sizes will be used to train the estimator and a score for each training subset size and the test set will be computed. Afterwards, the scores will be averaged over all k runs for each training subset size. Read more in the User Guide.  Parameters

estimatorobject type that implements the “fit” and “predict” methods

An object of that type which is cloned for each validation.
Xarray-like of shape (n_samples, n_features)

Training vector, where n_samples is the number of samples and n_features is the number of features.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

Target relative to X for classification or regression; None for unsupervised learning.
groupsarray-like of shape (n_samples,), default=None

Group labels for the samples used while splitting the dataset into train/test set. Only used in conjunction with a “Group” cv instance (e.g., GroupKFold).
train_sizesarray-like of shape (n_ticks,), default=np.linspace(0.1, 1.0, 5)

Relative or absolute numbers of training examples that will be used to generate the learning curve. If the dtype is float, it is regarded as a fraction of the maximum size of the training set (that is determined by the selected validation method), i.e. it has to be within (0, 1]. Otherwise it is interpreted as absolute sizes of the training sets. Note that for classification the number of samples usually have to be big enough to contain at least one sample from each class.
cvint, cross-validation generator or an iterable, default=None

Determines the cross-validation splitting strategy. Possible inputs for cv are:  None, to use the default 5-fold cross validation, int, to specify the number of folds in a (Stratified)KFold,
CV splitter, An iterable yielding (train, test) splits as arrays of indices.  For int/None inputs, if the estimator is a classifier and y is either binary or multiclass, StratifiedKFold is used. In all other cases, KFold is used. Refer User Guide for the various cross-validation strategies that can be used here.  Changed in version 0.22: cv default value if None changed from 3-fold to 5-fold.
scoringstr or callable, default=None

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

If the estimator supports incremental learning, this will be used to speed up fitting for different training set sizes.
n_jobsint, default=None

Number of jobs to run in parallel. Training the estimator and computing the score are parallelized over the different training and test sets. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
pre_dispatchint or str, default=’all’

Number of predispatched jobs for parallel execution (default is all). The option can reduce the allocated memory. The str can be an expression like ‘2*n_jobs’.
verboseint, default=0

Controls the verbosity: the higher, the more messages.
shufflebool, default=False

Whether to shuffle training data before taking prefixes of it based on``train_sizes``.
random_stateint, RandomState instance or None, default=None

Used when shuffle is True. Pass an int for reproducible output across multiple function calls. See Glossary.
error_score‘raise’ or numeric, default=np.nan

Value to assign to the score if an error occurs in estimator fitting. If set to ‘raise’, the error is raised. If a numeric value is given, FitFailedWarning is raised.  New in version 0.20.
return_timesbool, default=False

Whether to return the fit and score times.
fit_paramsdict, default=None

Parameters to pass to the fit method of the estimator.  New in version 0.24.     Returns

train_sizes_absarray of shape (n_unique_ticks,)

Numbers of training examples that has been used to generate the learning curve. Note that the number of ticks might be less than n_ticks because duplicate entries will be removed.
train_scoresarray of shape (n_ticks, n_cv_folds)

Scores on training sets.
test_scoresarray of shape (n_ticks, n_cv_folds)

Scores on test set.
fit_timesarray of shape (n_ticks, n_cv_folds)

Times spent for fitting in seconds. Only present if return_times is True.
score_timesarray of shape (n_ticks, n_cv_folds)

Times spent for scoring in seconds. Only present if return_times is True.     Notes See examples/model_selection/plot_learning_curve.py
 Examples using sklearn.model_selection.learning_curve

  Comparison of kernel ridge regression and SVR

  Plotting Learning Curves


sklearn.model_selection.permutation_test_score
sklearn.model_selection.permutation_test_score(estimator, X, y, *, groups=None, cv=None, n_permutations=100, n_jobs=None, random_state=0, verbose=0, scoring=None, fit_params=None) [source]

Evaluate the significance of a cross-validated score with permutations Permutes targets to generate ‘randomized data’ and compute the empirical p-value against the null hypothesis that features and targets are independent. The p-value represents the fraction of randomized data sets where the estimator performed as well or better than in the original data. A small p-value suggests that there is a real dependency between features and targets which has been used by the estimator to give good predictions. A large p-value may be due to lack of real dependency between features and targets or the estimator was not able to use the dependency to give good predictions. Read more in the User Guide.  Parameters

estimatorestimator object implementing ‘fit’

The object to use to fit the data.
Xarray-like of shape at least 2D

The data to fit.
yarray-like of shape (n_samples,) or (n_samples, n_outputs) or None

The target variable to try to predict in the case of supervised learning.
groupsarray-like of shape (n_samples,), default=None

Labels to constrain permutation within groups, i.e. y values are permuted among samples with the same group identifier. When not specified, y values are permuted among all samples. When a grouped cross-validator is used, the group labels are also passed on to the split method of the cross-validator. The cross-validator uses them for grouping the samples while splitting the dataset into train/test set.
scoringstr or callable, default=None

A single str (see The scoring parameter: defining model evaluation rules) or a callable (see Defining your scoring strategy from metric functions) to evaluate the predictions on the test set. If None the estimator’s score method is used.
cvint, cross-validation generator or an iterable, default=None

Determines the cross-validation splitting strategy. Possible inputs for cv are:  None, to use the default 5-fold cross validation, int, to specify the number of folds in a (Stratified)KFold,
CV splitter, An iterable yielding (train, test) splits as arrays of indices.  For int/None inputs, if the estimator is a classifier and y is either binary or multiclass, StratifiedKFold is used. In all other cases, KFold is used. Refer User Guide for the various cross-validation strategies that can be used here.  Changed in version 0.22: cv default value if None changed from 3-fold to 5-fold.
n_permutationsint, default=100

Number of times to permute y.
n_jobsint, default=None

Number of jobs to run in parallel. Training the estimator and computing the cross-validated score are parallelized over the permutations. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
random_stateint, RandomState instance or None, default=0

Pass an int for reproducible output for permutation of y values among samples. See Glossary.
verboseint, default=0

The verbosity level.
fit_paramsdict, default=None

Parameters to pass to the fit method of the estimator.  New in version 0.24.     Returns

scorefloat

The true score without permuting targets.
permutation_scoresarray of shape (n_permutations,)

The scores obtained for each permutations.
pvaluefloat

The p-value, which approximates the probability that the score would be obtained by chance. This is calculated as: (C + 1) / (n_permutations + 1) Where C is the number of permutations whose score >= the true score. The best possible p-value is 1/(n_permutations + 1), the worst is 1.0.     Notes This function implements Test 1 in: Ojala and Garriga. Permutation Tests for Studying Classifier Performance. The Journal of Machine Learning Research (2010) vol. 11
 Examples using sklearn.model_selection.permutation_test_score

  Test with permutations the significance of a classification score


sklearn.model_selection.train_test_split
sklearn.model_selection.train_test_split(*arrays, test_size=None, train_size=None, random_state=None, shuffle=True, stratify=None) [source]

Split arrays or matrices into random train and test subsets Quick utility that wraps input validation and next(ShuffleSplit().split(X, y)) and application to input data into a single call for splitting (and optionally subsampling) data in a oneliner. Read more in the User Guide.  Parameters

*arrayssequence of indexables with same length / shape[0]

Allowed inputs are lists, numpy arrays, scipy-sparse matrices or pandas dataframes.
test_sizefloat or int, default=None

If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the test split. If int, represents the absolute number of test samples. If None, the value is set to the complement of the train size. If train_size is also None, it will be set to 0.25.
train_sizefloat or int, default=None

If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the train split. If int, represents the absolute number of train samples. If None, the value is automatically set to the complement of the test size.
random_stateint, RandomState instance or None, default=None

Controls the shuffling applied to the data before applying the split. Pass an int for reproducible output across multiple function calls. See Glossary.
shufflebool, default=True

Whether or not to shuffle the data before splitting. If shuffle=False then stratify must be None.
stratifyarray-like, default=None

If not None, data is split in a stratified fashion, using this as the class labels. Read more in the User Guide.    Returns

splittinglist, length=2 * len(arrays)

List containing train-test split of inputs.  New in version 0.16: If the input is sparse, the output will be a scipy.sparse.csr_matrix. Else, output type is the same as the input type.      Examples >>> import numpy as np
>>> from sklearn.model_selection import train_test_split
>>> X, y = np.arange(10).reshape((5, 2)), range(5)
>>> X
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])
>>> list(y)
[0, 1, 2, 3, 4]
 >>> X_train, X_test, y_train, y_test = train_test_split(
...     X, y, test_size=0.33, random_state=42)
...
>>> X_train
array([[4, 5],
       [0, 1],
       [6, 7]])
>>> y_train
[2, 0, 3]
>>> X_test
array([[2, 3],
       [8, 9]])
>>> y_test
[1, 4]
 >>> train_test_split(y, shuffle=False)
[[0, 1, 2], [3, 4]]

 Examples using sklearn.model_selection.train_test_split

  Release Highlights for scikit-learn 0.23

  Release Highlights for scikit-learn 0.24

  Release Highlights for scikit-learn 0.22

  Probability Calibration curves

  Probability calibration of classifiers

  Recognizing hand-written digits

  Classifier comparison

  Principal Component Regression vs Partial Least Squares Regression

  Post pruning decision trees with cost complexity pruning

  Understanding the decision tree structure

  Comparing random forests and the multi-output meta estimator

  Gradient Boosting regression

  Early stopping of Gradient Boosting

  Feature transformations with ensembles of trees

  Gradient Boosting Out-of-Bag estimates

  Faces recognition example using eigenfaces and SVMs

  Prediction Latency

  Pipeline Anova SVM

  Univariate Feature Selection

  Non-negative least squares

  Comparing various online solvers

  MNIST classification using multinomial logistic + L1

  Multiclass sparse logistic regression on 20newgroups

  Early stopping of Stochastic Gradient Descent

  Poisson regression and non-normal loss

  Tweedie regression on insurance claims

  Permutation Importance with Multicollinear or Correlated Features

  Permutation Importance vs Random Forest Feature Importance (MDI)

  Partial Dependence and Individual Conditional Expectation Plots

  Common pitfalls in interpretation of coefficients of linear models

  Scalable learning with polynomial kernel aproximation

  ROC Curve with Visualization API

  Visualizations with Display Objects

  Confusion matrix

  Detection error tradeoff (DET) curve

  Parameter estimation using grid search with cross-validation

  Receiver Operating Characteristic (ROC)

  Precision-Recall

  Classifier Chain

  Comparing Nearest Neighbors with and without Neighborhood Components Analysis

  Dimensionality Reduction with Neighborhood Components Analysis

  Restricted Boltzmann Machine features for digit classification

  Varying regularization in Multi-layer Perceptron

  Column Transformer with Mixed Types

  Effect of transforming the targets in regression model

  Importance of Feature Scaling

  Map data to a normal distribution

  Feature discretization

  Semi-supervised Classification on a Text Dataset


sklearn.model_selection.validation_curve
sklearn.model_selection.validation_curve(estimator, X, y, *, param_name, param_range, groups=None, cv=None, scoring=None, n_jobs=None, pre_dispatch='all', verbose=0, error_score=nan, fit_params=None) [source]

Validation curve. Determine training and test scores for varying parameter values. Compute scores for an estimator with different values of a specified parameter. This is similar to grid search with one parameter. However, this will also compute training scores and is merely a utility for plotting the results. Read more in the User Guide.  Parameters

estimatorobject type that implements the “fit” and “predict” methods

An object of that type which is cloned for each validation.
Xarray-like of shape (n_samples, n_features)

Training vector, where n_samples is the number of samples and n_features is the number of features.
yarray-like of shape (n_samples,) or (n_samples, n_outputs) or None

Target relative to X for classification or regression; None for unsupervised learning.
param_namestr

Name of the parameter that will be varied.
param_rangearray-like of shape (n_values,)

The values of the parameter that will be evaluated.
groupsarray-like of shape (n_samples,), default=None

Group labels for the samples used while splitting the dataset into train/test set. Only used in conjunction with a “Group” cv instance (e.g., GroupKFold).
cvint, cross-validation generator or an iterable, default=None

Determines the cross-validation splitting strategy. Possible inputs for cv are:  None, to use the default 5-fold cross validation, int, to specify the number of folds in a (Stratified)KFold,
CV splitter, An iterable yielding (train, test) splits as arrays of indices.  For int/None inputs, if the estimator is a classifier and y is either binary or multiclass, StratifiedKFold is used. In all other cases, KFold is used. Refer User Guide for the various cross-validation strategies that can be used here.  Changed in version 0.22: cv default value if None changed from 3-fold to 5-fold.
scoringstr or callable, default=None

A str (see model evaluation documentation) or a scorer callable object / function with signature scorer(estimator, X, y).
n_jobsint, default=None

Number of jobs to run in parallel. Training the estimator and computing the score are parallelized over the combinations of each parameter value and each cross-validation split. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
pre_dispatchint or str, default=’all’

Number of predispatched jobs for parallel execution (default is all). The option can reduce the allocated memory. The str can be an expression like ‘2*n_jobs’.
verboseint, default=0

Controls the verbosity: the higher, the more messages.
fit_paramsdict, default=None

Parameters to pass to the fit method of the estimator.  New in version 0.24.
error_score‘raise’ or numeric, default=np.nan

Value to assign to the score if an error occurs in estimator fitting. If set to ‘raise’, the error is raised. If a numeric value is given, FitFailedWarning is raised.  New in version 0.20.     Returns

train_scoresarray of shape (n_ticks, n_cv_folds)

Scores on training sets.
test_scoresarray of shape (n_ticks, n_cv_folds)

Scores on test set.     Notes See Plotting Validation Curves
 Examples using sklearn.model_selection.validation_curve

  Plotting Validation Curves


sklearn.neighbors.kneighbors_graph
sklearn.neighbors.kneighbors_graph(X, n_neighbors, *, mode='connectivity', metric='minkowski', p=2, metric_params=None, include_self=False, n_jobs=None) [source]

Computes the (weighted) graph of k-Neighbors for points in X Read more in the User Guide.  Parameters

Xarray-like of shape (n_samples, n_features) or BallTree

Sample data, in the form of a numpy array or a precomputed BallTree.
n_neighborsint

Number of neighbors for each sample.
mode{‘connectivity’, ‘distance’}, default=’connectivity’

Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, and ‘distance’ will return the distances between neighbors according to the given metric.
metricstr, default=’minkowski’

The distance metric used to calculate the k-Neighbors for each sample point. The DistanceMetric class gives a list of available metrics. The default distance is ‘euclidean’ (‘minkowski’ metric with the p param equal to 2.)
pint, default=2

Power parameter for the Minkowski metric. When p = 1, this is equivalent to using manhattan_distance (l1), and euclidean_distance (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.
metric_paramsdict, default=None

additional keyword arguments for the metric function.
include_selfbool or ‘auto’, default=False

Whether or not to mark each sample as the first nearest neighbor to itself. If ‘auto’, then True is used for mode=’connectivity’ and False for mode=’distance’.
n_jobsint, 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

Asparse matrix of shape (n_samples, n_samples)

Graph where A[i, j] is assigned the weight of edge that connects i to j. The matrix is of CSR format.      See also
radius_neighbors_graph

  Examples >>> X = [[0], [3], [1]]
>>> from sklearn.neighbors import kneighbors_graph
>>> A = kneighbors_graph(X, 2, mode='connectivity', include_self=True)
>>> A.toarray()
array([[1., 0., 1.],
       [0., 1., 1.],
       [1., 0., 1.]])

 Examples using sklearn.neighbors.kneighbors_graph

  Agglomerative clustering with and without structure

  Hierarchical clustering: structured vs unstructured ward

  Comparing different clustering algorithms on toy datasets


sklearn.neighbors.radius_neighbors_graph
sklearn.neighbors.radius_neighbors_graph(X, radius, *, mode='connectivity', metric='minkowski', p=2, metric_params=None, include_self=False, n_jobs=None) [source]

Computes the (weighted) graph of Neighbors for points in X Neighborhoods are restricted the points at a distance lower than radius. Read more in the User Guide.  Parameters

Xarray-like of shape (n_samples, n_features) or BallTree

Sample data, in the form of a numpy array or a precomputed BallTree.
radiusfloat

Radius of neighborhoods.
mode{‘connectivity’, ‘distance’}, default=’connectivity’

Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, and ‘distance’ will return the distances between neighbors according to the given metric.
metricstr, default=’minkowski’

The distance metric used to calculate the neighbors within a given radius for each sample point. The DistanceMetric class gives a list of available metrics. The default distance is ‘euclidean’ (‘minkowski’ metric with the param equal to 2.)
pint, default=2

Power parameter for the Minkowski metric. When p = 1, this is equivalent to using manhattan_distance (l1), and euclidean_distance (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.
metric_paramsdict, default=None

additional keyword arguments for the metric function.
include_selfbool or ‘auto’, default=False

Whether or not to mark each sample as the first nearest neighbor to itself. If ‘auto’, then True is used for mode=’connectivity’ and False for mode=’distance’.
n_jobsint, 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

Asparse matrix of shape (n_samples, n_samples)

Graph where A[i, j] is assigned the weight of edge that connects i to j. The matrix is of CSR format.      See also
kneighbors_graph

  Examples >>> X = [[0], [3], [1]]
>>> from sklearn.neighbors import radius_neighbors_graph
>>> A = radius_neighbors_graph(X, 1.5, mode='connectivity',
...                            include_self=True)
>>> A.toarray()
array([[1., 0., 1.],
       [0., 1., 0.],
       [1., 0., 1.]])


sklearn.pipeline.make_pipeline
sklearn.pipeline.make_pipeline(*steps, memory=None, verbose=False) [source]

Construct a Pipeline from the given estimators. This is a shorthand for the Pipeline constructor; it does not require, and does not permit, naming the estimators. Instead, their names will be set to the lowercase of their types automatically.  Parameters

*stepslist of estimators.

memorystr or object with the joblib.Memory interface, default=None

Used to cache the fitted transformers of the pipeline. By default, no caching is performed. If a string is given, it is the path to the caching directory. Enabling caching triggers a clone of the transformers before fitting. Therefore, the transformer instance given to the pipeline cannot be inspected directly. Use the attribute named_steps or steps to inspect estimators within the pipeline. Caching the transformers is advantageous when fitting is time consuming.
verbosebool, default=False

If True, the time elapsed while fitting each step will be printed as it is completed.    Returns

pPipeline
    See also
Pipeline

Class for creating a pipeline of transforms with a final estimator.    Examples >>> from sklearn.naive_bayes import GaussianNB
>>> from sklearn.preprocessing import StandardScaler
>>> make_pipeline(StandardScaler(), GaussianNB(priors=None))
Pipeline(steps=[('standardscaler', StandardScaler()),
                ('gaussiannb', GaussianNB())])

 Examples using sklearn.pipeline.make_pipeline

  Release Highlights for scikit-learn 0.23

  Release Highlights for scikit-learn 0.24

  Release Highlights for scikit-learn 0.22

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

  Principal Component Regression vs Partial Least Squares Regression

  Feature transformations with ensembles of trees

  Categorical Feature Support in Gradient Boosting

  Combine predictors using stacking

  Pipeline Anova SVM

  Univariate Feature Selection

  Polynomial interpolation

  Robust linear estimator fitting

  Poisson regression and non-normal loss

  Tweedie regression on insurance claims

  Partial Dependence and Individual Conditional Expectation Plots

  Common pitfalls in interpretation of coefficients of linear models

  Scalable learning with polynomial kernel aproximation

  Visualizations with Display Objects

  Advanced Plotting With Partial Dependence

  Imputing missing values with variants of IterativeImputer

  Imputing missing values before building an estimator

  Detection error tradeoff (DET) curve

  Dimensionality Reduction with Neighborhood Components Analysis

  Approximate nearest neighbors in TSNE

  Varying regularization in Multi-layer Perceptron

  Importance of Feature Scaling

  Feature discretization

  Clustering text documents using k-means


sklearn.pipeline.make_union
sklearn.pipeline.make_union(*transformers, n_jobs=None, verbose=False) [source]

Construct a FeatureUnion from the given transformers. This is a shorthand for the FeatureUnion constructor; it does not require, and does not permit, naming the transformers. Instead, they will be given names automatically based on their types. It also does not allow weighting.  Parameters

*transformerslist of estimators

n_jobsint, default=None

Number of jobs to run in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.  Changed in version v0.20: n_jobs default changed from 1 to None
verbosebool, default=False

If True, the time elapsed while fitting each transformer will be printed as it is completed.    Returns

fFeatureUnion
    See also
FeatureUnion

Class for concatenating the results of multiple transformer objects.    Examples >>> from sklearn.decomposition import PCA, TruncatedSVD
>>> from sklearn.pipeline import make_union
>>> make_union(PCA(), TruncatedSVD())
 FeatureUnion(transformer_list=[('pca', PCA()),
                               ('truncatedsvd', TruncatedSVD())])


sklearn.preprocessing.add_dummy_feature
sklearn.preprocessing.add_dummy_feature(X, value=1.0) [source]

Augment dataset with an additional dummy feature. This is useful for fitting an intercept term with implementations which cannot otherwise fit it directly.  Parameters

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

Data.
valuefloat

Value to use for the dummy feature.    Returns

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

Same data with dummy feature added as first column.     Examples >>> from sklearn.preprocessing import add_dummy_feature
>>> add_dummy_feature([[0, 1], [1, 0]])
array([[1., 0., 1.],
       [1., 1., 0.]])


sklearn.preprocessing.binarize
sklearn.preprocessing.binarize(X, *, threshold=0.0, copy=True) [source]

Boolean thresholding of array-like or scipy.sparse matrix. Read more in the User Guide.  Parameters

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

The data to binarize, element by element. scipy.sparse matrices should be in CSR or CSC format to avoid an un-necessary copy.
thresholdfloat, default=0.0

Feature values below or equal to this are replaced by 0, above it by 1. Threshold may not be less than 0 for operations on sparse matrices.
copybool, default=True

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

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

The transformed data.      See also
Binarizer

Performs binarization using the Transformer API (e.g. as part of a preprocessing Pipeline).


sklearn.preprocessing.label_binarize
sklearn.preprocessing.label_binarize(y, *, classes, neg_label=0, pos_label=1, sparse_output=False) [source]

Binarize labels in a one-vs-all fashion. Several regression and binary classification algorithms are available in scikit-learn. A simple way to extend these algorithms to the multi-class classification case is to use the so-called one-vs-all scheme. This function makes it possible to compute this transformation for a fixed set of class labels known ahead of time.  Parameters

yarray-like

Sequence of integer labels or multilabel data to encode.
classesarray-like of shape (n_classes,)

Uniquely holds the label for each class.
neg_labelint, default=0

Value with which negative labels must be encoded.
pos_labelint, default=1

Value with which positive labels must be encoded.
sparse_outputbool, default=False,

Set to true if output binary array is desired in CSR sparse format.    Returns

Y{ndarray, sparse matrix} of shape (n_samples, n_classes)

Shape will be (n_samples, 1) for binary problems. Sparse matrix will be of CSR format.      See also
LabelBinarizer

Class used to wrap the functionality of label_binarize and allow for fitting to classes independently of the transform operation.    Examples >>> from sklearn.preprocessing import label_binarize
>>> label_binarize([1, 6], classes=[1, 2, 4, 6])
array([[1, 0, 0, 0],
       [0, 0, 0, 1]])
 The class ordering is preserved: >>> label_binarize([1, 6], classes=[1, 6, 4, 2])
array([[1, 0, 0, 0],
       [0, 1, 0, 0]])
 Binary targets transform to a column vector >>> label_binarize(['yes', 'no', 'no', 'yes'], classes=['no', 'yes'])
array([[1],
       [0],
       [0],
       [1]])

 Examples using sklearn.preprocessing.label_binarize

  Receiver Operating Characteristic (ROC)

  Precision-Recall


sklearn.preprocessing.maxabs_scale
sklearn.preprocessing.maxabs_scale(X, *, axis=0, copy=True) [source]

Scale each feature to the [-1, 1] range without breaking the sparsity. This estimator scales each feature individually such that the maximal absolute value of each feature in the training set will be 1.0. This scaler can also be applied to sparse CSR or CSC matrices.  Parameters

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

The data.
axisint, default=0

axis used to scale along. If 0, independently scale each feature, otherwise (if 1) scale each sample.
copybool, default=True

Set to False to perform inplace scaling and avoid a copy (if the input is already a numpy array).    Returns

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

The transformed data.    Warning Risk of data leak Do not use maxabs_scale unless you know what you are doing. A common mistake is to apply it to the entire data before splitting into training and test sets. This will bias the model evaluation because information would have leaked from the test set to the training set. In general, we recommend using MaxAbsScaler within a Pipeline in order to prevent most risks of data leaking: pipe = make_pipeline(MaxAbsScaler(), LogisticRegression()).     See also
MaxAbsScaler

Performs scaling to the [-1, 1] range using the Transformer API (e.g. as part of a preprocessing Pipeline).    Notes NaNs are treated as missing values: disregarded to compute the statistics, and maintained during the data transformation. For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py.


sklearn.preprocessing.minmax_scale
sklearn.preprocessing.minmax_scale(X, feature_range=0, 1, *, axis=0, copy=True) [source]

Transform features by scaling each feature to a given range. This estimator scales and translates each feature individually such that it is in the given range on the training set, i.e. between zero and one. The transformation is given by (when axis=0): X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))
X_scaled = X_std * (max - min) + min
 where min, max = feature_range. The transformation is calculated as (when axis=0): X_scaled = scale * X + min - X.min(axis=0) * scale
where scale = (max - min) / (X.max(axis=0) - X.min(axis=0))
 This transformation is often used as an alternative to zero mean, unit variance scaling. Read more in the User Guide.  New in version 0.17: minmax_scale function interface to MinMaxScaler.   Parameters

Xarray-like of shape (n_samples, n_features)

The data.
feature_rangetuple (min, max), default=(0, 1)

Desired range of transformed data.
axisint, default=0

Axis used to scale along. If 0, independently scale each feature, otherwise (if 1) scale each sample.
copybool, default=True

Set to False to perform inplace scaling and avoid a copy (if the input is already a numpy array).    Returns

X_trndarray of shape (n_samples, n_features)

The transformed data.    Warning Risk of data leak Do not use minmax_scale unless you know what you are doing. A common mistake is to apply it to the entire data before splitting into training and test sets. This will bias the model evaluation because information would have leaked from the test set to the training set. In general, we recommend using MinMaxScaler within a Pipeline in order to prevent most risks of data leaking: pipe = make_pipeline(MinMaxScaler(), LogisticRegression()).     See also
MinMaxScaler

Performs scaling to a given range using the Transformer API (e.g. as part of a preprocessing Pipeline).    Notes For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py.
 Examples using sklearn.preprocessing.minmax_scale

  Compare the effect of different scalers on data with outliers


sklearn.preprocessing.normalize
sklearn.preprocessing.normalize(X, norm='l2', *, axis=1, copy=True, return_norm=False) [source]

Scale input vectors individually to unit norm (vector length). Read more in the User Guide.  Parameters

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

The data to normalize, element by element. scipy.sparse matrices should be in CSR format to avoid an un-necessary copy.
norm{‘l1’, ‘l2’, ‘max’}, default=’l2’

The norm to use to normalize each non zero sample (or each non-zero feature if axis is 0).
axis{0, 1}, default=1

axis used to normalize the data along. If 1, independently normalize each sample, otherwise (if 0) normalize each feature.
copybool, default=True

set to False to perform inplace row normalization and avoid a copy (if the input is already a numpy array or a scipy.sparse CSR matrix and if axis is 1).
return_normbool, default=False

whether to return the computed norms    Returns

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

Normalized input X.
normsndarray of shape (n_samples, ) if axis=1 else (n_features, )

An array of norms along given axis for X. When X is sparse, a NotImplementedError will be raised for norm ‘l1’ or ‘l2’.      See also
Normalizer

Performs normalization using the Transformer API (e.g. as part of a preprocessing Pipeline).    Notes For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py.


sklearn.preprocessing.power_transform
sklearn.preprocessing.power_transform(X, method='yeo-johnson', *, standardize=True, copy=True) [source]

Power transforms are a family of parametric, monotonic transformations that are applied to make data more Gaussian-like. This is useful for modeling issues related to heteroscedasticity (non-constant variance), or other situations where normality is desired. Currently, power_transform supports the Box-Cox transform and the Yeo-Johnson transform. The optimal parameter for stabilizing variance and minimizing skewness is estimated through maximum likelihood. Box-Cox requires input data to be strictly positive, while Yeo-Johnson supports both positive or negative data. By default, zero-mean, unit-variance normalization is applied to the transformed data. Read more in the User Guide.  Parameters

Xarray-like of shape (n_samples, n_features)

The data to be transformed using a power transformation.
method{‘yeo-johnson’, ‘box-cox’}, default=’yeo-johnson’

The power transform method. Available methods are:  ‘yeo-johnson’ [1], works with positive and negative values ‘box-cox’ [2], only works with strictly positive values   Changed in version 0.23: The default value of the method parameter changed from ‘box-cox’ to ‘yeo-johnson’ in 0.23.
standardizebool, default=True

Set to True to apply zero-mean, unit-variance normalization to the transformed output.
copybool, default=True

Set to False to perform inplace computation during transformation.    Returns

X_transndarray of shape (n_samples, n_features)

The transformed data.      See also
PowerTransformer

Equivalent transformation with the Transformer API (e.g. as part of a preprocessing Pipeline).
quantile_transform

Maps data to a standard normal distribution with the parameter output_distribution='normal'.    Notes NaNs are treated as missing values: disregarded in fit, and maintained in transform. For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py. References
1
I.K. Yeo and R.A. Johnson, “A new family of power transformations to improve normality or symmetry.” Biometrika, 87(4), pp.954-959, (2000).
2
G.E.P. Box and D.R. Cox, “An Analysis of Transformations”, Journal of the Royal Statistical Society B, 26, 211-252 (1964).   Examples >>> import numpy as np
>>> from sklearn.preprocessing import power_transform
>>> data = [[1, 2], [3, 2], [4, 5]]
>>> print(power_transform(data, method='box-cox'))
[[-1.332... -0.707...]
 [ 0.256... -0.707...]
 [ 1.076...  1.414...]]
  Warning Risk of data leak. Do not use power_transform unless you know what you are doing. A common mistake is to apply it to the entire data before splitting into training and test sets. This will bias the model evaluation because information would have leaked from the test set to the training set. In general, we recommend using PowerTransformer within a Pipeline in order to prevent most risks of data leaking, e.g.: pipe = make_pipeline(PowerTransformer(),
LogisticRegression()).


sklearn.preprocessing.quantile_transform
sklearn.preprocessing.quantile_transform(X, *, axis=0, n_quantiles=1000, output_distribution='uniform', ignore_implicit_zeros=False, subsample=100000, random_state=None, copy=True) [source]

Transform features using quantiles information. This method transforms the features to follow a uniform or a normal distribution. Therefore, for a given feature, this transformation tends to spread out the most frequent values. It also reduces the impact of (marginal) outliers: this is therefore a robust preprocessing scheme. The transformation is applied on each feature independently. First an estimate of the cumulative distribution function of a feature is used to map the original values to a uniform distribution. The obtained values are then mapped to the desired output distribution using the associated quantile function. Features values of new/unseen data that fall below or above the fitted range will be mapped to the bounds of the output distribution. Note that this transform is non-linear. It may distort linear correlations between variables measured at the same scale but renders variables measured at different scales more directly comparable. Read more in the User Guide.  Parameters

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

The data to transform.
axisint, default=0

Axis used to compute the means and standard deviations along. If 0, transform each feature, otherwise (if 1) transform each sample.
n_quantilesint, default=1000 or n_samples

Number of quantiles to be computed. It corresponds to the number of landmarks used to discretize the cumulative distribution function. If n_quantiles is larger than the number of samples, n_quantiles is set to the number of samples as a larger number of quantiles does not give a better approximation of the cumulative distribution function estimator.
output_distribution{‘uniform’, ‘normal’}, default=’uniform’

Marginal distribution for the transformed data. The choices are ‘uniform’ (default) or ‘normal’.
ignore_implicit_zerosbool, default=False

Only applies to sparse matrices. If True, the sparse entries of the matrix are discarded to compute the quantile statistics. If False, these entries are treated as zeros.
subsampleint, default=1e5

Maximum number of samples used to estimate the quantiles for computational efficiency. Note that the subsampling procedure may differ for value-identical sparse and dense matrices.
random_stateint, RandomState instance or None, default=None

Determines random number generation for subsampling and smoothing noise. Please see subsample for more details. Pass an int for reproducible results across multiple function calls. See Glossary
copybool, default=True

Set to False to perform inplace transformation and avoid a copy (if the input is already a numpy array). If True, a copy of X is transformed, leaving the original X unchanged  ..versionchanged:: 0.23

The default value of copy changed from False to True in 0.23.      Returns

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

The transformed data.      See also
QuantileTransformer

Performs quantile-based scaling using the Transformer API (e.g. as part of a preprocessing Pipeline).
power_transform

Maps data to a normal distribution using a power transformation.
scale

Performs standardization that is faster, but less robust to outliers.
robust_scale

Performs robust standardization that removes the influence of outliers but does not put outliers and inliers on the same scale.    Notes NaNs are treated as missing values: disregarded in fit, and maintained in transform.  Warning Risk of data leak Do not use quantile_transform unless you know what you are doing. A common mistake is to apply it to the entire data before splitting into training and test sets. This will bias the model evaluation because information would have leaked from the test set to the training set. In general, we recommend using QuantileTransformer within a Pipeline in order to prevent most risks of data leaking:pipe = make_pipeline(QuantileTransformer(),
LogisticRegression()).  For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py. Examples >>> import numpy as np
>>> from sklearn.preprocessing import quantile_transform
>>> rng = np.random.RandomState(0)
>>> X = np.sort(rng.normal(loc=0.5, scale=0.25, size=(25, 1)), axis=0)
>>> quantile_transform(X, n_quantiles=10, random_state=0, copy=True)
array([...])

 Examples using sklearn.preprocessing.quantile_transform

  Effect of transforming the targets in regression model


sklearn.preprocessing.robust_scale
sklearn.preprocessing.robust_scale(X, *, axis=0, with_centering=True, with_scaling=True, quantile_range=25.0, 75.0, copy=True, unit_variance=False) [source]

Standardize a dataset along any axis Center to the median and component wise scale according to the interquartile range. Read more in the User Guide.  Parameters

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

The data to center and scale.
axisint, default=0

axis used to compute the medians and IQR along. If 0, independently scale each feature, otherwise (if 1) scale each sample.
with_centeringbool, default=True

If True, center the data before scaling.
with_scalingbool, default=True

If True, scale the data to unit variance (or equivalently, unit standard deviation).
quantile_rangetuple (q_min, q_max), 0.0 < q_min < q_max < 100.0

default=(25.0, 75.0), == (1st quantile, 3rd quantile), == IQR Quantile range used to calculate scale_.  New in version 0.18.
copybool, default=True

set to False to perform inplace row normalization and avoid a copy (if the input is already a numpy array or a scipy.sparse CSR matrix and if axis is 1).
unit_variancebool, default=False

If True, scale data so that normally distributed features have a variance of 1. In general, if the difference between the x-values of q_max and q_min for a standard normal distribution is greater than 1, the dataset will be scaled down. If less than 1, the dataset will be scaled up.  New in version 0.24.     Returns

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

The transformed data.      See also
RobustScaler

Performs centering and scaling using the Transformer API (e.g. as part of a preprocessing Pipeline).    Notes This implementation will refuse to center scipy.sparse matrices since it would make them non-sparse and would potentially crash the program with memory exhaustion problems. Instead the caller is expected to either set explicitly with_centering=False (in that case, only variance scaling will be performed on the features of the CSR matrix) or to call X.toarray() if he/she expects the materialized dense array to fit in memory. To avoid memory copy the caller should pass a CSR matrix. For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py.  Warning Risk of data leak Do not use robust_scale unless you know what you are doing. A common mistake is to apply it to the entire data before splitting into training and test sets. This will bias the model evaluation because information would have leaked from the test set to the training set. In general, we recommend using RobustScaler within a Pipeline in order to prevent most risks of data leaking: pipe = make_pipeline(RobustScaler(), LogisticRegression()).


sklearn.preprocessing.scale
sklearn.preprocessing.scale(X, *, axis=0, with_mean=True, with_std=True, copy=True) [source]

Standardize a dataset along any axis. Center to the mean and component wise scale to unit variance. Read more in the User Guide.  Parameters

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

The data to center and scale.
axisint, default=0

axis used to compute the means and standard deviations along. If 0, independently standardize each feature, otherwise (if 1) standardize each sample.
with_meanbool, default=True

If True, center the data before scaling.
with_stdbool, default=True

If True, scale the data to unit variance (or equivalently, unit standard deviation).
copybool, default=True

set to False to perform inplace row normalization and avoid a copy (if the input is already a numpy array or a scipy.sparse CSC matrix and if axis is 1).    Returns

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

The transformed data.      See also
StandardScaler

Performs scaling to unit variance using the Transformer API (e.g. as part of a preprocessing Pipeline).    Notes This implementation will refuse to center scipy.sparse matrices since it would make them non-sparse and would potentially crash the program with memory exhaustion problems. Instead the caller is expected to either set explicitly with_mean=False (in that case, only variance scaling will be performed on the features of the CSC matrix) or to call X.toarray() if he/she expects the materialized dense array to fit in memory. To avoid memory copy the caller should pass a CSC matrix. NaNs are treated as missing values: disregarded to compute the statistics, and maintained during the data transformation. We use a biased estimator for the standard deviation, equivalent to numpy.std(x, ddof=0). Note that the choice of ddof is unlikely to affect model performance. For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py.  Warning Risk of data leak Do not use scale unless you know what you are doing. A common mistake is to apply it to the entire data before splitting into training and test sets. This will bias the model evaluation because information would have leaked from the test set to the training set. In general, we recommend using StandardScaler within a Pipeline in order to prevent most risks of data leaking: pipe = make_pipeline(StandardScaler(), LogisticRegression()).


sklearn.random_projection.johnson_lindenstrauss_min_dim
sklearn.random_projection.johnson_lindenstrauss_min_dim(n_samples, *, eps=0.1) [source]

Find a ‘safe’ number of components to randomly project to. The distortion introduced by a random projection p only changes the distance between two points by a factor (1 +- eps) in an euclidean space with good probability. The projection p is an eps-embedding as defined by: (1 - eps) ||u - v||^2 < ||p(u) - p(v)||^2 < (1 + eps) ||u - v||^2 Where u and v are any rows taken from a dataset of shape (n_samples, n_features), eps is in ]0, 1[ and p is a projection by a random Gaussian N(0, 1) matrix of shape (n_components, n_features) (or a sparse Achlioptas matrix). The minimum number of components to guarantee the eps-embedding is given by: n_components >= 4 log(n_samples) / (eps^2 / 2 - eps^3 / 3) Note that the number of dimensions is independent of the original number of features but instead depends on the size of the dataset: the larger the dataset, the higher is the minimal dimensionality of an eps-embedding. Read more in the User Guide.  Parameters

n_samplesint or array-like of int

Number of samples that should be a integer greater than 0. If an array is given, it will compute a safe number of components array-wise.
epsfloat or ndarray of shape (n_components,), dtype=float, default=0.1

Maximum distortion rate in the range (0,1 ) as defined by the Johnson-Lindenstrauss lemma. If an array is given, it will compute a safe number of components array-wise.    Returns

n_componentsint or ndarray of int

The minimal number of components to guarantee with good probability an eps-embedding with n_samples.     References
1
https://en.wikipedia.org/wiki/Johnson%E2%80%93Lindenstrauss_lemma
2
Sanjoy Dasgupta and Anupam Gupta, 1999, “An elementary proof of the Johnson-Lindenstrauss Lemma.” http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.45.3654   Examples >>> johnson_lindenstrauss_min_dim(1e6, eps=0.5)
663
 >>> johnson_lindenstrauss_min_dim(1e6, eps=[0.5, 0.1, 0.01])
array([    663,   11841, 1112658])
 >>> johnson_lindenstrauss_min_dim([1e4, 1e5, 1e6], eps=0.1)
array([ 7894,  9868, 11841])

 Examples using sklearn.random_projection.johnson_lindenstrauss_min_dim

  The Johnson-Lindenstrauss bound for embedding with random projections


sklearn.set_config
sklearn.set_config(assume_finite=None, working_memory=None, print_changed_only=None, display=None) [source]

Set global scikit-learn configuration  New in version 0.19.   Parameters

assume_finitebool, default=None

If True, validation for finiteness will be skipped, saving time, but leading to potential crashes. If False, validation for finiteness will be performed, avoiding error. Global default: False.  New in version 0.19.
working_memoryint, default=None

If set, scikit-learn will attempt to limit the size of temporary arrays to this number of MiB (per job when parallelised), often saving both computation time and memory on expensive operations that can be performed in chunks. Global default: 1024.  New in version 0.20.
print_changed_onlybool, default=None

If True, only the parameters that were set to non-default values will be printed when printing an estimator. For example, print(SVC()) while True will only print ‘SVC()’ while the default behaviour would be to print ‘SVC(C=1.0, cache_size=200, …)’ with all the non-changed parameters.  New in version 0.21.
display{‘text’, ‘diagram’}, default=None

If ‘diagram’, estimators will be displayed as a diagram in a Jupyter lab or notebook context. If ‘text’, estimators will be displayed as text. Default is ‘text’.  New in version 0.23.       See also
config_context

Context manager for global scikit-learn configuration.
get_config

Retrieve current values of the global configuration.
 Examples using sklearn.set_config

  Release Highlights for scikit-learn 0.23

  Compact estimator representations

  Column Transformer with Mixed Types


sklearn.show_versions
sklearn.show_versions() [source]

Print useful debugging information”  New in version 0.20.


sklearn.svm.l1_min_c
sklearn.svm.l1_min_c(X, y, *, loss='squared_hinge', fit_intercept=True, intercept_scaling=1.0) [source]

Return the lowest bound for C such that for C in (l1_min_C, infinity) the model is guaranteed not to be empty. This applies to l1 penalized classifiers, such as LinearSVC with penalty=’l1’ and linear_model.LogisticRegression with penalty=’l1’. This value is valid if class_weight parameter in fit() is not set.  Parameters

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

Training vector, where n_samples in the number of samples and n_features is the number of features.
yarray-like of shape (n_samples,)

Target vector relative to X.
loss{‘squared_hinge’, ‘log’}, default=’squared_hinge’

Specifies the loss function. With ‘squared_hinge’ it is the squared hinge loss (a.k.a. L2 loss). With ‘log’ it is the loss of logistic regression models.
fit_interceptbool, default=True

Specifies if the intercept should be fitted by the model. It must match the fit() method parameter.
intercept_scalingfloat, default=1.0

when fit_intercept is True, instance vector x becomes [x, intercept_scaling], i.e. a “synthetic” feature with constant value equals to intercept_scaling is appended to the instance vector. It must match the fit() method parameter.    Returns

l1_min_cfloat

minimum value for C
 Examples using sklearn.svm.l1_min_c

  Regularization path of L1- Logistic Regression


sklearn.tree.export_graphviz
sklearn.tree.export_graphviz(decision_tree, out_file=None, *, max_depth=None, feature_names=None, class_names=None, label='all', filled=False, leaves_parallel=False, impurity=True, node_ids=False, proportion=False, rotate=False, rounded=False, special_characters=False, precision=3) [source]

Export a decision tree in DOT format. This function generates a GraphViz representation of the decision tree, which is then written into out_file. Once exported, graphical renderings can be generated using, for example: $ dot -Tps tree.dot -o tree.ps      (PostScript format)
$ dot -Tpng tree.dot -o tree.png    (PNG format)
 The sample counts that are shown are weighted with any sample_weights that might be present. Read more in the User Guide.  Parameters

decision_treedecision tree classifier

The decision tree to be exported to GraphViz.
out_fileobject or str, default=None

Handle or name of the output file. If None, the result is returned as a string.  Changed in version 0.20: Default of out_file changed from “tree.dot” to None.
max_depthint, default=None

The maximum depth of the representation. If None, the tree is fully generated.
feature_nameslist of str, default=None

Names of each of the features. If None generic names will be used (“feature_0”, “feature_1”, …).
class_nameslist of str or bool, default=None

Names of each of the target classes in ascending numerical order. Only relevant for classification and not supported for multi-output. If True, shows a symbolic representation of the class name.
label{‘all’, ‘root’, ‘none’}, default=’all’

Whether to show informative labels for impurity, etc. Options include ‘all’ to show at every node, ‘root’ to show only at the top root node, or ‘none’ to not show at any node.
filledbool, default=False

When set to True, paint nodes to indicate majority class for classification, extremity of values for regression, or purity of node for multi-output.
leaves_parallelbool, default=False

When set to True, draw all leaf nodes at the bottom of the tree.
impuritybool, default=True

When set to True, show the impurity at each node.
node_idsbool, default=False

When set to True, show the ID number on each node.
proportionbool, default=False

When set to True, change the display of ‘values’ and/or ‘samples’ to be proportions and percentages respectively.
rotatebool, default=False

When set to True, orient tree left to right rather than top-down.
roundedbool, default=False

When set to True, draw node boxes with rounded corners and use Helvetica fonts instead of Times-Roman.
special_charactersbool, default=False

When set to False, ignore special characters for PostScript compatibility.
precisionint, default=3

Number of digits of precision for floating point in the values of impurity, threshold and value attributes of each node.    Returns

dot_datastring

String representation of the input tree in GraphViz dot format. Only returned if out_file is None.  New in version 0.18.      Examples >>> from sklearn.datasets import load_iris
>>> from sklearn import tree
 >>> clf = tree.DecisionTreeClassifier()
>>> iris = load_iris()
 >>> clf = clf.fit(iris.data, iris.target)
>>> tree.export_graphviz(clf)
'digraph Tree {...