sklearn.cluster.affinity_propagation
sklearn.cluster.affinity_propagation(S, *, preference=None, convergence_iter=15, max_iter=200, damping=0.5, copy=True, verbose=False, return_n_iter=False, random_state='warn') [source]

Perform Affinity Propagation Clustering of data. Read more in the User Guide.  Parameters

Sarray-like of shape (n_samples, n_samples)

Matrix of similarities between points.
preferencearray-like of shape (n_samples,) or float, default=None

Preferences for each point - points with larger values of preferences are more likely to be chosen as exemplars. The number of exemplars, i.e. of clusters, is influenced by the input preferences value. If the preferences are not passed as arguments, they will be set to the median of the input similarities (resulting in a moderate number of clusters). For a smaller amount of clusters, this can be set to the minimum value of the similarities.
convergence_iterint, default=15

Number of iterations with no change in the number of estimated clusters that stops the convergence.
max_iterint, default=200

Maximum number of iterations
dampingfloat, default=0.5

Damping factor between 0.5 and 1.
copybool, default=True

If copy is False, the affinity matrix is modified inplace by the algorithm, for memory efficiency.
verbosebool, default=False

The verbosity level.
return_n_iterbool, default=False

Whether or not to return the number of iterations.
random_stateint, RandomState instance or None, default=0

Pseudo-random number generator to control the starting state. Use an int for reproducible results across function calls. See the Glossary.  New in version 0.23: this parameter was previously hardcoded as 0.     Returns

cluster_centers_indicesndarray of shape (n_clusters,)

Index of clusters centers.
labelsndarray of shape (n_samples,)

Cluster labels for each point.
n_iterint

Number of iterations run. Returned only if return_n_iter is set to True.     Notes For an example, see examples/cluster/plot_affinity_propagation.py. When the algorithm does not converge, it returns an empty array as cluster_center_indices and -1 as label for each training sample. When all training samples have equal similarities and equal preferences, the assignment of cluster centers and labels depends on the preference. If the preference is smaller than the similarities, a single cluster center and label 0 for every sample will be returned. Otherwise, every training sample becomes its own cluster center and is assigned a unique label. References Brendan J. Frey and Delbert Dueck, “Clustering by Passing Messages Between Data Points”, Science Feb. 2007
 Examples using sklearn.cluster.affinity_propagation

  Visualizing the stock market structure


sklearn.cluster.cluster_optics_dbscan
sklearn.cluster.cluster_optics_dbscan(*, reachability, core_distances, ordering, eps) [source]

Performs DBSCAN extraction for an arbitrary epsilon. Extracting the clusters runs in linear time. Note that this results in labels_ which are close to a DBSCAN with similar settings and eps, only if eps is close to max_eps.  Parameters

reachabilityarray of shape (n_samples,)

Reachability distances calculated by OPTICS (reachability_)
core_distancesarray of shape (n_samples,)

Distances at which points become core (core_distances_)
orderingarray of shape (n_samples,)

OPTICS ordered point indices (ordering_)
epsfloat

DBSCAN eps parameter. Must be set to < max_eps. Results will be close to DBSCAN algorithm if eps and max_eps are close to one another.    Returns

labels_array of shape (n_samples,)

The estimated labels.
 Examples using sklearn.cluster.cluster_optics_dbscan

  Demo of OPTICS clustering algorithm


sklearn.cluster.cluster_optics_xi
sklearn.cluster.cluster_optics_xi(*, reachability, predecessor, ordering, min_samples, min_cluster_size=None, xi=0.05, predecessor_correction=True) [source]

Automatically extract clusters according to the Xi-steep method.  Parameters

reachabilityndarray of shape (n_samples,)

Reachability distances calculated by OPTICS (reachability_)
predecessorndarray of shape (n_samples,)

Predecessors calculated by OPTICS.
orderingndarray of shape (n_samples,)

OPTICS ordered point indices (ordering_)
min_samplesint > 1 or float between 0 and 1

The same as the min_samples given to OPTICS. Up and down steep regions can’t have more then min_samples consecutive non-steep points. Expressed as an absolute number or a fraction of the number of samples (rounded to be at least 2).
min_cluster_sizeint > 1 or float between 0 and 1, default=None

Minimum number of samples in an OPTICS cluster, expressed as an absolute number or a fraction of the number of samples (rounded to be at least 2). If None, the value of min_samples is used instead.
xifloat between 0 and 1, default=0.05

Determines the minimum steepness on the reachability plot that constitutes a cluster boundary. For example, an upwards point in the reachability plot is defined by the ratio from one point to its successor being at most 1-xi.
predecessor_correctionbool, default=True

Correct clusters based on the calculated predecessors.    Returns

labelsndarray of shape (n_samples,)

The labels assigned to samples. Points which are not included in any cluster are labeled as -1.
clustersndarray of shape (n_clusters, 2)

The list of clusters in the form of [start, end] in each row, with all indices inclusive. The clusters are ordered according to (end,
-start) (ascending) so that larger clusters encompassing smaller clusters come after such nested smaller clusters. Since labels does not reflect the hierarchy, usually len(clusters) >
np.unique(labels).


sklearn.cluster.compute_optics_graph
sklearn.cluster.compute_optics_graph(X, *, min_samples, max_eps, metric, p, metric_params, algorithm, leaf_size, n_jobs) [source]

Computes the OPTICS reachability graph. Read more in the User Guide.  Parameters

Xndarray of shape (n_samples, n_features), or (n_samples, n_samples) if metric=’precomputed’.

A feature array, or array of distances between samples if metric=’precomputed’
min_samplesint > 1 or float between 0 and 1

The number of samples in a neighborhood for a point to be considered as a core point. Expressed as an absolute number or a fraction of the number of samples (rounded to be at least 2).
max_epsfloat, default=np.inf

The maximum distance between two samples for one to be considered as in the neighborhood of the other. Default value of np.inf will identify clusters across all scales; reducing max_eps will result in shorter run times.
metricstr or callable, default=’minkowski’

Metric to use for distance computation. Any metric from scikit-learn or scipy.spatial.distance can be used. If metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays as input and return one value indicating the distance between them. This works for Scipy’s metrics, but is less efficient than passing the metric name as a string. If metric is “precomputed”, X is assumed to be a distance matrix and must be square. Valid values for metric are:  from scikit-learn: [‘cityblock’, ‘cosine’, ‘euclidean’, ‘l1’, ‘l2’, ‘manhattan’] from scipy.spatial.distance: [‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘correlation’, ‘dice’, ‘hamming’, ‘jaccard’, ‘kulsinski’, ‘mahalanobis’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘yule’]  See the documentation for scipy.spatial.distance for details on these metrics.
pint, default=2

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

Additional keyword arguments for the metric function.
algorithm{‘auto’, ‘ball_tree’, ‘kd_tree’, ‘brute’}, default=’auto’

