Perform DBSCAN clustering from vector array or distance matrix.
Read more in the User Guide.
A feature array, or array of distances between samples if metric='precomputed'.
The maximum distance between two samples for one to be considered as in the neighborhood of the other. This is not a maximum bound on the distances of points within a cluster. This is the most important DBSCAN parameter to choose appropriately for your data set and distance function.
The number of samples (or total weight) in a neighborhood for a point to be considered as a core point. This includes the point itself.
The metric to use when calculating distance between instances in a feature array. If metric is a string or callable, it must be one of the options allowed by sklearn.metrics.pairwise_distances for its metric parameter. If metric is “precomputed”, X is assumed to be a distance matrix and must be square during fit. X may be a sparse graph, in which case only “nonzero” elements may be considered neighbors.
Additional keyword arguments for the metric function.
New in version 0.19.
The algorithm to be used by the NearestNeighbors module to compute pointwise distances and find nearest neighbors. See NearestNeighbors module documentation for details.
Leaf size passed to BallTree or cKDTree. This can affect the speed of the construction and query, as well as the memory required to store the tree. The optimal value depends on the nature of the problem.
The power of the Minkowski metric to be used to calculate distance between points.
Weight of each sample, such that a sample with a weight of at least min_samples is by itself a core sample; a sample with negative weight may inhibit its eps-neighbor from being core. Note that weights are absolute, and default to 1.
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. If precomputed distance are used, parallel execution is not available and thus n_jobs will have no effect.
Indices of core samples.
Cluster labels for each point. Noisy samples are given the label -1.
See also
An estimator interface for this clustering algorithm.
A similar estimator interface clustering at multiple values of eps. Our implementation is optimized for memory usage.
For an example, see examples/cluster/plot_dbscan.py.
This implementation bulk-computes all neighborhood queries, which increases the memory complexity to O(n.d) where d is the average number of neighbors, while original DBSCAN had memory complexity O(n). It may attract a higher memory complexity when querying these nearest neighborhoods, depending on the algorithm.
One way to avoid the query complexity is to pre-compute sparse neighborhoods in chunks using NearestNeighbors.radius_neighbors_graph with mode='distance', then using metric='precomputed' here.
Another way to reduce memory and computation time is to remove (near-)duplicate points and use sample_weight instead.
cluster.optics provides a similar clustering with lower memory usage.
Ester, M., H. P. Kriegel, J. Sander, and X. Xu, “A Density-Based Algorithm for Discovering Clusters in Large Spatial Databases with Noise”. In: Proceedings of the 2nd International Conference on Knowledge Discovery and Data Mining, Portland, OR, AAAI Press, pp. 226-231. 1996
Schubert, E., Sander, J., Ester, M., Kriegel, H. P., & Xu, X. (2017). DBSCAN revisited, revisited: why and how you should (still) use DBSCAN. ACM Transactions on Database Systems (TODS), 42(3), 19.


Estimate the bandwidth to use with the mean-shift algorithm.
That this function takes time at least quadratic in n_samples. For large datasets, it’s wise to set that parameter to a small value.
Input points.
should be between [0, 1] 0.5 means that the median of all pairwise distances is used.
The number of samples to use. If not given, all samples are used.
The generator used to randomly select the samples from input points for bandwidth estimation. Use an int to make the randomness deterministic. See Glossary.
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.
The bandwidth parameter.
A demo of the mean-shift clustering algorithm
Comparing different clustering algorithms on toy datasets


Init n_clusters seeds according to k-means++
New in version 0.24.
The data to pick seeds from.
The number of centroids to initialize
Squared Euclidean norm of each data point.
Determines random number generation for centroid initialization. Pass an int for reproducible output across multiple function calls. See Glossary.
The number of seeding trials for each center (except the first), of which the one reducing inertia the most is greedily chosen. Set to None to make the number of trials depend logarithmically on the number of seeds (2+log(k)).
The inital centers for k-means.
The index location of the chosen centers in the data array X. For a given index and center, X[index] = center.
Selects initial cluster centers for k-mean clustering in a smart way to speed up convergence. see: Arthur, D. and Vassilvitskii, S. “k-means++: the advantages of careful seeding”. ACM-SIAM symposium on Discrete algorithms. 2007
An example of K-Means++ initialization


K-means clustering algorithm.
Read more in the User Guide.
The observations to cluster. It must be noted that the data will be converted to C ordering, which will cause a memory copy if the given data is not C-contiguous.
The number of clusters to form as well as the number of centroids to generate.
The weights for each observation in X. If None, all observations are assigned equal weight.
Method for initialization:
‘k-means++’ : selects initial cluster centers for k-mean clustering in a smart way to speed up convergence. See section Notes in k_init for more details.
‘random’: choose n_clusters observations (rows) at random from data for the initial centroids.
If an array is passed, it should be of shape (n_clusters, n_features) and gives the initial centers.
If a callable is passed, it should take arguments X, n_clusters and a random state and return an initialization.
Precompute distances (faster but takes more memory).
‘auto’ : do not precompute distances if n_samples * n_clusters > 12 million. This corresponds to about 100MB overhead per job using double precision.
True : always precompute distances
False : never precompute distances
Deprecated since version 0.23: ‘precompute_distances’ was deprecated in version 0.23 and will be removed in 1.0 (renaming of 0.25). It has no effect.
Number of time the k-means algorithm will be run with different centroid seeds. The final results will be the best output of n_init consecutive runs in terms of inertia.
Maximum number of iterations of the k-means algorithm to run.
Verbosity mode.
Relative tolerance with regards to Frobenius norm of the difference in the cluster centers of two consecutive iterations to declare convergence.
Determines random number generation for centroid initialization. Use an int to make the randomness deterministic. See Glossary.
When pre-computing distances it is more numerically accurate to center the data first. If copy_x is True (default), then the original data is not modified. If False, the original data is modified, and put back before the function returns, but small numerical differences may be introduced by subtracting and then adding the data mean. Note that if the original data is not C-contiguous, a copy will be made even if copy_x is False. If the original data is sparse, but not in CSR format, a copy will be made even if copy_x is False.
The number of OpenMP threads to use for the computation. Parallelism is sample-wise on the main cython loop which assigns each sample to its closest center.
None or -1 means using all processors.
Deprecated since version 0.23: n_jobs was deprecated in version 0.23 and will be removed in 1.0 (renaming of 0.25).
K-means algorithm to use. The classical EM-style algorithm is “full”. The “elkan” variation is more efficient on data with well-defined clusters, by using the triangle inequality. However it’s more memory intensive due to the allocation of an extra array of shape (n_samples, n_clusters).
For now “auto” (kept for backward compatibiliy) chooses “elkan” but it might change in the future for a better heuristic.
Whether or not to return the number of iterations.
Centroids found at the last iteration of k-means.
label[i] is the code or index of the centroid the i’th observation is closest to.
The final value of the inertia criterion (sum of squared distances to the closest centroid for all observations in the training set).
Number of iterations corresponding to the best results. Returned only if return_n_iter is set to True.


Perform mean shift clustering of data using a flat kernel.
Read more in the User Guide.
Input data.
Kernel bandwidth.
If bandwidth is not given, it is determined using a heuristic based on the median of all pairwise distances. This will take quadratic time in the number of samples. The sklearn.cluster.estimate_bandwidth function can be used to do this more efficiently.
Point used as initial kernel locations. If None and bin_seeding=False, each data point is used as a seed. If None and bin_seeding=True, see bin_seeding.
If true, initial kernel locations are not locations of all points, but rather the location of the discretized version of points, where points are binned onto a grid whose coarseness corresponds to the bandwidth. Setting this option to True will speed up the algorithm because fewer seeds will be initialized. Ignored if seeds argument is not None.
To speed up the algorithm, accept only those bins with at least min_bin_freq points as seeds.
If true, then all points are clustered, even those orphans that are not within any kernel. Orphans are assigned to the nearest kernel. If false, then orphans are given cluster label -1.
Maximum number of iterations, per seed point before the clustering operation terminates (for that seed point), if has not converged yet.
The number of jobs to use for the computation. This works by computing each of the n_init runs in parallel.
None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
New in version 0.17: Parallel Execution using n_jobs.
Coordinates of cluster centers.
Cluster labels for each point.
For an example, see examples/cluster/plot_mean_shift.py.


Apply clustering to a projection of the normalized Laplacian.
In practice Spectral Clustering is very useful when the structure of the individual clusters is highly non-convex or more generally when a measure of the center and spread of the cluster is not a suitable description of the complete cluster. For instance, when clusters are nested circles on the 2D plane.
If affinity is the adjacency matrix of a graph, this method can be used to find normalized graph cuts.
Read more in the User Guide.
The affinity matrix describing the relationship of the samples to embed. Must be symmetric.
Number of clusters to extract.
Number of eigen vectors to use for the spectral embedding
The eigenvalue decomposition strategy to use. AMG requires pyamg to be installed. It can be faster on very large, sparse problems, but may also lead to instabilities. If None, then 'arpack' is used.
A pseudo random number generator used for the initialization of the lobpcg eigen vectors decomposition when eigen_solver == ‘amg’ and by the K-Means initialization. Use an int to make the randomness deterministic. See Glossary.
Number of time the k-means algorithm will be run with different centroid seeds. The final results will be the best output of n_init consecutive runs in terms of inertia.
Stopping criterion for eigendecomposition of the Laplacian matrix when using arpack eigen_solver.
The strategy to use to assign labels in the embedding space. There are two ways to assign labels after the laplacian embedding. k-means can be applied and is a popular choice. But it can also be sensitive to initialization. Discretization is another approach which is less sensitive to random initialization. See the ‘Multiclass spectral clustering’ paper referenced below for more details on the discretization approach.
Verbosity mode.
New in version 0.24.
The labels of the clusters.
The graph should contain only one connect component, elsewhere the results make little sense.
This algorithm solves the normalized cut for k=2: it is a normalized spectral clustering.
Segmenting the picture of greek coins in regions
Spectral clustering for image segmentation


