This page

Citing

Please consider citing the scikit-learn.

9. Class reference

9.1. Support Vector Machines

Support Vector Machine algorithms.

svm.SVC([C, kernel, degree, gamma, coef0, ...]) C-Support Vector Classification.
svm.LinearSVC([penalty, loss, dual, tol, C, ...]) Linear Support Vector Classification.
svm.NuSVC([nu, kernel, degree, gamma, ...]) Nu-Support Vector Classification.
svm.SVR([kernel, degree, gamma, coef0, tol, ...]) epsilon-Support Vector Regression.
svm.NuSVR([nu, C, kernel, degree, gamma, ...]) Nu Support Vector Regression.
svm.OneClassSVM([kernel, degree, gamma, ...]) Unsupervised Outliers Detection.
svm.l1_min_c(X, y[, loss, fit_intercept, ...]) Return the maximum value for C that yields a model with coefficients

9.1.8. For sparse data

Support Vector Machine algorithms for sparse matrices.

This module should have the same API as sklearn.svm, except that matrices are expected to be in some sparse format supported by scipy.sparse.

Note

Some fields, like dual_coef_ are not sparse matrices strictly speaking. However, they are converted to a sparse matrix for consistency and efficiency when multiplying to other sparse matrices.

svm.sparse.SVC([C, kernel, degree, gamma, ...]) SVC for sparse matrices (csr).
svm.sparse.NuSVC([nu, kernel, degree, ...]) NuSVC for sparse matrices (csr).
svm.sparse.SVR([kernel, degree, gamma, ...]) SVR for sparse matrices (csr)
svm.sparse.NuSVR([nu, C, kernel, degree, ...]) NuSVR for sparse matrices (csr)
svm.sparse.OneClassSVM([kernel, degree, ...]) NuSVR for sparse matrices (csr)
svm.sparse.LinearSVC([penalty, loss, dual, ...]) Linear Support Vector Classification, Sparse Version

9.1.9. Low-level methods

svm.libsvm.fit Train the model using libsvm (low-level method)
svm.libsvm.decision_function Predict margin (libsvm name for this is predict_values)
svm.libsvm.predict Predict target values of X given a model (low-level method)
svm.libsvm.predict_proba Predict probabilities svm_model stores all parameters needed to predict a given value.
svm.libsvm.cross_validation Binding of the cross-validation routine (low-level routine)

9.2. Generalized Linear Models

sklearn.linear_model is a module to fit genelarized linear models. It includes Ridge regression, Bayesian Regression, Lasso and Elastic Net estimators computed with Least Angle Regression and coordinate descent.

It also implements Stochastic Gradient Descent related algorithms.

linear_model.LinearRegression([...]) Ordinary least squares Linear Regression.
linear_model.Ridge([alpha, fit_intercept, ...]) Ridge regression.
linear_model.RidgeCV([alphas, ...]) Ridge regression with built-in cross-validation.
linear_model.Lasso([alpha, fit_intercept, ...]) Linear Model trained with L1 prior as regularizer (aka the Lasso)
linear_model.LassoCV([eps, n_alphas, ...]) Lasso linear model with iterative fitting along a regularization path
linear_model.ElasticNet([alpha, rho, ...]) Linear Model trained with L1 and L2 prior as regularizer
linear_model.ElasticNetCV([rho, eps, ...]) Elastic Net model with iterative fitting along a regularization path
linear_model.Lars([fit_intercept, verbose, ...]) Least Angle Regression model a.k.a. LAR
linear_model.LassoLars([alpha, ...]) Lasso model fit with Least Angle Regression a.k.a. Lars
linear_model.LarsCV([fit_intercept, ...]) Cross-validated Least Angle Regression model
linear_model.LassoLarsCV([fit_intercept, ...]) Cross-validated Lasso, using the LARS algorithm
linear_model.LassoLarsIC([criterion, ...]) Lasso model fit with Lars using BIC or AIC for model selection
linear_model.LogisticRegression([penalty, ...]) Logistic Regression.
linear_model.OrthogonalMatchingPursuit([...]) Orthogonal Mathching Pursuit model (OMP)
linear_model.SGDClassifier([loss, penalty, ...]) Linear model fitted by minimizing a regularized empirical loss with SGD.
linear_model.SGDRegressor([loss, penalty, ...]) Linear model fitted by minimizing a regularized empirical loss with SGD
linear_model.BayesianRidge([n_iter, tol, ...]) Bayesian ridge regression
linear_model.ARDRegression([n_iter, tol, ...]) Bayesian ARD regression.
linear_model.lasso_path(X, y[, eps, ...]) Compute Lasso path with coordinate descent
linear_model.lars_path(X, y[, Xy, Gram, ...]) Compute Least Angle Regression and LASSO path
linear_model.orthogonal_mp(X, y[, ...]) Orthogonal Matching Pursuit (OMP)
linear_model.orthogonal_mp_gram(Gram, Xy[, ...]) Gram Orthogonal Matching Pursuit (OMP)