Algorithm used to compute the nearest neighbors:  ‘ball_tree’ will use BallTree
 ‘kd_tree’ will use KDTree
 ‘brute’ will use a brute-force search. ‘auto’ will attempt to decide the most appropriate algorithm based on the values passed to fit method. (default)  Note: fitting on sparse input will override the setting of this parameter, using brute force.
leaf_sizeint, default=30

Leaf size passed to BallTree or KDTree. 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.
n_jobsint, default=None

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

ordering_array of shape (n_samples,)

The cluster ordered list of sample indices.
core_distances_array of shape (n_samples,)

Distance at which each sample becomes a core point, indexed by object order. Points which will never be core have a distance of inf. Use clust.core_distances_[clust.ordering_] to access in cluster order.
reachability_array of shape (n_samples,)

Reachability distances per sample, indexed by object order. Use clust.reachability_[clust.ordering_] to access in cluster order.
predecessor_array of shape (n_samples,)

Point that a sample was reached from, indexed by object order. Seed points have a predecessor of -1.     References
1
Ankerst, Mihael, Markus M. Breunig, Hans-Peter Kriegel, and Jörg Sander. “OPTICS: ordering points to identify the clustering structure.” ACM SIGMOD Record 28, no. 2 (1999): 49-60.


sklearn.cluster.dbscan
sklearn.cluster.dbscan(X, eps=0.5, *, min_samples=5, metric='minkowski', metric_params=None, algorithm='auto', leaf_size=30, p=2, sample_weight=None, n_jobs=None) [source]

Perform DBSCAN clustering from vector array or distance matrix. Read more in the User Guide.  Parameters

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

A feature array, or array of distances between samples if metric='precomputed'.
epsfloat, default=0.5

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.
min_samplesint, default=5

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.
metricstr or callable, default=’minkowski’

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.
metric_paramsdict, default=None

Additional keyword arguments for the metric function.  New in version 0.19.
algorithm{‘auto’, ‘ball_tree’, ‘kd_tree’, ‘brute’}, default=’auto’

The algorithm to be used by the NearestNeighbors module to compute pointwise distances and find nearest neighbors. See NearestNeighbors module documentation for details.
leaf_sizeint, default=30

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.
pfloat, default=2

The power of the Minkowski metric to be used to calculate distance between points.
sample_weightarray-like of shape (n_samples,), default=None

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.
n_jobsint, default=None

The number of parallel jobs to run for neighbors search. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details. If precomputed distance are used, parallel execution is not available and thus n_jobs will have no effect.    Returns

core_samplesndarray of shape (n_core_samples,)

Indices of core samples.
labelsndarray of shape (n_samples,)

Cluster labels for each point. Noisy samples are given the label -1.      See also
DBSCAN

An estimator interface for this clustering algorithm.
OPTICS

A similar estimator interface clustering at multiple values of eps. Our implementation is optimized for memory usage.    Notes 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. References 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.


sklearn.cluster.estimate_bandwidth
sklearn.cluster.estimate_bandwidth(X, *, quantile=0.3, n_samples=None, random_state=0, n_jobs=None) [source]

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.  Parameters

Xarray-like of shape (n_samples, n_features)

Input points.
quantilefloat, default=0.3

should be between [0, 1] 0.5 means that the median of all pairwise distances is used.
n_samplesint, default=None

The number of samples to use. If not given, all samples are used.
random_stateint, RandomState instance, default=None

The generator used to randomly select the samples from input points for bandwidth estimation. Use an int to make the randomness deterministic. See Glossary.
n_jobsint, default=None

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

bandwidthfloat

The bandwidth parameter.
 Examples using sklearn.cluster.estimate_bandwidth

  A demo of the mean-shift clustering algorithm

  Comparing different clustering algorithms on toy datasets


sklearn.cluster.kmeans_plusplus
sklearn.cluster.kmeans_plusplus(X, n_clusters, *, x_squared_norms=None, random_state=None, n_local_trials=None) [source]

Init n_clusters seeds according to k-means++  New in version 0.24.   Parameters

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

The data to pick seeds from.
n_clustersint

The number of centroids to initialize
x_squared_normsarray-like of shape (n_samples,), default=None

Squared Euclidean norm of each data point.
random_stateint or RandomState instance, default=None

Determines random number generation for centroid initialization. Pass an int for reproducible output across multiple function calls. See Glossary.
n_local_trialsint, default=None

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)).    Returns

centersndarray of shape (n_clusters, n_features)

The inital centers for k-means.
indicesndarray of shape (n_clusters,)

The index location of the chosen centers in the data array X. For a given index and center, X[index] = center.     Notes 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 Examples >>> from sklearn.cluster import kmeans_plusplus
>>> import numpy as np
>>> X = np.array([[1, 2], [1, 4], [1, 0],
...               [10, 2], [10, 4], [10, 0]])
>>> centers, indices = kmeans_plusplus(X, n_clusters=2, random_state=0)
>>> centers
array([[10,  4],
       [ 1,  0]])
>>> indices
array([4, 2])

 Examples using sklearn.cluster.kmeans_plusplus

  An example of K-Means++ initialization


sklearn.cluster.k_means
sklearn.cluster.k_means(X, n_clusters, *, sample_weight=None, init='k-means++', precompute_distances='deprecated', n_init=10, max_iter=300, verbose=False, tol=0.0001, random_state=None, copy_x=True, n_jobs='deprecated', algorithm='auto', return_n_iter=False) [source]

K-means clustering algorithm. Read more in the User Guide.  Parameters

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

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.
n_clustersint

The number of clusters to form as well as the number of centroids to generate.
sample_weightarray-like of shape (n_samples,), default=None

The weights for each observation in X. If None, all observations are assigned equal weight.
init{‘k-means++’, ‘random’}, callable or array-like of shape (n_clusters, n_features), default=’k-means++’

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{‘auto’, True, False}

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.
n_initint, default=10

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.
max_iterint, default=300

Maximum number of iterations of the k-means algorithm to run.
verbosebool, default=False

Verbosity mode.
tolfloat, default=1e-4

Relative tolerance with regards to Frobenius norm of the difference in the cluster centers of two consecutive iterations to declare convergence.
random_stateint, RandomState instance or None, default=None

Determines random number generation for centroid initialization. Use an int to make the randomness deterministic. See Glossary.
copy_xbool, default=True

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.
n_jobsint, default=None

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).
algorithm{“auto”, “full”, “elkan”}, default=”auto”

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.
return_n_iterbool, default=False

Whether or not to return the number of iterations.    Returns

centroidndarray of shape (n_clusters, n_features)

Centroids found at the last iteration of k-means.
labelndarray of shape (n_samples,)

label[i] is the code or index of the centroid the i’th observation is closest to.
inertiafloat