Ward clustering based on a Feature matrix.
Recursively merges the pair of clusters that minimally increases within-cluster variance.
The inertia matrix uses a Heapq-based representation.
This is the structured version, that takes into account some topological structure between samples.
Read more in the User Guide.
feature matrix representing n_samples samples to be clustered
connectivity matrix. Defines for each sample the neighboring samples following a given structure of the data. The matrix is assumed to be symmetric and only the upper triangular half is used. Default is None, i.e, the Ward algorithm is unstructured.
Stop early the construction of the tree at n_clusters. This is useful to decrease computation time if the number of clusters is not small compared to the number of samples. In this case, the complete tree is not computed, thus the ‘children’ output is of limited use, and the ‘parents’ output should rather be used. This option is valid only when specifying a connectivity matrix.
If True, return the distance between the clusters.
The children of each non-leaf node. Values less than n_samples correspond to leaves of the tree which are the original samples. A node i greater than or equal to n_samples is a non-leaf node and has children children_[i - n_samples]. Alternatively at the i-th iteration, children[i][0] and children[i][1] are merged to form node n_samples + i
The number of connected components in the graph.
The number of leaves in the tree
The parent of each node. Only returned when a connectivity matrix is specified, elsewhere ‘None’ is returned.
Only returned if return_distance is set to True (for compatibility). The distances between the centers of the nodes. distances[i] corresponds to a weighted euclidean distance between the nodes children[i, 1] and children[i, 2]. If the nodes refer to leaves of the tree, then distances[i] is their unweighted euclidean distance. Distances are updated in the following way (from scipy.hierarchy.linkage):
The new entry \(d(u,v)\) is computed as follows,
where \(u\) is the newly joined cluster consisting of clusters \(s\) and \(t\), \(v\) is an unused cluster in the forest, \(T=|v|+|s|+|t|\), and \(|*|\) is the cardinality of its argument. This is also known as the incremental algorithm.


Create a callable to select columns to be used with ColumnTransformer.
make_column_selector can select columns based on datatype or the columns name with a regex. When using multiple selection criteria, all criteria must match for a column to be selected.
Name of columns containing this regex pattern will be included. If None, column selection will not be selected based on pattern.
A selection of dtypes to include. For more details, see pandas.DataFrame.select_dtypes.
A selection of dtypes to exclude. For more details, see pandas.DataFrame.select_dtypes.
Callable for column selection to be used by a ColumnTransformer.
See also
Class that allows combining the outputs of multiple transformer objects used on column subsets of the data into a single feature space.
Categorical Feature Support in Gradient Boosting
Column Transformer with Mixed Types


Construct a ColumnTransformer from the given transformers.
This is a shorthand for the ColumnTransformer 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 with transformer_weights.
Read more in the User Guide.
Tuples of the form (transformer, columns) specifying the transformer objects to be applied to subsets of the data.
Estimator must support fit and transform. Special-cased strings ‘drop’ and ‘passthrough’ are accepted as well, to indicate to drop the columns or to pass them through untransformed, respectively.
Indexes the data on its second axis. Integers are interpreted as positional columns, while strings can reference DataFrame columns by name. A scalar string or int should be used where transformer expects X to be a 1d array-like (vector), otherwise a 2d array will be passed to the transformer. A callable is passed the input data X and can return any of the above. To select multiple columns by name or dtype, you can use make_column_selector.
By default, only the specified columns in transformers are transformed and combined in the output, and the non-specified columns are dropped. (default of 'drop'). By specifying remainder='passthrough', all remaining columns that were not specified in transformers will be automatically passed through. This subset of columns is concatenated with the output of the transformers. By setting remainder to be an estimator, the remaining non-specified columns will use the remainder estimator. The estimator must support fit and transform.
If the transformed output consists of a mix of sparse and dense data, it will be stacked as a sparse matrix if the density is lower than this value. Use sparse_threshold=0 to always return dense. When the transformed output consists of all sparse or all dense data, the stacked result will be sparse or dense, respectively, and this keyword will be ignored.
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.
If True, the time elapsed while fitting each transformer will be printed as it is completed.
See also
Class that allows combining the outputs of multiple transformer objects used on column subsets of the data into a single feature space.
Release Highlights for scikit-learn 0.23
Categorical Feature Support in Gradient Boosting
Combine predictors using stacking
Common pitfalls in interpretation of coefficients of linear models


Context manager for global scikit-learn configuration
If True, validation for finiteness will be skipped, saving time, but leading to potential crashes. If False, validation for finiteness will be performed, avoiding error. Global default: False.
If set, scikit-learn will attempt to limit the size of temporary arrays to this number of MiB (per job when parallelised), often saving both computation time and memory on expensive operations that can be performed in chunks. Global default: 1024.
If True, only the parameters that were set to non-default values will be printed when printing an estimator. For example, print(SVC()) while True will only print ‘SVC()’, but would print ‘SVC(C=1.0, cache_size=200, …)’ with all the non-changed parameters when False. Default is True.
Changed in version 0.23: Default changed from False to True.
If ‘diagram’, estimators will be displayed as a diagram in a Jupyter lab or notebook context. If ‘text’, estimators will be displayed as text. Default is ‘text’.
New in version 0.23.
See also
Set global scikit-learn configuration.
Retrieve current values of the global configuration.
All settings, not just those presently modified, will be returned to their previous values when the context manager is exited. This is not thread-safe.


Computes the Maximum likelihood covariance estimator
Data from which to compute the covariance estimate
If True, data will not be centered before computation. Useful when working with data whose mean is almost, but not exactly zero. If False, data will be centered before computation.
Empirical covariance (Maximum Likelihood Estimator).
Shrinkage covariance estimation: LedoitWolf vs OAS and max-likelihood


l1-penalized covariance estimator
Read more in the User Guide.
Changed in version v0.20: graph_lasso has been renamed to graphical_lasso
Empirical covariance from which to compute the covariance estimate.
The regularization parameter: the higher alpha, the more regularization, the sparser the inverse covariance. Range is (0, inf].
The initial guess for the covariance. If None, then the empirical covariance is used.
The Lasso solver to use: coordinate descent or LARS. Use LARS for very sparse underlying graphs, where p > n. Elsewhere prefer cd which is more numerically stable.
The tolerance to declare convergence: if the dual gap goes below this value, iterations are stopped. Range is (0, inf].
The tolerance for the elastic net solver used to calculate the descent direction. This parameter controls the accuracy of the search direction for a given column update, not of the overall parameter estimate. Only used for mode=’cd’. Range is (0, inf].
The maximum number of iterations.
If verbose is True, the objective function and dual gap are printed at each iteration.
If return_costs is True, the objective function and dual gap at each iteration are returned.
The machine-precision regularization in the computation of the Cholesky diagonal factors. Increase this for very ill-conditioned systems. Default is np.finfo(np.float64).eps.
Whether or not to return the number of iterations.
The estimated covariance matrix.
The estimated (sparse) precision matrix.
The list of values of the objective function and the dual gap at each iteration. Returned only if return_costs is True.
Number of iterations. Returned only if return_n_iter is set to True.
See also
The algorithm employed to solve this problem is the GLasso algorithm, from the Friedman 2008 Biostatistics paper. It is the same algorithm as in the R glasso package.
One possible difference with the glasso R package is that the diagonal coefficients are not penalized.


Estimates the shrunk Ledoit-Wolf covariance matrix.
Read more in the User Guide.
Data from which to compute the covariance estimate
If True, data will not be centered before computation. Useful to work with data whose mean is significantly equal to zero but is not exactly zero. If False, data will be centered before computation.
Size of blocks into which the covariance matrix will be split. This is purely a memory optimization and does not affect results.
Shrunk covariance.
Coefficient in the convex combination used for the computation of the shrunk estimate.
The regularized (shrunk) covariance is:
(1 - shrinkage) * cov + shrinkage * mu * np.identity(n_features)
where mu = trace(cov) / n_features
Sparse inverse covariance estimation


Estimate covariance with the Oracle Approximating Shrinkage algorithm.
Data from which to compute the covariance estimate.
If True, data will not be centered before computation. Useful to work with data whose mean is significantly equal to zero but is not exactly zero. If False, data will be centered before computation.
Shrunk covariance.
Coefficient in the convex combination used for the computation of the shrunk estimate.
The regularised (shrunk) covariance is:
(1 - shrinkage) * cov + shrinkage * mu * np.identity(n_features)
where mu = trace(cov) / n_features
The formula we used to implement the OAS is slightly modified compared to the one given in the article. See OAS for more details.


Calculates a covariance matrix shrunk on the diagonal
Read more in the User Guide.
Covariance matrix to be shrunk
Coefficient in the convex combination used for the computation of the shrunk estimate. Range is [0, 1].
Shrunk covariance.
The regularized (shrunk) covariance is given by:
(1 - shrinkage) * cov + shrinkage * mu * np.identity(n_features)
where mu = trace(cov) / n_features


Delete all the content of the data home cache.
The path to scikit-learn data directory. If None, the default path is ~/sklearn_learn_data.