9.2.23. For sparse data

sklearn.linear_model.sparse is the sparse counterpart of sklearn.linear_model.

linear_model.sparse.Lasso([alpha, ...]) Linear Model trained with L1 prior as regularizer
linear_model.sparse.ElasticNet([alpha, rho, ...]) Linear Model trained with L1 and L2 prior as regularizer
linear_model.sparse.SGDClassifier([loss, ...]) Linear model fitted by minimizing a regularized empirical loss with SGD
linear_model.sparse.SGDRegressor([loss, ...]) Linear model fitted by minimizing a regularized empirical loss with SGD
linear_model.sparse.LogisticRegression([...]) Logistic Regression.

9.3. Naive Bayes

9.3.1. Naive Bayes models

Naive Bayes algorithms are a set of supervised learning methods based on applying Bayes’ theorem with strong (naive) feature independence assumptions.

See http://scikit-learn.sourceforge.net/modules/naive_bayes.html for complete documentation.

naive_bayes.GaussianNB Gaussian Naive Bayes (GaussianNB)
naive_bayes.MultinomialNB([alpha, fit_prior]) Naive Bayes classifier for multinomial models
naive_bayes.BernoulliNB([alpha, binarize, ...]) Naive Bayes classifier for multivariate Bernoulli models.

9.4. Nearest Neighbors

neighbors.NearestNeighbors([n_neighbors, ...]) Unsupervised learner for implementing neighbor searches.
neighbors.KNeighborsClassifier([...]) Classifier implementing the k-nearest neighbors vote.
neighbors.RadiusNeighborsClassifier([...]) Classifier implementing a vote among neighbors within a given radius
neighbors.NeighborsClassifier(*args, **kwargs) Classifier implementing the nearest neighbors vote.
neighbors.KNeighborsRegressor([n_neighbors, ...]) Regression based on k-nearest neighbors.
neighbors.RadiusNeighborsRegressor([radius, ...]) Regression based on neighbors within a fixed radius.
neighbors.NeighborsRegressor(*args, **kwargs) Regression based on nearest neighbors.
neighbors.BallTree Ball Tree for fast nearest-neighbor searches :
neighbors.kneighbors_graph(X, n_neighbors[, ...]) Computes the (weighted) graph of k-Neighbors for points in X
neighbors.radius_neighbors_graph(X, radius) Computes the (weighted) graph of Neighbors for points in X

9.5. Gaussian Mixture Models

Mixture modeling algorithms

mixture.GMM([n_components, cvtype, ...]) Gaussian Mixture Model
mixture.DPGMM([n_components, cvtype, alpha, ...]) Variational Inference for the Infinite Gaussian Mixture Model.
mixture.VBGMM([n_components, cvtype, alpha, ...]) Variational Inference for the Gaussian Mixture Model

9.6. Hidden Markov Models

hmm.GaussianHMM([n_components, cvtype, ...]) Hidden Markov Model with Gaussian emissions
hmm.MultinomialHMM([n_components, ...]) Hidden Markov Model with multinomial (discrete) emissions
hmm.GMMHMM([n_components, n_mix, startprob, ...]) Hidden Markov Model with Gaussin mixture emissions

9.7. Clustering

Clustering algorithms

cluster.KMeans([k, init, n_init, max_iter, ...]) K-Means clustering
cluster.MiniBatchKMeans([k, init, max_iter, ...]) Mini-Batch K-Means clustering
cluster.MeanShift([bandwidth, seeds, ...]) MeanShift clustering
cluster.SpectralClustering([k, mode, ...]) Apply k-means to a projection to the normalized laplacian
cluster.AffinityPropagation([damping, ...]) Perform Affinity Propagation Clustering of data
cluster.Ward([n_clusters, memory, ...]) Ward hierarchical clustering: constructs a tree and cuts it.

9.8. Metrics

9.8.2. Classification metrics

Metrics module with score functions, performance metrics and pairwise metrics or distances computation