The final value of the inertia criterion (sum of squared distances to the closest centroid for all observations in the training set).
best_n_iterint

Number of iterations corresponding to the best results. Returned only if return_n_iter is set to True.


sklearn.cluster.mean_shift
sklearn.cluster.mean_shift(X, *, bandwidth=None, seeds=None, bin_seeding=False, min_bin_freq=1, cluster_all=True, max_iter=300, n_jobs=None) [source]

Perform mean shift clustering of data using a flat kernel. Read more in the User Guide.  Parameters

Xarray-like of shape (n_samples, n_features)

Input data.
bandwidthfloat, default=None

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.
seedsarray-like of shape (n_seeds, n_features) or None

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.
bin_seedingbool, default=False

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.
min_bin_freqint, default=1

To speed up the algorithm, accept only those bins with at least min_bin_freq points as seeds.
cluster_allbool, default=True

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.
max_iterint, default=300

Maximum number of iterations, per seed point before the clustering operation terminates (for that seed point), if has not converged yet.
n_jobsint, default=None

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.     Returns

cluster_centersndarray of shape (n_clusters, n_features)

Coordinates of cluster centers.
labelsndarray of shape (n_samples,)

Cluster labels for each point.     Notes For an example, see examples/cluster/plot_mean_shift.py.


sklearn.cluster.spectral_clustering
sklearn.cluster.spectral_clustering(affinity, *, n_clusters=8, n_components=None, eigen_solver=None, random_state=None, n_init=10, eigen_tol=0.0, assign_labels='kmeans', verbose=False) [source]

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.  Parameters

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

The affinity matrix describing the relationship of the samples to embed. Must be symmetric.  Possible examples:

 adjacency matrix of a graph, heat kernel of the pairwise distance matrix of the samples, symmetric k-nearest neighbours connectivity matrix of the samples.
n_clustersint, default=None

Number of clusters to extract.
n_componentsint, default=n_clusters

Number of eigen vectors to use for the spectral embedding
eigen_solver{None, ‘arpack’, ‘lobpcg’, or ‘amg’}

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

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.
n_initint, default=10

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.
eigen_tolfloat, default=0.0

Stopping criterion for eigendecomposition of the Laplacian matrix when using arpack eigen_solver.
assign_labels{‘kmeans’, ‘discretize’}, default=’kmeans’

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.
verbosebool, default=False

Verbosity mode.  New in version 0.24.     Returns

labelsarray of integers, shape: n_samples

The labels of the clusters.     Notes 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. References  Normalized cuts and image segmentation, 2000 Jianbo Shi, Jitendra Malik http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.160.2324
 A Tutorial on Spectral Clustering, 2007 Ulrike von Luxburg http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.165.9323
 Multiclass spectral clustering, 2003 Stella X. Yu, Jianbo Shi https://www1.icsi.berkeley.edu/~stellayu/publication/doc/2003kwayICCV.pdf

 Examples using sklearn.cluster.spectral_clustering

  Segmenting the picture of greek coins in regions

  Spectral clustering for image segmentation


sklearn.cluster.ward_tree
sklearn.cluster.ward_tree(X, *, connectivity=None, n_clusters=None, return_distance=False) [source]

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.  Parameters

Xarray-like of shape (n_samples, n_features)

feature matrix representing n_samples samples to be clustered
connectivitysparse matrix, default=None

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.
n_clustersint, default=None

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.
return_distancebool, default=None

If True, return the distance between the clusters.    Returns

childrenndarray of shape (n_nodes-1, 2)

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
n_connected_componentsint

The number of connected components in the graph.
n_leavesint

The number of leaves in the tree
parentsndarray of shape (n_nodes,) or None

The parent of each node. Only returned when a connectivity matrix is specified, elsewhere ‘None’ is returned.
distancesndarray of shape (n_nodes-1,)

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,  \[d(u,v) = \sqrt{\frac{|v|+|s|} {T}d(v,s)^2 + \frac{|v|+|t|} {T}d(v,t)^2 - \frac{|v|} {T}d(s,t)^2}\] 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.


sklearn.compose.make_column_selector
sklearn.compose.make_column_selector(pattern=None, *, dtype_include=None, dtype_exclude=None) [source]

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.  Parameters

patternstr, default=None

Name of columns containing this regex pattern will be included. If None, column selection will not be selected based on pattern.
dtype_includecolumn dtype or list of column dtypes, default=None

A selection of dtypes to include. For more details, see pandas.DataFrame.select_dtypes.
dtype_excludecolumn dtype or list of column dtypes, default=None

A selection of dtypes to exclude. For more details, see pandas.DataFrame.select_dtypes.    Returns

selectorcallable

Callable for column selection to be used by a ColumnTransformer.      See also
ColumnTransformer

Class that allows combining the outputs of multiple transformer objects used on column subsets of the data into a single feature space.    Examples >>> from sklearn.preprocessing import StandardScaler, OneHotEncoder
>>> from sklearn.compose import make_column_transformer
>>> from sklearn.compose import make_column_selector
>>> import pandas as pd
>>> X = pd.DataFrame({'city': ['London', 'London', 'Paris', 'Sallisaw'],
...                   'rating': [5, 3, 4, 5]})
>>> ct = make_column_transformer(
...       (StandardScaler(),
...        make_column_selector(dtype_include=np.number)),  # rating
...       (OneHotEncoder(),
...        make_column_selector(dtype_include=object)))  # city
>>> ct.fit_transform(X)
array([[ 0.90453403,  1.        ,  0.        ,  0.        ],
       [-1.50755672,  1.        ,  0.        ,  0.        ],
       [-0.30151134,  0.        ,  1.        ,  0.        ],
       [ 0.90453403,  0.        ,  0.        ,  1.        ]])

 Examples using sklearn.compose.make_column_selector

  Categorical Feature Support in Gradient Boosting

  Column Transformer with Mixed Types


sklearn.compose.make_column_transformer
sklearn.compose.make_column_transformer(*transformers, remainder='drop', sparse_threshold=0.3, n_jobs=None, verbose=False) [source]

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.  Parameters

*transformerstuples

Tuples of the form (transformer, columns) specifying the transformer objects to be applied to subsets of the data.
transformer{‘drop’, ‘passthrough’} or estimator

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.
columnsstr, array-like of str, int, array-like of int, slice, array-like of bool or callable

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.
remainder{‘drop’, ‘passthrough’} or estimator, default=’drop’

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.
sparse_thresholdfloat, default=0.3

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.
n_jobsint, default=None

Number of jobs to run in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.
verbosebool, default=False

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

ctColumnTransformer
    See also
ColumnTransformer