Dump the dataset in svmlight / libsvm file format.
This format is a text-based format, with one sample per line. It does not store zero valued features hence is suitable for sparse dataset.
The first element of each line can be used to store a target variable to predict.
Training vectors, where n_samples is the number of samples and n_features is the number of features.
Target values. Class labels must be an integer or float, or array-like objects of integer or float for multilabel classifications.
If string, specifies the path that will contain the data. If file-like, data will be written to f. f should be opened in binary mode.
Whether column indices should be written zero-based (True) or one-based (False).
Comment to insert at the top of the file. This should be either a Unicode string, which will be encoded as UTF-8, or an ASCII byte string. If a comment is given, then it will be preceded by one that identifies the file as having been dumped by scikit-learn. Note that not all tools grok comments in SVMlight files.
Array containing pairwise preference constraints (qid in svmlight format).
Samples may have several labels each (see https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multilabel.html)
New in version 0.17: parameter multilabel to support multilabel datasets.
Libsvm GUI


Load the filenames and data from the 20 newsgroups dataset (classification).
Download it if necessary.
Classes
20
Samples total
18846
Dimensionality
1
Features
text
Read more in the User Guide.
Specify a download and cache folder for the datasets. If None, all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
Select the dataset to load: ‘train’ for the training set, ‘test’ for the test set, ‘all’ for both, with shuffled ordering.
If None (default), load all the categories. If not None, list of category names to load (other categories ignored).
Whether or not to shuffle the data: might be important for models that make the assumption that the samples are independent and identically distributed (i.i.d.), such as stochastic gradient descent.
Determines random number generation for dataset shuffling. Pass an int for reproducible output across multiple function calls. See Glossary.
May contain any subset of (‘headers’, ‘footers’, ‘quotes’). Each of these are kinds of text that will be detected and removed from the newsgroup posts, preventing classifiers from overfitting on metadata.
‘headers’ removes newsgroup headers, ‘footers’ removes blocks at the ends of posts that look like signatures, and ‘quotes’ removes lines that appear to be quoting another post.
‘headers’ follows an exact standard; the other filters are not always correct.
If False, raise an IOError if the data is not locally available instead of trying to download the data from the source site.
If True, returns (data.data, data.target) instead of a Bunch object.
New in version 0.22.
Dictionary-like object, with the following attributes.
The data list to learn.
The target labels.
The path to the location of the data.
The full description of the dataset.
The names of target classes.
New in version 0.22.
Biclustering documents with the Spectral Co-clustering algorithm
Topic extraction with Non-negative Matrix Factorization and Latent Dirichlet Allocation
Sample pipeline for text feature extraction and evaluation
Column Transformer with Heterogeneous Data Sources
Semi-supervised Classification on a Text Dataset
FeatureHasher and DictVectorizer Comparison
Clustering text documents using k-means
Classification of text documents using sparse features


Load and vectorize the 20 newsgroups dataset (classification).
Download it if necessary.
This is a convenience function; the transformation is done using the default settings for CountVectorizer. For more advanced usage (stopword filtering, n-gram extraction, etc.), combine fetch_20newsgroups with a custom CountVectorizer, HashingVectorizer, TfidfTransformer or TfidfVectorizer.
The resulting counts are normalized using sklearn.preprocessing.normalize unless normalize is set to False.
Classes
20
Samples total
18846
Dimensionality
130107
Features
real
Read more in the User Guide.
Select the dataset to load: ‘train’ for the training set, ‘test’ for the test set, ‘all’ for both, with shuffled ordering.
May contain any subset of (‘headers’, ‘footers’, ‘quotes’). Each of these are kinds of text that will be detected and removed from the newsgroup posts, preventing classifiers from overfitting on metadata.
‘headers’ removes newsgroup headers, ‘footers’ removes blocks at the ends of posts that look like signatures, and ‘quotes’ removes lines that appear to be quoting another post.
Specify an download and cache folder for the datasets. If None, all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
If False, raise an IOError if the data is not locally available instead of trying to download the data from the source site.
If True, returns (data.data, data.target) instead of a Bunch object.
New in version 0.20.
If True, normalizes each document’s feature vector to unit norm using sklearn.preprocessing.normalize.
New in version 0.22.
If True, the data is a pandas DataFrame including columns with appropriate dtypes (numeric, string, or categorical). The target is a pandas DataFrame or Series depending on the number of target_columns.
New in version 0.24.
Dictionary-like object, with the following attributes.
The input data matrix. If as_frame is True, data is a pandas DataFrame with sparse columns.
The target labels. If as_frame is True, target is a pandas Series.
The names of target classes.
The full description of the dataset.
Only present when as_frame=True. Pandas DataFrame with data and target.
New in version 0.24.
data and target would be of the format defined in the Bunch description above.
New in version 0.20.
Model Complexity Influence
Multiclass sparse logistic regression on 20newgroups
The Johnson-Lindenstrauss bound for embedding with random projections


Load the California housing dataset (regression).
Samples total
20640
Dimensionality
8
Features
real
Target
real 0.15 - 5.
Read more in the User Guide.
Specify another download and cache folder for the datasets. By default all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site.
If True, returns (data.data, data.target) instead of a Bunch object.
New in version 0.20.
If True, the data is a pandas DataFrame including columns with appropriate dtypes (numeric, string or categorical). The target is a pandas DataFrame or Series depending on the number of target_columns.
New in version 0.23.
Dictionary-like object, with the following attributes.
Each row corresponding to the 8 feature values in order. If as_frame is True, data is a pandas object.
Each value corresponds to the average house value in units of 100,000. If as_frame is True, target is a pandas object.
Array of ordered feature names used in the dataset.
Description of the California housing dataset.
Only present when as_frame=True. DataFrame with data and target.
New in version 0.23.
New in version 0.20.
This dataset consists of 20,640 samples and 9 features.
Release Highlights for scikit-learn 0.24
Partial Dependence and Individual Conditional Expectation Plots
Imputing missing values with variants of IterativeImputer
Imputing missing values before building an estimator
Compare the effect of different scalers on data with outliers


Load the covertype dataset (classification).
Download it if necessary.
Classes
7
Samples total
581012
Dimensionality
54
Features
int
Read more in the User Guide.
Specify another download and cache folder for the datasets. By default all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site.
Determines random number generation for dataset shuffling. Pass an int for reproducible output across multiple function calls. See Glossary.
Whether to shuffle dataset.
If True, returns (data.data, data.target) instead of a Bunch object.
New in version 0.20.
If True, the data is a pandas DataFrame including columns with appropriate dtypes (numeric). The target is a pandas DataFrame or Series depending on the number of target columns. If return_X_y is True, then (data, target) will be pandas DataFrames or Series as described below.
New in version 0.24.
Dictionary-like object, with the following attributes.
Each row corresponds to the 54 features in the dataset.
Each value corresponds to one of the 7 forest covertypes with values ranging between 1 to 7.
Only present when as_frame=True. Contains data and target.
Description of the forest covertype dataset.
The names of the dataset columns.
The names of the target columns.
New in version 0.20.
Release Highlights for scikit-learn 0.24
Scalable learning with polynomial kernel aproximation


Load the kddcup99 dataset (classification).
Download it if necessary.
Classes
23
Samples total
4898431
Dimensionality
41
Features
discrete (int) or continuous (float)
Read more in the User Guide.
New in version 0.18.
To return the corresponding classical subsets of kddcup 99. If None, return the entire kddcup 99 dataset.
Specify another download and cache folder for the datasets. By default all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders. .. versionadded:: 0.19
Whether to shuffle dataset.
Determines random number generation for dataset shuffling and for selection of abnormal samples if subset='SA'. Pass an int for reproducible output across multiple function calls. See Glossary.
Whether to load only 10 percent of the data.
If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site.
If True, returns (data, target) instead of a Bunch object. See below for more information about the data and target object.
New in version 0.20.
If True, returns a pandas Dataframe for the data and target objects in the Bunch returned object; Bunch return object will also have a frame member.
New in version 0.24.
Dictionary-like object, with the following attributes.
The data matrix to learn. If as_frame=True, data will be a pandas DataFrame.
The regression target for each sample. If as_frame=True, target will be a pandas Series.
Only present when as_frame=True. Contains data and target.
The full description of the dataset.
The names of the dataset columns
The names of the target columns
New in version 0.20.


Load the Labeled Faces in the Wild (LFW) pairs dataset (classification).
Download it if necessary.
Classes
2
Samples total
13233
Dimensionality
5828
Features
real, between 0 and 255
In the official README.txt this task is described as the “Restricted” task. As I am not sure as to implement the “Unrestricted” variant correctly, I left it as unsupported for now.
The original images are 250 x 250 pixels, but the default slice and resize arguments reduce them to 62 x 47.
Read more in the User Guide.
Select the dataset to load: ‘train’ for the development training set, ‘test’ for the development test set, and ‘10_folds’ for the official evaluation set that is meant to be used with a 10-folds cross validation.
Specify another download and cache folder for the datasets. By default all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
Download and use the funneled variant of the dataset.
Ratio used to resize the each face picture.
Keep the 3 RGB channels instead of averaging them to a single gray level channel. If color is True the shape of the data has one more dimension than the shape with color = False.
Provide a custom 2D slice (height, width) to extract the ‘interesting’ part of the jpeg files and avoid use statistical correlation from the background
If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site.
Dictionary-like object, with the following attributes.
Each row corresponds to 2 ravel’d face images of original size 62 x 47 pixels. Changing the slice_, resize or subset parameters will change the shape of the output.
Each row has 2 face images corresponding to same or different person from the dataset containing 5749 people. Changing the slice_, resize or subset parameters will change the shape of the output.
Labels associated to each pair of images. The two label values being different persons or the same person.
Description of the Labeled Faces in the Wild (LFW) dataset.