metrics.confusion_matrix(y_true, y_pred[, ...]) Compute confusion matrix to evaluate the accuracy of a classification
metrics.roc_curve(y_true, y_score) compute Receiver operating characteristic (ROC)
metrics.auc(x, y) Compute Area Under the Curve (AUC) using the trapezoidal rule
metrics.precision_score(y_true, y_pred[, ...]) Compute the precision
metrics.recall_score(y_true, y_pred[, pos_label]) Compute the recall
metrics.fbeta_score(y_true, y_pred, beta[, ...]) Compute fbeta score
metrics.f1_score(y_true, y_pred[, pos_label]) Compute f1 score
metrics.precision_recall_fscore_support(...) Compute precisions, recalls, f-measures and support for each class
metrics.classification_report(y_true, y_pred) Build a text report showing the main classification metrics
metrics.precision_recall_curve(y_true, ...) Compute precision-recall pairs for different probability thresholds
metrics.zero_one_score(y_true, y_pred) Zero-One classification score
metrics.zero_one(y_true, y_pred) Zero-One classification loss
metrics.hinge_loss(y_true, pred_decision[, ...]) Cumulated hinge loss (non-regularized).

9.8.2. Regression metrics

Metrics module with score functions, performance metrics and pairwise metrics or distances computation

metrics.r2_score(y_true, y_pred) R^2 (coefficient of determination) regression score function
metrics.mean_square_error(y_true, y_pred) Mean square error regression loss

9.8.3. Clustering metrics

Utilities to evaluate the clustering performance of models

Functions named as *_score return a scalar value to maximize: the higher the better.

metrics.adjusted_rand_score(labels_true, ...) Rand index adjusted for chance
metrics.homogeneity_completeness_v_measure(...) Compute the homogeneity and completeness and V-measure scores at once
metrics.homogeneity_score(labels_true, ...) Homogeneity metric of a cluster labeling given a ground truth
metrics.completeness_score(labels_true, ...) Completeness metric of a cluster labeling given a ground truth
metrics.v_measure_score(labels_true, labels_pred) V-Measure cluster labeling given a ground truth

9.8.4. Pairwise metrics

Utilities to evaluate pairwise distances or affinity of sets of samples.

This module contains both distance metrics and kernels. A brief summary is given on the two here.

Distance metrics are a function d(a, b) such that d(a, b) < d(a, c) if objects a and b are considered “more similar” to objects a and c. Two objects exactly alike would have a distance of zero. One of the most popular examples is Euclidean distance. To be a ‘true’ metric, it must obey the following four conditions:

  1. d(a, b) >= 0, for all a and b
  2. d(a, b) == 0, if and only if a = b, positive definiteness
  3. d(a, b) == d(b, a), symmetry
  4. d(a, c) <= d(a, b) + d(b, c), the triangle inequality

Kernels are measures of similarity, i.e. s(a, b) > s(a, c) if objects a and b are considered “more similar” to objects a and c. A kernel must also be positive semi-definite.

There are a number of ways to convert between a distance metric and a similarity measure, such as a kernel. Let D be the distance, and S be the kernel:

  1. S = np.exp(-D * gamma), where one heuristic for choosing gamma is 1 / num_features
  2. S = 1. / (D / np.max(D))
metrics.pairwise.euclidean_distances(X[, Y, ...]) Considering the rows of X (and Y=X) as vectors, compute the
metrics.pairwise.l1_distances
metrics.pairwise.linear_kernel(X[, Y]) Compute the linear kernel between X and Y.
metrics.pairwise.polynomial_kernel(X[, Y, ...]) Compute the polynomial kernel between X and Y.
metrics.pairwise.rbf_kernel(X[, Y, gamma]) Compute the rbf (gaussian) kernel between X and Y.
metrics.pairwise.distance_metrics() Valid metrics for pairwise_distances
metrics.pairwise.pairwise_distances(X[, Y, ...]) Compute the distance matrix from a vector array X and optional Y.
metrics.pairwise.kernel_metrics() Valid metrics for pairwise_kernels
metrics.pairwise.pairwise_kernels(X[, Y, metric]) Compute the kernel between arrays X and optional array Y.

9.9. Covariance Estimators

9.9.1. Covariance estimators

sklearn.covariance is a module to fit to estimate robustly the covariance of features given a set of points. The precision matrix defined as the inverse of the covariance is also estimated. Covariance estimation is closely related to the theory of Gaussian Graphical Models.

covariance.EmpiricalCovariance([store_precision]) Maximum likelihood covariance estimator
covariance.ShrunkCovariance([...]) Covariance estimator with shrinkage
covariance.LedoitWolf([store_precision]) Ledoit-Wolf shrinkage estimator
covariance.OAS([store_precision]) Oracle Approximating Shrinkage Estimator
covariance.empirical_covariance(X[, ...]) Computes the Maximum likelihood covariance estimator
covariance.ledoit_wolf(X[, assume_centered]) Estimates the shrunk Ledoit-Wolf covariance matrix.
covariance.shrunk_covariance(emp_cov[, ...]) Calculates a covariance matrix shrunk on the diagonal
covariance.oas(X[, assume_centered]) Estimate covariance with the Oracle Approximating Shrinkage algorithm.