Class that allows combining the outputs of multiple transformer objects used on column subsets of the data into a single feature space.    Examples >>> from sklearn.preprocessing import StandardScaler, OneHotEncoder
>>> from sklearn.compose import make_column_transformer
>>> make_column_transformer(
...     (StandardScaler(), ['numerical_column']),
...     (OneHotEncoder(), ['categorical_column']))
ColumnTransformer(transformers=[('standardscaler', StandardScaler(...),
                                 ['numerical_column']),
                                ('onehotencoder', OneHotEncoder(...),
                                 ['categorical_column'])])

 Examples using sklearn.compose.make_column_transformer

  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


sklearn.config_context
sklearn.config_context(**new_config) [source]

Context manager for global scikit-learn configuration  Parameters

assume_finitebool, default=False

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.
working_memoryint, default=1024

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.
print_changed_onlybool, default=True

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.
display{‘text’, ‘diagram’}, default=’text’

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_config

Set global scikit-learn configuration.
get_config

Retrieve current values of the global configuration.    Notes 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. Examples >>> import sklearn
>>> from sklearn.utils.validation import assert_all_finite
>>> with sklearn.config_context(assume_finite=True):
...     assert_all_finite([float('nan')])
>>> with sklearn.config_context(assume_finite=True):
...     with sklearn.config_context(assume_finite=False):
...         assert_all_finite([float('nan')])
Traceback (most recent call last):
...
ValueError: Input contains NaN, ...


sklearn.covariance.empirical_covariance
sklearn.covariance.empirical_covariance(X, *, assume_centered=False) [source]

Computes the Maximum likelihood covariance estimator  Parameters

Xndarray of shape (n_samples, n_features)

Data from which to compute the covariance estimate
assume_centeredbool, default=False

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.    Returns

covariancendarray of shape (n_features, n_features)

Empirical covariance (Maximum Likelihood Estimator).     Examples >>> from sklearn.covariance import empirical_covariance
>>> X = [[1,1,1],[1,1,1],[1,1,1],
...      [0,0,0],[0,0,0],[0,0,0]]
>>> empirical_covariance(X)
array([[0.25, 0.25, 0.25],
       [0.25, 0.25, 0.25],
       [0.25, 0.25, 0.25]])

 Examples using sklearn.covariance.empirical_covariance

  Shrinkage covariance estimation: LedoitWolf vs OAS and max-likelihood


sklearn.covariance.graphical_lasso
sklearn.covariance.graphical_lasso(emp_cov, alpha, *, cov_init=None, mode='cd', tol=0.0001, enet_tol=0.0001, max_iter=100, verbose=False, return_costs=False, eps=2.220446049250313e-16, return_n_iter=False) [source]

l1-penalized covariance estimator Read more in the User Guide.  Changed in version v0.20: graph_lasso has been renamed to graphical_lasso   Parameters

emp_covndarray of shape (n_features, n_features)

Empirical covariance from which to compute the covariance estimate.
alphafloat

The regularization parameter: the higher alpha, the more regularization, the sparser the inverse covariance. Range is (0, inf].
cov_initarray of shape (n_features, n_features), default=None

The initial guess for the covariance. If None, then the empirical covariance is used.
mode{‘cd’, ‘lars’}, default=’cd’

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.
tolfloat, default=1e-4

The tolerance to declare convergence: if the dual gap goes below this value, iterations are stopped. Range is (0, inf].
enet_tolfloat, default=1e-4

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].
max_iterint, default=100

The maximum number of iterations.
verbosebool, default=False

If verbose is True, the objective function and dual gap are printed at each iteration.
return_costsbool, default=Flase

If return_costs is True, the objective function and dual gap at each iteration are returned.
epsfloat, default=eps

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.
return_n_iterbool, default=False

Whether or not to return the number of iterations.    Returns

covariancendarray of shape (n_features, n_features)

The estimated covariance matrix.
precisionndarray of shape (n_features, n_features)

The estimated (sparse) precision matrix.
costslist of (objective, dual_gap) pairs

The list of values of the objective function and the dual gap at each iteration. Returned only if return_costs is True.
n_iterint

Number of iterations. Returned only if return_n_iter is set to True.      See also
GraphicalLasso, GraphicalLassoCV

  Notes 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.


sklearn.covariance.ledoit_wolf
sklearn.covariance.ledoit_wolf(X, *, assume_centered=False, block_size=1000) [source]

Estimates the shrunk Ledoit-Wolf covariance matrix. Read more in the User Guide.  Parameters

Xarray-like of shape (n_samples, n_features)

Data from which to compute the covariance estimate
assume_centeredbool, default=False

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.
block_sizeint, default=1000

Size of blocks into which the covariance matrix will be split. This is purely a memory optimization and does not affect results.    Returns

shrunk_covndarray of shape (n_features, n_features)

Shrunk covariance.
shrinkagefloat

Coefficient in the convex combination used for the computation of the shrunk estimate.     Notes The regularized (shrunk) covariance is: (1 - shrinkage) * cov + shrinkage * mu * np.identity(n_features) where mu = trace(cov) / n_features
 Examples using sklearn.covariance.ledoit_wolf

  Sparse inverse covariance estimation


sklearn.covariance.oas
sklearn.covariance.oas(X, *, assume_centered=False) [source]

Estimate covariance with the Oracle Approximating Shrinkage algorithm.  Parameters

Xarray-like of shape (n_samples, n_features)

Data from which to compute the covariance estimate.
assume_centeredbool, default=False

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.    Returns

shrunk_covarray-like of shape (n_features, n_features)

Shrunk covariance.
shrinkagefloat

Coefficient in the convex combination used for the computation of the shrunk estimate.     Notes 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.


sklearn.covariance.shrunk_covariance
sklearn.covariance.shrunk_covariance(emp_cov, shrinkage=0.1) [source]

Calculates a covariance matrix shrunk on the diagonal Read more in the User Guide.  Parameters

emp_covarray-like of shape (n_features, n_features)

Covariance matrix to be shrunk
shrinkagefloat, default=0.1

Coefficient in the convex combination used for the computation of the shrunk estimate. Range is [0, 1].    Returns

shrunk_covndarray of shape (n_features, n_features)

Shrunk covariance.     Notes The regularized (shrunk) covariance is given by: (1 - shrinkage) * cov + shrinkage * mu * np.identity(n_features) where mu = trace(cov) / n_features


sklearn.datasets.clear_data_home
sklearn.datasets.clear_data_home(data_home=None) [source]

Delete all the content of the data home cache.  Parameters

data_homestr, default=None

The path to scikit-learn data directory. If None, the default path is ~/sklearn_learn_data.


sklearn.datasets.dump_svmlight_file
sklearn.datasets.dump_svmlight_file(X, y, f, *, zero_based=True, comment=None, query_id=None, multilabel=False) [source]

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.  Parameters

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

Training vectors, where n_samples is the number of samples and n_features is the number of features.
y{array-like, sparse matrix}, shape = [n_samples (, n_labels)]