Load the Labeled Faces in the Wild (LFW) people dataset (classification).
Download it if necessary.
Classes
5749
Samples total
13233
Dimensionality
5828
Features
real, between 0 and 255
Read more in the User Guide.
Specify another download and cache folder for the datasets. By default all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
Download and use the funneled variant of the dataset.
Ratio used to resize the each face picture.
The extracted dataset will only retain pictures of people that have at least min_faces_per_person different pictures.
Keep the 3 RGB channels instead of averaging them to a single gray level channel. If color is True the shape of the data has one more dimension than the shape with color = False.
Provide a custom 2D slice (height, width) to extract the ‘interesting’ part of the jpeg files and avoid use statistical correlation from the background
If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site.
If True, returns (dataset.data, dataset.target) instead of a Bunch object. See below for more information about the dataset.data and dataset.target object.
New in version 0.20.
Dictionary-like object, with the following attributes.
Each row corresponds to a ravelled face image of original size 62 x 47 pixels. Changing the slice_ or resize parameters will change the shape of the output.
Each row is a face image corresponding to one of the 5749 people in the dataset. Changing the slice_ or resize parameters will change the shape of the output.
Labels associated to each face image. Those labels range from 0-5748 and correspond to the person IDs.
Description of the Labeled Faces in the Wild (LFW) dataset.
New in version 0.20.
Faces recognition example using eigenfaces and SVMs


Load the Olivetti faces data-set from AT&T (classification).
Download it if necessary.
Classes
40
Samples total
400
Dimensionality
4096
Features
real, between 0 and 1
Read more in the User Guide.
Specify another download and cache folder for the datasets. By default all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
If True the order of the dataset is shuffled to avoid having images of the same person grouped.
Determines random number generation for dataset shuffling. Pass an int for reproducible output across multiple function calls. See Glossary.
If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site.
If True, returns (data, target) instead of a Bunch object. See below for more information about the data and target object.
New in version 0.22.
Dictionary-like object, with the following attributes.
Each row corresponds to a ravelled face image of original size 64 x 64 pixels.
Each row is a face image corresponding to one of the 40 subjects of the dataset.
Labels associated to each face image. Those labels are ranging from 0-39 and correspond to the Subject IDs.
Description of the modified Olivetti Faces Dataset.
New in version 0.22.
Online learning of a dictionary of parts of faces
Faces dataset decompositions
Pixel importances with a parallel forest of trees
Face completion with a multi-output estimators


Fetch dataset from openml by name or dataset id.
Datasets are uniquely identified by either an integer ID or by a combination of name and version (i.e. there might be multiple versions of the ‘iris’ dataset). Please give either name or data_id (not both). In case a name is given, a version can also be provided.
Read more in the User Guide.
New in version 0.20.
Note
EXPERIMENTAL
The API is experimental (particularly the return value structure), and might have small backward-incompatible changes without notice or warning in future releases.
String identifier of the dataset. Note that OpenML can have multiple datasets with the same name.
Version of the dataset. Can only be provided if also name is given. If ‘active’ the oldest version that’s still active is used. Since there may be more than one active version of a dataset, and those versions may fundamentally be different from one another, setting an exact version is highly recommended.
OpenML ID of the dataset. The most specific way of retrieving a dataset. If data_id is not given, name (and potential version) are used to obtain a dataset.
Specify another download and cache folder for the data sets. By default all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
Specify the column name in the data to use as target. If ‘default-target’, the standard target column a stored on the server is used. If None, all columns are returned as data and the target is None. If list (of strings), all columns with these names are returned as multi-target (Note: not all scikit-learn classifiers can handle all types of multi-output combinations)
Whether to cache downloaded datasets using joblib.
If True, returns (data, target) instead of a Bunch object. See below for more information about the data and target objects.
If True, the data is a pandas DataFrame including columns with appropriate dtypes (numeric, string or categorical). The target is a pandas DataFrame or Series depending on the number of target_columns. The Bunch will contain a frame attribute with the target and the data. If return_X_y is True, then (data, target) will be pandas DataFrames or Series as describe above.
If as_frame is ‘auto’, the data and target will be converted to DataFrame or Series as if as_frame is set to True, unless the dataset is stored in sparse format.
Changed in version 0.24: The default value of as_frame changed from False to 'auto' in 0.24.
Dictionary-like object, with the following attributes.
The feature matrix. Categorical features are encoded as ordinals.
The regression target or classification labels, if applicable. Dtype is float if numeric, and object if categorical. If as_frame is True, target is a pandas object.
The full description of the dataset
The names of the dataset columns
The names of the target columns
New in version 0.22.
Maps each categorical feature name to a list of values, such that the value encoded as i is ith in the list. If as_frame is True, this is None.
More metadata from OpenML
Only present when as_frame=True. DataFrame with data and target.
Note
EXPERIMENTAL
This interface is experimental and subsequent releases may change attributes without notice (although there should only be minor changes to data and target).
Missing values in the ‘data’ are represented as NaN’s. Missing values in ‘target’ are represented as NaN’s (numerical target) or None (categorical target)
Release Highlights for scikit-learn 0.22
Categorical Feature Support in Gradient Boosting
Combine predictors using stacking
Gaussian process regression (GPR) on Mauna Loa CO2 data.
MNIST classification using multinomial logistic + L1
Early stopping of Stochastic Gradient Descent
Poisson regression and non-normal loss
Tweedie regression on insurance claims
Permutation Importance vs Random Forest Feature Importance (MDI)
Common pitfalls in interpretation of coefficients of linear models
Visualizations with Display Objects
Classifier Chain
Approximate nearest neighbors in TSNE
Visualization of MLP weights on MNIST
Column Transformer with Mixed Types
Effect of transforming the targets in regression model


Load the RCV1 multilabel dataset (classification).
Download it if necessary.
Version: RCV1-v2, vectors, full sets, topics multilabels.
Classes
103
Samples total
804414
Dimensionality
47236
Features
real, between 0 and 1
Read more in the User Guide.
New in version 0.17.
Specify another download and cache folder for the datasets. By default all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
Select the dataset to load: ‘train’ for the training set (23149 samples), ‘test’ for the test set (781265 samples), ‘all’ for both, with the training samples first if shuffle is False. This follows the official LYRL2004 chronological split.
If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site.
Determines random number generation for dataset shuffling. Pass an int for reproducible output across multiple function calls. See Glossary.
Whether to shuffle dataset.
If True, returns (dataset.data, dataset.target) instead of a Bunch object. See below for more information about the dataset.data and dataset.target object.
New in version 0.20.
Dictionary-like object, with the following attributes.
The array has 0.16% of non zero values. Will be of CSR format.
Each sample has a value of 1 in its categories, and 0 in others. The array has 3.15% of non zero values. Will be of CSR format.
Identification number of each sample, as ordered in dataset.data.
Names of each target (RCV1 topics), as ordered in dataset.target.
Description of the RCV1 dataset.
New in version 0.20.


Loader for species distribution dataset from Phillips et. al. (2006)
Read more in the User Guide.
Specify another download and cache folder for the datasets. By default all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site.
Dictionary-like object, with the following attributes.
These represent the 14 features measured at each point of the map grid. The latitude/longitude values for the grid are discussed below. Missing data is represented by the value -9999.
The training points for the data. Each point has three fields:
The test points for the data. Same format as the training data.
The number of longitudes (x) and latitudes (y) in the grid
The (x,y) position of the lower-left corner, in degrees
The spacing between points of the grid, in degrees
This dataset represents the geographic distribution of species. The dataset is provided by Phillips et. al. (2006).
The two species are:
Species distribution modeling
Kernel Density Estimate of Species Distributions


Return the path of the scikit-learn data dir.
This folder is used by some large dataset loaders to avoid downloading the data several times.
By default the data dir is set to a folder named ‘scikit_learn_data’ in the user home folder.
Alternatively, it can be set by the ‘SCIKIT_LEARN_DATA’ environment variable or programmatically by giving an explicit folder path. The ‘~’ symbol is expanded to the user home folder.
If the folder does not already exist, it is automatically created.
The path to scikit-learn data directory. If None, the default path is ~/sklearn_learn_data.
Out-of-core classification of text documents


Load and return the boston house-prices dataset (regression).
Samples total
506
Dimensionality
13
Features
real, positive
Targets
real 5. - 50.
Read more in the User Guide.
If True, returns (data, target) instead of a Bunch object. See below for more information about the data and target object.
New in version 0.18.
Dictionary-like object, with the following attributes.
The data matrix.
The regression target.
The physical location of boston csv dataset.
New in version 0.20.
The full description of the dataset.
The names of features
New in version 0.18.
Changed in version 0.20: Fixed a wrong data point at [445, 0].


Load and return the breast cancer wisconsin dataset (classification).
The breast cancer dataset is a classic and very easy binary classification dataset.
Classes
2
Samples per class
212(M),357(B)
Samples total
569
Dimensionality
30
Features
real, positive
Read more in the User Guide.
If True, returns (data, target) instead of a Bunch object. See below for more information about the data and target object.
New in version 0.18.
If True, the data is a pandas DataFrame including columns with appropriate dtypes (numeric). The target is a pandas DataFrame or Series depending on the number of target columns. If return_X_y is True, then (data, target) will be pandas DataFrames or Series as described below.
New in version 0.23.
Dictionary-like object, with the following attributes.
The data matrix. If as_frame=True, data will be a pandas DataFrame.
The classification target. If as_frame=True, target will be a pandas Series.
The names of the dataset columns.
The names of target classes.
Only present when as_frame=True. DataFrame with data and target.
New in version 0.23.
The full description of the dataset.
The path to the location of the data.
New in version 0.20.
New in version 0.18.
Let’s say you are interested in the samples 10, 50, and 85, and want to know their class name.
Post pruning decision trees with cost complexity pruning
Permutation Importance with Multicollinear or Correlated Features
Effect of varying threshold for self-training