9.10. Signal Decomposition

Matrix decomposition algorithms

decomposition.PCA([n_components, copy, whiten]) Principal component analysis (PCA)
decomposition.ProbabilisticPCA([...]) Additional layer on top of PCA that adds a probabilistic evaluation
decomposition.ProjectedGradientNMF([...]) Non-Negative matrix factorization by Projected Gradient (NMF)
decomposition.RandomizedPCA(n_components[, ...]) Principal component analysis (PCA) using randomized SVD
decomposition.KernelPCA([n_components, ...]) Kernel Principal component analysis (KPCA)
decomposition.FastICA([n_components, ...]) FastICA; a fast algorithm for Independent Component Analysis
decomposition.NMF([n_components, init, ...]) Non-Negative matrix factorization by Projected Gradient (NMF)
decomposition.SparsePCA(n_components[, ...]) Sparse Principal Components Analysis (SparsePCA)
decomposition.MiniBatchSparsePCA(n_components) Mini-batch Sparse Principal Components Analysis
decomposition.DictionaryLearning(n_atoms[, ...]) Dictionary learning
decomposition.MiniBatchDictionaryLearning(n_atoms) Mini-batch dictionary learning
decomposition.fastica(X[, n_components, ...]) Perform Fast Independent Component Analysis.
decomposition.dict_learning(X, n_atoms, alpha) Solves a dictionary learning matrix factorization problem.
decomposition.dict_learning_online(X, ...[, ...]) Solves a dictionary learning matrix factorization problem online.
decomposition.sparse_encode(X, Y[, gram, ...]) Generic sparse coding
decomposition.sparse_encode_parallel(X, Y[, ...]) Parallel sparse coding using joblib

9.11. Linear Discriminant Analysis

lda.LDA([n_components, priors]) Linear Discriminant Analysis (LDA)

9.12. Partial Least Squares

Partial Least Squares regression

pls.PLSRegression([n_components, scale, ...]) PLS regression
pls.PLSCanonical([n_components, scale, ...]) PLS canonical. PLSCanonical inherits from PLS with mode=”A” and
pls.CCA([n_components, scale, algorithm, ...]) CCA Canonical Correlation Analysis. CCA inherits from PLS with
pls.PLSSVD([n_components, scale, copy]) Partial Least Square SVD

9.13. Cross Validation

Utilities for cross validation and performance evaluation

cross_validation.LeaveOneOut(n[, indices]) Leave-One-Out cross validation iterator.
cross_validation.LeavePOut(n, p[, indices]) Leave-P-Out cross validation iterator
cross_validation.KFold(n, k[, indices]) K-Folds cross validation iterator
cross_validation.StratifiedKFold(y, k[, indices]) Stratified K-Folds cross validation iterator
cross_validation.LeaveOneLabelOut(labels[, ...]) Leave-One-Label_Out cross-validation iterator
cross_validation.LeavePLabelOut(labels, p[, ...]) Leave-P-Label_Out cross-validation iterator
cross_validation.Bootstrap(n[, ...]) Random sampling with replacement cross-validation iterator
cross_validation.ShuffleSplit(n[, ...]) Random permutation cross-validation iterator.

9.15. Feature Selection

Feature slection module for python

feature_selection.rfe.RFE(estimator, ...[, step]) Feature ranking with recursive feature elimination.
feature_selection.rfe.RFECV(estimator[, ...]) Feature ranking with recursive feature elimination and cross-validated selection of the best number of features.
feature_selection.univariate_selection.chi2(X, y) Compute χ² (chi-squared) statistic for each class/feature combination.

9.16. Feature Extraction

Package for modules that deal with feature extraction from raw data

9.16.1. From images

Utilities to extract features from images.

feature_extraction.image.img_to_graph(img[, ...]) Graph of the pixel-to-pixel gradient connections
feature_extraction.image.grid_to_graph(n_x, n_y) Graph of the pixel-to-pixel connections
feature_extraction.image.extract_patches_2d(...) Reshape a 2D image into a collection of patches
feature_extraction.image.reconstruct_from_patches_2d(...) Reconstruct the image from all of its patches.
feature_extraction.image.PatchExtractor(...) Extracts patches from a collection of images

9.16.2. From text

Utilities to build feature vectors from text documents