Target values. Class labels must be an integer or float, or array-like objects of integer or float for multilabel classifications.
fstring or file-like in binary mode

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.
zero_basedboolean, default=True

Whether column indices should be written zero-based (True) or one-based (False).
commentstring, default=None

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.
query_idarray-like of shape (n_samples,), default=None

Array containing pairwise preference constraints (qid in svmlight format).
multilabelboolean, default=False

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.
 Examples using sklearn.datasets.dump_svmlight_file

  Libsvm GUI


sklearn.datasets.fetch_20newsgroups
sklearn.datasets.fetch_20newsgroups(*, data_home=None, subset='train', categories=None, shuffle=True, random_state=42, remove=(), download_if_missing=True, return_X_y=False) [source]

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.  Parameters

data_homestr, default=None

Specify a download and cache folder for the datasets. If None, all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
subset{‘train’, ‘test’, ‘all’}, default=’train’

Select the dataset to load: ‘train’ for the training set, ‘test’ for the test set, ‘all’ for both, with shuffled ordering.
categoriesarray-like, dtype=str or unicode, default=None

If None (default), load all the categories. If not None, list of category names to load (other categories ignored).
shufflebool, default=True

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.
random_stateint, RandomState instance or None, default=None

Determines random number generation for dataset shuffling. Pass an int for reproducible output across multiple function calls. See Glossary.
removetuple, default=()

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.
download_if_missingbool, default=True

If False, raise an IOError if the data is not locally available instead of trying to download the data from the source site.
return_X_ybool, default=False

If True, returns (data.data, data.target) instead of a Bunch object.  New in version 0.22.     Returns

bunchBunch

Dictionary-like object, with the following attributes.
datalist of shape (n_samples,)

The data list to learn.  target: ndarray of shape (n_samples,)

The target labels.  filenames: list of shape (n_samples,)

The path to the location of the data.  DESCR: str

The full description of the dataset.  target_names: list of shape (n_classes,)

The names of target classes.
(data, target)tuple if return_X_y=True

 New in version 0.22.
 Examples using sklearn.datasets.fetch_20newsgroups

  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


sklearn.datasets.fetch_20newsgroups_vectorized
sklearn.datasets.fetch_20newsgroups_vectorized(*, subset='train', remove=(), data_home=None, download_if_missing=True, return_X_y=False, normalize=True, as_frame=False) [source]

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.  Parameters

subset{‘train’, ‘test’, ‘all’}, default=’train’

Select the dataset to load: ‘train’ for the training set, ‘test’ for the test set, ‘all’ for both, with shuffled ordering.
removetuple, default=()

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.
data_homestr, default=None

Specify an download and cache folder for the datasets. If None, all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
download_if_missingbool, default=True

If False, raise an IOError if the data is not locally available instead of trying to download the data from the source site.
return_X_ybool, default=False

If True, returns (data.data, data.target) instead of a Bunch object.  New in version 0.20.
normalizebool, default=True

If True, normalizes each document’s feature vector to unit norm using sklearn.preprocessing.normalize.  New in version 0.22.
as_framebool, default=False

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.     Returns

bunchBunch

Dictionary-like object, with the following attributes.  data: {sparse matrix, dataframe} of shape (n_samples, n_features)

The input data matrix. If as_frame is True, data is a pandas DataFrame with sparse columns.  target: {ndarray, series} of shape (n_samples,)

The target labels. If as_frame is True, target is a pandas Series.  target_names: list of shape (n_classes,)

The names of target classes.  DESCR: str

The full description of the dataset.  frame: dataframe of shape (n_samples, n_features + 1)

Only present when as_frame=True. Pandas DataFrame with data and target.  New in version 0.24.
(data, target)tuple if return_X_y is True

data and target would be of the format defined in the Bunch description above.  New in version 0.20.
 Examples using sklearn.datasets.fetch_20newsgroups_vectorized

  Model Complexity Influence

  Multiclass sparse logistic regression on 20newgroups

  The Johnson-Lindenstrauss bound for embedding with random projections


sklearn.datasets.fetch_california_housing
sklearn.datasets.fetch_california_housing(*, data_home=None, download_if_missing=True, return_X_y=False, as_frame=False) [source]

Load the California housing dataset (regression).
Samples total 20640
Dimensionality 8
Features real
Target real 0.15 - 5.   Read more in the User Guide.  Parameters

data_homestr, default=None

Specify another download and cache folder for the datasets. By default all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
download_if_missingbool, default=True

If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site.
return_X_ybool, default=False.

If True, returns (data.data, data.target) instead of a Bunch object.  New in version 0.20.
as_framebool, default=False

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.     Returns

datasetBunch

Dictionary-like object, with the following attributes.
datandarray, shape (20640, 8)

Each row corresponding to the 8 feature values in order. If as_frame is True, data is a pandas object.
targetnumpy array of shape (20640,)

Each value corresponds to the average house value in units of 100,000. If as_frame is True, target is a pandas object.
feature_nameslist of length 8

Array of ordered feature names used in the dataset.
DESCRstring

Description of the California housing dataset.
framepandas DataFrame

Only present when as_frame=True. DataFrame with data and target.  New in version 0.23.
(data, target)tuple if return_X_y is True

 New in version 0.20.      Notes This dataset consists of 20,640 samples and 9 features.
 Examples using sklearn.datasets.fetch_california_housing

  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


sklearn.datasets.fetch_covtype
sklearn.datasets.fetch_covtype(*, data_home=None, download_if_missing=True, random_state=None, shuffle=False, return_X_y=False, as_frame=False) [source]

Load the covertype dataset (classification). Download it if necessary.
Classes 7
Samples total 581012
Dimensionality 54
Features int   Read more in the User Guide.  Parameters

data_homestr, default=None

Specify another download and cache folder for the datasets. By default all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
download_if_missingbool, default=True

If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site.
random_stateint, RandomState instance or None, default=None

Determines random number generation for dataset shuffling. Pass an int for reproducible output across multiple function calls. See Glossary.
shufflebool, default=False

Whether to shuffle dataset.
return_X_ybool, default=False

If True, returns (data.data, data.target) instead of a Bunch object.  New in version 0.20.
as_framebool, default=False

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.     Returns

datasetBunch

Dictionary-like object, with the following attributes.
datandarray of shape (581012, 54)

Each row corresponds to the 54 features in the dataset.
targetndarray of shape (581012,)

Each value corresponds to one of the 7 forest covertypes with values ranging between 1 to 7.
framedataframe of shape (581012, 53)

Only present when as_frame=True. Contains data and target.
DESCRstr

Description of the forest covertype dataset.
feature_nameslist

The names of the dataset columns.  target_names: list