Load and return the diabetes dataset (regression).
Samples total
442
Dimensionality
10
Features
real, -.2 < x < .2
Targets
integer 25 - 346
Read more in the User Guide.
If True, returns (data, target) instead of a Bunch object. See below for more information about the data and target object.
New in version 0.18.
If True, the data is a pandas DataFrame including columns with appropriate dtypes (numeric). The target is a pandas DataFrame or Series depending on the number of target columns. If return_X_y is True, then (data, target) will be pandas DataFrames or Series as described below.
New in version 0.23.
Dictionary-like object, with the following attributes.
The data matrix. If as_frame=True, data will be a pandas DataFrame.
The regression target. If as_frame=True, target will be a pandas Series.
The names of the dataset columns.
Only present when as_frame=True. DataFrame with data and target.
New in version 0.23.
The full description of the dataset.
The path to the location of the data.
The path to the location of the target.
New in version 0.18.
Plot individual and voting regression predictions
Gradient Boosting regression
Model Complexity Influence
Model-based and sequential feature selection
Lasso path using LARS
Linear Regression Example
Sparsity Example: Fitting only features 1 and 2
Lasso and Elastic Net
Lasso model selection: Cross-Validation / AIC / BIC
Advanced Plotting With Partial Dependence
Imputing missing values before building an estimator
Plotting Cross-Validated Predictions
Cross-validation on diabetes Dataset Exercise


Load and return the digits dataset (classification).
Each datapoint is a 8x8 image of a digit.
Classes
10
Samples per class
~180
Samples total
1797
Dimensionality
64
Features
integers 0-16
Read more in the User Guide.
The number of classes to return. Between 0 and 10.
If True, returns (data, target) instead of a Bunch object. See below for more information about the data and target object.
New in version 0.18.
If True, the data is a pandas DataFrame including columns with appropriate dtypes (numeric). The target is a pandas DataFrame or Series depending on the number of target columns. If return_X_y is True, then (data, target) will be pandas DataFrames or Series as described below.
New in version 0.23.
Dictionary-like object, with the following attributes.
The flattened data matrix. If as_frame=True, data will be a pandas DataFrame.
The classification target. If as_frame=True, target will be a pandas Series.
The names of the dataset columns.
The names of target classes.
New in version 0.20.
Only present when as_frame=True. DataFrame with data and target.
New in version 0.23.
The raw image data.
The full description of the dataset.
New in version 0.18.
To load the data and visualize the images:
Recognizing hand-written digits
Feature agglomeration
Various Agglomerative Clustering on a 2D embedding of digits
A demo of K-Means clustering on the handwritten digits data
The Digit Dataset
Early stopping of Gradient Boosting
Recursive feature elimination
Comparing various online solvers
L1 Penalty and Sparsity in Logistic Regression
Manifold learning on handwritten digits: Locally Linear Embedding, Isomap…
The Johnson-Lindenstrauss bound for embedding with random projections
Explicit feature map approximation for RBF kernels
Plotting Validation Curves
Parameter estimation using grid search with cross-validation
Comparing randomized search and grid search for hyperparameter estimation
Balance model complexity and cross-validated score
Plotting Learning Curves
Kernel Density Estimation
Caching nearest neighbors
Dimensionality Reduction with Neighborhood Components Analysis
Restricted Boltzmann Machine features for digit classification
Compare Stochastic learning strategies for MLPClassifier
Pipelining: chaining a PCA and a logistic regression
Selecting dimensionality reduction with Pipeline and GridSearchCV
Label Propagation digits: Demonstrating performance
Label Propagation digits active learning
Digits Classification Exercise
Cross-validation on Digits Dataset Exercise


Load text files with categories as subfolder names.
Individual samples are assumed to be files stored a two levels folder structure such as the following:
file_1.txt file_2.txt … file_42.txt
file_43.txt file_44.txt …
The folder names are used as supervised signal label names. The individual file names are not important.
This function does not try to extract features into a numpy array or scipy sparse matrix. In addition, if load_content is false it does not try to load the files in memory.
To use text files in a scikit-learn classification or clustering algorithm, you will need to use the :mod`~sklearn.feature_extraction.text` module to build a feature extraction transformer that suits your problem.
If you set load_content=True, you should also specify the encoding of the text using the ‘encoding’ parameter. For many modern text files, ‘utf-8’ will be the correct encoding. If you leave encoding equal to None, then the content will be made of bytes instead of Unicode, and you will not be able to use most functions in text.
Similar feature extractors should be built for other kind of unstructured data input such as images, audio, video, …
Read more in the User Guide.
Path to the main folder holding one subfolder per category
A paragraph describing the characteristic of the dataset: its source, reference, etc.
If None (default), load all the categories. If not None, list of category names to load (other categories ignored).
Whether to load or not the content of the different files. If true a ‘data’ attribute containing the text information is present in the data structure returned. If not, a filenames attribute gives the path to the files.
Whether or not to shuffle the data: might be important for models that make the assumption that the samples are independent and identically distributed (i.i.d.), such as stochastic gradient descent.
If None, do not try to decode the content of the files (e.g. for images or other non-text content). If not None, encoding to use to decode text files to Unicode if load_content is True.
Instruction on what to do if a byte sequence is given to analyze that contains characters not of the given encoding. Passed as keyword argument ‘errors’ to bytes.decode.
Determines random number generation for dataset shuffling. Pass an int for reproducible output across multiple function calls. See Glossary.
Dictionary-like object, with the following attributes.
Only present when load_content=True. The raw text data to learn.
The target labels (integer index).
The names of target classes.
The full description of the dataset.
The filenames holding the dataset.


Load and return the iris dataset (classification).
The iris dataset is a classic and very easy multi-class classification dataset.
Classes
3
Samples per class
50
Samples total
150
Dimensionality
4
Features
real, positive
Read more in the User Guide.
If True, returns (data, target) instead of a Bunch object. See below for more information about the data and target object.
New in version 0.18.
If True, the data is a pandas DataFrame including columns with appropriate dtypes (numeric). The target is a pandas DataFrame or Series depending on the number of target columns. If return_X_y is True, then (data, target) will be pandas DataFrames or Series as described below.
New in version 0.23.
Dictionary-like object, with the following attributes.
The data matrix. If as_frame=True, data will be a pandas DataFrame.
The classification target. If as_frame=True, target will be a pandas Series.
The names of the dataset columns.
The names of target classes.
Only present when as_frame=True. DataFrame with data and target.
New in version 0.23.
The full description of the dataset.
The path to the location of the data.
New in version 0.20.
New in version 0.18.
Changed in version 0.20: Fixed two wrong data points according to Fisher’s paper. The new version is the same as in R, but not as in the UCI Machine Learning Repository.
Let’s say you are interested in the samples 10, 25, and 50, and want to know their class name.
Release Highlights for scikit-learn 0.24
Release Highlights for scikit-learn 0.22
Plot classification probability
Plot Hierarchical Clustering Dendrogram
K-means Clustering
The Iris Dataset
Plot the decision surface of a decision tree on the iris dataset
Understanding the decision tree structure
PCA example with Iris Data-set
Incremental PCA
Comparison of LDA and PCA 2D projection of Iris dataset
Factor Analysis (with rotation) to visualize patterns
Plot the decision boundaries of a VotingClassifier
Early stopping of Gradient Boosting
Plot the decision surfaces of ensembles of trees on the iris dataset
Test with permutations the significance of a classification score
Univariate Feature Selection
GMM covariances
Gaussian process classification (GPC) on iris dataset
Regularization path of L1- Logistic Regression
Logistic Regression 3-class Classifier
Plot multi-class SGD on the iris dataset
Confusion matrix
Receiver Operating Characteristic (ROC) with cross validation
Nested versus non-nested cross-validation
Receiver Operating Characteristic (ROC)
Precision-Recall
Nearest Centroid Classification
Nearest Neighbors Classification
Comparing Nearest Neighbors with and without Neighborhood Components Analysis
Compare Stochastic learning strategies for MLPClassifier
Concatenating multiple feature extraction methods
Decision boundary of semi-supervised classifiers versus SVM on the Iris dataset
SVM with custom kernel
SVM-Anova: SVM with univariate feature selection
Plot different SVM classifiers in the iris dataset
RBF SVM parameters
SVM Exercise


Load and return the physical excercise linnerud dataset.
This dataset is suitable for multi-ouput regression tasks.
Samples total
20
Dimensionality
3 (for both data and target)
Features
integer
Targets
integer
Read more in the User Guide.
If True, returns (data, target) instead of a Bunch object. See below for more information about the data and target object.
New in version 0.18.
If True, the data is a pandas DataFrame including columns with appropriate dtypes (numeric, string or categorical). The target is a pandas DataFrame or Series depending on the number of target columns. If return_X_y is True, then (data, target) will be pandas DataFrames or Series as described below.
New in version 0.23.
Dictionary-like object, with the following attributes.
The data matrix. If as_frame=True, data will be a pandas DataFrame.
The regression targets. If as_frame=True, target will be a pandas DataFrame.
The names of the dataset columns.
The names of the target columns.
Only present when as_frame=True. DataFrame with data and target.
New in version 0.23.
The full description of the dataset.
The path to the location of the data.
The path to the location of the target.
New in version 0.20.
New in version 0.18.


Load the numpy array of a single sample image
Read more in the User Guide.
The name of the sample image loaded
The image as a numpy array: height x width x color
Color Quantization using K-Means


