doc_content
stringlengths
1
386k
doc_id
stringlengths
5
188
sklearn.metrics.average_precision_score sklearn.metrics.average_precision_score(y_true, y_score, *, average='macro', pos_label=1, sample_weight=None) [source] Compute average precision (AP) from prediction scores. AP summarizes a precision-recall curve as the weighted mean of precisions achieved at each threshold, with the increase in recall from the previous threshold used as the weight: \[\text{AP} = \sum_n (R_n - R_{n-1}) P_n\] where \(P_n\) and \(R_n\) are the precision and recall at the nth threshold [1]. This implementation is not interpolated and is different from computing the area under the precision-recall curve with the trapezoidal rule, which uses linear interpolation and can be too optimistic. Note: this implementation is restricted to the binary classification task or multilabel classification task. Read more in the User Guide. Parameters y_truendarray of shape (n_samples,) or (n_samples, n_classes) True binary labels or binary label indicators. y_scorendarray of shape (n_samples,) or (n_samples, n_classes) Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by decision_function on some classifiers). average{‘micro’, ‘samples’, ‘weighted’, ‘macro’} or None, default=’macro’ If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data: 'micro': Calculate metrics globally by considering each element of the label indicator matrix as a label. 'macro': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. 'weighted': Calculate metrics for each label, and find their average, weighted by support (the number of true instances for each label). 'samples': Calculate metrics for each instance, and find their average. Will be ignored when y_true is binary. pos_labelint or str, default=1 The label of the positive class. Only applied to binary y_true. For multilabel-indicator y_true, pos_label is fixed to 1. sample_weightarray-like of shape (n_samples,), default=None Sample weights. Returns average_precisionfloat See also roc_auc_score Compute the area under the ROC curve. precision_recall_curve Compute precision-recall pairs for different probability thresholds. Notes Changed in version 0.19: Instead of linearly interpolating between operating points, precisions are weighted by the change in recall since the last operating point. References 1 Wikipedia entry for the Average precision Examples >>> import numpy as np >>> from sklearn.metrics import average_precision_score >>> y_true = np.array([0, 0, 1, 1]) >>> y_scores = np.array([0.1, 0.4, 0.35, 0.8]) >>> average_precision_score(y_true, y_scores) 0.83... Examples using sklearn.metrics.average_precision_score Precision-Recall
sklearn.modules.generated.sklearn.metrics.average_precision_score
sklearn.metrics.balanced_accuracy_score sklearn.metrics.balanced_accuracy_score(y_true, y_pred, *, sample_weight=None, adjusted=False) [source] Compute the balanced accuracy. The balanced accuracy in binary and multiclass classification problems to deal with imbalanced datasets. It is defined as the average of recall obtained on each class. The best value is 1 and the worst value is 0 when adjusted=False. Read more in the User Guide. New in version 0.20. Parameters y_true1d array-like Ground truth (correct) target values. y_pred1d array-like Estimated targets as returned by a classifier. sample_weightarray-like of shape (n_samples,), default=None Sample weights. adjustedbool, default=False When true, the result is adjusted for chance, so that random performance would score 0, and perfect performance scores 1. Returns balanced_accuracyfloat See also recall_score, roc_auc_score Notes Some literature promotes alternative definitions of balanced accuracy. Our definition is equivalent to accuracy_score with class-balanced sample weights, and shares desirable properties with the binary case. See the User Guide. References 1 Brodersen, K.H.; Ong, C.S.; Stephan, K.E.; Buhmann, J.M. (2010). The balanced accuracy and its posterior distribution. Proceedings of the 20th International Conference on Pattern Recognition, 3121-24. 2 John. D. Kelleher, Brian Mac Namee, Aoife D’Arcy, (2015). Fundamentals of Machine Learning for Predictive Data Analytics: Algorithms, Worked Examples, and Case Studies. Examples >>> from sklearn.metrics import balanced_accuracy_score >>> y_true = [0, 1, 0, 0, 1, 0] >>> y_pred = [0, 1, 0, 0, 0, 1] >>> balanced_accuracy_score(y_true, y_pred) 0.625
sklearn.modules.generated.sklearn.metrics.balanced_accuracy_score
sklearn.metrics.brier_score_loss sklearn.metrics.brier_score_loss(y_true, y_prob, *, sample_weight=None, pos_label=None) [source] Compute the Brier score loss. The smaller the Brier score loss, the better, hence the naming with “loss”. The Brier score measures the mean squared difference between the predicted probability and the actual outcome. The Brier score always takes on a value between zero and one, since this is the largest possible difference between a predicted probability (which must be between zero and one) and the actual outcome (which can take on values of only 0 and 1). It can be decomposed is the sum of refinement loss and calibration loss. The Brier score is appropriate for binary and categorical outcomes that can be structured as true or false, but is inappropriate for ordinal variables which can take on three or more values (this is because the Brier score assumes that all possible outcomes are equivalently “distant” from one another). Which label is considered to be the positive label is controlled via the parameter pos_label, which defaults to the greater label unless y_true is all 0 or all -1, in which case pos_label defaults to 1. Read more in the User Guide. Parameters y_truearray of shape (n_samples,) True targets. y_probarray of shape (n_samples,) Probabilities of the positive class. sample_weightarray-like of shape (n_samples,), default=None Sample weights. pos_labelint or str, default=None Label of the positive class. pos_label will be infered in the following manner: if y_true in {-1, 1} or {0, 1}, pos_label defaults to 1; else if y_true contains string, an error will be raised and pos_label should be explicitely specified; otherwise, pos_label defaults to the greater label, i.e. np.unique(y_true)[-1]. Returns scorefloat Brier score loss. References 1 Wikipedia entry for the Brier score. Examples >>> import numpy as np >>> from sklearn.metrics import brier_score_loss >>> y_true = np.array([0, 1, 1, 0]) >>> y_true_categorical = np.array(["spam", "ham", "ham", "spam"]) >>> y_prob = np.array([0.1, 0.9, 0.8, 0.3]) >>> brier_score_loss(y_true, y_prob) 0.037... >>> brier_score_loss(y_true, 1-y_prob, pos_label=0) 0.037... >>> brier_score_loss(y_true_categorical, y_prob, pos_label="ham") 0.037... >>> brier_score_loss(y_true, np.array(y_prob) > 0.5) 0.0 Examples using sklearn.metrics.brier_score_loss Probability Calibration curves Probability calibration of classifiers
sklearn.modules.generated.sklearn.metrics.brier_score_loss
sklearn.metrics.calinski_harabasz_score sklearn.metrics.calinski_harabasz_score(X, labels) [source] Compute the Calinski and Harabasz score. It is also known as the Variance Ratio Criterion. The score is defined as ratio between the within-cluster dispersion and the between-cluster dispersion. Read more in the User Guide. Parameters Xarray-like of shape (n_samples, n_features) A list of n_features-dimensional data points. Each row corresponds to a single data point. labelsarray-like of shape (n_samples,) Predicted labels for each sample. Returns scorefloat The resulting Calinski-Harabasz score. References 1 T. Calinski and J. Harabasz, 1974. “A dendrite method for cluster analysis”. Communications in Statistics
sklearn.modules.generated.sklearn.metrics.calinski_harabasz_score
sklearn.metrics.check_scoring sklearn.metrics.check_scoring(estimator, scoring=None, *, allow_none=False) [source] Determine scorer from user options. A TypeError will be thrown if the estimator cannot be scored. Parameters estimatorestimator object implementing ‘fit’ The object to use to fit the data. scoringstr or callable, default=None A string (see model evaluation documentation) or a scorer callable object / function with signature scorer(estimator, X, y). allow_nonebool, default=False If no scoring is specified and the estimator has no score function, we can either return None or raise an exception. Returns scoringcallable A scorer callable object / function with signature scorer(estimator, X, y).
sklearn.modules.generated.sklearn.metrics.check_scoring
sklearn.metrics.classification_report sklearn.metrics.classification_report(y_true, y_pred, *, labels=None, target_names=None, sample_weight=None, digits=2, output_dict=False, zero_division='warn') [source] Build a text report showing the main classification metrics. Read more in the User Guide. Parameters y_true1d array-like, or label indicator array / sparse matrix Ground truth (correct) target values. y_pred1d array-like, or label indicator array / sparse matrix Estimated targets as returned by a classifier. labelsarray-like of shape (n_labels,), default=None Optional list of label indices to include in the report. target_nameslist of str of shape (n_labels,), default=None Optional display names matching the labels (same order). sample_weightarray-like of shape (n_samples,), default=None Sample weights. digitsint, default=2 Number of digits for formatting output floating point values. When output_dict is True, this will be ignored and the returned values will not be rounded. output_dictbool, default=False If True, return output as dict. New in version 0.20. zero_division“warn”, 0 or 1, default=”warn” Sets the value to return when there is a zero division. If set to “warn”, this acts as 0, but warnings are also raised. Returns reportstring / dict Text summary of the precision, recall, F1 score for each class. Dictionary returned if output_dict is True. Dictionary has the following structure: {'label 1': {'precision':0.5, 'recall':1.0, 'f1-score':0.67, 'support':1}, 'label 2': { ... }, ... } The reported averages include macro average (averaging the unweighted mean per label), weighted average (averaging the support-weighted mean per label), and sample average (only for multilabel classification). Micro average (averaging the total true positives, false negatives and false positives) is only shown for multi-label or multi-class with a subset of classes, because it corresponds to accuracy otherwise and would be the same for all metrics. See also precision_recall_fscore_support for more details on averages. Note that in binary classification, recall of the positive class is also known as “sensitivity”; recall of the negative class is “specificity”. See also precision_recall_fscore_support, confusion_matrix multilabel_confusion_matrix Examples >>> from sklearn.metrics import classification_report >>> y_true = [0, 1, 2, 2, 2] >>> y_pred = [0, 0, 2, 2, 1] >>> target_names = ['class 0', 'class 1', 'class 2'] >>> print(classification_report(y_true, y_pred, target_names=target_names)) precision recall f1-score support class 0 0.50 1.00 0.67 1 class 1 0.00 0.00 0.00 1 class 2 1.00 0.67 0.80 3 accuracy 0.60 5 macro avg 0.50 0.56 0.49 5 weighted avg 0.70 0.60 0.61 5 >>> y_pred = [1, 1, 0] >>> y_true = [1, 1, 1] >>> print(classification_report(y_true, y_pred, labels=[1, 2, 3])) precision recall f1-score support 1 1.00 0.67 0.80 3 2 0.00 0.00 0.00 0 3 0.00 0.00 0.00 0 micro avg 1.00 0.67 0.80 3 macro avg 0.33 0.22 0.27 3 weighted avg 1.00 0.67 0.80 3 Examples using sklearn.metrics.classification_report Recognizing hand-written digits Faces recognition example using eigenfaces and SVMs Pipeline Anova SVM Parameter estimation using grid search with cross-validation Restricted Boltzmann Machine features for digit classification Column Transformer with Heterogeneous Data Sources Label Propagation digits: Demonstrating performance Label Propagation digits active learning Classification of text documents using sparse features
sklearn.modules.generated.sklearn.metrics.classification_report
sklearn.metrics.cluster.contingency_matrix sklearn.metrics.cluster.contingency_matrix(labels_true, labels_pred, *, eps=None, sparse=False, dtype=<class 'numpy.int64'>) [source] Build a contingency matrix describing the relationship between labels. Parameters labels_trueint array, shape = [n_samples] Ground truth class labels to be used as a reference. labels_predarray-like of shape (n_samples,) Cluster labels to evaluate. epsfloat, default=None If a float, that value is added to all values in the contingency matrix. This helps to stop NaN propagation. If None, nothing is adjusted. sparsebool, default=False If True, return a sparse CSR continency matrix. If eps is not None and sparse is True will raise ValueError. New in version 0.18. dtypenumeric type, default=np.int64 Output dtype. Ignored if eps is not None. New in version 0.24. Returns contingency{array-like, sparse}, shape=[n_classes_true, n_classes_pred] Matrix \(C\) such that \(C_{i, j}\) is the number of samples in true class \(i\) and in predicted class \(j\). If eps is None, the dtype of this array will be integer unless set otherwise with the dtype argument. If eps is given, the dtype will be float. Will be a sklearn.sparse.csr_matrix if sparse=True.
sklearn.modules.generated.sklearn.metrics.cluster.contingency_matrix
sklearn.metrics.cluster.pair_confusion_matrix sklearn.metrics.cluster.pair_confusion_matrix(labels_true, labels_pred) [source] Pair confusion matrix arising from two clusterings. The pair confusion matrix \(C\) computes a 2 by 2 similarity matrix between two clusterings by considering all pairs of samples and counting pairs that are assigned into the same or into different clusters under the true and predicted clusterings. Considering a pair of samples that is clustered together a positive pair, then as in binary classification the count of true negatives is \(C_{00}\), false negatives is \(C_{10}\), true positives is \(C_{11}\) and false positives is \(C_{01}\). Read more in the User Guide. Parameters labels_truearray-like of shape (n_samples,), dtype=integral Ground truth class labels to be used as a reference. labels_predarray-like of shape (n_samples,), dtype=integral Cluster labels to evaluate. Returns Cndarray of shape (2, 2), dtype=np.int64 The contingency matrix. See also rand_score Rand Score adjusted_rand_score Adjusted Rand Score adjusted_mutual_info_score Adjusted Mutual Information References Examples Perfectly matching labelings have all non-zero entries on the diagonal regardless of actual label values: >>> from sklearn.metrics.cluster import pair_confusion_matrix >>> pair_confusion_matrix([0, 0, 1, 1], [1, 1, 0, 0]) array([[8, 0], [0, 4]]... Labelings that assign all classes members to the same clusters are complete but may be not always pure, hence penalized, and have some off-diagonal non-zero entries: >>> pair_confusion_matrix([0, 0, 1, 2], [0, 0, 1, 1]) array([[8, 2], [0, 2]]... Note that the matrix is not symmetric.
sklearn.modules.generated.sklearn.metrics.cluster.pair_confusion_matrix
sklearn.metrics.cohen_kappa_score sklearn.metrics.cohen_kappa_score(y1, y2, *, labels=None, weights=None, sample_weight=None) [source] Cohen’s kappa: a statistic that measures inter-annotator agreement. This function computes Cohen’s kappa [1], a score that expresses the level of agreement between two annotators on a classification problem. It is defined as \[\kappa = (p_o - p_e) / (1 - p_e)\] where \(p_o\) is the empirical probability of agreement on the label assigned to any sample (the observed agreement ratio), and \(p_e\) is the expected agreement when both annotators assign labels randomly. \(p_e\) is estimated using a per-annotator empirical prior over the class labels [2]. Read more in the User Guide. Parameters y1array of shape (n_samples,) Labels assigned by the first annotator. y2array of shape (n_samples,) Labels assigned by the second annotator. The kappa statistic is symmetric, so swapping y1 and y2 doesn’t change the value. labelsarray-like of shape (n_classes,), default=None List of labels to index the matrix. This may be used to select a subset of labels. If None, all labels that appear at least once in y1 or y2 are used. weights{‘linear’, ‘quadratic’}, default=None Weighting type to calculate the score. None means no weighted; “linear” means linear weighted; “quadratic” means quadratic weighted. sample_weightarray-like of shape (n_samples,), default=None Sample weights. Returns kappafloat The kappa statistic, which is a number between -1 and 1. The maximum value means complete agreement; zero or lower means chance agreement. References 1 J. Cohen (1960). “A coefficient of agreement for nominal scales”. Educational and Psychological Measurement 20(1):37-46. doi:10.1177/001316446002000104. 2 R. Artstein and M. Poesio (2008). “Inter-coder agreement for computational linguistics”. Computational Linguistics 34(4):555-596. 3 Wikipedia entry for the Cohen’s kappa.
sklearn.modules.generated.sklearn.metrics.cohen_kappa_score
sklearn.metrics.completeness_score sklearn.metrics.completeness_score(labels_true, labels_pred) [source] Completeness metric of a cluster labeling given a ground truth. A clustering result satisfies completeness if all the data points that are members of a given class are elements of the same cluster. This metric is independent of the absolute values of the labels: a permutation of the class or cluster label values won’t change the score value in any way. This metric is not symmetric: switching label_true with label_pred will return the homogeneity_score which will be different in general. Read more in the User Guide. Parameters labels_trueint array, shape = [n_samples] ground truth class labels to be used as a reference labels_predarray-like of shape (n_samples,) cluster labels to evaluate Returns completenessfloat score between 0.0 and 1.0. 1.0 stands for perfectly complete labeling See also homogeneity_score v_measure_score References 1 Andrew Rosenberg and Julia Hirschberg, 2007. V-Measure: A conditional entropy-based external cluster evaluation measure Examples Perfect labelings are complete: >>> from sklearn.metrics.cluster import completeness_score >>> completeness_score([0, 0, 1, 1], [1, 1, 0, 0]) 1.0 Non-perfect labelings that assign all classes members to the same clusters are still complete: >>> print(completeness_score([0, 0, 1, 1], [0, 0, 0, 0])) 1.0 >>> print(completeness_score([0, 1, 2, 3], [0, 0, 1, 1])) 0.999... If classes members are split across different clusters, the assignment cannot be complete: >>> print(completeness_score([0, 0, 1, 1], [0, 1, 0, 1])) 0.0 >>> print(completeness_score([0, 0, 0, 0], [0, 1, 2, 3])) 0.0 Examples using sklearn.metrics.completeness_score Release Highlights for scikit-learn 0.23 Demo of affinity propagation clustering algorithm Demo of DBSCAN clustering algorithm A demo of K-Means clustering on the handwritten digits data Clustering text documents using k-means
sklearn.modules.generated.sklearn.metrics.completeness_score
sklearn.metrics.confusion_matrix sklearn.metrics.confusion_matrix(y_true, y_pred, *, labels=None, sample_weight=None, normalize=None) [source] Compute confusion matrix to evaluate the accuracy of a classification. By definition a confusion matrix \(C\) is such that \(C_{i, j}\) is equal to the number of observations known to be in group \(i\) and predicted to be in group \(j\). Thus in binary classification, the count of true negatives is \(C_{0,0}\), false negatives is \(C_{1,0}\), true positives is \(C_{1,1}\) and false positives is \(C_{0,1}\). Read more in the User Guide. Parameters y_truearray-like of shape (n_samples,) Ground truth (correct) target values. y_predarray-like of shape (n_samples,) Estimated targets as returned by a classifier. labelsarray-like of shape (n_classes), default=None List of labels to index the matrix. This may be used to reorder or select a subset of labels. If None is given, those that appear at least once in y_true or y_pred are used in sorted order. sample_weightarray-like of shape (n_samples,), default=None Sample weights. New in version 0.18. normalize{‘true’, ‘pred’, ‘all’}, default=None Normalizes confusion matrix over the true (rows), predicted (columns) conditions or all the population. If None, confusion matrix will not be normalized. Returns Cndarray of shape (n_classes, n_classes) Confusion matrix whose i-th row and j-th column entry indicates the number of samples with true label being i-th class and predicted label being j-th class. See also plot_confusion_matrix Plot Confusion Matrix. ConfusionMatrixDisplay Confusion Matrix visualization. References 1 Wikipedia entry for the Confusion matrix (Wikipedia and other references may use a different convention for axes). Examples >>> from sklearn.metrics import confusion_matrix >>> y_true = [2, 0, 2, 2, 0, 1] >>> y_pred = [0, 0, 2, 2, 0, 2] >>> confusion_matrix(y_true, y_pred) array([[2, 0, 0], [0, 0, 1], [1, 0, 2]]) >>> y_true = ["cat", "ant", "cat", "cat", "ant", "bird"] >>> y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"] >>> confusion_matrix(y_true, y_pred, labels=["ant", "bird", "cat"]) array([[2, 0, 0], [0, 0, 1], [1, 0, 2]]) In the binary case, we can extract true positives, etc as follows: >>> tn, fp, fn, tp = confusion_matrix([0, 1, 0, 1], [1, 1, 1, 0]).ravel() >>> (tn, fp, fn, tp) (0, 2, 1, 1) Examples using sklearn.metrics.confusion_matrix Faces recognition example using eigenfaces and SVMs Visualizations with Display Objects Label Propagation digits: Demonstrating performance Label Propagation digits active learning Classification of text documents using sparse features
sklearn.modules.generated.sklearn.metrics.confusion_matrix
sklearn.metrics.consensus_score sklearn.metrics.consensus_score(a, b, *, similarity='jaccard') [source] The similarity of two sets of biclusters. Similarity between individual biclusters is computed. Then the best matching between sets is found using the Hungarian algorithm. The final score is the sum of similarities divided by the size of the larger set. Read more in the User Guide. Parameters a(rows, columns) Tuple of row and column indicators for a set of biclusters. b(rows, columns) Another set of biclusters like a. similarity‘jaccard’ or callable, default=’jaccard’ May be the string “jaccard” to use the Jaccard coefficient, or any function that takes four arguments, each of which is a 1d indicator vector: (a_rows, a_columns, b_rows, b_columns). References Hochreiter, Bodenhofer, et. al., 2010. FABIA: factor analysis for bicluster acquisition. Examples using sklearn.metrics.consensus_score A demo of the Spectral Co-Clustering algorithm A demo of the Spectral Biclustering algorithm
sklearn.modules.generated.sklearn.metrics.consensus_score
sklearn.metrics.coverage_error sklearn.metrics.coverage_error(y_true, y_score, *, sample_weight=None) [source] Coverage error measure. Compute how far we need to go through the ranked scores to cover all true labels. The best value is equal to the average number of labels in y_true per sample. Ties in y_scores are broken by giving maximal rank that would have been assigned to all tied values. Note: Our implementation’s score is 1 greater than the one given in Tsoumakas et al., 2010. This extends it to handle the degenerate case in which an instance has 0 true labels. Read more in the User Guide. Parameters y_truendarray of shape (n_samples, n_labels) True binary labels in binary indicator format. y_scorendarray of shape (n_samples, n_labels) Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers). sample_weightarray-like of shape (n_samples,), default=None Sample weights. Returns coverage_errorfloat References 1 Tsoumakas, G., Katakis, I., & Vlahavas, I. (2010). Mining multi-label data. In Data mining and knowledge discovery handbook (pp. 667-685). Springer US.
sklearn.modules.generated.sklearn.metrics.coverage_error
sklearn.metrics.davies_bouldin_score sklearn.metrics.davies_bouldin_score(X, labels) [source] Computes the Davies-Bouldin score. The score is defined as the average similarity measure of each cluster with its most similar cluster, where similarity is the ratio of within-cluster distances to between-cluster distances. Thus, clusters which are farther apart and less dispersed will result in a better score. The minimum score is zero, with lower values indicating better clustering. Read more in the User Guide. New in version 0.20. Parameters Xarray-like of shape (n_samples, n_features) A list of n_features-dimensional data points. Each row corresponds to a single data point. labelsarray-like of shape (n_samples,) Predicted labels for each sample. Returns score: float The resulting Davies-Bouldin score. References 1 Davies, David L.; Bouldin, Donald W. (1979). “A Cluster Separation Measure”. IEEE Transactions on Pattern Analysis and Machine Intelligence. PAMI-1 (2): 224-227
sklearn.modules.generated.sklearn.metrics.davies_bouldin_score
sklearn.metrics.dcg_score sklearn.metrics.dcg_score(y_true, y_score, *, k=None, log_base=2, sample_weight=None, ignore_ties=False) [source] Compute Discounted Cumulative Gain. Sum the true scores ranked in the order induced by the predicted scores, after applying a logarithmic discount. This ranking metric yields a high value if true labels are ranked high by y_score. Usually the Normalized Discounted Cumulative Gain (NDCG, computed by ndcg_score) is preferred. Parameters y_truendarray of shape (n_samples, n_labels) True targets of multilabel classification, or true scores of entities to be ranked. y_scorendarray of shape (n_samples, n_labels) Target scores, can either be probability estimates, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers). kint, default=None Only consider the highest k scores in the ranking. If None, use all outputs. log_basefloat, default=2 Base of the logarithm used for the discount. A low value means a sharper discount (top results are more important). sample_weightndarray of shape (n_samples,), default=None Sample weights. If None, all samples are given the same weight. ignore_tiesbool, default=False Assume that there are no ties in y_score (which is likely to be the case if y_score is continuous) for efficiency gains. Returns discounted_cumulative_gainfloat The averaged sample DCG scores. See also ndcg_score The Discounted Cumulative Gain divided by the Ideal Discounted Cumulative Gain (the DCG obtained for a perfect ranking), in order to have a score between 0 and 1. References Wikipedia entry for Discounted Cumulative Gain. Jarvelin, K., & Kekalainen, J. (2002). Cumulated gain-based evaluation of IR techniques. ACM Transactions on Information Systems (TOIS), 20(4), 422-446. Wang, Y., Wang, L., Li, Y., He, D., Chen, W., & Liu, T. Y. (2013, May). A theoretical analysis of NDCG ranking measures. In Proceedings of the 26th Annual Conference on Learning Theory (COLT 2013). McSherry, F., & Najork, M. (2008, March). Computing information retrieval performance measures efficiently in the presence of tied scores. In European conference on information retrieval (pp. 414-421). Springer, Berlin, Heidelberg. Examples >>> from sklearn.metrics import dcg_score >>> # we have groud-truth relevance of some answers to a query: >>> true_relevance = np.asarray([[10, 0, 0, 1, 5]]) >>> # we predict scores for the answers >>> scores = np.asarray([[.1, .2, .3, 4, 70]]) >>> dcg_score(true_relevance, scores) 9.49... >>> # we can set k to truncate the sum; only top k answers contribute >>> dcg_score(true_relevance, scores, k=2) 5.63... >>> # now we have some ties in our prediction >>> scores = np.asarray([[1, 0, 0, 0, 1]]) >>> # by default ties are averaged, so here we get the average true >>> # relevance of our top predictions: (10 + 5) / 2 = 7.5 >>> dcg_score(true_relevance, scores, k=1) 7.5 >>> # we can choose to ignore ties for faster results, but only >>> # if we know there aren't ties in our scores, otherwise we get >>> # wrong results: >>> dcg_score(true_relevance, ... scores, k=1, ignore_ties=True) 5.0
sklearn.modules.generated.sklearn.metrics.dcg_score
sklearn.metrics.det_curve sklearn.metrics.det_curve(y_true, y_score, pos_label=None, sample_weight=None) [source] Compute error rates for different probability thresholds. Note This metric is used for evaluation of ranking and error tradeoffs of a binary classification task. Read more in the User Guide. New in version 0.24. Parameters y_truendarray of shape (n_samples,) True binary labels. If labels are not either {-1, 1} or {0, 1}, then pos_label should be explicitly given. y_scorendarray of shape of (n_samples,) Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers). pos_labelint or str, default=None The label of the positive class. When pos_label=None, if y_true is in {-1, 1} or {0, 1}, pos_label is set to 1, otherwise an error will be raised. sample_weightarray-like of shape (n_samples,), default=None Sample weights. Returns fprndarray of shape (n_thresholds,) False positive rate (FPR) such that element i is the false positive rate of predictions with score >= thresholds[i]. This is occasionally referred to as false acceptance propability or fall-out. fnrndarray of shape (n_thresholds,) False negative rate (FNR) such that element i is the false negative rate of predictions with score >= thresholds[i]. This is occasionally referred to as false rejection or miss rate. thresholdsndarray of shape (n_thresholds,) Decreasing score values. See also plot_det_curve Plot detection error tradeoff (DET) curve. DetCurveDisplay DET curve visualization. roc_curve Compute Receiver operating characteristic (ROC) curve. precision_recall_curve Compute precision-recall curve. Examples >>> import numpy as np >>> from sklearn.metrics import det_curve >>> y_true = np.array([0, 0, 1, 1]) >>> y_scores = np.array([0.1, 0.4, 0.35, 0.8]) >>> fpr, fnr, thresholds = det_curve(y_true, y_scores) >>> fpr array([0.5, 0.5, 0. ]) >>> fnr array([0. , 0.5, 0.5]) >>> thresholds array([0.35, 0.4 , 0.8 ]) Examples using sklearn.metrics.det_curve Detection error tradeoff (DET) curve
sklearn.modules.generated.sklearn.metrics.det_curve
sklearn.metrics.explained_variance_score sklearn.metrics.explained_variance_score(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average') [source] Explained variance regression score function. Best possible score is 1.0, lower values are worse. Read more in the User Guide. Parameters y_truearray-like of shape (n_samples,) or (n_samples, n_outputs) Ground truth (correct) target values. y_predarray-like of shape (n_samples,) or (n_samples, n_outputs) Estimated target values. sample_weightarray-like of shape (n_samples,), default=None Sample weights. multioutput{‘raw_values’, ‘uniform_average’, ‘variance_weighted’} or array-like of shape (n_outputs,), default=’uniform_average’ Defines aggregating of multiple output scores. Array-like value defines weights used to average scores. ‘raw_values’ : Returns a full set of scores in case of multioutput input. ‘uniform_average’ : Scores of all outputs are averaged with uniform weight. ‘variance_weighted’ : Scores of all outputs are averaged, weighted by the variances of each individual output. Returns scorefloat or ndarray of floats The explained variance or ndarray if ‘multioutput’ is ‘raw_values’. Notes This is not a symmetric function. Examples >>> from sklearn.metrics import explained_variance_score >>> y_true = [3, -0.5, 2, 7] >>> y_pred = [2.5, 0.0, 2, 8] >>> explained_variance_score(y_true, y_pred) 0.957... >>> y_true = [[0.5, 1], [-1, 1], [7, -6]] >>> y_pred = [[0, 2], [-1, 2], [8, -5]] >>> explained_variance_score(y_true, y_pred, multioutput='uniform_average') 0.983...
sklearn.modules.generated.sklearn.metrics.explained_variance_score
sklearn.metrics.f1_score sklearn.metrics.f1_score(y_true, y_pred, *, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn') [source] Compute the F1 score, also known as balanced F-score or F-measure. The F1 score can be interpreted as a weighted average of the precision and recall, where an F1 score reaches its best value at 1 and worst score at 0. The relative contribution of precision and recall to the F1 score are equal. The formula for the F1 score is: F1 = 2 * (precision * recall) / (precision + recall) In the multi-class and multi-label case, this is the average of the F1 score of each class with weighting depending on the average parameter. Read more in the User Guide. Parameters y_true1d array-like, or label indicator array / sparse matrix Ground truth (correct) target values. y_pred1d array-like, or label indicator array / sparse matrix Estimated targets as returned by a classifier. labelsarray-like, default=None The set of labels to include when average != 'binary', and their order if average is None. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Changed in version 0.17: Parameter labels improved for multiclass problem. pos_labelstr or int, default=1 The class to report if average='binary' and the data is binary. If the data are multiclass or multilabel, this will be ignored; setting labels=[pos_label] and average != 'binary' will report scores for that label only. average{‘micro’, ‘macro’, ‘samples’,’weighted’, ‘binary’} or None, default=’binary’ This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data: 'binary': Only report results for the class specified by pos_label. This is applicable only if targets (y_{true,pred}) are binary. 'micro': Calculate metrics globally by counting the total true positives, false negatives and false positives. 'macro': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. 'weighted': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters ‘macro’ to account for label imbalance; it can result in an F-score that is not between precision and recall. 'samples': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification where this differs from accuracy_score). sample_weightarray-like of shape (n_samples,), default=None Sample weights. zero_division“warn”, 0 or 1, default=”warn” Sets the value to return when there is a zero division, i.e. when all predictions and labels are negative. If set to “warn”, this acts as 0, but warnings are also raised. Returns f1_scorefloat or array of float, shape = [n_unique_labels] F1 score of the positive class in binary classification or weighted average of the F1 scores of each class for the multiclass task. See also fbeta_score, precision_recall_fscore_support, jaccard_score multilabel_confusion_matrix Notes When true positive + false positive == 0, precision is undefined. When true positive + false negative == 0, recall is undefined. In such cases, by default the metric will be set to 0, as will f-score, and UndefinedMetricWarning will be raised. This behavior can be modified with zero_division. References 1 Wikipedia entry for the F1-score. Examples >>> from sklearn.metrics import f1_score >>> y_true = [0, 1, 2, 0, 1, 2] >>> y_pred = [0, 2, 1, 0, 0, 1] >>> f1_score(y_true, y_pred, average='macro') 0.26... >>> f1_score(y_true, y_pred, average='micro') 0.33... >>> f1_score(y_true, y_pred, average='weighted') 0.26... >>> f1_score(y_true, y_pred, average=None) array([0.8, 0. , 0. ]) >>> y_true = [0, 0, 0, 0, 0, 0] >>> y_pred = [0, 0, 0, 0, 0, 0] >>> f1_score(y_true, y_pred, zero_division=1) 1.0... Examples using sklearn.metrics.f1_score Probability Calibration curves Precision-Recall Semi-supervised Classification on a Text Dataset
sklearn.modules.generated.sklearn.metrics.f1_score
sklearn.metrics.fbeta_score sklearn.metrics.fbeta_score(y_true, y_pred, *, beta, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn') [source] Compute the F-beta score. The F-beta score is the weighted harmonic mean of precision and recall, reaching its optimal value at 1 and its worst value at 0. The beta parameter determines the weight of recall in the combined score. beta < 1 lends more weight to precision, while beta > 1 favors recall (beta -> 0 considers only precision, beta -> +inf only recall). Read more in the User Guide. Parameters y_true1d array-like, or label indicator array / sparse matrix Ground truth (correct) target values. y_pred1d array-like, or label indicator array / sparse matrix Estimated targets as returned by a classifier. betafloat Determines the weight of recall in the combined score. labelsarray-like, default=None The set of labels to include when average != 'binary', and their order if average is None. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Changed in version 0.17: Parameter labels improved for multiclass problem. pos_labelstr or int, default=1 The class to report if average='binary' and the data is binary. If the data are multiclass or multilabel, this will be ignored; setting labels=[pos_label] and average != 'binary' will report scores for that label only. average{‘micro’, ‘macro’, ‘samples’, ‘weighted’, ‘binary’} or None default=’binary’ This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data: 'binary': Only report results for the class specified by pos_label. This is applicable only if targets (y_{true,pred}) are binary. 'micro': Calculate metrics globally by counting the total true positives, false negatives and false positives. 'macro': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. 'weighted': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters ‘macro’ to account for label imbalance; it can result in an F-score that is not between precision and recall. 'samples': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification where this differs from accuracy_score). sample_weightarray-like of shape (n_samples,), default=None Sample weights. zero_division“warn”, 0 or 1, default=”warn” Sets the value to return when there is a zero division, i.e. when all predictions and labels are negative. If set to “warn”, this acts as 0, but warnings are also raised. Returns fbeta_scorefloat (if average is not None) or array of float, shape = [n_unique_labels] F-beta score of the positive class in binary classification or weighted average of the F-beta score of each class for the multiclass task. See also precision_recall_fscore_support, multilabel_confusion_matrix Notes When true positive + false positive == 0 or true positive + false negative == 0, f-score returns 0 and raises UndefinedMetricWarning. This behavior can be modified with zero_division. References 1 R. Baeza-Yates and B. Ribeiro-Neto (2011). Modern Information Retrieval. Addison Wesley, pp. 327-328. 2 Wikipedia entry for the F1-score. Examples >>> from sklearn.metrics import fbeta_score >>> y_true = [0, 1, 2, 0, 1, 2] >>> y_pred = [0, 2, 1, 0, 0, 1] >>> fbeta_score(y_true, y_pred, average='macro', beta=0.5) 0.23... >>> fbeta_score(y_true, y_pred, average='micro', beta=0.5) 0.33... >>> fbeta_score(y_true, y_pred, average='weighted', beta=0.5) 0.23... >>> fbeta_score(y_true, y_pred, average=None, beta=0.5) array([0.71..., 0. , 0. ])
sklearn.modules.generated.sklearn.metrics.fbeta_score
sklearn.metrics.fowlkes_mallows_score sklearn.metrics.fowlkes_mallows_score(labels_true, labels_pred, *, sparse=False) [source] Measure the similarity of two clusterings of a set of points. New in version 0.18. The Fowlkes-Mallows index (FMI) is defined as the geometric mean between of the precision and recall: FMI = TP / sqrt((TP + FP) * (TP + FN)) Where TP is the number of True Positive (i.e. the number of pair of points that belongs in the same clusters in both labels_true and labels_pred), FP is the number of False Positive (i.e. the number of pair of points that belongs in the same clusters in labels_true and not in labels_pred) and FN is the number of False Negative (i.e the number of pair of points that belongs in the same clusters in labels_pred and not in labels_True). The score ranges from 0 to 1. A high value indicates a good similarity between two clusters. Read more in the User Guide. Parameters labels_trueint array, shape = (n_samples,) A clustering of the data into disjoint subsets. labels_predarray, shape = (n_samples, ) A clustering of the data into disjoint subsets. sparsebool, default=False Compute contingency matrix internally with sparse matrix. Returns scorefloat The resulting Fowlkes-Mallows score. References 1 E. B. Fowkles and C. L. Mallows, 1983. “A method for comparing two hierarchical clusterings”. Journal of the American Statistical Association 2 Wikipedia entry for the Fowlkes-Mallows Index Examples Perfect labelings are both homogeneous and complete, hence have score 1.0: >>> from sklearn.metrics.cluster import fowlkes_mallows_score >>> fowlkes_mallows_score([0, 0, 1, 1], [0, 0, 1, 1]) 1.0 >>> fowlkes_mallows_score([0, 0, 1, 1], [1, 1, 0, 0]) 1.0 If classes members are completely split across different clusters, the assignment is totally random, hence the FMI is null: >>> fowlkes_mallows_score([0, 0, 0, 0], [0, 1, 2, 3]) 0.0
sklearn.modules.generated.sklearn.metrics.fowlkes_mallows_score
sklearn.metrics.get_scorer sklearn.metrics.get_scorer(scoring) [source] Get a scorer from string. Read more in the User Guide. Parameters scoringstr or callable Scoring method as string. If callable it is returned as is. Returns scorercallable The scorer.
sklearn.modules.generated.sklearn.metrics.get_scorer
sklearn.metrics.hamming_loss sklearn.metrics.hamming_loss(y_true, y_pred, *, sample_weight=None) [source] Compute the average Hamming loss. The Hamming loss is the fraction of labels that are incorrectly predicted. Read more in the User Guide. Parameters y_true1d array-like, or label indicator array / sparse matrix Ground truth (correct) labels. y_pred1d array-like, or label indicator array / sparse matrix Predicted labels, as returned by a classifier. sample_weightarray-like of shape (n_samples,), default=None Sample weights. New in version 0.18. Returns lossfloat or int Return the average Hamming loss between element of y_true and y_pred. See also accuracy_score, jaccard_score, zero_one_loss Notes In multiclass classification, the Hamming loss corresponds to the Hamming distance between y_true and y_pred which is equivalent to the subset zero_one_loss function, when normalize parameter is set to True. In multilabel classification, the Hamming loss is different from the subset zero-one loss. The zero-one loss considers the entire set of labels for a given sample incorrect if it does not entirely match the true set of labels. Hamming loss is more forgiving in that it penalizes only the individual labels. The Hamming loss is upperbounded by the subset zero-one loss, when normalize parameter is set to True. It is always between 0 and 1, lower being better. References 1 Grigorios Tsoumakas, Ioannis Katakis. Multi-Label Classification: An Overview. International Journal of Data Warehousing & Mining, 3(3), 1-13, July-September 2007. 2 Wikipedia entry on the Hamming distance. Examples >>> from sklearn.metrics import hamming_loss >>> y_pred = [1, 2, 3, 4] >>> y_true = [2, 2, 3, 4] >>> hamming_loss(y_true, y_pred) 0.25 In the multilabel case with binary label indicators: >>> import numpy as np >>> hamming_loss(np.array([[0, 1], [1, 1]]), np.zeros((2, 2))) 0.75 Examples using sklearn.metrics.hamming_loss Model Complexity Influence
sklearn.modules.generated.sklearn.metrics.hamming_loss
sklearn.metrics.hinge_loss sklearn.metrics.hinge_loss(y_true, pred_decision, *, labels=None, sample_weight=None) [source] Average hinge loss (non-regularized). In binary class case, assuming labels in y_true are encoded with +1 and -1, when a prediction mistake is made, margin = y_true * pred_decision is always negative (since the signs disagree), implying 1 - margin is always greater than 1. The cumulated hinge loss is therefore an upper bound of the number of mistakes made by the classifier. In multiclass case, the function expects that either all the labels are included in y_true or an optional labels argument is provided which contains all the labels. The multilabel margin is calculated according to Crammer-Singer’s method. As in the binary case, the cumulated hinge loss is an upper bound of the number of mistakes made by the classifier. Read more in the User Guide. Parameters y_truearray of shape (n_samples,) True target, consisting of integers of two values. The positive label must be greater than the negative label. pred_decisionarray of shape (n_samples,) or (n_samples, n_classes) Predicted decisions, as output by decision_function (floats). labelsarray-like, default=None Contains all the labels for the problem. Used in multiclass hinge loss. sample_weightarray-like of shape (n_samples,), default=None Sample weights. Returns lossfloat References 1 Wikipedia entry on the Hinge loss. 2 Koby Crammer, Yoram Singer. On the Algorithmic Implementation of Multiclass Kernel-based Vector Machines. Journal of Machine Learning Research 2, (2001), 265-292. 3 L1 AND L2 Regularization for Multiclass Hinge Loss Models by Robert C. Moore, John DeNero. Examples >>> from sklearn import svm >>> from sklearn.metrics import hinge_loss >>> X = [[0], [1]] >>> y = [-1, 1] >>> est = svm.LinearSVC(random_state=0) >>> est.fit(X, y) LinearSVC(random_state=0) >>> pred_decision = est.decision_function([[-2], [3], [0.5]]) >>> pred_decision array([-2.18..., 2.36..., 0.09...]) >>> hinge_loss([-1, 1, 1], pred_decision) 0.30... In the multiclass case: >>> import numpy as np >>> X = np.array([[0], [1], [2], [3]]) >>> Y = np.array([0, 1, 2, 3]) >>> labels = np.array([0, 1, 2, 3]) >>> est = svm.LinearSVC() >>> est.fit(X, Y) LinearSVC() >>> pred_decision = est.decision_function([[-1], [2], [3]]) >>> y_true = [0, 2, 3] >>> hinge_loss(y_true, pred_decision, labels=labels) 0.56...
sklearn.modules.generated.sklearn.metrics.hinge_loss
sklearn.metrics.homogeneity_completeness_v_measure sklearn.metrics.homogeneity_completeness_v_measure(labels_true, labels_pred, *, beta=1.0) [source] Compute the homogeneity and completeness and V-Measure scores at once. Those metrics are based on normalized conditional entropy measures of the clustering labeling to evaluate given the knowledge of a Ground Truth class labels of the same samples. A clustering result satisfies homogeneity if all of its clusters contain only data points which are members of a single class. A clustering result satisfies completeness if all the data points that are members of a given class are elements of the same cluster. Both scores have positive values between 0.0 and 1.0, larger values being desirable. Those 3 metrics are independent of the absolute values of the labels: a permutation of the class or cluster label values won’t change the score values in any way. V-Measure is furthermore symmetric: swapping labels_true and label_pred will give the same score. This does not hold for homogeneity and completeness. V-Measure is identical to normalized_mutual_info_score with the arithmetic averaging method. Read more in the User Guide. Parameters labels_trueint array, shape = [n_samples] ground truth class labels to be used as a reference labels_predarray-like of shape (n_samples,) cluster labels to evaluate betafloat, default=1.0 Ratio of weight attributed to homogeneity vs completeness. If beta is greater than 1, completeness is weighted more strongly in the calculation. If beta is less than 1, homogeneity is weighted more strongly. Returns homogeneityfloat score between 0.0 and 1.0. 1.0 stands for perfectly homogeneous labeling completenessfloat score between 0.0 and 1.0. 1.0 stands for perfectly complete labeling v_measurefloat harmonic mean of the first two See also homogeneity_score completeness_score v_measure_score
sklearn.modules.generated.sklearn.metrics.homogeneity_completeness_v_measure
sklearn.metrics.homogeneity_score sklearn.metrics.homogeneity_score(labels_true, labels_pred) [source] Homogeneity metric of a cluster labeling given a ground truth. A clustering result satisfies homogeneity if all of its clusters contain only data points which are members of a single class. This metric is independent of the absolute values of the labels: a permutation of the class or cluster label values won’t change the score value in any way. This metric is not symmetric: switching label_true with label_pred will return the completeness_score which will be different in general. Read more in the User Guide. Parameters labels_trueint array, shape = [n_samples] ground truth class labels to be used as a reference labels_predarray-like of shape (n_samples,) cluster labels to evaluate Returns homogeneityfloat score between 0.0 and 1.0. 1.0 stands for perfectly homogeneous labeling See also completeness_score v_measure_score References 1 Andrew Rosenberg and Julia Hirschberg, 2007. V-Measure: A conditional entropy-based external cluster evaluation measure Examples Perfect labelings are homogeneous: >>> from sklearn.metrics.cluster import homogeneity_score >>> homogeneity_score([0, 0, 1, 1], [1, 1, 0, 0]) 1.0 Non-perfect labelings that further split classes into more clusters can be perfectly homogeneous: >>> print("%.6f" % homogeneity_score([0, 0, 1, 1], [0, 0, 1, 2])) 1.000000 >>> print("%.6f" % homogeneity_score([0, 0, 1, 1], [0, 1, 2, 3])) 1.000000 Clusters that include samples from different classes do not make for an homogeneous labeling: >>> print("%.6f" % homogeneity_score([0, 0, 1, 1], [0, 1, 0, 1])) 0.0... >>> print("%.6f" % homogeneity_score([0, 0, 1, 1], [0, 0, 0, 0])) 0.0... Examples using sklearn.metrics.homogeneity_score Demo of affinity propagation clustering algorithm Demo of DBSCAN clustering algorithm A demo of K-Means clustering on the handwritten digits data Clustering text documents using k-means
sklearn.modules.generated.sklearn.metrics.homogeneity_score
sklearn.metrics.jaccard_score sklearn.metrics.jaccard_score(y_true, y_pred, *, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn') [source] Jaccard similarity coefficient score. The Jaccard index [1], or Jaccard similarity coefficient, defined as the size of the intersection divided by the size of the union of two label sets, is used to compare set of predicted labels for a sample to the corresponding set of labels in y_true. Read more in the User Guide. Parameters y_true1d array-like, or label indicator array / sparse matrix Ground truth (correct) labels. y_pred1d array-like, or label indicator array / sparse matrix Predicted labels, as returned by a classifier. labelsarray-like of shape (n_classes,), 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. 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{None, ‘micro’, ‘macro’, ‘samples’, ‘weighted’, ‘binary’}, default=’binary’ 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. 'samples': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). sample_weightarray-like of shape (n_samples,), default=None Sample weights. zero_division“warn”, {0.0, 1.0}, default=”warn” Sets the value to return when there is a zero division, i.e. when there there are no negative values in predictions and labels. If set to “warn”, this acts like 0, but a warning is also raised. Returns scorefloat (if average is not None) or array of floats, shape = [n_unique_labels] See also accuracy_score, f_score, multilabel_confusion_matrix Notes jaccard_score may be a poor metric if there are no positives for some samples or classes. Jaccard is undefined if there are no true or predicted labels, and our implementation will return a score of 0 with a warning. References 1 Wikipedia entry for the Jaccard index. Examples >>> import numpy as np >>> from sklearn.metrics import jaccard_score >>> y_true = np.array([[0, 1, 1], ... [1, 1, 0]]) >>> y_pred = np.array([[1, 1, 1], ... [1, 0, 0]]) In the binary case: >>> jaccard_score(y_true[0], y_pred[0]) 0.6666... In the multilabel case: >>> jaccard_score(y_true, y_pred, average='samples') 0.5833... >>> jaccard_score(y_true, y_pred, average='macro') 0.6666... >>> jaccard_score(y_true, y_pred, average=None) array([0.5, 0.5, 1. ]) In the multiclass case: >>> y_pred = [0, 2, 1, 2] >>> y_true = [0, 1, 2, 2] >>> jaccard_score(y_true, y_pred, average=None) array([1. , 0. , 0.33...]) Examples using sklearn.metrics.jaccard_score Classifier Chain
sklearn.modules.generated.sklearn.metrics.jaccard_score
sklearn.metrics.label_ranking_average_precision_score sklearn.metrics.label_ranking_average_precision_score(y_true, y_score, *, sample_weight=None) [source] Compute ranking-based average precision. Label ranking average precision (LRAP) is the average over each ground truth label assigned to each sample, of the ratio of true vs. total labels with lower score. This metric is used in multilabel ranking problem, where the goal is to give better rank to the labels associated to each sample. The obtained score is always strictly greater than 0 and the best value is 1. Read more in the User Guide. Parameters y_true{ndarray, sparse matrix} of shape (n_samples, n_labels) True binary labels in binary indicator format. y_scorendarray of shape (n_samples, n_labels) Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers). sample_weightarray-like of shape (n_samples,), default=None Sample weights. New in version 0.20. Returns scorefloat Examples >>> import numpy as np >>> from sklearn.metrics import label_ranking_average_precision_score >>> y_true = np.array([[1, 0, 0], [0, 0, 1]]) >>> y_score = np.array([[0.75, 0.5, 1], [1, 0.2, 0.1]]) >>> label_ranking_average_precision_score(y_true, y_score) 0.416...
sklearn.modules.generated.sklearn.metrics.label_ranking_average_precision_score
sklearn.metrics.label_ranking_loss sklearn.metrics.label_ranking_loss(y_true, y_score, *, sample_weight=None) [source] Compute Ranking loss measure. Compute the average number of label pairs that are incorrectly ordered given y_score weighted by the size of the label set and the number of labels not in the label set. This is similar to the error set size, but weighted by the number of relevant and irrelevant labels. The best performance is achieved with a ranking loss of zero. Read more in the User Guide. New in version 0.17: A function label_ranking_loss Parameters y_true{ndarray, sparse matrix} of shape (n_samples, n_labels) True binary labels in binary indicator format. y_scorendarray of shape (n_samples, n_labels) Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers). sample_weightarray-like of shape (n_samples,), default=None Sample weights. Returns lossfloat References 1 Tsoumakas, G., Katakis, I., & Vlahavas, I. (2010). Mining multi-label data. In Data mining and knowledge discovery handbook (pp. 667-685). Springer US.
sklearn.modules.generated.sklearn.metrics.label_ranking_loss
sklearn.metrics.log_loss sklearn.metrics.log_loss(y_true, y_pred, *, eps=1e-15, normalize=True, sample_weight=None, labels=None) [source] Log loss, aka logistic loss or cross-entropy loss. This is the loss function used in (multinomial) logistic regression and extensions of it such as neural networks, defined as the negative log-likelihood of a logistic model that returns y_pred probabilities for its training data y_true. The log loss is only defined for two or more labels. For a single sample with true label \(y \in \{0,1\}\) and and a probability estimate \(p = \operatorname{Pr}(y = 1)\), the log loss is: \[L_{\log}(y, p) = -(y \log (p) + (1 - y) \log (1 - p))\] Read more in the User Guide. Parameters y_truearray-like or label indicator matrix Ground truth (correct) labels for n_samples samples. y_predarray-like of float, shape = (n_samples, n_classes) or (n_samples,) Predicted probabilities, as returned by a classifier’s predict_proba method. If y_pred.shape = (n_samples,) the probabilities provided are assumed to be that of the positive class. The labels in y_pred are assumed to be ordered alphabetically, as done by preprocessing.LabelBinarizer. epsfloat, default=1e-15 Log loss is undefined for p=0 or p=1, so probabilities are clipped to max(eps, min(1 - eps, p)). normalizebool, default=True If true, return the mean loss per sample. Otherwise, return the sum of the per-sample losses. sample_weightarray-like of shape (n_samples,), default=None Sample weights. labelsarray-like, default=None If not provided, labels will be inferred from y_true. If labels is None and y_pred has shape (n_samples,) the labels are assumed to be binary and are inferred from y_true. New in version 0.18. Returns lossfloat Notes The logarithm used is the natural logarithm (base-e). References C.M. Bishop (2006). Pattern Recognition and Machine Learning. Springer, p. 209. Examples >>> from sklearn.metrics import log_loss >>> log_loss(["spam", "ham", "ham", "spam"], ... [[.1, .9], [.9, .1], [.8, .2], [.35, .65]]) 0.21616... Examples using sklearn.metrics.log_loss Probability Calibration for 3-class classification Probabilistic predictions with Gaussian process classification (GPC)
sklearn.modules.generated.sklearn.metrics.log_loss
sklearn.metrics.make_scorer sklearn.metrics.make_scorer(score_func, *, greater_is_better=True, needs_proba=False, needs_threshold=False, **kwargs) [source] Make a scorer from a performance metric or loss function. This factory function wraps scoring functions for use in GridSearchCV and cross_val_score. It takes a score function, such as accuracy_score, mean_squared_error, adjusted_rand_index or average_precision and returns a callable that scores an estimator’s output. The signature of the call is (estimator, X, y) where estimator is the model to be evaluated, X is the data and y is the ground truth labeling (or None in the case of unsupervised models). Read more in the User Guide. Parameters score_funccallable Score function (or loss function) with signature score_func(y, y_pred, **kwargs). greater_is_betterbool, default=True Whether score_func is a score function (default), meaning high is good, or a loss function, meaning low is good. In the latter case, the scorer object will sign-flip the outcome of the score_func. needs_probabool, default=False Whether score_func requires predict_proba to get probability estimates out of a classifier. If True, for binary y_true, the score function is supposed to accept a 1D y_pred (i.e., probability of the positive class, shape (n_samples,)). needs_thresholdbool, default=False Whether score_func takes a continuous decision certainty. This only works for binary classification using estimators that have either a decision_function or predict_proba method. If True, for binary y_true, the score function is supposed to accept a 1D y_pred (i.e., probability of the positive class or the decision function, shape (n_samples,)). For example average_precision or the area under the roc curve can not be computed using discrete predictions alone. **kwargsadditional arguments Additional parameters to be passed to score_func. Returns scorercallable Callable object that returns a scalar score; greater is better. Notes If needs_proba=False and needs_threshold=False, the score function is supposed to accept the output of predict. If needs_proba=True, the score function is supposed to accept the output of predict_proba (For binary y_true, the score function is supposed to accept probability of the positive class). If needs_threshold=True, the score function is supposed to accept the output of decision_function. Examples >>> from sklearn.metrics import fbeta_score, make_scorer >>> ftwo_scorer = make_scorer(fbeta_score, beta=2) >>> ftwo_scorer make_scorer(fbeta_score, beta=2) >>> from sklearn.model_selection import GridSearchCV >>> from sklearn.svm import LinearSVC >>> grid = GridSearchCV(LinearSVC(), param_grid={'C': [1, 10]}, ... scoring=ftwo_scorer) Examples using sklearn.metrics.make_scorer Demonstration of multi-metric evaluation on cross_val_score and GridSearchCV
sklearn.modules.generated.sklearn.metrics.make_scorer
sklearn.metrics.matthews_corrcoef sklearn.metrics.matthews_corrcoef(y_true, y_pred, *, sample_weight=None) [source] Compute the Matthews correlation coefficient (MCC). The Matthews correlation coefficient is used in machine learning as a measure of the quality of binary and multiclass classifications. It takes into account true and false positives and negatives and is generally regarded as a balanced measure which can be used even if the classes are of very different sizes. The MCC is in essence a correlation coefficient value between -1 and +1. A coefficient of +1 represents a perfect prediction, 0 an average random prediction and -1 an inverse prediction. The statistic is also known as the phi coefficient. [source: Wikipedia] Binary and multiclass labels are supported. Only in the binary case does this relate to information about true and false positives and negatives. See references below. Read more in the User Guide. Parameters y_truearray, shape = [n_samples] Ground truth (correct) target values. y_predarray, shape = [n_samples] Estimated targets as returned by a classifier. sample_weightarray-like of shape (n_samples,), default=None Sample weights. New in version 0.18. Returns mccfloat The Matthews correlation coefficient (+1 represents a perfect prediction, 0 an average random prediction and -1 and inverse prediction). References 1 Baldi, Brunak, Chauvin, Andersen and Nielsen, (2000). Assessing the accuracy of prediction algorithms for classification: an overview. 2 Wikipedia entry for the Matthews Correlation Coefficient. 3 Gorodkin, (2004). Comparing two K-category assignments by a K-category correlation coefficient. 4 Jurman, Riccadonna, Furlanello, (2012). A Comparison of MCC and CEN Error Measures in MultiClass Prediction. Examples >>> from sklearn.metrics import matthews_corrcoef >>> y_true = [+1, +1, +1, -1] >>> y_pred = [+1, -1, +1, +1] >>> matthews_corrcoef(y_true, y_pred) -0.33...
sklearn.modules.generated.sklearn.metrics.matthews_corrcoef
sklearn.metrics.max_error sklearn.metrics.max_error(y_true, y_pred) [source] max_error metric calculates the maximum residual error. Read more in the User Guide. Parameters y_truearray-like of shape (n_samples,) Ground truth (correct) target values. y_predarray-like of shape (n_samples,) Estimated target values. Returns max_errorfloat A positive floating point value (the best value is 0.0). Examples >>> from sklearn.metrics import max_error >>> y_true = [3, 2, 7, 1] >>> y_pred = [4, 2, 7, 1] >>> max_error(y_true, y_pred) 1
sklearn.modules.generated.sklearn.metrics.max_error
sklearn.metrics.mean_absolute_error sklearn.metrics.mean_absolute_error(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average') [source] Mean absolute error regression loss. Read more in the User Guide. Parameters y_truearray-like of shape (n_samples,) or (n_samples, n_outputs) Ground truth (correct) target values. y_predarray-like of shape (n_samples,) or (n_samples, n_outputs) Estimated target values. sample_weightarray-like of shape (n_samples,), default=None Sample weights. multioutput{‘raw_values’, ‘uniform_average’} or array-like of shape (n_outputs,), default=’uniform_average’ Defines aggregating of multiple output values. Array-like value defines weights used to average errors. ‘raw_values’ : Returns a full set of errors in case of multioutput input. ‘uniform_average’ : Errors of all outputs are averaged with uniform weight. Returns lossfloat or ndarray of floats If multioutput is ‘raw_values’, then mean absolute error is returned for each output separately. If multioutput is ‘uniform_average’ or an ndarray of weights, then the weighted average of all output errors is returned. MAE output is non-negative floating point. The best value is 0.0. Examples >>> from sklearn.metrics import mean_absolute_error >>> y_true = [3, -0.5, 2, 7] >>> y_pred = [2.5, 0.0, 2, 8] >>> mean_absolute_error(y_true, y_pred) 0.5 >>> y_true = [[0.5, 1], [-1, 1], [7, -6]] >>> y_pred = [[0, 2], [-1, 2], [8, -5]] >>> mean_absolute_error(y_true, y_pred) 0.75 >>> mean_absolute_error(y_true, y_pred, multioutput='raw_values') array([0.5, 1. ]) >>> mean_absolute_error(y_true, y_pred, multioutput=[0.3, 0.7]) 0.85... Examples using sklearn.metrics.mean_absolute_error Poisson regression and non-normal loss Tweedie regression on insurance claims
sklearn.modules.generated.sklearn.metrics.mean_absolute_error
sklearn.metrics.mean_absolute_percentage_error sklearn.metrics.mean_absolute_percentage_error(y_true, y_pred, sample_weight=None, multioutput='uniform_average') [source] Mean absolute percentage error regression loss. Note here that we do not represent the output as a percentage in range [0, 100]. Instead, we represent it in range [0, 1/eps]. Read more in the User Guide. New in version 0.24. Parameters y_truearray-like of shape (n_samples,) or (n_samples, n_outputs) Ground truth (correct) target values. y_predarray-like of shape (n_samples,) or (n_samples, n_outputs) Estimated target values. sample_weightarray-like of shape (n_samples,), default=None Sample weights. multioutput{‘raw_values’, ‘uniform_average’} or array-like Defines aggregating of multiple output values. Array-like value defines weights used to average errors. If input is list then the shape must be (n_outputs,). ‘raw_values’ : Returns a full set of errors in case of multioutput input. ‘uniform_average’ : Errors of all outputs are averaged with uniform weight. Returns lossfloat or ndarray of floats in the range [0, 1/eps] If multioutput is ‘raw_values’, then mean absolute percentage error is returned for each output separately. If multioutput is ‘uniform_average’ or an ndarray of weights, then the weighted average of all output errors is returned. MAPE output is non-negative floating point. The best value is 0.0. But note the fact that bad predictions can lead to arbitarily large MAPE values, especially if some y_true values are very close to zero. Note that we return a large value instead of inf when y_true is zero. Examples >>> from sklearn.metrics import mean_absolute_percentage_error >>> y_true = [3, -0.5, 2, 7] >>> y_pred = [2.5, 0.0, 2, 8] >>> mean_absolute_percentage_error(y_true, y_pred) 0.3273... >>> y_true = [[0.5, 1], [-1, 1], [7, -6]] >>> y_pred = [[0, 2], [-1, 2], [8, -5]] >>> mean_absolute_percentage_error(y_true, y_pred) 0.5515... >>> mean_absolute_percentage_error(y_true, y_pred, multioutput=[0.3, 0.7]) 0.6198...
sklearn.modules.generated.sklearn.metrics.mean_absolute_percentage_error
sklearn.metrics.mean_gamma_deviance sklearn.metrics.mean_gamma_deviance(y_true, y_pred, *, sample_weight=None) [source] Mean Gamma deviance regression loss. Gamma deviance is equivalent to the Tweedie deviance with the power parameter power=2. It is invariant to scaling of the target variable, and measures relative errors. Read more in the User Guide. Parameters y_truearray-like of shape (n_samples,) Ground truth (correct) target values. Requires y_true > 0. y_predarray-like of shape (n_samples,) Estimated target values. Requires y_pred > 0. sample_weightarray-like of shape (n_samples,), default=None Sample weights. Returns lossfloat A non-negative floating point value (the best value is 0.0). Examples >>> from sklearn.metrics import mean_gamma_deviance >>> y_true = [2, 0.5, 1, 4] >>> y_pred = [0.5, 0.5, 2., 2.] >>> mean_gamma_deviance(y_true, y_pred) 1.0568...
sklearn.modules.generated.sklearn.metrics.mean_gamma_deviance
sklearn.metrics.mean_poisson_deviance sklearn.metrics.mean_poisson_deviance(y_true, y_pred, *, sample_weight=None) [source] Mean Poisson deviance regression loss. Poisson deviance is equivalent to the Tweedie deviance with the power parameter power=1. Read more in the User Guide. Parameters y_truearray-like of shape (n_samples,) Ground truth (correct) target values. Requires y_true >= 0. y_predarray-like of shape (n_samples,) Estimated target values. Requires y_pred > 0. sample_weightarray-like of shape (n_samples,), default=None Sample weights. Returns lossfloat A non-negative floating point value (the best value is 0.0). Examples >>> from sklearn.metrics import mean_poisson_deviance >>> y_true = [2, 0, 1, 4] >>> y_pred = [0.5, 0.5, 2., 2.] >>> mean_poisson_deviance(y_true, y_pred) 1.4260... Examples using sklearn.metrics.mean_poisson_deviance Poisson regression and non-normal loss
sklearn.modules.generated.sklearn.metrics.mean_poisson_deviance
sklearn.metrics.mean_squared_error sklearn.metrics.mean_squared_error(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average', squared=True) [source] Mean squared error regression loss. Read more in the User Guide. Parameters y_truearray-like of shape (n_samples,) or (n_samples, n_outputs) Ground truth (correct) target values. y_predarray-like of shape (n_samples,) or (n_samples, n_outputs) Estimated target values. sample_weightarray-like of shape (n_samples,), default=None Sample weights. multioutput{‘raw_values’, ‘uniform_average’} or array-like of shape (n_outputs,), default=’uniform_average’ Defines aggregating of multiple output values. Array-like value defines weights used to average errors. ‘raw_values’ : Returns a full set of errors in case of multioutput input. ‘uniform_average’ : Errors of all outputs are averaged with uniform weight. squaredbool, default=True If True returns MSE value, if False returns RMSE value. Returns lossfloat or ndarray of floats A non-negative floating point value (the best value is 0.0), or an array of floating point values, one for each individual target. Examples >>> from sklearn.metrics import mean_squared_error >>> y_true = [3, -0.5, 2, 7] >>> y_pred = [2.5, 0.0, 2, 8] >>> mean_squared_error(y_true, y_pred) 0.375 >>> y_true = [3, -0.5, 2, 7] >>> y_pred = [2.5, 0.0, 2, 8] >>> mean_squared_error(y_true, y_pred, squared=False) 0.612... >>> y_true = [[0.5, 1],[-1, 1],[7, -6]] >>> y_pred = [[0, 2],[-1, 2],[8, -5]] >>> mean_squared_error(y_true, y_pred) 0.708... >>> mean_squared_error(y_true, y_pred, squared=False) 0.822... >>> mean_squared_error(y_true, y_pred, multioutput='raw_values') array([0.41666667, 1. ]) >>> mean_squared_error(y_true, y_pred, multioutput=[0.3, 0.7]) 0.825... Examples using sklearn.metrics.mean_squared_error Gradient Boosting regression Model Complexity Influence Plot Ridge coefficients as a function of the L2 regularization Linear Regression Example Robust linear estimator fitting Poisson regression and non-normal loss Tweedie regression on insurance claims
sklearn.modules.generated.sklearn.metrics.mean_squared_error
sklearn.metrics.mean_squared_log_error sklearn.metrics.mean_squared_log_error(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average') [source] Mean squared logarithmic error regression loss. Read more in the User Guide. Parameters y_truearray-like of shape (n_samples,) or (n_samples, n_outputs) Ground truth (correct) target values. y_predarray-like of shape (n_samples,) or (n_samples, n_outputs) Estimated target values. sample_weightarray-like of shape (n_samples,), default=None Sample weights. multioutput{‘raw_values’, ‘uniform_average’} or array-like of shape (n_outputs,), default=’uniform_average’ Defines aggregating of multiple output values. Array-like value defines weights used to average errors. ‘raw_values’ : Returns a full set of errors when the input is of multioutput format. ‘uniform_average’ : Errors of all outputs are averaged with uniform weight. Returns lossfloat or ndarray of floats A non-negative floating point value (the best value is 0.0), or an array of floating point values, one for each individual target. Examples >>> from sklearn.metrics import mean_squared_log_error >>> y_true = [3, 5, 2.5, 7] >>> y_pred = [2.5, 5, 4, 8] >>> mean_squared_log_error(y_true, y_pred) 0.039... >>> y_true = [[0.5, 1], [1, 2], [7, 6]] >>> y_pred = [[0.5, 2], [1, 2.5], [8, 8]] >>> mean_squared_log_error(y_true, y_pred) 0.044... >>> mean_squared_log_error(y_true, y_pred, multioutput='raw_values') array([0.00462428, 0.08377444]) >>> mean_squared_log_error(y_true, y_pred, multioutput=[0.3, 0.7]) 0.060...
sklearn.modules.generated.sklearn.metrics.mean_squared_log_error
sklearn.metrics.mean_tweedie_deviance sklearn.metrics.mean_tweedie_deviance(y_true, y_pred, *, sample_weight=None, power=0) [source] Mean Tweedie deviance regression loss. Read more in the User Guide. Parameters y_truearray-like of shape (n_samples,) Ground truth (correct) target values. y_predarray-like of shape (n_samples,) Estimated target values. sample_weightarray-like of shape (n_samples,), default=None Sample weights. powerfloat, default=0 Tweedie power parameter. Either power <= 0 or power >= 1. The higher p the less weight is given to extreme deviations between true and predicted targets. power < 0: Extreme stable distribution. Requires: y_pred > 0. power = 0 : Normal distribution, output corresponds to mean_squared_error. y_true and y_pred can be any real numbers. power = 1 : Poisson distribution. Requires: y_true >= 0 and y_pred > 0. 1 < p < 2 : Compound Poisson distribution. Requires: y_true >= 0 and y_pred > 0. power = 2 : Gamma distribution. Requires: y_true > 0 and y_pred > 0. power = 3 : Inverse Gaussian distribution. Requires: y_true > 0 and y_pred > 0. otherwise : Positive stable distribution. Requires: y_true > 0 and y_pred > 0. Returns lossfloat A non-negative floating point value (the best value is 0.0). Examples >>> from sklearn.metrics import mean_tweedie_deviance >>> y_true = [2, 0, 1, 4] >>> y_pred = [0.5, 0.5, 2., 2.] >>> mean_tweedie_deviance(y_true, y_pred, power=1) 1.4260... Examples using sklearn.metrics.mean_tweedie_deviance Tweedie regression on insurance claims
sklearn.modules.generated.sklearn.metrics.mean_tweedie_deviance
sklearn.metrics.median_absolute_error sklearn.metrics.median_absolute_error(y_true, y_pred, *, multioutput='uniform_average', sample_weight=None) [source] Median absolute error regression loss. Median absolute error output is non-negative floating point. The best value is 0.0. Read more in the User Guide. Parameters y_truearray-like of shape = (n_samples) or (n_samples, n_outputs) Ground truth (correct) target values. y_predarray-like of shape = (n_samples) or (n_samples, n_outputs) Estimated target values. multioutput{‘raw_values’, ‘uniform_average’} or array-like of shape (n_outputs,), default=’uniform_average’ Defines aggregating of multiple output values. Array-like value defines weights used to average errors. ‘raw_values’ : Returns a full set of errors in case of multioutput input. ‘uniform_average’ : Errors of all outputs are averaged with uniform weight. sample_weightarray-like of shape (n_samples,), default=None Sample weights. New in version 0.24. Returns lossfloat or ndarray of floats If multioutput is ‘raw_values’, then mean absolute error is returned for each output separately. If multioutput is ‘uniform_average’ or an ndarray of weights, then the weighted average of all output errors is returned. Examples >>> from sklearn.metrics import median_absolute_error >>> y_true = [3, -0.5, 2, 7] >>> y_pred = [2.5, 0.0, 2, 8] >>> median_absolute_error(y_true, y_pred) 0.5 >>> y_true = [[0.5, 1], [-1, 1], [7, -6]] >>> y_pred = [[0, 2], [-1, 2], [8, -5]] >>> median_absolute_error(y_true, y_pred) 0.75 >>> median_absolute_error(y_true, y_pred, multioutput='raw_values') array([0.5, 1. ]) >>> median_absolute_error(y_true, y_pred, multioutput=[0.3, 0.7]) 0.85 Examples using sklearn.metrics.median_absolute_error Common pitfalls in interpretation of coefficients of linear models Effect of transforming the targets in regression model
sklearn.modules.generated.sklearn.metrics.median_absolute_error
sklearn.metrics.multilabel_confusion_matrix sklearn.metrics.multilabel_confusion_matrix(y_true, y_pred, *, sample_weight=None, labels=None, samplewise=False) [source] Compute a confusion matrix for each class or sample. New in version 0.21. Compute class-wise (default) or sample-wise (samplewise=True) multilabel confusion matrix to evaluate the accuracy of a classification, and output confusion matrices for each class or sample. In multilabel confusion matrix \(MCM\), the count of true negatives is \(MCM_{:,0,0}\), false negatives is \(MCM_{:,1,0}\), true positives is \(MCM_{:,1,1}\) and false positives is \(MCM_{:,0,1}\). Multiclass data will be treated as if binarized under a one-vs-rest transformation. Returned confusion matrices will be in the order of sorted unique labels in the union of (y_true, y_pred). Read more in the User Guide. Parameters y_true{array-like, sparse matrix} of shape (n_samples, n_outputs) or (n_samples,) Ground truth (correct) target values. y_pred{array-like, sparse matrix} of shape (n_samples, n_outputs) or (n_samples,) Estimated targets as returned by a classifier. sample_weightarray-like of shape (n_samples,), default=None Sample weights. labelsarray-like of shape (n_classes,), default=None A list of classes or column indices to select some (or to force inclusion of classes absent from the data). samplewisebool, default=False In the multilabel case, this calculates a confusion matrix per sample. Returns multi_confusionndarray of shape (n_outputs, 2, 2) A 2x2 confusion matrix corresponding to each output in the input. When calculating class-wise multi_confusion (default), then n_outputs = n_labels; when calculating sample-wise multi_confusion (samplewise=True), n_outputs = n_samples. If labels is defined, the results will be returned in the order specified in labels, otherwise the results will be returned in sorted order by default. See also confusion_matrix Notes The multilabel_confusion_matrix calculates class-wise or sample-wise multilabel confusion matrices, and in multiclass tasks, labels are binarized under a one-vs-rest way; while confusion_matrix calculates one confusion matrix for confusion between every two classes. Examples Multilabel-indicator case: >>> import numpy as np >>> from sklearn.metrics import multilabel_confusion_matrix >>> y_true = np.array([[1, 0, 1], ... [0, 1, 0]]) >>> y_pred = np.array([[1, 0, 0], ... [0, 1, 1]]) >>> multilabel_confusion_matrix(y_true, y_pred) array([[[1, 0], [0, 1]], [[1, 0], [0, 1]], [[0, 1], [1, 0]]]) Multiclass case: >>> y_true = ["cat", "ant", "cat", "cat", "ant", "bird"] >>> y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"] >>> multilabel_confusion_matrix(y_true, y_pred, ... labels=["ant", "bird", "cat"]) array([[[3, 1], [0, 2]], [[5, 0], [1, 0]], [[2, 1], [1, 2]]])
sklearn.modules.generated.sklearn.metrics.multilabel_confusion_matrix
sklearn.metrics.mutual_info_score sklearn.metrics.mutual_info_score(labels_true, labels_pred, *, contingency=None) [source] Mutual Information between two clusterings. The Mutual Information is a measure of the similarity between two labels of the same data. Where \(|U_i|\) is the number of the samples in cluster \(U_i\) and \(|V_j|\) is the number of the samples in cluster \(V_j\), the Mutual Information between clusterings \(U\) and \(V\) is given as: \[MI(U,V)=\sum_{i=1}^{|U|} \sum_{j=1}^{|V|} \frac{|U_i\cap V_j|}{N} \log\frac{N|U_i \cap V_j|}{|U_i||V_j|}\] 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] A clustering of the data into disjoint subsets. labels_predint array-like of shape (n_samples,) A clustering of the data into disjoint subsets. contingency{ndarray, sparse matrix} of shape (n_classes_true, n_classes_pred), default=None A contingency matrix given by the contingency_matrix function. If value is None, it will be computed, otherwise the given value is used, with labels_true and labels_pred ignored. Returns mifloat Mutual information, a non-negative value See also adjusted_mutual_info_score Adjusted against chance Mutual Information. normalized_mutual_info_score Normalized Mutual Information. Notes The logarithm used is the natural logarithm (base-e). Examples using sklearn.metrics.mutual_info_score Adjustment for chance in clustering performance evaluation
sklearn.modules.generated.sklearn.metrics.mutual_info_score
sklearn.metrics.ndcg_score sklearn.metrics.ndcg_score(y_true, y_score, *, k=None, sample_weight=None, ignore_ties=False) [source] Compute Normalized Discounted Cumulative Gain. Sum the true scores ranked in the order induced by the predicted scores, after applying a logarithmic discount. Then divide by the best possible score (Ideal DCG, obtained for a perfect ranking) to obtain a score between 0 and 1. This ranking metric yields a high value if true labels are ranked high by y_score. Parameters y_truendarray of shape (n_samples, n_labels) True targets of multilabel classification, or true scores of entities to be ranked. y_scorendarray of shape (n_samples, n_labels) Target scores, can either be probability estimates, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers). kint, default=None Only consider the highest k scores in the ranking. If None, use all outputs. sample_weightndarray of shape (n_samples,), default=None Sample weights. If None, all samples are given the same weight. ignore_tiesbool, default=False Assume that there are no ties in y_score (which is likely to be the case if y_score is continuous) for efficiency gains. Returns normalized_discounted_cumulative_gainfloat in [0., 1.] The averaged NDCG scores for all samples. See also dcg_score Discounted Cumulative Gain (not normalized). References Wikipedia entry for Discounted Cumulative Gain Jarvelin, K., & Kekalainen, J. (2002). Cumulated gain-based evaluation of IR techniques. ACM Transactions on Information Systems (TOIS), 20(4), 422-446. Wang, Y., Wang, L., Li, Y., He, D., Chen, W., & Liu, T. Y. (2013, May). A theoretical analysis of NDCG ranking measures. In Proceedings of the 26th Annual Conference on Learning Theory (COLT 2013) McSherry, F., & Najork, M. (2008, March). Computing information retrieval performance measures efficiently in the presence of tied scores. In European conference on information retrieval (pp. 414-421). Springer, Berlin, Heidelberg. Examples >>> from sklearn.metrics import ndcg_score >>> # we have groud-truth relevance of some answers to a query: >>> true_relevance = np.asarray([[10, 0, 0, 1, 5]]) >>> # we predict some scores (relevance) for the answers >>> scores = np.asarray([[.1, .2, .3, 4, 70]]) >>> ndcg_score(true_relevance, scores) 0.69... >>> scores = np.asarray([[.05, 1.1, 1., .5, .0]]) >>> ndcg_score(true_relevance, scores) 0.49... >>> # we can set k to truncate the sum; only top k answers contribute. >>> ndcg_score(true_relevance, scores, k=4) 0.35... >>> # the normalization takes k into account so a perfect answer >>> # would still get 1.0 >>> ndcg_score(true_relevance, true_relevance, k=4) 1.0 >>> # now we have some ties in our prediction >>> scores = np.asarray([[1, 0, 0, 0, 1]]) >>> # by default ties are averaged, so here we get the average (normalized) >>> # true relevance of our top predictions: (10 / 10 + 5 / 10) / 2 = .75 >>> ndcg_score(true_relevance, scores, k=1) 0.75 >>> # we can choose to ignore ties for faster results, but only >>> # if we know there aren't ties in our scores, otherwise we get >>> # wrong results: >>> ndcg_score(true_relevance, ... scores, k=1, ignore_ties=True) 0.5
sklearn.modules.generated.sklearn.metrics.ndcg_score
sklearn.metrics.normalized_mutual_info_score sklearn.metrics.normalized_mutual_info_score(labels_true, labels_pred, *, average_method='arithmetic') [source] Normalized Mutual Information between two clusterings. Normalized Mutual Information (NMI) is a normalization of the Mutual Information (MI) score to scale the results between 0 (no mutual information) and 1 (perfect correlation). In this function, mutual information is normalized by some generalized mean of H(labels_true) and H(labels_pred)), defined by the average_method. This measure is not adjusted for chance. Therefore adjusted_mutual_info_score might be preferred. 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] A clustering of the data into disjoint subsets. labels_predint array-like of shape (n_samples,) A clustering of the data into disjoint subsets. average_methodstr, default=’arithmetic’ How to compute the normalizer in the denominator. Possible options are ‘min’, ‘geometric’, ‘arithmetic’, and ‘max’. New in version 0.20. Changed in version 0.22: The default value of average_method changed from ‘geometric’ to ‘arithmetic’. Returns nmifloat score between 0.0 and 1.0. 1.0 stands for perfectly complete labeling See also v_measure_score V-Measure (NMI with arithmetic mean option). adjusted_rand_score Adjusted Rand Index. adjusted_mutual_info_score Adjusted Mutual Information (adjusted against chance). Examples Perfect labelings are both homogeneous and complete, hence have score 1.0: >>> from sklearn.metrics.cluster import normalized_mutual_info_score >>> normalized_mutual_info_score([0, 0, 1, 1], [0, 0, 1, 1]) ... 1.0 >>> normalized_mutual_info_score([0, 0, 1, 1], [1, 1, 0, 0]) ... 1.0 If classes members are completely split across different clusters, the assignment is totally in-complete, hence the NMI is null: >>> normalized_mutual_info_score([0, 0, 0, 0], [0, 1, 2, 3]) ... 0.0
sklearn.modules.generated.sklearn.metrics.normalized_mutual_info_score
sklearn.metrics.pairwise.additive_chi2_kernel sklearn.metrics.pairwise.additive_chi2_kernel(X, Y=None) [source] Computes the additive chi-squared kernel between observations in X and Y. The chi-squared kernel is computed between each pair of rows in X and Y. X and Y have to be non-negative. This kernel is most commonly applied to histograms. The chi-squared kernel is given by: k(x, y) = -Sum [(x - y)^2 / (x + y)] It can be interpreted as a weighted difference per entry. Read more in the User Guide. Parameters Xarray-like of shape (n_samples_X, n_features) Yndarray of shape (n_samples_Y, n_features), default=None Returns kernel_matrixndarray of shape (n_samples_X, n_samples_Y) See also chi2_kernel The exponentiated version of the kernel, which is usually preferable. sklearn.kernel_approximation.AdditiveChi2Sampler A Fourier approximation to this kernel. Notes As the negative of a distance, this kernel is only conditionally positive definite. References Zhang, J. and Marszalek, M. and Lazebnik, S. and Schmid, C. Local features and kernels for classification of texture and object categories: A comprehensive study International Journal of Computer Vision 2007 https://research.microsoft.com/en-us/um/people/manik/projects/trade-off/papers/ZhangIJCV06.pdf
sklearn.modules.generated.sklearn.metrics.pairwise.additive_chi2_kernel
sklearn.metrics.pairwise.chi2_kernel sklearn.metrics.pairwise.chi2_kernel(X, Y=None, gamma=1.0) [source] Computes the exponential chi-squared kernel X and Y. The chi-squared kernel is computed between each pair of rows in X and Y. X and Y have to be non-negative. This kernel is most commonly applied to histograms. The chi-squared kernel is given by: k(x, y) = exp(-gamma Sum [(x - y)^2 / (x + y)]) It can be interpreted as a weighted difference per entry. Read more in the User Guide. Parameters Xarray-like of shape (n_samples_X, n_features) Yndarray of shape (n_samples_Y, n_features), default=None gammafloat, default=1. Scaling parameter of the chi2 kernel. Returns kernel_matrixndarray of shape (n_samples_X, n_samples_Y) See also additive_chi2_kernel The additive version of this kernel. sklearn.kernel_approximation.AdditiveChi2Sampler A Fourier approximation to the additive version of this kernel. References Zhang, J. and Marszalek, M. and Lazebnik, S. and Schmid, C. Local features and kernels for classification of texture and object categories: A comprehensive study International Journal of Computer Vision 2007 https://research.microsoft.com/en-us/um/people/manik/projects/trade-off/papers/ZhangIJCV06.pdf
sklearn.modules.generated.sklearn.metrics.pairwise.chi2_kernel
sklearn.metrics.pairwise.cosine_distances sklearn.metrics.pairwise.cosine_distances(X, Y=None) [source] Compute cosine distance between samples in X and Y. Cosine distance is defined as 1.0 minus the cosine similarity. Read more in the User Guide. Parameters X{array-like, sparse matrix} of shape (n_samples_X, n_features) Matrix X. Y{array-like, sparse matrix} of shape (n_samples_Y, n_features), default=None Matrix Y. Returns distance matrixndarray of shape (n_samples_X, n_samples_Y) See also cosine_similarity scipy.spatial.distance.cosine Dense matrices only.
sklearn.modules.generated.sklearn.metrics.pairwise.cosine_distances
sklearn.metrics.pairwise.cosine_similarity sklearn.metrics.pairwise.cosine_similarity(X, Y=None, dense_output=True) [source] Compute cosine similarity between samples in X and Y. Cosine similarity, or the cosine kernel, computes similarity as the normalized dot product of X and Y: K(X, Y) = <X, Y> / (||X||*||Y||) On L2-normalized data, this function is equivalent to linear_kernel. Read more in the User Guide. Parameters X{ndarray, sparse matrix} of shape (n_samples_X, n_features) Input data. Y{ndarray, sparse matrix} of shape (n_samples_Y, n_features), default=None Input data. If None, the output will be the pairwise similarities between all samples in X. dense_outputbool, default=True Whether to return dense output even when the input is sparse. If False, the output is sparse if both input arrays are sparse. New in version 0.17: parameter dense_output for dense output. Returns kernel matrixndarray of shape (n_samples_X, n_samples_Y)
sklearn.modules.generated.sklearn.metrics.pairwise.cosine_similarity
sklearn.metrics.pairwise.distance_metrics sklearn.metrics.pairwise.distance_metrics() [source] Valid metrics for pairwise_distances. This function simply returns the valid pairwise distance metrics. It exists to allow for a description of the mapping for each of the valid strings. The valid distance metrics, and the function they map to, are: metric Function ‘cityblock’ metrics.pairwise.manhattan_distances ‘cosine’ metrics.pairwise.cosine_distances ‘euclidean’ metrics.pairwise.euclidean_distances ‘haversine’ metrics.pairwise.haversine_distances ‘l1’ metrics.pairwise.manhattan_distances ‘l2’ metrics.pairwise.euclidean_distances ‘manhattan’ metrics.pairwise.manhattan_distances ‘nan_euclidean’ metrics.pairwise.nan_euclidean_distances Read more in the User Guide.
sklearn.modules.generated.sklearn.metrics.pairwise.distance_metrics
sklearn.metrics.pairwise.euclidean_distances sklearn.metrics.pairwise.euclidean_distances(X, Y=None, *, Y_norm_squared=None, squared=False, X_norm_squared=None) [source] Considering the rows of X (and Y=X) as vectors, compute the distance matrix between each pair of vectors. For efficiency reasons, the euclidean distance between a pair of row vector x and y is computed as: dist(x, y) = sqrt(dot(x, x) - 2 * dot(x, y) + dot(y, y)) This formulation has two advantages over other ways of computing distances. First, it is computationally efficient when dealing with sparse data. Second, if one argument varies but the other remains unchanged, then dot(x, x) and/or dot(y, y) can be pre-computed. However, this is not the most precise way of doing this computation, because this equation potentially suffers from “catastrophic cancellation”. Also, the distance matrix returned by this function may not be exactly symmetric as required by, e.g., scipy.spatial.distance functions. Read more in the User Guide. Parameters X{array-like, sparse matrix} of shape (n_samples_X, n_features) Y{array-like, sparse matrix} of shape (n_samples_Y, n_features), default=None Y_norm_squaredarray-like of shape (n_samples_Y,), default=None Pre-computed dot-products of vectors in Y (e.g., (Y**2).sum(axis=1)) May be ignored in some cases, see the note below. squaredbool, default=False Return squared Euclidean distances. X_norm_squaredarray-like of shape (n_samples,), default=None Pre-computed dot-products of vectors in X (e.g., (X**2).sum(axis=1)) May be ignored in some cases, see the note below. Returns distancesndarray of shape (n_samples_X, n_samples_Y) See also paired_distances Distances betweens pairs of elements of X and Y. Notes To achieve better accuracy, X_norm_squared and Y_norm_squared may be unused if they are passed as float32. Examples >>> from sklearn.metrics.pairwise import euclidean_distances >>> X = [[0, 1], [1, 1]] >>> # distance between rows of X >>> euclidean_distances(X, X) array([[0., 1.], [1., 0.]]) >>> # get distance to origin >>> euclidean_distances(X, [[0, 0]]) array([[1. ], [1.41421356]])
sklearn.modules.generated.sklearn.metrics.pairwise.euclidean_distances
sklearn.metrics.pairwise.haversine_distances sklearn.metrics.pairwise.haversine_distances(X, Y=None) [source] Compute the Haversine distance between samples in X and Y. The Haversine (or great circle) distance is the angular distance between two points on the surface of a sphere. The first coordinate of each point is assumed to be the latitude, the second is the longitude, given in radians. The dimension of the data must be 2. \[D(x, y) = 2\arcsin[\sqrt{\sin^2((x1 - y1) / 2) + \cos(x1)\cos(y1)\sin^2((x2 - y2) / 2)}]\] Parameters Xarray-like of shape (n_samples_X, 2) Yarray-like of shape (n_samples_Y, 2), default=None Returns distancendarray of shape (n_samples_X, n_samples_Y) Notes As the Earth is nearly spherical, the haversine formula provides a good approximation of the distance between two points of the Earth surface, with a less than 1% error on average. Examples We want to calculate the distance between the Ezeiza Airport (Buenos Aires, Argentina) and the Charles de Gaulle Airport (Paris, France). >>> from sklearn.metrics.pairwise import haversine_distances >>> from math import radians >>> bsas = [-34.83333, -58.5166646] >>> paris = [49.0083899664, 2.53844117956] >>> bsas_in_radians = [radians(_) for _ in bsas] >>> paris_in_radians = [radians(_) for _ in paris] >>> result = haversine_distances([bsas_in_radians, paris_in_radians]) >>> result * 6371000/1000 # multiply by Earth radius to get kilometers array([[ 0. , 11099.54035582], [11099.54035582, 0. ]])
sklearn.modules.generated.sklearn.metrics.pairwise.haversine_distances
sklearn.metrics.pairwise.kernel_metrics sklearn.metrics.pairwise.kernel_metrics() [source] Valid metrics for pairwise_kernels. This function simply returns the valid pairwise distance metrics. It exists, however, to allow for a verbose description of the mapping for each of the valid strings. The valid distance metrics, and the function they map to, are: metric Function ‘additive_chi2’ sklearn.pairwise.additive_chi2_kernel ‘chi2’ sklearn.pairwise.chi2_kernel ‘linear’ sklearn.pairwise.linear_kernel ‘poly’ sklearn.pairwise.polynomial_kernel ‘polynomial’ sklearn.pairwise.polynomial_kernel ‘rbf’ sklearn.pairwise.rbf_kernel ‘laplacian’ sklearn.pairwise.laplacian_kernel ‘sigmoid’ sklearn.pairwise.sigmoid_kernel ‘cosine’ sklearn.pairwise.cosine_similarity Read more in the User Guide.
sklearn.modules.generated.sklearn.metrics.pairwise.kernel_metrics
sklearn.metrics.pairwise.laplacian_kernel sklearn.metrics.pairwise.laplacian_kernel(X, Y=None, gamma=None) [source] Compute the laplacian kernel between X and Y. The laplacian kernel is defined as: K(x, y) = exp(-gamma ||x-y||_1) for each pair of rows x in X and y in Y. Read more in the User Guide. New in version 0.17. Parameters Xndarray of shape (n_samples_X, n_features) Yndarray of shape (n_samples_Y, n_features), default=None gammafloat, default=None If None, defaults to 1.0 / n_features. Returns kernel_matrixndarray of shape (n_samples_X, n_samples_Y)
sklearn.modules.generated.sklearn.metrics.pairwise.laplacian_kernel
sklearn.metrics.pairwise.linear_kernel sklearn.metrics.pairwise.linear_kernel(X, Y=None, dense_output=True) [source] Compute the linear kernel between X and Y. Read more in the User Guide. Parameters Xndarray of shape (n_samples_X, n_features) Yndarray of shape (n_samples_Y, n_features), default=None dense_outputbool, default=True Whether to return dense output even when the input is sparse. If False, the output is sparse if both input arrays are sparse. New in version 0.20. Returns Gram matrixndarray of shape (n_samples_X, n_samples_Y)
sklearn.modules.generated.sklearn.metrics.pairwise.linear_kernel
sklearn.metrics.pairwise.manhattan_distances sklearn.metrics.pairwise.manhattan_distances(X, Y=None, *, sum_over_features=True) [source] Compute the L1 distances between the vectors in X and Y. With sum_over_features equal to False it returns the componentwise distances. Read more in the User Guide. Parameters Xarray-like of shape (n_samples_X, n_features) Yarray-like of shape (n_samples_Y, n_features), default=None sum_over_featuresbool, default=True If True the function returns the pairwise distance matrix else it returns the componentwise L1 pairwise-distances. Not supported for sparse matrix inputs. Returns Dndarray of shape (n_samples_X * n_samples_Y, n_features) or (n_samples_X, n_samples_Y) If sum_over_features is False shape is (n_samples_X * n_samples_Y, n_features) and D contains the componentwise L1 pairwise-distances (ie. absolute difference), else shape is (n_samples_X, n_samples_Y) and D contains the pairwise L1 distances. Notes When X and/or Y are CSR sparse matrices and they are not already in canonical format, this function modifies them in-place to make them canonical. Examples >>> from sklearn.metrics.pairwise import manhattan_distances >>> manhattan_distances([[3]], [[3]]) array([[0.]]) >>> manhattan_distances([[3]], [[2]]) array([[1.]]) >>> manhattan_distances([[2]], [[3]]) array([[1.]]) >>> manhattan_distances([[1, 2], [3, 4]], [[1, 2], [0, 3]]) array([[0., 2.], [4., 4.]]) >>> import numpy as np >>> X = np.ones((1, 2)) >>> y = np.full((2, 2), 2.) >>> manhattan_distances(X, y, sum_over_features=False) array([[1., 1.], [1., 1.]])
sklearn.modules.generated.sklearn.metrics.pairwise.manhattan_distances
sklearn.metrics.pairwise.nan_euclidean_distances sklearn.metrics.pairwise.nan_euclidean_distances(X, Y=None, *, squared=False, missing_values=nan, copy=True) [source] Calculate the euclidean distances in the presence of missing values. Compute the euclidean distance between each pair of samples in X and Y, where Y=X is assumed if Y=None. When calculating the distance between a pair of samples, this formulation ignores feature coordinates with a missing value in either sample and scales up the weight of the remaining coordinates: dist(x,y) = sqrt(weight * sq. distance from present coordinates) where, weight = Total # of coordinates / # of present coordinates For example, the distance between [3, na, na, 6] and [1, na, 4, 5] is: \[\sqrt{\frac{4}{2}((3-1)^2 + (6-5)^2)}\] If all the coordinates are missing or if there are no common present coordinates then NaN is returned for that pair. Read more in the User Guide. New in version 0.22. Parameters Xarray-like of shape=(n_samples_X, n_features) Yarray-like of shape=(n_samples_Y, n_features), default=None squaredbool, default=False Return squared Euclidean distances. missing_valuesnp.nan or int, default=np.nan Representation of missing value. copybool, default=True Make and use a deep copy of X and Y (if Y exists). Returns distancesndarray of shape (n_samples_X, n_samples_Y) See also paired_distances Distances between pairs of elements of X and Y. References John K. Dixon, “Pattern Recognition with Partly Missing Data”, IEEE Transactions on Systems, Man, and Cybernetics, Volume: 9, Issue: 10, pp. 617 - 621, Oct. 1979. http://ieeexplore.ieee.org/abstract/document/4310090/ Examples >>> from sklearn.metrics.pairwise import nan_euclidean_distances >>> nan = float("NaN") >>> X = [[0, 1], [1, nan]] >>> nan_euclidean_distances(X, X) # distance between rows of X array([[0. , 1.41421356], [1.41421356, 0. ]]) >>> # get distance to origin >>> nan_euclidean_distances(X, [[0, 0]]) array([[1. ], [1.41421356]])
sklearn.modules.generated.sklearn.metrics.pairwise.nan_euclidean_distances
sklearn.metrics.pairwise.paired_cosine_distances sklearn.metrics.pairwise.paired_cosine_distances(X, Y) [source] Computes the paired cosine distances between X and Y. Read more in the User Guide. Parameters Xarray-like of shape (n_samples, n_features) Yarray-like of shape (n_samples, n_features) Returns distancesndarray of shape (n_samples,) Notes The cosine distance is equivalent to the half the squared euclidean distance if each sample is normalized to unit norm.
sklearn.modules.generated.sklearn.metrics.pairwise.paired_cosine_distances
sklearn.metrics.pairwise.paired_distances sklearn.metrics.pairwise.paired_distances(X, Y, *, metric='euclidean', **kwds) [source] Computes the paired distances between X and Y. Computes the distances between (X[0], Y[0]), (X[1], Y[1]), etc… Read more in the User Guide. Parameters Xndarray of shape (n_samples, n_features) Array 1 for distance computation. Yndarray of shape (n_samples, n_features) Array 2 for distance computation. 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 specified in PAIRED_DISTANCES, including “euclidean”, “manhattan”, or “cosine”. Alternatively, if metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays from X as input and return a value indicating the distance between them. Returns distancesndarray of shape (n_samples,) See also pairwise_distances Computes the distance between every pair of samples. Examples >>> from sklearn.metrics.pairwise import paired_distances >>> X = [[0, 1], [1, 1]] >>> Y = [[0, 1], [2, 1]] >>> paired_distances(X, Y) array([0., 1.])
sklearn.modules.generated.sklearn.metrics.pairwise.paired_distances
sklearn.metrics.pairwise.paired_euclidean_distances sklearn.metrics.pairwise.paired_euclidean_distances(X, Y) [source] Computes the paired euclidean distances between X and Y. Read more in the User Guide. Parameters Xarray-like of shape (n_samples, n_features) Yarray-like of shape (n_samples, n_features) Returns distancesndarray of shape (n_samples,)
sklearn.modules.generated.sklearn.metrics.pairwise.paired_euclidean_distances
sklearn.metrics.pairwise.paired_manhattan_distances sklearn.metrics.pairwise.paired_manhattan_distances(X, Y) [source] Compute the L1 distances between the vectors in X and Y. Read more in the User Guide. Parameters Xarray-like of shape (n_samples, n_features) Yarray-like of shape (n_samples, n_features) Returns distancesndarray of shape (n_samples,)
sklearn.modules.generated.sklearn.metrics.pairwise.paired_manhattan_distances
sklearn.metrics.pairwise.pairwise_kernels sklearn.metrics.pairwise.pairwise_kernels(X, Y=None, metric='linear', *, filter_params=False, n_jobs=None, **kwds) [source] Compute the kernel between arrays X and optional array Y. This method takes either a vector array or a kernel matrix, and returns a kernel matrix. If the input is a vector array, the kernels are computed. If the input is a kernel matrix, it is returned instead. This method provides a safe way to take a kernel matrix as input, while preserving compatibility with many other algorithms that take a vector array. If Y is given (default is None), then the returned matrix is the pairwise kernel between the arrays from both X and Y. Valid values for metric are: [‘additive_chi2’, ‘chi2’, ‘linear’, ‘poly’, ‘polynomial’, ‘rbf’, ‘laplacian’, ‘sigmoid’, ‘cosine’] Read more in the User Guide. Parameters Xndarray of shape (n_samples_X, n_samples_X) or (n_samples_X, n_features) Array of pairwise kernels between samples, or a feature array. The shape of the array should be (n_samples_X, n_samples_X) if metric == “precomputed” and (n_samples_X, n_features) otherwise. Yndarray of shape (n_samples_Y, n_features), default=None A second feature array only if X has shape (n_samples_X, n_features). metricstr or callable, default=”linear” The metric to use when calculating kernel between instances in a feature array. If metric is a string, it must be one of the metrics in pairwise.PAIRWISE_KERNEL_FUNCTIONS. If metric is “precomputed”, X is assumed to be a kernel matrix. Alternatively, if metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two rows from X as input and return the corresponding kernel value as a single number. This means that callables from sklearn.metrics.pairwise are not allowed, as they operate on matrices, not single samples. Use the string identifying the kernel instead. filter_paramsbool, default=False Whether to filter invalid parameters or not. n_jobsint, default=None The number of jobs to use for the computation. This works by breaking down the pairwise matrix into n_jobs even slices and computing them in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details. **kwdsoptional keyword parameters Any further parameters are passed directly to the kernel function. Returns Kndarray of shape (n_samples_X, n_samples_X) or (n_samples_X, n_samples_Y) A kernel matrix K such that K_{i, j} is the kernel between the ith and jth vectors of the given matrix X, if Y is None. If Y is not None, then K_{i, j} is the kernel between the ith array from X and the jth array from Y. Notes If metric is ‘precomputed’, Y is ignored and X is returned.
sklearn.modules.generated.sklearn.metrics.pairwise.pairwise_kernels
sklearn.metrics.pairwise.polynomial_kernel sklearn.metrics.pairwise.polynomial_kernel(X, Y=None, degree=3, gamma=None, coef0=1) [source] Compute the polynomial kernel between X and Y: K(X, Y) = (gamma <X, Y> + coef0)^degree Read more in the User Guide. Parameters Xndarray of shape (n_samples_X, n_features) Yndarray of shape (n_samples_Y, n_features), default=None degreeint, default=3 gammafloat, default=None If None, defaults to 1.0 / n_features. coef0float, default=1 Returns Gram matrixndarray of shape (n_samples_X, n_samples_Y)
sklearn.modules.generated.sklearn.metrics.pairwise.polynomial_kernel
sklearn.metrics.pairwise.rbf_kernel sklearn.metrics.pairwise.rbf_kernel(X, Y=None, gamma=None) [source] Compute the rbf (gaussian) kernel between X and Y: K(x, y) = exp(-gamma ||x-y||^2) for each pair of rows x in X and y in Y. Read more in the User Guide. Parameters Xndarray of shape (n_samples_X, n_features) Yndarray of shape (n_samples_Y, n_features), default=None gammafloat, default=None If None, defaults to 1.0 / n_features. Returns kernel_matrixndarray of shape (n_samples_X, n_samples_Y)
sklearn.modules.generated.sklearn.metrics.pairwise.rbf_kernel
sklearn.metrics.pairwise.sigmoid_kernel sklearn.metrics.pairwise.sigmoid_kernel(X, Y=None, gamma=None, coef0=1) [source] Compute the sigmoid kernel between X and Y: K(X, Y) = tanh(gamma <X, Y> + coef0) Read more in the User Guide. Parameters Xndarray of shape (n_samples_X, n_features) Yndarray of shape (n_samples_Y, n_features), default=None gammafloat, default=None If None, defaults to 1.0 / n_features. coef0float, default=1 Returns Gram matrixndarray of shape (n_samples_X, n_samples_Y)
sklearn.modules.generated.sklearn.metrics.pairwise.sigmoid_kernel
sklearn.metrics.pairwise_distances sklearn.metrics.pairwise_distances(X, Y=None, metric='euclidean', *, n_jobs=None, force_all_finite=True, **kwds) [source] Compute the distance matrix from a vector array X and optional Y. This method takes either a vector array or a distance matrix, and returns a distance matrix. If the input is a vector array, the distances are computed. If the input is a distances matrix, it is returned instead. This method provides a safe way to take a distance matrix as input, while preserving compatibility with many other algorithms that take a vector array. If Y is given (default is None), then the returned matrix is the pairwise distance between the arrays from both X and Y. Valid values for metric are: From scikit-learn: [‘cityblock’, ‘cosine’, ‘euclidean’, ‘l1’, ‘l2’, ‘manhattan’]. These metrics support sparse matrix inputs. [‘nan_euclidean’] but it does not yet support sparse matrices. From scipy.spatial.distance: [‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘correlation’, ‘dice’, ‘hamming’, ‘jaccard’, ‘kulsinski’, ‘mahalanobis’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘yule’] See the documentation for scipy.spatial.distance for details on these metrics. These metrics do not support sparse matrix inputs. Note that in the case of ‘cityblock’, ‘cosine’ and ‘euclidean’ (which are valid scipy.spatial.distance metrics), the scikit-learn implementation will be used, which is faster and has support for sparse matrices (except for ‘cityblock’). For a verbose description of the metrics from scikit-learn, see the __doc__ of the sklearn.pairwise.distance_metrics function. Read more in the User Guide. Parameters Xndarray of shape (n_samples_X, n_samples_X) or (n_samples_X, n_features) Array of pairwise distances between samples, or a feature array. The shape of the array should be (n_samples_X, n_samples_X) if metric == “precomputed” and (n_samples_X, n_features) otherwise. Yndarray of shape (n_samples_Y, n_features), default=None An optional second feature array. Only allowed if metric != “precomputed”. 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 scipy.spatial.distance.pdist for its metric parameter, or a metric listed in pairwise.PAIRWISE_DISTANCE_FUNCTIONS. If metric is “precomputed”, X is assumed to be a distance matrix. Alternatively, if metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays from X as input and return a value indicating the distance between them. n_jobsint, default=None The number of jobs to use for the computation. This works by breaking down the pairwise matrix into n_jobs even slices and computing them in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details. force_all_finitebool or ‘allow-nan’, default=True Whether to raise an error on np.inf, np.nan, pd.NA in array. Ignored for a metric listed in pairwise.PAIRWISE_DISTANCE_FUNCTIONS. The possibilities are: True: Force all values of array to be finite. False: accepts np.inf, np.nan, pd.NA in array. ‘allow-nan’: accepts only np.nan and pd.NA values in array. Values cannot be infinite. New in version 0.22: force_all_finite accepts the string 'allow-nan'. Changed in version 0.23: Accepts pd.NA and converts it into np.nan. **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 Dndarray of shape (n_samples_X, n_samples_X) or (n_samples_X, n_samples_Y) A distance matrix D such that D_{i, j} is the distance between the ith and jth vectors of the given matrix X, if Y is None. If Y is not None, then D_{i, j} is the distance between the ith array from X and the jth array from Y. See also pairwise_distances_chunked Performs the same calculation as this function, but returns a generator of chunks of the distance matrix, in order to limit memory usage. paired_distances Computes the distances between corresponding elements of two arrays. Examples using sklearn.metrics.pairwise_distances Agglomerative clustering with different metrics
sklearn.modules.generated.sklearn.metrics.pairwise_distances
sklearn.metrics.pairwise_distances_argmin sklearn.metrics.pairwise_distances_argmin(X, Y, *, axis=1, metric='euclidean', metric_kwargs=None) [source] Compute minimum distances between one point and a set of points. This function computes for each row in X, the index of the row of Y which is closest (according to the specified distance). This is mostly equivalent to calling: pairwise_distances(X, Y=Y, metric=metric).argmin(axis=axis) but uses much less memory, and is faster for large arrays. This function works with dense 2D arrays only. Parameters Xarray-like of shape (n_samples_X, n_features) Array containing points. Yarray-like of shape (n_samples_Y, n_features) Arrays containing points. axisint, default=1 Axis along which the argmin and distances are to be computed. metricstr or callable, default=”euclidean” Metric to use for distance computation. Any metric from scikit-learn or scipy.spatial.distance can be used. If metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays as input and return one value indicating the distance between them. This works for Scipy’s metrics, but is less efficient than passing the metric name as a string. Distance matrices are not supported. Valid values for metric are: from scikit-learn: [‘cityblock’, ‘cosine’, ‘euclidean’, ‘l1’, ‘l2’, ‘manhattan’] from scipy.spatial.distance: [‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘correlation’, ‘dice’, ‘hamming’, ‘jaccard’, ‘kulsinski’, ‘mahalanobis’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘yule’] See the documentation for scipy.spatial.distance for details on these metrics. metric_kwargsdict, default=None Keyword arguments to pass to specified metric function. Returns argminnumpy.ndarray Y[argmin[i], :] is the row in Y that is closest to X[i, :]. See also sklearn.metrics.pairwise_distances sklearn.metrics.pairwise_distances_argmin_min Examples using sklearn.metrics.pairwise_distances_argmin Color Quantization using K-Means Comparison of the K-Means and MiniBatchKMeans clustering algorithms
sklearn.modules.generated.sklearn.metrics.pairwise_distances_argmin
sklearn.metrics.pairwise_distances_argmin_min sklearn.metrics.pairwise_distances_argmin_min(X, Y, *, axis=1, metric='euclidean', metric_kwargs=None) [source] Compute minimum distances between one point and a set of points. This function computes for each row in X, the index of the row of Y which is closest (according to the specified distance). The minimal distances are also returned. This is mostly equivalent to calling: (pairwise_distances(X, Y=Y, metric=metric).argmin(axis=axis), pairwise_distances(X, Y=Y, metric=metric).min(axis=axis)) but uses much less memory, and is faster for large arrays. Parameters X{array-like, sparse matrix} of shape (n_samples_X, n_features) Array containing points. Y{array-like, sparse matrix} of shape (n_samples_Y, n_features) Array containing points. axisint, default=1 Axis along which the argmin and distances are to be computed. metricstr or callable, default=’euclidean’ Metric to use for distance computation. Any metric from scikit-learn or scipy.spatial.distance can be used. If metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays as input and return one value indicating the distance between them. This works for Scipy’s metrics, but is less efficient than passing the metric name as a string. Distance matrices are not supported. Valid values for metric are: from scikit-learn: [‘cityblock’, ‘cosine’, ‘euclidean’, ‘l1’, ‘l2’, ‘manhattan’] from scipy.spatial.distance: [‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘correlation’, ‘dice’, ‘hamming’, ‘jaccard’, ‘kulsinski’, ‘mahalanobis’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘yule’] See the documentation for scipy.spatial.distance for details on these metrics. metric_kwargsdict, default=None Keyword arguments to pass to specified metric function. Returns argminndarray Y[argmin[i], :] is the row in Y that is closest to X[i, :]. distancesndarray distances[i] is the distance between the i-th row in X and the argmin[i]-th row in Y. See also sklearn.metrics.pairwise_distances sklearn.metrics.pairwise_distances_argmin
sklearn.modules.generated.sklearn.metrics.pairwise_distances_argmin_min
sklearn.metrics.pairwise_distances_chunked sklearn.metrics.pairwise_distances_chunked(X, Y=None, *, reduce_func=None, metric='euclidean', n_jobs=None, working_memory=None, **kwds) [source] Generate a distance matrix chunk by chunk with optional reduction. In cases where not all of a pairwise distance matrix needs to be stored at once, this is used to calculate pairwise distances in working_memory-sized chunks. If reduce_func is given, it is run on each chunk and its return values are concatenated into lists, arrays or sparse matrices. Parameters Xndarray of shape (n_samples_X, n_samples_X) or (n_samples_X, n_features) Array of pairwise distances between samples, or a feature array. The shape the array should be (n_samples_X, n_samples_X) if metric=’precomputed’ and (n_samples_X, n_features) otherwise. Yndarray of shape (n_samples_Y, n_features), default=None An optional second feature array. Only allowed if metric != “precomputed”. reduce_funccallable, default=None The function which is applied on each chunk of the distance matrix, reducing it to needed values. reduce_func(D_chunk, start) is called repeatedly, where D_chunk is a contiguous vertical slice of the pairwise distance matrix, starting at row start. It should return one of: None; an array, a list, or a sparse matrix of length D_chunk.shape[0]; or a tuple of such objects. Returning None is useful for in-place operations, rather than reductions. If None, pairwise_distances_chunked returns a generator of vertical chunks of the distance matrix. 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 scipy.spatial.distance.pdist for its metric parameter, or a metric listed in pairwise.PAIRWISE_DISTANCE_FUNCTIONS. If metric is “precomputed”, X is assumed to be a distance matrix. Alternatively, if metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays from X as input and return a value indicating the distance between them. n_jobsint, default=None The number of jobs to use for the computation. This works by breaking down the pairwise matrix into n_jobs even slices and computing them in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details. working_memoryint, default=None The sought maximum memory for temporary distance matrix chunks. When None (default), the value of sklearn.get_config()['working_memory'] is used. `**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. Yields D_chunk{ndarray, sparse matrix} A contiguous slice of distance matrix, optionally processed by reduce_func. Examples Without reduce_func: >>> import numpy as np >>> from sklearn.metrics import pairwise_distances_chunked >>> X = np.random.RandomState(0).rand(5, 3) >>> D_chunk = next(pairwise_distances_chunked(X)) >>> D_chunk array([[0. ..., 0.29..., 0.41..., 0.19..., 0.57...], [0.29..., 0. ..., 0.57..., 0.41..., 0.76...], [0.41..., 0.57..., 0. ..., 0.44..., 0.90...], [0.19..., 0.41..., 0.44..., 0. ..., 0.51...], [0.57..., 0.76..., 0.90..., 0.51..., 0. ...]]) Retrieve all neighbors and average distance within radius r: >>> r = .2 >>> def reduce_func(D_chunk, start): ... neigh = [np.flatnonzero(d < r) for d in D_chunk] ... avg_dist = (D_chunk * (D_chunk < r)).mean(axis=1) ... return neigh, avg_dist >>> gen = pairwise_distances_chunked(X, reduce_func=reduce_func) >>> neigh, avg_dist = next(gen) >>> neigh [array([0, 3]), array([1]), array([2]), array([0, 3]), array([4])] >>> avg_dist array([0.039..., 0. , 0. , 0.039..., 0. ]) Where r is defined per sample, we need to make use of start: >>> r = [.2, .4, .4, .3, .1] >>> def reduce_func(D_chunk, start): ... neigh = [np.flatnonzero(d < r[i]) ... for i, d in enumerate(D_chunk, start)] ... return neigh >>> neigh = next(pairwise_distances_chunked(X, reduce_func=reduce_func)) >>> neigh [array([0, 3]), array([0, 1]), array([2]), array([0, 3]), array([4])] Force row-by-row generation by reducing working_memory: >>> gen = pairwise_distances_chunked(X, reduce_func=reduce_func, ... working_memory=0) >>> next(gen) [array([0, 3])] >>> next(gen) [array([0, 1])]
sklearn.modules.generated.sklearn.metrics.pairwise_distances_chunked
sklearn.metrics.plot_confusion_matrix sklearn.metrics.plot_confusion_matrix(estimator, X, y_true, *, labels=None, sample_weight=None, normalize=None, display_labels=None, include_values=True, xticks_rotation='horizontal', values_format=None, cmap='viridis', ax=None, colorbar=True) [source] Plot Confusion Matrix. Read more in the User Guide. Parameters estimatorestimator instance Fitted classifier or a fitted Pipeline in which the last estimator is a classifier. X{array-like, sparse matrix} of shape (n_samples, n_features) Input values. y_truearray-like of shape (n_samples,) Target values. labelsarray-like of shape (n_classes,), default=None List of labels to index the matrix. This may be used to reorder or select a subset of labels. If None is given, those that appear at least once in y_true or y_pred are used in sorted order. sample_weightarray-like of shape (n_samples,), default=None Sample weights. normalize{‘true’, ‘pred’, ‘all’}, default=None Normalizes confusion matrix over the true (rows), predicted (columns) conditions or all the population. If None, confusion matrix will not be normalized. display_labelsarray-like of shape (n_classes,), default=None Target names used for plotting. By default, labels will be used if it is defined, otherwise the unique labels of y_true and y_pred will be used. include_valuesbool, default=True Includes values in confusion matrix. xticks_rotation{‘vertical’, ‘horizontal’} or float, default=’horizontal’ Rotation of xtick labels. values_formatstr, default=None Format specification for values in confusion matrix. If None, the format specification is ‘d’ or ‘.2g’ whichever is shorter. cmapstr or matplotlib Colormap, default=’viridis’ Colormap recognized by matplotlib. axmatplotlib Axes, default=None Axes object to plot on. If None, a new figure and axes is created. colorbarbool, default=True Whether or not to add a colorbar to the plot. New in version 0.24. Returns displayConfusionMatrixDisplay See also confusion_matrix Compute Confusion Matrix to evaluate the accuracy of a classification. ConfusionMatrixDisplay Confusion Matrix visualization. Examples >>> import matplotlib.pyplot as plt >>> from sklearn.datasets import make_classification >>> from sklearn.metrics import plot_confusion_matrix >>> from sklearn.model_selection import train_test_split >>> from sklearn.svm import SVC >>> X, y = make_classification(random_state=0) >>> X_train, X_test, y_train, y_test = train_test_split( ... X, y, random_state=0) >>> clf = SVC(random_state=0) >>> clf.fit(X_train, y_train) SVC(random_state=0) >>> plot_confusion_matrix(clf, X_test, y_test) >>> plt.show() Examples using sklearn.metrics.plot_confusion_matrix Recognizing hand-written digits Confusion matrix
sklearn.modules.generated.sklearn.metrics.plot_confusion_matrix
sklearn.metrics.plot_det_curve sklearn.metrics.plot_det_curve(estimator, X, y, *, sample_weight=None, response_method='auto', name=None, ax=None, pos_label=None, **kwargs) [source] Plot detection error tradeoff (DET) curve. Extra keyword arguments will be passed to matplotlib’s plot. Read more in the User Guide. New in version 0.24. Parameters estimatorestimator instance Fitted classifier or a fitted Pipeline in which the last estimator is a classifier. X{array-like, sparse matrix} of shape (n_samples, n_features) Input values. yarray-like of shape (n_samples,) Target values. sample_weightarray-like of shape (n_samples,), default=None Sample weights. response_method{‘predict_proba’, ‘decision_function’, ‘auto’} default=’auto’ Specifies whether to use predict_proba or decision_function as the predicted target response. If set to ‘auto’, predict_proba is tried first and if it does not exist decision_function is tried next. namestr, default=None Name of DET curve for labeling. If None, use the name of the estimator. axmatplotlib axes, default=None Axes object to plot on. If None, a new figure and axes is created. pos_labelstr or int, 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. Returns displayDetCurveDisplay Object that stores computed values. See also det_curve Compute error rates for different probability thresholds. DetCurveDisplay DET curve visualization. plot_roc_curve Plot Receiver operating characteristic (ROC) curve. Examples >>> import matplotlib.pyplot as plt >>> from sklearn import datasets, metrics, model_selection, svm >>> X, y = datasets.make_classification(random_state=0) >>> X_train, X_test, y_train, y_test = model_selection.train_test_split( ... X, y, random_state=0) >>> clf = svm.SVC(random_state=0) >>> clf.fit(X_train, y_train) SVC(random_state=0) >>> metrics.plot_det_curve(clf, X_test, y_test) >>> plt.show() Examples using sklearn.metrics.plot_det_curve Detection error tradeoff (DET) curve
sklearn.modules.generated.sklearn.metrics.plot_det_curve
sklearn.metrics.plot_precision_recall_curve sklearn.metrics.plot_precision_recall_curve(estimator, X, y, *, sample_weight=None, response_method='auto', name=None, ax=None, pos_label=None, **kwargs) [source] Plot Precision Recall Curve for binary classifiers. Extra keyword arguments will be passed to matplotlib’s plot. Read more in the User Guide. Parameters estimatorestimator instance Fitted classifier or a fitted Pipeline in which the last estimator is a classifier. X{array-like, sparse matrix} of shape (n_samples, n_features) Input values. yarray-like of shape (n_samples,) Binary target values. sample_weightarray-like of shape (n_samples,), default=None Sample weights. response_method{‘predict_proba’, ‘decision_function’, ‘auto’}, default=’auto’ Specifies whether to use predict_proba or decision_function as the target response. If set to ‘auto’, predict_proba is tried first and if it does not exist decision_function is tried next. namestr, default=None Name for labeling curve. If None, the name of the estimator is used. axmatplotlib axes, default=None Axes object to plot on. If None, a new figure and axes is created. pos_labelstr or int, default=None The class considered as the positive class when computing the precision and recall metrics. By default, estimators.classes_[1] is considered as the positive class. New in version 0.24. **kwargsdict Keyword arguments to be passed to matplotlib’s plot. Returns displayPrecisionRecallDisplay Object that stores computed values. See also precision_recall_curve Compute precision-recall pairs for different probability thresholds. PrecisionRecallDisplay Precision Recall visualization. Examples using sklearn.metrics.plot_precision_recall_curve Precision-Recall
sklearn.modules.generated.sklearn.metrics.plot_precision_recall_curve
sklearn.metrics.plot_roc_curve sklearn.metrics.plot_roc_curve(estimator, X, y, *, sample_weight=None, drop_intermediate=True, response_method='auto', name=None, ax=None, pos_label=None, **kwargs) [source] Plot Receiver operating characteristic (ROC) curve. Extra keyword arguments will be passed to matplotlib’s plot. Read more in the User Guide. Parameters estimatorestimator instance Fitted classifier or a fitted Pipeline in which the last estimator is a classifier. X{array-like, sparse matrix} of shape (n_samples, n_features) Input values. yarray-like of shape (n_samples,) Target values. sample_weightarray-like of shape (n_samples,), default=None Sample weights. drop_intermediateboolean, 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. response_method{‘predict_proba’, ‘decision_function’, ‘auto’} default=’auto’ Specifies whether to use predict_proba or decision_function as the target response. If set to ‘auto’, predict_proba is tried first and if it does not exist decision_function is tried next. namestr, default=None Name of ROC Curve for labeling. If None, use the name of the estimator. axmatplotlib axes, default=None Axes object to plot on. If None, a new figure and axes is created. pos_labelstr or int, default=None The class considered as the positive class when computing the roc auc metrics. By default, estimators.classes_[1] is considered as the positive class. New in version 0.24. Returns displayRocCurveDisplay Object that stores computed values. See also roc_curve Compute Receiver operating characteristic (ROC) curve. RocCurveDisplay ROC Curve visualization. roc_auc_score Compute the area under the ROC curve. Examples >>> import matplotlib.pyplot as plt >>> from sklearn import datasets, metrics, model_selection, svm >>> X, y = datasets.make_classification(random_state=0) >>> X_train, X_test, y_train, y_test = model_selection.train_test_split( ... X, y, random_state=0) >>> clf = svm.SVC(random_state=0) >>> clf.fit(X_train, y_train) SVC(random_state=0) >>> metrics.plot_roc_curve(clf, X_test, y_test) >>> plt.show() Examples using sklearn.metrics.plot_roc_curve Release Highlights for scikit-learn 0.22 ROC Curve with Visualization API Detection error tradeoff (DET) curve Receiver Operating Characteristic (ROC) with cross validation
sklearn.modules.generated.sklearn.metrics.plot_roc_curve
sklearn.metrics.precision_recall_curve sklearn.metrics.precision_recall_curve(y_true, probas_pred, *, pos_label=None, sample_weight=None) [source] Compute precision-recall pairs for different probability thresholds. Note: this implementation is restricted to the binary classification task. The precision is the ratio tp / (tp + fp) where tp is the number of true positives and fp the number of false positives. The precision is intuitively the ability of the classifier not to label as positive a sample that is negative. 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 last precision and recall values are 1. and 0. respectively and do not have a corresponding threshold. This ensures that the graph starts on the y axis. 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. probas_predndarray of shape (n_samples,) Estimated probabilities or output of a decision function. pos_labelint or str, default=None The label of the positive class. When pos_label=None, if y_true is in {-1, 1} or {0, 1}, pos_label is set to 1, otherwise an error will be raised. sample_weightarray-like of shape (n_samples,), default=None Sample weights. Returns precisionndarray of shape (n_thresholds + 1,) Precision values such that element i is the precision of predictions with score >= thresholds[i] and the last element is 1. recallndarray of shape (n_thresholds + 1,) Decreasing recall values such that element i is the recall of predictions with score >= thresholds[i] and the last element is 0. thresholdsndarray of shape (n_thresholds,) Increasing thresholds on the decision function used to compute precision and recall. n_thresholds <= len(np.unique(probas_pred)). See also plot_precision_recall_curve Plot Precision Recall Curve for binary classifiers. PrecisionRecallDisplay Precision Recall visualization. average_precision_score Compute average precision from prediction scores. det_curve Compute error rates for different probability thresholds. roc_curve Compute Receiver operating characteristic (ROC) curve. Examples >>> import numpy as np >>> from sklearn.metrics import precision_recall_curve >>> y_true = np.array([0, 0, 1, 1]) >>> y_scores = np.array([0.1, 0.4, 0.35, 0.8]) >>> precision, recall, thresholds = precision_recall_curve( ... y_true, y_scores) >>> precision array([0.66666667, 0.5 , 1. , 1. ]) >>> recall array([1. , 0.5, 0.5, 0. ]) >>> thresholds array([0.35, 0.4 , 0.8 ]) Examples using sklearn.metrics.precision_recall_curve Visualizations with Display Objects Precision-Recall
sklearn.modules.generated.sklearn.metrics.precision_recall_curve
sklearn.metrics.precision_recall_fscore_support sklearn.metrics.precision_recall_fscore_support(y_true, y_pred, *, beta=1.0, labels=None, pos_label=1, average=None, warn_for='precision', 'recall', 'f-score', sample_weight=None, zero_division='warn') [source] Compute precision, recall, F-measure and support for each class. The precision is the ratio tp / (tp + fp) where tp is the number of true positives and fp the number of false positives. The precision is intuitively the ability of the classifier not to label as positive a sample that is negative. 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 F-beta score can be interpreted as a weighted harmonic mean of the precision and recall, where an F-beta score reaches its best value at 1 and worst score at 0. The F-beta score weights recall more than precision by a factor of beta. beta == 1.0 means recall and precision are equally important. The support is the number of occurrences of each class in y_true. If pos_label is None and in binary classification, this function returns the average precision, recall and F-measure if average is one of 'micro', 'macro', 'weighted' or 'samples'. Read more in the User Guide. Parameters y_true1d array-like, or label indicator array / sparse matrix Ground truth (correct) target values. y_pred1d array-like, or label indicator array / sparse matrix Estimated targets as returned by a classifier. betafloat, default=1.0 The strength of recall versus precision in the F-score. labelsarray-like, default=None The set of labels to include when average != 'binary', and their order if average is None. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. 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{‘binary’, ‘micro’, ‘macro’, ‘samples’,’weighted’}, default=None 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). warn_fortuple or set, for internal use This determines which warnings will be made in the case that this function is being used to return only one of its metrics. 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: recall: when there are no positive labels precision: when there are no positive predictions f-score: both If set to “warn”, this acts as 0, but warnings are also raised. Returns precisionfloat (if average is not None) or array of float, shape = [n_unique_labels] recallfloat (if average is not None) or array of float, , shape = [n_unique_labels] fbeta_scorefloat (if average is not None) or array of float, shape = [n_unique_labels] supportNone (if average is not None) or array of int, shape = [n_unique_labels] The number of occurrences of each label in y_true. Notes When true positive + false positive == 0, precision is undefined. When true positive + false negative == 0, recall is undefined. In such cases, by default the metric will be set to 0, as will f-score, and UndefinedMetricWarning will be raised. This behavior can be modified with zero_division. References 1 Wikipedia entry for the Precision and recall. 2 Wikipedia entry for the F1-score. 3 Discriminative Methods for Multi-labeled Classification Advances in Knowledge Discovery and Data Mining (2004), pp. 22-30 by Shantanu Godbole, Sunita Sarawagi. Examples >>> import numpy as np >>> from sklearn.metrics import precision_recall_fscore_support >>> y_true = np.array(['cat', 'dog', 'pig', 'cat', 'dog', 'pig']) >>> y_pred = np.array(['cat', 'pig', 'dog', 'cat', 'cat', 'dog']) >>> precision_recall_fscore_support(y_true, y_pred, average='macro') (0.22..., 0.33..., 0.26..., None) >>> precision_recall_fscore_support(y_true, y_pred, average='micro') (0.33..., 0.33..., 0.33..., None) >>> precision_recall_fscore_support(y_true, y_pred, average='weighted') (0.22..., 0.33..., 0.26..., None) It is possible to compute per-label precisions, recalls, F1-scores and supports instead of averaging: >>> precision_recall_fscore_support(y_true, y_pred, average=None, ... labels=['pig', 'dog', 'cat']) (array([0. , 0. , 0.66...]), array([0., 0., 1.]), array([0. , 0. , 0.8]), array([2, 2, 2]))
sklearn.modules.generated.sklearn.metrics.precision_recall_fscore_support
sklearn.metrics.precision_score sklearn.metrics.precision_score(y_true, y_pred, *, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn') [source] Compute the precision. The precision is the ratio tp / (tp + fp) where tp is the number of true positives and fp the number of false positives. The precision is intuitively the ability of the classifier not to label as positive a sample that is negative. 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 precisionfloat (if average is not None) or array of float of shape (n_unique_labels,) Precision of the positive class in binary classification or weighted average of the precision of each class for the multiclass task. See also precision_recall_fscore_support, multilabel_confusion_matrix Notes When true positive + false positive == 0, precision returns 0 and raises UndefinedMetricWarning. This behavior can be modified with zero_division. Examples >>> from sklearn.metrics import precision_score >>> y_true = [0, 1, 2, 0, 1, 2] >>> y_pred = [0, 2, 1, 0, 0, 1] >>> precision_score(y_true, y_pred, average='macro') 0.22... >>> precision_score(y_true, y_pred, average='micro') 0.33... >>> precision_score(y_true, y_pred, average='weighted') 0.22... >>> precision_score(y_true, y_pred, average=None) array([0.66..., 0. , 0. ]) >>> y_pred = [0, 0, 0, 0, 0, 0] >>> precision_score(y_true, y_pred, average=None) array([0.33..., 0. , 0. ]) >>> precision_score(y_true, y_pred, average=None, zero_division=1) array([0.33..., 1. , 1. ]) Examples using sklearn.metrics.precision_score Probability Calibration curves Precision-Recall
sklearn.modules.generated.sklearn.metrics.precision_score
sklearn.metrics.r2_score sklearn.metrics.r2_score(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average') [source] R^2 (coefficient of determination) regression score function. Best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a R^2 score of 0.0. Read more in the User Guide. Parameters y_truearray-like of shape (n_samples,) or (n_samples, n_outputs) Ground truth (correct) target values. y_predarray-like of shape (n_samples,) or (n_samples, n_outputs) Estimated target values. sample_weightarray-like of shape (n_samples,), default=None Sample weights. multioutput{‘raw_values’, ‘uniform_average’, ‘variance_weighted’}, array-like of shape (n_outputs,) or None, default=’uniform_average’ Defines aggregating of multiple output scores. Array-like value defines weights used to average scores. Default is “uniform_average”. ‘raw_values’ : Returns a full set of scores in case of multioutput input. ‘uniform_average’ : Scores of all outputs are averaged with uniform weight. ‘variance_weighted’ : Scores of all outputs are averaged, weighted by the variances of each individual output. Changed in version 0.19: Default value of multioutput is ‘uniform_average’. Returns zfloat or ndarray of floats The R^2 score or ndarray of scores if ‘multioutput’ is ‘raw_values’. Notes This is not a symmetric function. Unlike most other scores, R^2 score may be negative (it need not actually be the square of a quantity R). This metric is not well-defined for single samples and will return a NaN value if n_samples is less than two. References 1 Wikipedia entry on the Coefficient of determination Examples >>> from sklearn.metrics import r2_score >>> y_true = [3, -0.5, 2, 7] >>> y_pred = [2.5, 0.0, 2, 8] >>> r2_score(y_true, y_pred) 0.948... >>> y_true = [[0.5, 1], [-1, 1], [7, -6]] >>> y_pred = [[0, 2], [-1, 2], [8, -5]] >>> r2_score(y_true, y_pred, ... multioutput='variance_weighted') 0.938... >>> y_true = [1, 2, 3] >>> y_pred = [1, 2, 3] >>> r2_score(y_true, y_pred) 1.0 >>> y_true = [1, 2, 3] >>> y_pred = [2, 2, 2] >>> r2_score(y_true, y_pred) 0.0 >>> y_true = [1, 2, 3] >>> y_pred = [3, 2, 1] >>> r2_score(y_true, y_pred) -3.0 Examples using sklearn.metrics.r2_score Non-negative least squares Linear Regression Example Lasso and Elastic Net for Sparse Signals Effect of transforming the targets in regression model
sklearn.modules.generated.sklearn.metrics.r2_score
sklearn.metrics.rand_score sklearn.metrics.rand_score(labels_true, labels_pred) [source] Rand index. The Rand Index computes a similarity measure between two clusterings by considering all pairs of samples and counting pairs that are assigned in the same or different clusters in the predicted and true clusterings. The raw RI score is: RI = (number of agreeing pairs) / (number of pairs) Read more in the User Guide. Parameters labels_truearray-like of shape (n_samples,), dtype=integral Ground truth class labels to be used as a reference. labels_predarray-like of shape (n_samples,), dtype=integral Cluster labels to evaluate. Returns RIfloat Similarity score between 0.0 and 1.0, inclusive, 1.0 stands for perfect match. See also adjusted_rand_score Adjusted Rand Score adjusted_mutual_info_score Adjusted Mutual Information References Examples Perfectly matching labelings have a score of 1 even >>> from sklearn.metrics.cluster import rand_score >>> rand_score([0, 0, 1, 1], [1, 1, 0, 0]) 1.0 Labelings that assign all classes members to the same clusters are complete but may not always be pure, hence penalized: >>> rand_score([0, 0, 1, 2], [0, 0, 1, 1]) 0.83...
sklearn.modules.generated.sklearn.metrics.rand_score
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.modules.generated.sklearn.metrics.recall_score
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.modules.generated.sklearn.metrics.roc_auc_score
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.modules.generated.sklearn.metrics.roc_curve
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.modules.generated.sklearn.metrics.silhouette_samples
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.modules.generated.sklearn.metrics.silhouette_score
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.modules.generated.sklearn.metrics.top_k_accuracy_score
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.modules.generated.sklearn.metrics.v_measure_score
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.modules.generated.sklearn.metrics.zero_one_loss
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.modules.generated.sklearn.model_selection.check_cv
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.modules.generated.sklearn.model_selection.cross_validate
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.modules.generated.sklearn.model_selection.cross_val_predict
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.modules.generated.sklearn.model_selection.cross_val_score
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.modules.generated.sklearn.model_selection.learning_curve
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.modules.generated.sklearn.model_selection.permutation_test_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.modules.generated.sklearn.model_selection.train_test_split
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.modules.generated.sklearn.model_selection.validation_curve
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.modules.generated.sklearn.neighbors.kneighbors_graph
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.modules.generated.sklearn.neighbors.radius_neighbors_graph
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.modules.generated.sklearn.pipeline.make_pipeline
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.modules.generated.sklearn.pipeline.make_union
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.modules.generated.sklearn.preprocessing.add_dummy_feature
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.modules.generated.sklearn.preprocessing.binarize
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.modules.generated.sklearn.preprocessing.label_binarize