The names of the target columns.
(data, target)tuple if return_X_y is True

 New in version 0.20.
 Examples using sklearn.datasets.fetch_covtype

  Release Highlights for scikit-learn 0.24

  Scalable learning with polynomial kernel aproximation


sklearn.datasets.fetch_kddcup99
sklearn.datasets.fetch_kddcup99(*, subset=None, data_home=None, shuffle=False, random_state=None, percent10=True, download_if_missing=True, return_X_y=False, as_frame=False) [source]

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.   Parameters

subset{‘SA’, ‘SF’, ‘http’, ‘smtp’}, default=None

To return the corresponding classical subsets of kddcup 99. If None, return the entire kddcup 99 dataset.
data_homestr, default=None

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
shufflebool, default=False

Whether to shuffle dataset.
random_stateint, RandomState instance or None, default=None

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.
percent10bool, default=True

Whether to load only 10 percent of the data.
download_if_missingbool, default=True

If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site.
return_X_ybool, default=False

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.
as_framebool, default=False

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.     Returns

dataBunch

Dictionary-like object, with the following attributes.
data{ndarray, dataframe} of shape (494021, 41)

The data matrix to learn. If as_frame=True, data will be a pandas DataFrame.
target{ndarray, series} of shape (494021,)

The regression target for each sample. If as_frame=True, target will be a pandas Series.
framedataframe of shape (494021, 42)

Only present when as_frame=True. Contains data and target.
DESCRstr

The full description of the dataset.
feature_nameslist

The names of the dataset columns  target_names: list

The names of the target columns
(data, target)tuple if return_X_y is True

 New in version 0.20.


sklearn.datasets.fetch_lfw_pairs
sklearn.datasets.fetch_lfw_pairs(*, subset='train', data_home=None, funneled=True, resize=0.5, color=False, slice_=slice(70, 195, None), slice(78, 172, None), download_if_missing=True) [source]

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.  Parameters

subset{‘train’, ‘test’, ‘10_folds’}, default=’train’

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.
data_homestr, default=None

Specify another download and cache folder for the datasets. By default all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
funneledbool, default=True

Download and use the funneled variant of the dataset.
resizefloat, default=0.5

Ratio used to resize the each face picture.
colorbool, default=False

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.
slice_tuple of slice, default=(slice(70, 195), slice(78, 172))

Provide a custom 2D slice (height, width) to extract the ‘interesting’ part of the jpeg files and avoid use statistical correlation from the background
download_if_missingbool, default=True

If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site.    Returns

dataBunch

Dictionary-like object, with the following attributes.
datandarray of shape (2200, 5828). Shape depends on subset.

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.
pairsndarray of shape (2200, 2, 62, 47). Shape depends on subset

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.
targetnumpy array of shape (2200,). Shape depends on subset.

Labels associated to each pair of images. The two label values being different persons or the same person.
DESCRstring

Description of the Labeled Faces in the Wild (LFW) dataset.


sklearn.datasets.fetch_lfw_people
sklearn.datasets.fetch_lfw_people(*, data_home=None, funneled=True, resize=0.5, min_faces_per_person=0, color=False, slice_=slice(70, 195, None), slice(78, 172, None), download_if_missing=True, return_X_y=False) [source]

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.  Parameters

data_homestr, default=None

Specify another download and cache folder for the datasets. By default all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
funneledbool, default=True

Download and use the funneled variant of the dataset.
resizefloat, default=0.5

Ratio used to resize the each face picture.
min_faces_per_personint, default=None

The extracted dataset will only retain pictures of people that have at least min_faces_per_person different pictures.
colorbool, default=False

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.
slice_tuple of slice, default=(slice(70, 195), slice(78, 172))

Provide a custom 2D slice (height, width) to extract the ‘interesting’ part of the jpeg files and avoid use statistical correlation from the background
download_if_missingbool, default=True

If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site.
return_X_ybool, default=False

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.     Returns

datasetBunch

Dictionary-like object, with the following attributes.
datanumpy array of shape (13233, 2914)

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.
imagesnumpy array of shape (13233, 62, 47)

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.
targetnumpy array of shape (13233,)

Labels associated to each face image. Those labels range from 0-5748 and correspond to the person IDs.
DESCRstring

Description of the Labeled Faces in the Wild (LFW) dataset.
(data, target)tuple if return_X_y is True

 New in version 0.20.
 Examples using sklearn.datasets.fetch_lfw_people

  Faces recognition example using eigenfaces and SVMs


sklearn.datasets.fetch_olivetti_faces
sklearn.datasets.fetch_olivetti_faces(*, data_home=None, shuffle=False, random_state=0, download_if_missing=True, return_X_y=False) [source]

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.  Parameters

data_homestr, default=None

Specify another download and cache folder for the datasets. By default all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
shufflebool, default=False

If True the order of the dataset is shuffled to avoid having images of the same person grouped.
random_stateint, RandomState instance or None, default=0

Determines random number generation for dataset shuffling. Pass an int for reproducible output across multiple function calls. See Glossary.
download_if_missingbool, default=True

If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site.
return_X_ybool, default=False

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.     Returns

dataBunch

Dictionary-like object, with the following attributes.  data: ndarray, shape (400, 4096)

Each row corresponds to a ravelled face image of original size 64 x 64 pixels.
imagesndarray, shape (400, 64, 64)

Each row is a face image corresponding to one of the 40 subjects of the dataset.
targetndarray, shape (400,)

Labels associated to each face image. Those labels are ranging from 0-39 and correspond to the Subject IDs.
DESCRstr

Description of the modified Olivetti Faces Dataset.
(data, target)tuple if return_X_y=True

 New in version 0.22.
 Examples using sklearn.datasets.fetch_olivetti_faces

  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


sklearn.datasets.fetch_openml
sklearn.datasets.fetch_openml(name: Optional[str] = None, *, version: Union[str, int] = 'active', data_id: Optional[int] = None, data_home: Optional[str] = None, target_column: Optional[Union[str, List]] = 'default-target', cache: bool = True, return_X_y: bool = False, as_frame: Union[str, bool] = 'auto') [source]

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.   Parameters

namestr, default=None

String identifier of the dataset. Note that OpenML can have multiple datasets with the same name.
versionint or ‘active’, default=’active’

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.
data_idint, default=None

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.
data_homestr, default=None

Specify another download and cache folder for the data sets. By default all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
target_columnstr, list or None, default=’default-target’

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)
cachebool, default=True

Whether to cache downloaded datasets using joblib.
return_X_ybool, default=False

If True, returns (data, target) instead of a Bunch object. See below for more information about the data and target objects.
as_framebool or ‘auto’, default=’auto’

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.     Returns

dataBunch

Dictionary-like object, with the following attributes.
datanp.array, scipy.sparse.csr_matrix of floats, or pandas DataFrame