Load sample images for image manipulation.
Loads both, china and flower.
Read more in the User Guide.
Dictionary-like object, with the following attributes.
The two sample image.
The filenames for the images.
The full description of the dataset.
To load the data and visualize the images:


Load datasets in the svmlight / libsvm format into sparse CSR matrix
This format is a text-based format, with one sample per line. It does not store zero valued features hence is suitable for sparse dataset.
The first element of each line can be used to store a target variable to predict.
This format is used as the default format for both svmlight and the libsvm command line programs.
Parsing a text based source can be expensive. When working on repeatedly on the same dataset, it is recommended to wrap this loader with joblib.Memory.cache to store a memmapped backup of the CSR results of the first call and benefit from the near instantaneous loading of memmapped structures for the subsequent calls.
In case the file contains a pairwise preference constraint (known as “qid” in the svmlight format) these are ignored unless the query_id parameter is set to True. These pairwise preference constraints can be used to constraint the combination of samples when using pairwise loss functions (as is the case in some learning to rank problems) so that only pairs with the same query_id value are considered.
This implementation is written in Cython and is reasonably fast. However, a faster API-compatible loader is also available at:
https://github.com/mblondel/svmlight-loader
(Path to) a file to load. If a path ends in “.gz” or “.bz2”, it will be uncompressed on the fly. If an integer is passed, it is assumed to be a file descriptor. A file-like or file descriptor will not be closed by this function. A file-like object must be opened in binary mode.
The number of features to use. If None, it will be inferred. This argument is useful to load several files that are subsets of a bigger sliced dataset: each subset might not have examples of every feature, hence the inferred shape might vary from one slice to another. n_features is only required if offset or length are passed a non-default value.
Data type of dataset to be loaded. This will be the data type of the output numpy arrays X and y.
Samples may have several labels each (see https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multilabel.html)
Whether column indices in f are zero-based (True) or one-based (False). If column indices are one-based, they are transformed to zero-based to match Python/NumPy conventions. If set to “auto”, a heuristic check is applied to determine this from the file contents. Both kinds of files occur “in the wild”, but they are unfortunately not self-identifying. Using “auto” or True should always be safe when no offset or length is passed. If offset or length are passed, the “auto” mode falls back to zero_based=True to avoid having the heuristic check yield inconsistent results on different segments of the file.
If True, will return the query_id array for each file.
Ignore the offset first bytes by seeking forward, then discarding the following bytes up until the next new line character.
If strictly positive, stop reading any new line of data once the position in the file has reached the (offset + length) bytes threshold.
tuples of length n_samples.
query_id for each sample. Only returned when query_id is set to True.
See also
Similar function for loading multiple files in this format, enforcing the same number of features/columns on all of them.
To use joblib.Memory to cache the svmlight file:


Load dataset from multiple files in SVMlight format
This function is equivalent to mapping load_svmlight_file over a list of files, except that the results are concatenated into a single, flat list and the samples vectors are constrained to all have the same number of features.
In case the file contains a pairwise preference constraint (known as “qid” in the svmlight format) these are ignored unless the query_id parameter is set to True. These pairwise preference constraints can be used to constraint the combination of samples when using pairwise loss functions (as is the case in some learning to rank problems) so that only pairs with the same query_id value are considered.
(Paths of) files to load. If a path ends in “.gz” or “.bz2”, it will be uncompressed on the fly. If an integer is passed, it is assumed to be a file descriptor. File-likes and file descriptors will not be closed by this function. File-like objects must be opened in binary mode.
The number of features to use. If None, it will be inferred from the maximum column index occurring in any of the files.
This can be set to a higher value than the actual number of features in any of the input files, but setting it to a lower value will cause an exception to be raised.
Data type of dataset to be loaded. This will be the data type of the output numpy arrays X and y.
Samples may have several labels each (see https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multilabel.html)
Whether column indices in f are zero-based (True) or one-based (False). If column indices are one-based, they are transformed to zero-based to match Python/NumPy conventions. If set to “auto”, a heuristic check is applied to determine this from the file contents. Both kinds of files occur “in the wild”, but they are unfortunately not self-identifying. Using “auto” or True should always be safe when no offset or length is passed. If offset or length are passed, the “auto” mode falls back to zero_based=True to avoid having the heuristic check yield inconsistent results on different segments of the file.
If True, will return the query_id array for each file.
Ignore the offset first bytes by seeking forward, then discarding the following bytes up until the next new line character.
If strictly positive, stop reading any new line of data once the position in the file has reached the (offset + length) bytes threshold.
See also
When fitting a model to a matrix X_train and evaluating it against a matrix X_test, it is essential that X_train and X_test have the same number of features (X_train.shape[1] == X_test.shape[1]). This may not be the case if you load the files individually with load_svmlight_file.


Load and return the wine dataset (classification).
New in version 0.18.
The wine dataset is a classic and very easy multi-class classification dataset.
Classes
3
Samples per class
[59,71,48]
Samples total
178
Dimensionality
13
Features
real, positive
Read more in the User Guide.
If True, returns (data, target) instead of a Bunch object. See below for more information about the data and target object.
If True, the data is a pandas DataFrame including columns with appropriate dtypes (numeric). The target is a pandas DataFrame or Series depending on the number of target columns. If return_X_y is True, then (data, target) will be pandas DataFrames or Series as described below.
New in version 0.23.
Dictionary-like object, with the following attributes.
The data matrix. If as_frame=True, data will be a pandas DataFrame.
The classification target. If as_frame=True, target will be a pandas Series.
The names of the dataset columns.
The names of target classes.
Only present when as_frame=True. DataFrame with data and target.
New in version 0.23.
The full description of the dataset.
Let’s say you are interested in the samples 10, 80, and 140, and want to know their class name.
Outlier detection on a real data set
ROC Curve with Visualization API
Importance of Feature Scaling


Generate an array with constant block diagonal structure for biclustering.
Read more in the User Guide.
The shape of the result.
The number of biclusters.
The standard deviation of the gaussian noise.
Minimum value of a bicluster.
Maximum value of a bicluster.
Shuffle the samples.
Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.
The generated array.
The indicators for cluster membership of each row.
The indicators for cluster membership of each column.
See also
Dhillon, I. S. (2001, August). Co-clustering documents and words using bipartite spectral graph partitioning. In Proceedings of the seventh ACM SIGKDD international conference on Knowledge discovery and data mining (pp. 269-274). ACM.
A demo of the Spectral Co-Clustering algorithm


Generate isotropic Gaussian blobs for clustering.
Read more in the User Guide.
If int, it is the total number of points equally divided among clusters. If array-like, each element of the sequence indicates the number of samples per cluster.
Changed in version v0.20: one can now pass an array-like to the n_samples parameter
The number of features for each sample.
The number of centers to generate, or the fixed center locations. If n_samples is an int and centers is None, 3 centers are generated. If n_samples is array-like, centers must be either None or an array of length equal to the length of n_samples.
The standard deviation of the clusters.
The bounding box for each cluster center when centers are generated at random.
Shuffle the samples.
Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.
If True, then return the centers of each cluster
New in version 0.23.
The generated samples.
The integer labels for cluster membership of each sample.
The centers of each cluster. Only returned if return_centers=True.
See also
A more intricate variant.
Release Highlights for scikit-learn 0.23
Probability calibration of classifiers
Probability Calibration for 3-class classification
Normal, Ledoit-Wolf and OAS Linear Discriminant Analysis for classification
An example of K-Means++ initialization
A demo of the mean-shift clustering algorithm
Demonstration of k-means assumptions
Demo of affinity propagation clustering algorithm
Demo of DBSCAN clustering algorithm
Inductive Clustering
Compare BIRCH and MiniBatchKMeans
Comparison of the K-Means and MiniBatchKMeans clustering algorithms
Comparing different hierarchical linkage methods on toy datasets
Selecting the number of clusters with silhouette analysis on KMeans clustering
Comparing different clustering algorithms on toy datasets
Plot randomly generated classification dataset
SGD: Maximum margin separating hyperplane
Plot multinomial and One-vs-Rest Logistic Regression
Comparing anomaly detection algorithms for outlier detection on toy datasets
Demonstrating the different strategies of KBinsDiscretizer
SVM: Maximum margin separating hyperplane
SVM Tie Breaking Example
Plot the support vectors in LinearSVC
SVM: Separating hyperplane for unbalanced classes


Generate an array with block checkerboard structure for biclustering.
Read more in the User Guide.
The shape of the result.
The number of row and column clusters.
The standard deviation of the gaussian noise.
Minimum value of a bicluster.
Maximum value of a bicluster.
Shuffle the samples.
Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.
The generated array.
The indicators for cluster membership of each row.
The indicators for cluster membership of each column.
See also
Kluger, Y., Basri, R., Chang, J. T., & Gerstein, M. (2003). Spectral biclustering of microarray data: coclustering genes and conditions. Genome research, 13(4), 703-716.
A demo of the Spectral Biclustering algorithm


Make a large circle containing a smaller circle in 2d.
A simple toy dataset to visualize clustering and classification algorithms.
Read more in the User Guide.
If int, it is the total number of points generated. For odd numbers, the inner circle will have one point more than the outer circle. If two-element tuple, number of points in outer circle and inner circle.
Changed in version 0.23: Added two-element tuple.
Whether to shuffle the samples.
Standard deviation of Gaussian noise added to the data.
Determines random number generation for dataset shuffling and noise. Pass an int for reproducible output across multiple function calls. See Glossary.
Scale factor between inner and outer circle in the range (0, 1).
The generated samples.
The integer labels (0 or 1) for class membership of each sample.
Classifier comparison
Comparing different hierarchical linkage methods on toy datasets
Comparing different clustering algorithms on toy datasets
Kernel PCA
Hashing feature transformation using Totally Random Trees
t-SNE: The effect of various perplexity values on the shape
Compare Stochastic learning strategies for MLPClassifier
Varying regularization in Multi-layer Perceptron
Feature discretization
Label Propagation learning a complex structure