feature_extraction.text.RomanPreprocessor Fast preprocessor suitable for Latin alphabet text
feature_extraction.text.WordNGramAnalyzer([...]) Simple analyzer: transform text document into a sequence of word tokens
feature_extraction.text.CharNGramAnalyzer([...]) Compute character n-grams features of a text document
feature_extraction.text.CountVectorizer(...) Convert a collection of raw documents to a matrix of token counts
feature_extraction.text.TfidfTransformer([...]) Transform a count matrix to a normalized tf or tf–idf representation
feature_extraction.text.Vectorizer(...[, ...]) Convert a collection of raw documents to a matrix

9.17. Preprocessing and normalization

Transformers to perform common preprocessing steps.

preprocessing.Scaler([copy, with_mean, with_std]) Standardize features by removing the mean and scaling to unit variance
preprocessing.Normalizer([norm, copy]) Normalize samples individually to unit norm
preprocessing.Binarizer([threshold, copy]) Binarize data (set feature values to 0 or 1) according to a threshold
preprocessing.LabelBinarizer Binarize labels in a one-vs-all fashion
preprocessing.KernelCenterer Center a kernel matrix
preprocessing.scale(X[, axis, with_mean, ...]) Standardize a dataset along any axis
preprocessing.normalize(X[, norm, axis, copy]) Normalize a dataset along any axis
preprocessing.binarize(X[, threshold, copy]) Boolean thresholding of array-like or scipy.sparse matrix

9.18. Manifold learning

manifold.LocallyLinearEmbedding([...]) Locally Linear Embedding
manifold.Isomap([n_neighbors, out_dim, ...]) Isomap Embedding
manifold.locally_linear_embedding(X, ...[, ...]) Perform a Locally Linear Embedding analysis on the data.

9.19. Datasets

9.19.2. Loaders

datasets.load_files(container_path[, ...]) Load text files with categories as subfolder names.
datasets.load_diabetes() Load and return the diabetes dataset (regression).
datasets.load_digits([n_class]) Load and return the digits dataset (classification).
datasets.load_iris() Load and return the iris dataset (classification).
datasets.load_linnerud() Load and return the linnerud dataset (multivariate regression).
datasets.load_lfw_pairs([download_if_missing]) Alias for fetch_lfw_pairs(download_if_missing=False)
datasets.fetch_lfw_pairs([subset, ...]) Loader for the Labeled Faces in the Wild (LFW) pairs dataset
datasets.load_lfw_people([download_if_missing]) Alias for fetch_lfw_people(download_if_missing=False)
datasets.fetch_lfw_people([data_home, ...]) Loader for the Labeled Faces in the Wild (LFW) people dataset
datasets.load_20newsgroups(*args, **kwargs) DEPRECATED: Use fetch_20newsgroups instead with download_if_missing=False
datasets.fetch_20newsgroups([data_home, ...]) Load the filenames of the 20 newsgroups dataset

9.19.2. Samples generator

datasets.make_classification([n_samples, ...]) Generate a random n-class classification problem.
datasets.make_regression([n_samples, ...]) Generate a random regression problem.
datasets.make_blobs([n_samples, n_features, ...]) Generate isotropic Gaussian blobs for clustering.
datasets.make_friedman1([n_samples, ...]) Generate the “Friedman #1” regression problem as described in Friedman [1]
datasets.make_friedman2([n_samples, noise, ...]) Generate the “Friedman #2” regression problem as described in Friedman [1]
datasets.make_friedman3([n_samples, noise, ...]) Generate the “Friedman #3” regression problem as described in Friedman [1]
datasets.make_low_rank_matrix([n_samples, ...]) Generate a mostly low rank random matrix with bell-shaped singular values profile.
datasets.make_sparse_coded_signal(n_samples, ...) Generate a signal as a sparse combination of dictionary elements.
datasets.make_sparse_uncorrelated([...]) Generate a random regression problem with sparse uncorrelated design as
datasets.make_spd_matrix(n_dim[, random_state]) Generate a random symmetric, positive-definite matrix.
datasets.make_swiss_roll([n_samples, noise, ...]) Generate a swiss roll dataset.
datasets.make_s_curve([n_samples, noise, ...]) Generate an S curve dataset.

9.20. Pipeline

Pipeline: chain transforms and estimators to build a composite estimator.

pipeline.Pipeline(steps) Pipeline of transforms with a final estimator

9.21. Utilities

utils.check_random_state(seed) Turn seed into a np.random.RandomState instance
utils.resample(*arrays, **options) Resample arrays or sparse matrices in a consistent way
utils.shuffle(*arrays, **options) Shuffle arrays or sparse matrices in a consistent way