The feature matrix. Categorical features are encoded as ordinals.
targetnp.array, pandas Series or DataFrame

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.
DESCRstr

The full description of the dataset
feature_nameslist

The names of the dataset columns  target_names: list

The names of the target columns    New in version 0.22.
categoriesdict or None

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.
detailsdict

More metadata from OpenML
framepandas DataFrame

Only present when as_frame=True. DataFrame with data and target.
(data, target)tuple if return_X_y is True

 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)
 Examples using sklearn.datasets.fetch_openml

  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


sklearn.datasets.fetch_rcv1
sklearn.datasets.fetch_rcv1(*, data_home=None, subset='all', download_if_missing=True, random_state=None, shuffle=False, return_X_y=False) [source]

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.   Parameters

data_homestr, default=None

Specify another download and cache folder for the datasets. By default all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
subset{‘train’, ‘test’, ‘all’}, default=’all’

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.
download_if_missingbool, default=True

If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site.
random_stateint, RandomState instance or None, default=None

Determines random number generation for dataset shuffling. Pass an int for reproducible output across multiple function calls. See Glossary.
shufflebool, default=False

Whether to shuffle dataset.
return_X_ybool, default=False

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.     Returns

datasetBunch

Dictionary-like object, with the following attributes.
datasparse matrix of shape (804414, 47236), dtype=np.float64

The array has 0.16% of non zero values. Will be of CSR format.
targetsparse matrix of shape (804414, 103), dtype=np.uint8

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.
sample_idndarray of shape (804414,), dtype=np.uint32,

Identification number of each sample, as ordered in dataset.data.
target_namesndarray of shape (103,), dtype=object

Names of each target (RCV1 topics), as ordered in dataset.target.
DESCRstr

Description of the RCV1 dataset.
(data, target)tuple if return_X_y is True

 New in version 0.20.


sklearn.datasets.fetch_species_distributions
sklearn.datasets.fetch_species_distributions(*, data_home=None, download_if_missing=True) [source]

Loader for species distribution dataset from Phillips et. al. (2006) Read more in the User Guide.  Parameters

data_homestr, default=None

Specify another download and cache folder for the datasets. By default all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.
download_if_missingbool, default=True

If False, raise a IOError if the data is not locally available instead of trying to download the data from the source site.    Returns

dataBunch

Dictionary-like object, with the following attributes.
coveragesarray, shape = [14, 1592, 1212]

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.
trainrecord array, shape = (1624,)

The training points for the data. Each point has three fields:  train[‘species’] is the species name train[‘dd long’] is the longitude, in degrees train[‘dd lat’] is the latitude, in degrees
testrecord array, shape = (620,)

The test points for the data. Same format as the training data.
Nx, Nyintegers

The number of longitudes (x) and latitudes (y) in the grid
x_left_lower_corner, y_left_lower_cornerfloats

The (x,y) position of the lower-left corner, in degrees
grid_sizefloat

The spacing between points of the grid, in degrees       Notes This dataset represents the geographic distribution of species. The dataset is provided by Phillips et. al. (2006). The two species are:
“Bradypus variegatus” , the Brown-throated Sloth.
“Microryzomys minutus” , also known as the Forest Small Rice Rat, a rodent that lives in Peru, Colombia, Ecuador, Peru, and Venezuela. For an example of using this dataset with scikit-learn, see examples/applications/plot_species_distribution_modeling.py.  References
“Maximum entropy modeling of species geographic distributions” S. J. Phillips, R. P. Anderson, R. E. Schapire - Ecological Modelling, 190:231-259, 2006.
 Examples using sklearn.datasets.fetch_species_distributions

  Species distribution modeling

  Kernel Density Estimate of Species Distributions


sklearn.datasets.get_data_home
sklearn.datasets.get_data_home(data_home=None) → str[source]

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.  Parameters

data_homestr, default=None

The path to scikit-learn data directory. If None, the default path is ~/sklearn_learn_data.
 Examples using sklearn.datasets.get_data_home

  Out-of-core classification of text documents


sklearn.datasets.load_boston
sklearn.datasets.load_boston(*, return_X_y=False) [source]

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.  Parameters

return_X_ybool, default=False

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.     Returns

dataBunch

Dictionary-like object, with the following attributes.
datandarray of shape (506, 13)

The data matrix.
targetndarray of shape (506, )

The regression target.
filenamestr

The physical location of boston csv dataset.  New in version 0.20.
DESCRstr

The full description of the dataset.
feature_namesndarray

The names of features
(data, target)tuple if return_X_y is True

 New in version 0.18.      Notes  Changed in version 0.20: Fixed a wrong data point at [445, 0].  Examples >>> from sklearn.datasets import load_boston
>>> X, y = load_boston(return_X_y=True)
>>> print(X.shape)
(506, 13)


sklearn.datasets.load_breast_cancer
sklearn.datasets.load_breast_cancer(*, return_X_y=False, as_frame=False) [source]

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.  Parameters

return_X_ybool, default=False

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.
as_framebool, default=False

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.     Returns

dataBunch

Dictionary-like object, with the following attributes.
data{ndarray, dataframe} of shape (569, 30)

The data matrix. If as_frame=True, data will be a pandas DataFrame.  target: {ndarray, Series} of shape (569,)

The classification target. If as_frame=True, target will be a pandas Series.  feature_names: list

The names of the dataset columns.  target_names: list

The names of target classes.  frame: DataFrame of shape (569, 31)

Only present when as_frame=True. DataFrame with data and target.  New in version 0.23.   DESCR: str

The full description of the dataset.  filename: str

The path to the location of the data.  New in version 0.20.
(data, target)tuple if return_X_y is True

 New in version 0.18.   The copy of UCI ML Breast Cancer Wisconsin (Diagnostic) dataset is
downloaded from:
https://goo.gl/U2Uwz2
   Examples Let’s say you are interested in the samples 10, 50, and 85, and want to know their class name. >>> from sklearn.datasets import load_breast_cancer
>>> data = load_breast_cancer()
>>> data.target[[10, 50, 85]]
array([0, 1, 0])
>>> list(data.target_names)
['malignant', 'benign']

 Examples using sklearn.datasets.load_breast_cancer

  Post pruning decision trees with cost complexity pruning

  Permutation Importance with Multicollinear or Correlated Features

  Effect of varying threshold for self-training


sklearn.datasets.load_diabetes
sklearn.datasets.load_diabetes(*, return_X_y=False, as_frame=False) [source]

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.  Parameters

return_X_ybool, default=False.

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.
as_framebool, default=False

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.     Returns

dataBunch

Dictionary-like object, with the following attributes.
data{ndarray, dataframe} of shape (442, 10)

The data matrix. If as_frame=True, data will be a pandas DataFrame.  target: {ndarray, Series} of shape (442,)

The regression target. If as_frame=True, target will be a pandas Series.  feature_names: list