Generate a random n-class classification problem.
This initially creates clusters of points normally distributed (std=1) about vertices of an n_informative-dimensional hypercube with sides of length 2*class_sep and assigns an equal number of clusters to each class. It introduces interdependence between these features and adds various types of further noise to the data.
Without shuffling, X horizontally stacks features in the following order: the primary n_informative features, followed by n_redundant linear combinations of the informative features, followed by n_repeated duplicates, drawn randomly with replacement from the informative and redundant features. The remaining features are filled with random noise. Thus, without shuffling, all useful features are contained in the columns X[:, :n_informative + n_redundant + n_repeated].
Read more in the User Guide.
The number of samples.
The total number of features. These comprise n_informative informative features, n_redundant redundant features, n_repeated duplicated features and n_features-n_informative-n_redundant-n_repeated useless features drawn at random.
The number of informative features. Each class is composed of a number of gaussian clusters each located around the vertices of a hypercube in a subspace of dimension n_informative. For each cluster, informative features are drawn independently from N(0, 1) and then randomly linearly combined within each cluster in order to add covariance. The clusters are then placed on the vertices of the hypercube.
The number of redundant features. These features are generated as random linear combinations of the informative features.
The number of duplicated features, drawn randomly from the informative and the redundant features.
The number of classes (or labels) of the classification problem.
The number of clusters per class.
The proportions of samples assigned to each class. If None, then classes are balanced. Note that if len(weights) == n_classes - 1, then the last class weight is automatically inferred. More than n_samples samples may be returned if the sum of weights exceeds 1. Note that the actual class proportions will not exactly match weights when flip_y isn’t 0.
The fraction of samples whose class is assigned randomly. Larger values introduce noise in the labels and make the classification task harder. Note that the default setting flip_y > 0 might lead to less than n_classes in y in some cases.
The factor multiplying the hypercube size. Larger values spread out the clusters/classes and make the classification task easier.
If True, the clusters are put on the vertices of a hypercube. If False, the clusters are put on the vertices of a random polytope.
Shift features by the specified value. If None, then features are shifted by a random value drawn in [-class_sep, class_sep].
Multiply features by the specified value. If None, then features are scaled by a random value drawn in [1, 100]. Note that scaling happens after shifting.
Shuffle the samples and the features.
Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.
The generated samples.
The integer labels for class membership of each sample.
See also
Simplified variant.
Unrelated generator for multilabel tasks.
The algorithm is adapted from Guyon [1] and was designed to generate the “Madelon” dataset.
I. Guyon, “Design of experiments for the NIPS 2003 variable selection benchmark”, 2003.
Release Highlights for scikit-learn 0.24
Release Highlights for scikit-learn 0.22
Comparison of Calibration of Classifiers
Probability Calibration curves
Classifier comparison
Plot randomly generated classification dataset
Feature importances with forests of trees
OOB Errors for Random Forests
Feature transformations with ensembles of trees
Pipeline Anova SVM
Recursive feature elimination with cross-validation
Detection error tradeoff (DET) curve
Successive Halving Iterations
Comparison between grid search and successive halving
Neighborhood Components Analysis Illustration
Varying regularization in Multi-layer Perceptron
Feature discretization
Scaling the regularization parameter for SVCs


Generate the “Friedman #1” regression problem.
This dataset is described in Friedman [1] and Breiman [2].
Inputs X are independent features uniformly distributed on the interval [0, 1]. The output y is created according to the formula:
Out of the n_features features, only 5 are actually used to compute y. The remaining features are independent of y.
The number of features has to be >= 5.
Read more in the User Guide.
The number of samples.
The number of features. Should be at least 5.
The standard deviation of the gaussian noise applied to the output.
Determines random number generation for dataset noise. Pass an int for reproducible output across multiple function calls. See Glossary.
The input samples.
The output values.
J. Friedman, “Multivariate adaptive regression splines”, The Annals of Statistics 19 (1), pages 1-67, 1991.
L. Breiman, “Bagging predictors”, Machine Learning 24, pages 123-140, 1996.


Generate the “Friedman #2” regression problem.
This dataset is described in Friedman [1] and Breiman [2].
Inputs X are 4 independent features uniformly distributed on the intervals:
The output y is created according to the formula:
Read more in the User Guide.
The number of samples.
The standard deviation of the gaussian noise applied to the output.
Determines random number generation for dataset noise. Pass an int for reproducible output across multiple function calls. See Glossary.
The input samples.
The output values.
J. Friedman, “Multivariate adaptive regression splines”, The Annals of Statistics 19 (1), pages 1-67, 1991.
L. Breiman, “Bagging predictors”, Machine Learning 24, pages 123-140, 1996.


Generate the “Friedman #3” regression problem.
This dataset is described in Friedman [1] and Breiman [2].
Inputs X are 4 independent features uniformly distributed on the intervals:
The output y is created according to the formula:
Read more in the User Guide.
The number of samples.
The standard deviation of the gaussian noise applied to the output.
Determines random number generation for dataset noise. Pass an int for reproducible output across multiple function calls. See Glossary.
The input samples.
The output values.
J. Friedman, “Multivariate adaptive regression splines”, The Annals of Statistics 19 (1), pages 1-67, 1991.
L. Breiman, “Bagging predictors”, Machine Learning 24, pages 123-140, 1996.


Generate isotropic Gaussian and label samples by quantile.
This classification dataset is constructed by taking a multi-dimensional standard normal distribution and defining classes separated by nested concentric multi-dimensional spheres such that roughly equal numbers of samples are in each class (quantiles of the \(\chi^2\) distribution).
Read more in the User Guide.
The mean of the multi-dimensional normal distribution. If None then use the origin (0, 0, …).
The covariance matrix will be this value times the unit matrix. This dataset only produces symmetric normal distributions.
The total number of points equally divided among classes.
The number of features for each sample.
The number of classes
Shuffle the samples.
Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.
The generated samples.
The integer labels for quantile membership of each sample.
The dataset is from Zhu et al [1].
Plot randomly generated classification dataset
Two-class AdaBoost
Multi-class AdaBoosted Decision Trees


Generates data for binary classification used in Hastie et al. 2009, Example 10.2.
The ten features are standard independent Gaussian and the target y is defined by:
Read more in the User Guide.
The number of samples.
Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.
The input samples.
The output values.
See also
A generalization of this dataset approach.
T. Hastie, R. Tibshirani and J. Friedman, “Elements of Statistical Learning Ed. 2”, Springer, 2009.
Gradient Boosting regularization
Discrete versus Real AdaBoost
Early stopping of Gradient Boosting
Demonstration of multi-metric evaluation on cross_val_score and GridSearchCV


Generate a mostly low rank matrix with bell-shaped singular values.
Most of the variance can be explained by a bell-shaped curve of width effective_rank: the low rank part of the singular values profile is:
The remaining singular values’ tail is fat, decreasing as:
The low rank part of the profile can be considered the structured signal part of the data while the tail can be considered the noisy part of the data that cannot be summarized by a low number of linear components (singular vectors).
Read more in the User Guide.
The number of samples.
The number of features.
The approximate number of singular vectors required to explain most of the data by linear combinations.
The relative importance of the fat noisy tail of the singular values profile. The value should be between 0 and 1.
Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.
The matrix.


Make two interleaving half circles.
A simple toy dataset to visualize clustering and classification algorithms. Read more in the User Guide.
If int, the total number of points generated. If two-element tuple, number of points in each of two moons.
Changed in version 0.23: Added two-element tuple.
Whether to shuffle the samples.
Standard deviation of Gaussian noise added to the data.
Determines random number generation for dataset shuffling and noise. Pass an int for reproducible output across multiple function calls. See Glossary.
The generated samples.
The integer labels (0 or 1) for class membership of each sample.
Classifier comparison
Comparing different hierarchical linkage methods on toy datasets
Comparing different clustering algorithms on toy datasets
Comparing anomaly detection algorithms for outlier detection on toy datasets
Statistical comparison of models using grid search
Compare Stochastic learning strategies for MLPClassifier
Varying regularization in Multi-layer Perceptron
Feature discretization


Generate a random multilabel classification problem.
In the above process, rejection sampling is used to make sure that n is never zero or more than n_classes, and that the document length is never zero. Likewise, we reject classes which have already been chosen.
Read more in the User Guide.
The number of samples.
The total number of features.
The number of classes of the classification problem.
The average number of labels per instance. More precisely, the number of labels per sample is drawn from a Poisson distribution with n_labels as its expected value, but samples are bounded (using rejection sampling) by n_classes, and must be nonzero if allow_unlabeled is False.
The sum of the features (number of words if documents) is drawn from a Poisson distribution with this expected value.
If True, some instances might not belong to any class.
If True, return a sparse feature matrix
New in version 0.17: parameter to allow sparse output.
If 'dense' return Y in the dense binary indicator format. If 'sparse' return Y in the sparse binary indicator format. False returns a list of lists of labels.
If True, return the prior class probability and conditional probabilities of features given classes, from which the data was drawn.
Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.
The generated samples.
The label sets. Sparse matrix should be of CSR format.
The probability of each class being drawn. Only returned if return_distributions=True.
The probability of each feature being drawn given each class. Only returned if return_distributions=True.
Plot randomly generated multilabel dataset
Multilabel classification


Generate a random regression problem.
The input set can either be well conditioned (by default) or have a low rank-fat tail singular profile. See make_low_rank_matrix for more details.
The output is generated by applying a (potentially biased) random linear regression model with n_informative nonzero regressors to the previously generated input and some gaussian centered noise with some adjustable scale.
Read more in the User Guide.
The number of samples.
The number of features.
The number of informative features, i.e., the number of features used to build the linear model used to generate the output.
The number of regression targets, i.e., the dimension of the y output vector associated with a sample. By default, the output is a scalar.
The bias term in the underlying linear model.
The approximate number of singular vectors required to explain most of the input data by linear combinations. Using this kind of singular spectrum in the input allows the generator to reproduce the correlations often observed in practice.
The input set is well conditioned, centered and gaussian with unit variance.
The relative importance of the fat noisy tail of the singular values profile if effective_rank is not None. When a float, it should be between 0 and 1.
The standard deviation of the gaussian noise applied to the output.
Shuffle the samples and the features.
If True, the coefficients of the underlying linear model are returned.
Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.
The input samples.
The output values.
The coefficient of the underlying linear model. It is returned only if coef is True.
Release Highlights for scikit-learn 0.23
Prediction Latency
Plot Ridge coefficients as a function of the L2 regularization
Robust linear model estimation using RANSAC
HuberRegressor vs Ridge on dataset with strong outliers
Lasso on dense and sparse data
Effect of transforming the targets in regression model


Generate a signal as a sparse combination of dictionary elements.
Returns a matrix Y = DX, such as D is (n_features, n_components), X is (n_components, n_samples) and each column of X has exactly n_nonzero_coefs non-zero elements.
Read more in the User Guide.
Number of samples to generate
Number of components in the dictionary
Number of features of the dataset to generate
Number of active (non-zero) coefficients in each sample
Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.
The encoded signal (Y).
The dictionary with normalized components (D).
The sparse code such that each column of this matrix has exactly n_nonzero_coefs non-zero items (X).
Orthogonal Matching Pursuit


Generate a sparse symmetric definite positive matrix.
Read more in the User Guide.
The size of the random matrix to generate.
The probability that a coefficient is zero (see notes). Larger values enforce more sparsity. The value should be in the range 0 and 1.
Whether to normalize the output matrix to make the leading diagonal elements all 1
The value of the smallest coefficient between 0 and 1.
The value of the largest coefficient between 0 and 1.
Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.
The generated matrix.
See also
The sparsity is actually imposed on the cholesky factor of the matrix. Thus alpha does not translate directly into the filling fraction of the matrix itself.
Sparse inverse covariance estimation


Generate a random regression problem with sparse uncorrelated design.
This dataset is described in Celeux et al [1]. as:
Only the first 4 features are informative. The remaining features are useless.
Read more in the User Guide.
The number of samples.
The number of features.
Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.
The input samples.
The output values.
G. Celeux, M. El Anbari, J.-M. Marin, C. P. Robert, “Regularization in regression: comparing Bayesian and frequentist methods in a poorly informative situation”, 2009.


Generate a random symmetric, positive-definite matrix.
Read more in the User Guide.
The matrix dimension.
Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.
The random symmetric, positive-definite matrix.
See also


Generate a swiss roll dataset.
Read more in the User Guide.
The number of sample points on the S curve.
The standard deviation of the gaussian noise.
Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.
The points.
The univariate position of the sample according to the main dimension of the points in the manifold.
The algorithm is from Marsland [1].
S. Marsland, “Machine Learning: An Algorithmic Perspective”, Chapter 10, 2009. http://seat.massey.ac.nz/personal/s.r.marsland/Code/10/lle.py
Hierarchical clustering: structured vs unstructured ward
Swiss Roll reduction with LLE


Generate an S curve dataset.
Read more in the User Guide.
The number of sample points on the S curve.
The standard deviation of the gaussian noise.
Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. See Glossary.
The points.
The univariate position of the sample according to the main dimension of the points in the manifold.
Comparison of Manifold Learning methods
t-SNE: The effect of various perplexity values on the shape


Solves a dictionary learning matrix factorization problem.
Finds the best dictionary and the corresponding sparse code for approximating the data matrix X by solving:
where V is the dictionary and U is the sparse code.
Read more in the User Guide.
Data matrix.
Number of dictionary atoms to extract.
Sparsity controlling parameter.
Maximum number of iterations to perform.
Tolerance for the stopping condition.
The method used:
problem (linear_model.lars_path);
Number of parallel jobs to run. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
Initial value for the dictionary for warm restart scenarios.
Initial value for the sparse code for warm restart scenarios.
Callable that gets invoked every five iterations
To control the verbosity of the procedure.
Used for randomly initializing the dictionary. Pass an int for reproducible results across multiple function calls. See Glossary.
Whether or not to return the number of iterations.
Whether to enforce positivity when finding the dictionary.
New in version 0.20.
Whether to enforce positivity when finding the code.
New in version 0.20.
Maximum number of iterations to perform.
New in version 0.22.
The sparse code factor in the matrix factorization.
The dictionary factor in the matrix factorization.
Vector of errors at each iteration.
Number of iterations run. Returned only if return_n_iter is set to True.
See also


Solves a dictionary learning matrix factorization problem online.
Finds the best dictionary and the corresponding sparse code for approximating the data matrix X by solving:
where V is the dictionary and U is the sparse code. This is accomplished by repeatedly iterating over mini-batches by slicing the input data.
Read more in the User Guide.
Data matrix.
Number of dictionary atoms to extract.
Sparsity controlling parameter.
Number of mini-batch iterations to perform.
Whether to also return the code U or just the dictionary V.
Initial value for the dictionary for warm restart scenarios.
callable that gets invoked every five iterations.
The number of samples to take in each batch.
To control the verbosity of the procedure.
Whether to shuffle the data before splitting it in batches.
Number of parallel jobs to run. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
Number of previous iterations completed on the dictionary used for initialization.
Used for initializing the dictionary when dict_init is not specified, randomly shuffling the data when shuffle is set to True, and updating the dictionary. Pass an int for reproducible results across multiple function calls. See Glossary.
Return the inner statistics A (dictionary covariance) and B (data approximation). Useful to restart the algorithm in an online setting. If return_inner_stats is True, return_code is ignored.
Inner sufficient statistics that are kept by the algorithm. Passing them at initialization is useful in online settings, to avoid losing the history of the evolution. A (n_components, n_components) is the dictionary covariance matrix. B (n_features, n_components) is the data approximation matrix.
Whether or not to return the number of iterations.
Whether to enforce positivity when finding the dictionary.
New in version 0.20.
Whether to enforce positivity when finding the code.
New in version 0.20.
Maximum number of iterations to perform when solving the lasso problem.
New in version 0.22.
The sparse code (only returned if return_code=True).
The solutions to the dictionary learning problem.
Number of iterations run. Returned only if return_n_iter is set to True.
See also


Perform Fast Independent Component Analysis.
Read more in the User Guide.
Training vector, where n_samples is the number of samples and n_features is the number of features.
Number of components to extract. If None no dimension reduction is performed.
Apply a parallel or deflational FASTICA algorithm.
If True perform an initial whitening of the data. If False, the data is assumed to have already been preprocessed: it should be centered, normed and white. Otherwise you will get incorrect results. In this case the parameter n_components will be ignored.
The functional form of the G function used in the approximation to neg-entropy. Could be either ‘logcosh’, ‘exp’, or ‘cube’. You can also provide your own function. It should return a tuple containing the value of the function, and of its derivative, in the point. The derivative should be averaged along its last dimension. Example:
return x ** 3, np.mean(3 * x ** 2, axis=-1)
Arguments to send to the functional form. If empty or None and if fun=’logcosh’, fun_args will take value {‘alpha’ : 1.0}
Maximum number of iterations to perform.
A positive scalar giving the tolerance at which the un-mixing matrix is considered to have converged.
Initial un-mixing array of dimension (n.comp,n.comp). If None (default) then an array of normal r.v.’s is used.
Used to initialize w_init when not specified, with a normal distribution. Pass an int, for reproducible results across multiple function calls. See Glossary.
If True, X_mean is returned too.
If False, sources are not computed, but only the rotation matrix. This can save memory when working with big data. Defaults to True.
Whether or not to return the number of iterations.
If whiten is ‘True’, K is the pre-whitening matrix that projects data onto the first n_components principal components. If whiten is ‘False’, K is ‘None’.
The square matrix that unmixes the data after whitening. The mixing matrix is the pseudo-inverse of matrix W K if K is not None, else it is the inverse of W.
Estimated source matrix
The mean over features. Returned only if return_X_mean is True.
If the algorithm is “deflation”, n_iter is the maximum number of iterations run across all components. Else they are just the number of iterations taken to converge. This is returned only when return_n_iter is set to True.
The data matrix X is considered to be a linear combination of non-Gaussian (independent) components i.e. X = AS where columns of S contain the independent components and A is a linear mixing matrix. In short ICA attempts to un-mix' the data by estimating an
un-mixing matrix W where ``S = W K X.` While FastICA was proposed to estimate as many sources as features, it is possible to estimate less by setting n_components < n_features. It this case K is not a square matrix and the estimated A is the pseudo-inverse of W K.
This implementation was originally made for data of shape [n_features, n_samples]. Now the input is transposed before the algorithm is applied. This makes it slightly faster for Fortran-ordered input.
Implemented using FastICA: A. Hyvarinen and E. Oja, Independent Component Analysis: Algorithms and Applications, Neural Networks, 13(4-5), 2000, pp. 411-430