The names of the dataset columns.  frame: DataFrame of shape (442, 11)

Only present when as_frame=True. DataFrame with data and target.  New in version 0.23.   DESCR: str

The full description of the dataset.  data_filename: str

The path to the location of the data.  target_filename: str

The path to the location of the target.
(data, target)tuple if return_X_y is True

 New in version 0.18.
 Examples using sklearn.datasets.load_diabetes

  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


sklearn.datasets.load_digits
sklearn.datasets.load_digits(*, n_class=10, return_X_y=False, as_frame=False) [source]

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.  Parameters

n_classint, default=10

The number of classes to return. Between 0 and 10.
return_X_ybool, default=False

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.
as_framebool, default=False

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.     Returns

dataBunch

Dictionary-like object, with the following attributes.
data{ndarray, dataframe} of shape (1797, 64)

The flattened data matrix. If as_frame=True, data will be a pandas DataFrame.  target: {ndarray, Series} of shape (1797,)

The classification target. If as_frame=True, target will be a pandas Series.  feature_names: list

The names of the dataset columns.  target_names: list

The names of target classes.  New in version 0.20.   frame: DataFrame of shape (1797, 65)

Only present when as_frame=True. DataFrame with data and target.  New in version 0.23.   images: {ndarray} of shape (1797, 8, 8)

The raw image data.  DESCR: str

The full description of the dataset.
(data, target)tuple if return_X_y is True

 New in version 0.18.   This is a copy of the test set of the UCI ML hand-written digits datasets
https://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten+Digits
   Examples To load the data and visualize the images: >>> from sklearn.datasets import load_digits
>>> digits = load_digits()
>>> print(digits.data.shape)
(1797, 64)
>>> import matplotlib.pyplot as plt
>>> plt.gray()
>>> plt.matshow(digits.images[0])
>>> plt.show()

 Examples using sklearn.datasets.load_digits

  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


sklearn.datasets.load_files
sklearn.datasets.load_files(container_path, *, description=None, categories=None, load_content=True, shuffle=True, encoding=None, decode_error='strict', random_state=0) [source]

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:  container_folder/

 category_1_folder/

file_1.txt file_2.txt … file_42.txt  category_2_folder/

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.  Parameters

container_pathstr or unicode

Path to the main folder holding one subfolder per category
descriptionstr or unicode, default=None

A paragraph describing the characteristic of the dataset: its source, reference, etc.
categorieslist of str, default=None

If None (default), load all the categories. If not None, list of category names to load (other categories ignored).
load_contentbool, default=True

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.
shufflebool, default=True

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.
encodingstr, default=None

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.
decode_error{‘strict’, ‘ignore’, ‘replace’}, default=’strict’

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.
random_stateint, RandomState instance or None, default=0

Determines random number generation for dataset shuffling. Pass an int for reproducible output across multiple function calls. See Glossary.    Returns

dataBunch

Dictionary-like object, with the following attributes.
datalist of str

Only present when load_content=True. The raw text data to learn.
targetndarray

The target labels (integer index).
target_nameslist

The names of target classes.
DESCRstr

The full description of the dataset.  filenames: ndarray

The filenames holding the dataset.


sklearn.datasets.load_iris
sklearn.datasets.load_iris(*, return_X_y=False, as_frame=False) [source]

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.  Parameters

return_X_ybool, default=False

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.
as_framebool, default=False

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.     Returns

dataBunch

Dictionary-like object, with the following attributes.
data{ndarray, dataframe} of shape (150, 4)

The data matrix. If as_frame=True, data will be a pandas DataFrame.  target: {ndarray, Series} of shape (150,)

The classification target. If as_frame=True, target will be a pandas Series.  feature_names: list

The names of the dataset columns.  target_names: list

The names of target classes.  frame: DataFrame of shape (150, 5)

Only present when as_frame=True. DataFrame with data and target.  New in version 0.23.   DESCR: str

The full description of the dataset.  filename: str

The path to the location of the data.  New in version 0.20.
(data, target)tuple if return_X_y is True

 New in version 0.18.      Notes  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.  Examples Let’s say you are interested in the samples 10, 25, and 50, and want to know their class name. >>> from sklearn.datasets import load_iris
>>> data = load_iris()
>>> data.target[[10, 25, 50]]
array([0, 0, 1])
>>> list(data.target_names)
['setosa', 'versicolor', 'virginica']

 Examples using sklearn.datasets.load_iris

  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


sklearn.datasets.load_linnerud
sklearn.datasets.load_linnerud(*, return_X_y=False, as_frame=False) [source]

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.  Parameters

return_X_ybool, default=False

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.
as_framebool, default=False

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.     Returns

dataBunch

Dictionary-like object, with the following attributes.
data{ndarray, dataframe} of shape (20, 3)

The data matrix. If as_frame=True, data will be a pandas DataFrame.  target: {ndarray, dataframe} of shape (20, 3)

The regression targets. If as_frame=True, target will be a pandas DataFrame.  feature_names: list

The names of the dataset columns.  target_names: list

The names of the target columns.  frame: DataFrame of shape (20, 6)

Only present when as_frame=True. DataFrame with data and target.  New in version 0.23.   DESCR: str

The full description of the dataset.  data_filename: str

The path to the location of the data.  target_filename: str

The path to the location of the target.  New in version 0.20.
(data, target)tuple if return_X_y is True

 New in version 0.18.


sklearn.datasets.load_sample_image
sklearn.datasets.load_sample_image(image_name) [source]

Load the numpy array of a single sample image Read more in the User Guide.  Parameters

image_name{china.jpg, flower.jpg}

The name of the sample image loaded    Returns

img3D array

The image as a numpy array: height x width x color     Examples >>> from sklearn.datasets import load_sample_image
>>> china = load_sample_image('china.jpg')
>>> china.dtype
dtype('uint8')
>>> china.shape
(427, 640, 3)
>>> flower = load_sample_image('flower.jpg')
>>> flower.dtype
dtype('uint8')
>>> flower.shape
(427, 640, 3)

 Examples using sklearn.datasets.load_sample_image

  Color Quantization using K-Means


sklearn.datasets.load_sample_images
sklearn.datasets.load_sample_images() [source]

Load sample images for image manipulation. Loads both, china and flower. Read more in the User Guide.  Returns

dataBunch

Dictionary-like object, with the following attributes.
imageslist of ndarray of shape (427, 640, 3)

The two sample image.
filenameslist

The filenames for the images.
DESCRstr

The full description of the dataset.       Examples To load the data and visualize the images: >>> from sklearn.datasets import load_sample_images
>>> dataset = load_sample_images()
>>> len(dataset.images)
2
>>> first_img_data = dataset.images[0]
>>> first_img_data.shape
(427, 640, 3)
>>> first_img_data.dtype
dtype('uint8')