fit_transform(X, y=None, sample_weight=None) [source]

Compute clustering and transform X to cluster-distance space. Equivalent to fit(X).transform(X), but more efficiently implemented.  Parameters

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

New data to transform.
yIgnored

Not used, present here for API consistency by convention.
sample_weightarray-like of shape (n_samples,), default=None

The weights for each observation in X. If None, all observations are assigned equal weight.    Returns

X_newndarray of shape (n_samples, n_clusters)

X transformed in the new space.


get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.


partial_fit(X, y=None, sample_weight=None) [source]

Update k means estimate on a single mini-batch X.  Parameters

Xarray-like of shape (n_samples, n_features)

Coordinates of the data points to cluster. It must be noted that X will be copied if it is not C-contiguous.
yIgnored

Not used, present here for API consistency by convention.
sample_weightarray-like of shape (n_samples,), default=None

The weights for each observation in X. If None, all observations are assigned equal weight (default: None).    Returns
 self


predict(X, sample_weight=None) [source]

Predict the closest cluster each sample in X belongs to. In the vector quantization literature, cluster_centers_ is called the code book and each value returned by predict is the index of the closest code in the code book.  Parameters

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

New data to predict.
sample_weightarray-like of shape (n_samples,), default=None

The weights for each observation in X. If None, all observations are assigned equal weight (default: None).    Returns

labelsndarray of shape (n_samples,)

Index of the cluster each sample belongs to.


score(X, y=None, sample_weight=None) [source]

Opposite of the value of X on the K-means objective.  Parameters

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

New data.
yIgnored

Not used, present here for API consistency by convention.
sample_weightarray-like of shape (n_samples,), default=None

The weights for each observation in X. If None, all observations are assigned equal weight.    Returns

scorefloat

Opposite of the value of X on the K-means objective.


set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


transform(X) [source]

Transform X to a cluster-distance space. In the new space, each dimension is the distance to the cluster centers. Note that even if X is sparse, the array returned by transform will typically be dense.  Parameters

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

New data to transform.    Returns

X_newndarray of shape (n_samples, n_clusters)

X transformed in the new space.


class sklearn.cluster.OPTICS(*, min_samples=5, max_eps=inf, metric='minkowski', p=2, metric_params=None, cluster_method='xi', eps=None, xi=0.05, predecessor_correction=True, min_cluster_size=None, algorithm='auto', leaf_size=30, n_jobs=None) [source]

Estimate clustering structure from vector array. OPTICS (Ordering Points To Identify the Clustering Structure), closely related to DBSCAN, finds core sample of high density and expands clusters from them [1]. Unlike DBSCAN, keeps cluster hierarchy for a variable neighborhood radius. Better suited for usage on large datasets than the current sklearn implementation of DBSCAN. Clusters are then extracted using a DBSCAN-like method (cluster_method = ‘dbscan’) or an automatic technique proposed in [1] (cluster_method = ‘xi’). This implementation deviates from the original OPTICS by first performing k-nearest-neighborhood searches on all points to identify core sizes, then computing only the distances to unprocessed points when constructing the cluster order. Note that we do not employ a heap to manage the expansion candidates, so the time complexity will be O(n^2). Read more in the User Guide.  Parameters

min_samplesint > 1 or float between 0 and 1, default=5

The number of samples in a neighborhood for a point to be considered as a core point. Also, up and down steep regions can’t have more than 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).
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.
cluster_methodstr, default=’xi’

The extraction method used to extract clusters using the calculated reachability and ordering. Possible values are “xi” and “dbscan”.
epsfloat, default=None

The maximum distance between two samples for one to be considered as in the neighborhood of the other. By default it assumes the same value as max_eps. Used only when cluster_method='dbscan'.
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. Used only when cluster_method='xi'.
predecessor_correctionbool, default=True

Correct clusters according to the predecessors calculated by OPTICS [2]. This parameter has minimal effect on most datasets. Used only when cluster_method='xi'.
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. Used only when cluster_method='xi'.
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.    Attributes

labels_ndarray of shape (n_samples,)

Cluster labels for each point in the dataset given to fit(). Noisy samples and points which are not included in a leaf cluster of cluster_hierarchy_ are labeled as -1.
reachability_ndarray of shape (n_samples,)

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

The cluster ordered list of sample indices.
core_distances_ndarray 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.
predecessor_ndarray of shape (n_samples,)

Point that a sample was reached from, indexed by object order. Seed points have a predecessor of -1.
cluster_hierarchy_ndarray 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 those smaller ones. Since labels_ does not reflect the hierarchy, usually len(cluster_hierarchy_) > np.unique(optics.labels_). Please also note that these indices are of the ordering_, i.e. X[ordering_][start:end + 1] form a cluster. Only available when cluster_method='xi'.      See also
DBSCAN

A similar clustering for a specified neighborhood radius (eps). Our implementation is optimized for runtime.    References
1(1,2)
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.
2
Schubert, Erich, Michael Gertz. “Improving the Cluster Structure Extracted from OPTICS Plots.” Proc. of the Conference “Lernen, Wissen, Daten, Analysen” (LWDA) (2018): 318-329.   Examples >>> from sklearn.cluster import OPTICS
>>> import numpy as np
>>> X = np.array([[1, 2], [2, 5], [3, 6],
...               [8, 7], [8, 8], [7, 3]])
>>> clustering = OPTICS(min_samples=2).fit(X)
>>> clustering.labels_
array([0, 0, 0, 1, 1, 1])
 Methods
fit(X[, y]) Perform OPTICS clustering.
fit_predict(X[, y]) Perform clustering on X and returns cluster labels.
get_params([deep]) Get parameters for this estimator.
set_params(**params) Set the parameters of this estimator.
fit(X, y=None) [source]

Perform OPTICS clustering. Extracts an ordered list of points and reachability distances, and performs initial clustering using max_eps distance specified at OPTICS object instantiation.  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’.
yignored

Ignored.    Returns

selfinstance of OPTICS

The instance.

fit_predict(X, y=None) [source]

Perform clustering on X and returns cluster labels.  Parameters

Xarray-like of shape (n_samples, n_features)

Input data.
yIgnored

Not used, present for API consistency by convention.    Returns

labelsndarray of shape (n_samples,), dtype=np.int64

Cluster labels.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


sklearn.cluster.OPTICS
class sklearn.cluster.OPTICS(*, min_samples=5, max_eps=inf, metric='minkowski', p=2, metric_params=None, cluster_method='xi', eps=None, xi=0.05, predecessor_correction=True, min_cluster_size=None, algorithm='auto', leaf_size=30, n_jobs=None) [source]

Estimate clustering structure from vector array. OPTICS (Ordering Points To Identify the Clustering Structure), closely related to DBSCAN, finds core sample of high density and expands clusters from them [1]. Unlike DBSCAN, keeps cluster hierarchy for a variable neighborhood radius. Better suited for usage on large datasets than the current sklearn implementation of DBSCAN. Clusters are then extracted using a DBSCAN-like method (cluster_method = ‘dbscan’) or an automatic technique proposed in [1] (cluster_method = ‘xi’). This implementation deviates from the original OPTICS by first performing k-nearest-neighborhood searches on all points to identify core sizes, then computing only the distances to unprocessed points when constructing the cluster order. Note that we do not employ a heap to manage the expansion candidates, so the time complexity will be O(n^2). Read more in the User Guide.  Parameters

min_samplesint > 1 or float between 0 and 1, default=5

The number of samples in a neighborhood for a point to be considered as a core point. Also, up and down steep regions can’t have more than 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).
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.
cluster_methodstr, default=’xi’

The extraction method used to extract clusters using the calculated reachability and ordering. Possible values are “xi” and “dbscan”.
epsfloat, default=None

The maximum distance between two samples for one to be considered as in the neighborhood of the other. By default it assumes the same value as max_eps. Used only when cluster_method='dbscan'.
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. Used only when cluster_method='xi'.
predecessor_correctionbool, default=True

Correct clusters according to the predecessors calculated by OPTICS [2]. This parameter has minimal effect on most datasets. Used only when cluster_method='xi'.
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. Used only when cluster_method='xi'.
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.    Attributes

labels_ndarray of shape (n_samples,)

Cluster labels for each point in the dataset given to fit(). Noisy samples and points which are not included in a leaf cluster of cluster_hierarchy_ are labeled as -1.
reachability_ndarray of shape (n_samples,)

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

The cluster ordered list of sample indices.
core_distances_ndarray 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.
predecessor_ndarray of shape (n_samples,)

Point that a sample was reached from, indexed by object order. Seed points have a predecessor of -1.
cluster_hierarchy_ndarray 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 those smaller ones. Since labels_ does not reflect the hierarchy, usually len(cluster_hierarchy_) > np.unique(optics.labels_). Please also note that these indices are of the ordering_, i.e. X[ordering_][start:end + 1] form a cluster. Only available when cluster_method='xi'.      See also
DBSCAN

A similar clustering for a specified neighborhood radius (eps). Our implementation is optimized for runtime.    References
1(1,2)
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.
2
Schubert, Erich, Michael Gertz. “Improving the Cluster Structure Extracted from OPTICS Plots.” Proc. of the Conference “Lernen, Wissen, Daten, Analysen” (LWDA) (2018): 318-329.   Examples >>> from sklearn.cluster import OPTICS
>>> import numpy as np
>>> X = np.array([[1, 2], [2, 5], [3, 6],
...               [8, 7], [8, 8], [7, 3]])
>>> clustering = OPTICS(min_samples=2).fit(X)
>>> clustering.labels_
array([0, 0, 0, 1, 1, 1])
 Methods
fit(X[, y]) Perform OPTICS clustering.
fit_predict(X[, y]) Perform clustering on X and returns cluster labels.
get_params([deep]) Get parameters for this estimator.
set_params(**params) Set the parameters of this estimator.
fit(X, y=None) [source]

Perform OPTICS clustering. Extracts an ordered list of points and reachability distances, and performs initial clustering using max_eps distance specified at OPTICS object instantiation.  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’.
yignored

Ignored.    Returns

selfinstance of OPTICS

The instance.

fit_predict(X, y=None) [source]

Perform clustering on X and returns cluster labels.  Parameters

Xarray-like of shape (n_samples, n_features)

Input data.
yIgnored

Not used, present for API consistency by convention.    Returns

labelsndarray of shape (n_samples,), dtype=np.int64

Cluster labels.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

 Examples using sklearn.cluster.OPTICS

  Demo of OPTICS clustering algorithm

  Comparing different clustering algorithms on toy datasets


fit(X, y=None) [source]

Perform OPTICS clustering. Extracts an ordered list of points and reachability distances, and performs initial clustering using max_eps distance specified at OPTICS object instantiation.  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’.
yignored

Ignored.    Returns

selfinstance of OPTICS

The instance.


fit_predict(X, y=None) [source]

Perform clustering on X and returns cluster labels.  Parameters

Xarray-like of shape (n_samples, n_features)

Input data.
yIgnored

Not used, present for API consistency by convention.    Returns

labelsndarray of shape (n_samples,), dtype=np.int64

Cluster labels.


class sklearn.cluster.SpectralBiclustering(n_clusters=3, *, method='bistochastic', n_components=6, n_best=3, svd_method='randomized', n_svd_vecs=None, mini_batch=False, init='k-means++', n_init=10, n_jobs='deprecated', random_state=None) [source]

Spectral biclustering (Kluger, 2003). Partitions rows and columns under the assumption that the data has an underlying checkerboard structure. For instance, if there are two row partitions and three column partitions, each row will belong to three biclusters, and each column will belong to two biclusters. The outer product of the corresponding row and column label vectors gives this checkerboard structure. Read more in the User Guide.  Parameters

n_clustersint or tuple (n_row_clusters, n_column_clusters), default=3

The number of row and column clusters in the checkerboard structure.
method{‘bistochastic’, ‘scale’, ‘log’}, default=’bistochastic’

Method of normalizing and converting singular vectors into biclusters. May be one of ‘scale’, ‘bistochastic’, or ‘log’. The authors recommend using ‘log’. If the data is sparse, however, log normalization will not work, which is why the default is ‘bistochastic’.  Warning if method='log', the data must be sparse.
n_componentsint, default=6

Number of singular vectors to check.
n_bestint, default=3

Number of best singular vectors to which to project the data for clustering.
svd_method{‘randomized’, ‘arpack’}, default=’randomized’

Selects the algorithm for finding singular vectors. May be ‘randomized’ or ‘arpack’. If ‘randomized’, uses randomized_svd, which may be faster for large matrices. If ‘arpack’, uses scipy.sparse.linalg.svds, which is more accurate, but possibly slower in some cases.
n_svd_vecsint, default=None

Number of vectors to use in calculating the SVD. Corresponds to ncv when svd_method=arpack and n_oversamples when svd_method is ‘randomized`.
mini_batchbool, default=False

Whether to use mini-batch k-means, which is faster but may get different results.
init{‘k-means++’, ‘random’} or ndarray of (n_clusters, n_features), default=’k-means++’

Method for initialization of k-means algorithm; defaults to ‘k-means++’.
n_initint, default=10

Number of random initializations that are tried with the k-means algorithm. If mini-batch k-means is used, the best initialization is chosen and the algorithm runs once. Otherwise, the algorithm is run for each initialization and the best solution chosen.
n_jobsint, default=None

The number of jobs to use for the computation. This works by breaking down the pairwise matrix into n_jobs even slices and computing them in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.  Deprecated since version 0.23: n_jobs was deprecated in version 0.23 and will be removed in 1.0 (renaming of 0.25).
random_stateint, RandomState instance, default=None

Used for randomizing the singular value decomposition and the k-means initialization. Use an int to make the randomness deterministic. See Glossary.    Attributes

rows_array-like of shape (n_row_clusters, n_rows)

Results of the clustering. rows[i, r] is True if cluster i contains row r. Available only after calling fit.
columns_array-like of shape (n_column_clusters, n_columns)

Results of the clustering, like rows.
row_labels_array-like of shape (n_rows,)

Row partition labels.
column_labels_array-like of shape (n_cols,)

Column partition labels.     References  Kluger, Yuval, et. al., 2003. Spectral biclustering of microarray data: coclustering genes and conditions.  Examples >>> from sklearn.cluster import SpectralBiclustering
>>> import numpy as np
>>> X = np.array([[1, 1], [2, 1], [1, 0],
...               [4, 7], [3, 5], [3, 6]])
>>> clustering = SpectralBiclustering(n_clusters=2, random_state=0).fit(X)
>>> clustering.row_labels_
array([1, 1, 1, 0, 0, 0], dtype=int32)
>>> clustering.column_labels_
array([0, 1], dtype=int32)
>>> clustering
SpectralBiclustering(n_clusters=2, random_state=0)
 Methods
fit(X[, y]) Creates a biclustering for X.
get_indices(i) Row and column indices of the i’th bicluster.
get_params([deep]) Get parameters for this estimator.
get_shape(i) Shape of the i’th bicluster.
get_submatrix(i, data) Return the submatrix corresponding to bicluster i.
set_params(**params) Set the parameters of this estimator.
property biclusters_
Convenient way to get row and column indicators together. Returns the rows_ and columns_ members.

fit(X, y=None) [source]

Creates a biclustering for X.  Parameters

Xarray-like of shape (n_samples, n_features)

yIgnored

get_indices(i) [source]

Row and column indices of the i’th bicluster. Only works if rows_ and columns_ attributes exist.  Parameters

iint

The index of the cluster.    Returns

row_indndarray, dtype=np.intp

Indices of rows in the dataset that belong to the bicluster.
col_indndarray, dtype=np.intp

Indices of columns in the dataset that belong to the bicluster.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

get_shape(i) [source]

Shape of the i’th bicluster.  Parameters

iint

The index of the cluster.    Returns

n_rowsint

Number of rows in the bicluster.
n_colsint

Number of columns in the bicluster.

get_submatrix(i, data) [source]

Return the submatrix corresponding to bicluster i.  Parameters

iint

The index of the cluster.
dataarray-like of shape (n_samples, n_features)

The data.    Returns

submatrixndarray of shape (n_rows, n_cols)

The submatrix corresponding to bicluster i.     Notes Works with sparse matrices. Only works if rows_ and columns_ attributes exist.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


sklearn.cluster.SpectralBiclustering
class sklearn.cluster.SpectralBiclustering(n_clusters=3, *, method='bistochastic', n_components=6, n_best=3, svd_method='randomized', n_svd_vecs=None, mini_batch=False, init='k-means++', n_init=10, n_jobs='deprecated', random_state=None) [source]

Spectral biclustering (Kluger, 2003). Partitions rows and columns under the assumption that the data has an underlying checkerboard structure. For instance, if there are two row partitions and three column partitions, each row will belong to three biclusters, and each column will belong to two biclusters. The outer product of the corresponding row and column label vectors gives this checkerboard structure. Read more in the User Guide.  Parameters

n_clustersint or tuple (n_row_clusters, n_column_clusters), default=3

The number of row and column clusters in the checkerboard structure.
method{‘bistochastic’, ‘scale’, ‘log’}, default=’bistochastic’

Method of normalizing and converting singular vectors into biclusters. May be one of ‘scale’, ‘bistochastic’, or ‘log’. The authors recommend using ‘log’. If the data is sparse, however, log normalization will not work, which is why the default is ‘bistochastic’.  Warning if method='log', the data must be sparse.
n_componentsint, default=6

Number of singular vectors to check.
n_bestint, default=3

Number of best singular vectors to which to project the data for clustering.
svd_method{‘randomized’, ‘arpack’}, default=’randomized’

Selects the algorithm for finding singular vectors. May be ‘randomized’ or ‘arpack’. If ‘randomized’, uses randomized_svd, which may be faster for large matrices. If ‘arpack’, uses scipy.sparse.linalg.svds, which is more accurate, but possibly slower in some cases.
n_svd_vecsint, default=None

Number of vectors to use in calculating the SVD. Corresponds to ncv when svd_method=arpack and n_oversamples when svd_method is ‘randomized`.
mini_batchbool, default=False

Whether to use mini-batch k-means, which is faster but may get different results.
init{‘k-means++’, ‘random’} or ndarray of (n_clusters, n_features), default=’k-means++’

Method for initialization of k-means algorithm; defaults to ‘k-means++’.
n_initint, default=10

Number of random initializations that are tried with the k-means algorithm. If mini-batch k-means is used, the best initialization is chosen and the algorithm runs once. Otherwise, the algorithm is run for each initialization and the best solution chosen.
n_jobsint, default=None

The number of jobs to use for the computation. This works by breaking down the pairwise matrix into n_jobs even slices and computing them in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.  Deprecated since version 0.23: n_jobs was deprecated in version 0.23 and will be removed in 1.0 (renaming of 0.25).
random_stateint, RandomState instance, default=None

Used for randomizing the singular value decomposition and the k-means initialization. Use an int to make the randomness deterministic. See Glossary.    Attributes

rows_array-like of shape (n_row_clusters, n_rows)

Results of the clustering. rows[i, r] is True if cluster i contains row r. Available only after calling fit.
columns_array-like of shape (n_column_clusters, n_columns)

Results of the clustering, like rows.
row_labels_array-like of shape (n_rows,)

Row partition labels.
column_labels_array-like of shape (n_cols,)

Column partition labels.     References  Kluger, Yuval, et. al., 2003. Spectral biclustering of microarray data: coclustering genes and conditions.  Examples >>> from sklearn.cluster import SpectralBiclustering
>>> import numpy as np
>>> X = np.array([[1, 1], [2, 1], [1, 0],
...               [4, 7], [3, 5], [3, 6]])
>>> clustering = SpectralBiclustering(n_clusters=2, random_state=0).fit(X)
>>> clustering.row_labels_
array([1, 1, 1, 0, 0, 0], dtype=int32)
>>> clustering.column_labels_
array([0, 1], dtype=int32)
>>> clustering
SpectralBiclustering(n_clusters=2, random_state=0)
 Methods
fit(X[, y]) Creates a biclustering for X.
get_indices(i) Row and column indices of the i’th bicluster.
get_params([deep]) Get parameters for this estimator.
get_shape(i) Shape of the i’th bicluster.
get_submatrix(i, data) Return the submatrix corresponding to bicluster i.
set_params(**params) Set the parameters of this estimator.
property biclusters_
Convenient way to get row and column indicators together. Returns the rows_ and columns_ members.

fit(X, y=None) [source]

Creates a biclustering for X.  Parameters

Xarray-like of shape (n_samples, n_features)

yIgnored

get_indices(i) [source]

Row and column indices of the i’th bicluster. Only works if rows_ and columns_ attributes exist.  Parameters

iint

The index of the cluster.    Returns

row_indndarray, dtype=np.intp

Indices of rows in the dataset that belong to the bicluster.
col_indndarray, dtype=np.intp

Indices of columns in the dataset that belong to the bicluster.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

get_shape(i) [source]

Shape of the i’th bicluster.  Parameters

iint

The index of the cluster.    Returns

n_rowsint

Number of rows in the bicluster.
n_colsint

Number of columns in the bicluster.

get_submatrix(i, data) [source]

Return the submatrix corresponding to bicluster i.  Parameters

iint

The index of the cluster.
dataarray-like of shape (n_samples, n_features)

The data.    Returns

submatrixndarray of shape (n_rows, n_cols)

The submatrix corresponding to bicluster i.     Notes Works with sparse matrices. Only works if rows_ and columns_ attributes exist.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

 Examples using sklearn.cluster.SpectralBiclustering

  A demo of the Spectral Biclustering algorithm


property biclusters_
Convenient way to get row and column indicators together. Returns the rows_ and columns_ members.


fit(X, y=None) [source]

Creates a biclustering for X.  Parameters

Xarray-like of shape (n_samples, n_features)

yIgnored


get_indices(i) [source]

Row and column indices of the i’th bicluster. Only works if rows_ and columns_ attributes exist.  Parameters

iint

The index of the cluster.    Returns

row_indndarray, dtype=np.intp

Indices of rows in the dataset that belong to the bicluster.
col_indndarray, dtype=np.intp

Indices of columns in the dataset that belong to the bicluster.


get_shape(i) [source]

Shape of the i’th bicluster.  Parameters

iint

The index of the cluster.    Returns

n_rowsint

Number of rows in the bicluster.
n_colsint

Number of columns in the bicluster.


get_submatrix(i, data) [source]

Return the submatrix corresponding to bicluster i.  Parameters

iint

The index of the cluster.
dataarray-like of shape (n_samples, n_features)

The data.    Returns

submatrixndarray of shape (n_rows, n_cols)

The submatrix corresponding to bicluster i.     Notes Works with sparse matrices. Only works if rows_ and columns_ attributes exist.


class sklearn.cluster.SpectralClustering(n_clusters=8, *, eigen_solver=None, n_components=None, random_state=None, n_init=10, gamma=1.0, affinity='rbf', n_neighbors=10, eigen_tol=0.0, assign_labels='kmeans', degree=3, coef0=1, kernel_params=None, n_jobs=None, 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. When calling fit, an affinity matrix is constructed using either kernel function such the Gaussian (aka RBF) kernel of the euclidean distanced d(X, X): np.exp(-gamma * d(X,X) ** 2)
 or a k-nearest neighbors connectivity matrix. Alternatively, using precomputed, a user-provided affinity matrix can be used. Read more in the User Guide.  Parameters

n_clustersint, default=8

The dimension of the projection subspace.
eigen_solver{‘arpack’, ‘lobpcg’, ‘amg’}, default=None

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.
n_componentsint, default=n_clusters

Number of eigen vectors to use for the spectral embedding
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.
gammafloat, default=1.0

Kernel coefficient for rbf, poly, sigmoid, laplacian and chi2 kernels. Ignored for affinity='nearest_neighbors'.
affinitystr or callable, default=’rbf’

 How to construct the affinity matrix.

 ‘nearest_neighbors’ : construct the affinity matrix by computing a graph of nearest neighbors. ‘rbf’ : construct the affinity matrix using a radial basis function (RBF) kernel. ‘precomputed’ : interpret X as a precomputed affinity matrix. ‘precomputed_nearest_neighbors’ : interpret X as a sparse graph of precomputed nearest neighbors, and constructs the affinity matrix by selecting the n_neighbors nearest neighbors. one of the kernels supported by pairwise_kernels.    Only kernels that produce similarity scores (non-negative values that increase with similarity) should be used. This property is not checked by the clustering algorithm.
n_neighborsint, default=10

Number of neighbors to use when constructing the affinity matrix using the nearest neighbors method. Ignored for affinity='rbf'.
eigen_tolfloat, default=0.0

Stopping criterion for eigendecomposition of the Laplacian matrix when eigen_solver='arpack'.
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.
degreefloat, default=3

Degree of the polynomial kernel. Ignored by other kernels.
coef0float, default=1

Zero coefficient for polynomial and sigmoid kernels. Ignored by other kernels.
kernel_paramsdict of str to any, default=None

Parameters (keyword arguments) and values for kernel passed as callable object. Ignored by other kernels.
n_jobsint, default=None

The number of parallel jobs to run when affinity='nearest_neighbors' or affinity='precomputed_nearest_neighbors'. The neighbors search will be done 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

Verbosity mode.  New in version 0.24.     Attributes

affinity_matrix_array-like of shape (n_samples, n_samples)

Affinity matrix used for clustering. Available only if after calling fit.
labels_ndarray of shape (n_samples,)

Labels of each point     Notes If you have an affinity matrix, such as a distance matrix, for which 0 means identical elements, and high values means very dissimilar elements, it can be transformed in a similarity matrix that is well suited for the algorithm by applying the Gaussian (RBF, heat) kernel: np.exp(- dist_matrix ** 2 / (2. * delta ** 2))
 Where delta is a free parameter representing the width of the Gaussian kernel. Another alternative is to take a symmetric version of the k nearest neighbors connectivity matrix of the points. If the pyamg package is installed, it is used: this greatly speeds up computation. 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 >>> from sklearn.cluster import SpectralClustering
>>> import numpy as np
>>> X = np.array([[1, 1], [2, 1], [1, 0],
...               [4, 7], [3, 5], [3, 6]])
>>> clustering = SpectralClustering(n_clusters=2,
...         assign_labels="discretize",
...         random_state=0).fit(X)
>>> clustering.labels_
array([1, 1, 1, 0, 0, 0])
>>> clustering
SpectralClustering(assign_labels='discretize', n_clusters=2,
    random_state=0)
 Methods
fit(X[, y]) Perform spectral clustering from features, or affinity matrix.
fit_predict(X[, y]) Perform spectral clustering from features, or affinity matrix, and return cluster labels.
get_params([deep]) Get parameters for this estimator.
set_params(**params) Set the parameters of this estimator.
fit(X, y=None) [source]

Perform spectral clustering from features, or affinity matrix.  Parameters

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

Training instances to cluster, or similarities / affinities between instances if affinity='precomputed'. If a sparse matrix is provided in a format other than csr_matrix, csc_matrix, or coo_matrix, it will be converted into a sparse csr_matrix.
yIgnored

Not used, present here for API consistency by convention.    Returns
 self

fit_predict(X, y=None) [source]

Perform spectral clustering from features, or affinity matrix, and return cluster labels.  Parameters

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

Training instances to cluster, or similarities / affinities between instances if affinity='precomputed'. If a sparse matrix is provided in a format other than csr_matrix, csc_matrix, or coo_matrix, it will be converted into a sparse csr_matrix.
yIgnored

Not used, present here for API consistency by convention.    Returns

labelsndarray of shape (n_samples,)

Cluster labels.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


sklearn.cluster.SpectralClustering
class sklearn.cluster.SpectralClustering(n_clusters=8, *, eigen_solver=None, n_components=None, random_state=None, n_init=10, gamma=1.0, affinity='rbf', n_neighbors=10, eigen_tol=0.0, assign_labels='kmeans', degree=3, coef0=1, kernel_params=None, n_jobs=None, 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. When calling fit, an affinity matrix is constructed using either kernel function such the Gaussian (aka RBF) kernel of the euclidean distanced d(X, X): np.exp(-gamma * d(X,X) ** 2)
 or a k-nearest neighbors connectivity matrix. Alternatively, using precomputed, a user-provided affinity matrix can be used. Read more in the User Guide.  Parameters

n_clustersint, default=8

The dimension of the projection subspace.
eigen_solver{‘arpack’, ‘lobpcg’, ‘amg’}, default=None

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.
n_componentsint, default=n_clusters

Number of eigen vectors to use for the spectral embedding
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.
gammafloat, default=1.0

Kernel coefficient for rbf, poly, sigmoid, laplacian and chi2 kernels. Ignored for affinity='nearest_neighbors'.
affinitystr or callable, default=’rbf’

 How to construct the affinity matrix.

 ‘nearest_neighbors’ : construct the affinity matrix by computing a graph of nearest neighbors. ‘rbf’ : construct the affinity matrix using a radial basis function (RBF) kernel. ‘precomputed’ : interpret X as a precomputed affinity matrix. ‘precomputed_nearest_neighbors’ : interpret X as a sparse graph of precomputed nearest neighbors, and constructs the affinity matrix by selecting the n_neighbors nearest neighbors. one of the kernels supported by pairwise_kernels.    Only kernels that produce similarity scores (non-negative values that increase with similarity) should be used. This property is not checked by the clustering algorithm.
n_neighborsint, default=10

Number of neighbors to use when constructing the affinity matrix using the nearest neighbors method. Ignored for affinity='rbf'.
eigen_tolfloat, default=0.0

Stopping criterion for eigendecomposition of the Laplacian matrix when eigen_solver='arpack'.
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.
degreefloat, default=3

Degree of the polynomial kernel. Ignored by other kernels.
coef0float, default=1

Zero coefficient for polynomial and sigmoid kernels. Ignored by other kernels.
kernel_paramsdict of str to any, default=None

Parameters (keyword arguments) and values for kernel passed as callable object. Ignored by other kernels.
n_jobsint, default=None

The number of parallel jobs to run when affinity='nearest_neighbors' or affinity='precomputed_nearest_neighbors'. The neighbors search will be done 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

Verbosity mode.  New in version 0.24.     Attributes

affinity_matrix_array-like of shape (n_samples, n_samples)

Affinity matrix used for clustering. Available only if after calling fit.
labels_ndarray of shape (n_samples,)

Labels of each point     Notes If you have an affinity matrix, such as a distance matrix, for which 0 means identical elements, and high values means very dissimilar elements, it can be transformed in a similarity matrix that is well suited for the algorithm by applying the Gaussian (RBF, heat) kernel: np.exp(- dist_matrix ** 2 / (2. * delta ** 2))
 Where delta is a free parameter representing the width of the Gaussian kernel. Another alternative is to take a symmetric version of the k nearest neighbors connectivity matrix of the points. If the pyamg package is installed, it is used: this greatly speeds up computation. 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 >>> from sklearn.cluster import SpectralClustering
>>> import numpy as np
>>> X = np.array([[1, 1], [2, 1], [1, 0],
...               [4, 7], [3, 5], [3, 6]])
>>> clustering = SpectralClustering(n_clusters=2,
...         assign_labels="discretize",
...         random_state=0).fit(X)
>>> clustering.labels_
array([1, 1, 1, 0, 0, 0])
>>> clustering
SpectralClustering(assign_labels='discretize', n_clusters=2,
    random_state=0)
 Methods
fit(X[, y]) Perform spectral clustering from features, or affinity matrix.
fit_predict(X[, y]) Perform spectral clustering from features, or affinity matrix, and return cluster labels.
get_params([deep]) Get parameters for this estimator.
set_params(**params) Set the parameters of this estimator.
fit(X, y=None) [source]

Perform spectral clustering from features, or affinity matrix.  Parameters

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

Training instances to cluster, or similarities / affinities between instances if affinity='precomputed'. If a sparse matrix is provided in a format other than csr_matrix, csc_matrix, or coo_matrix, it will be converted into a sparse csr_matrix.
yIgnored

Not used, present here for API consistency by convention.    Returns
 self

fit_predict(X, y=None) [source]

Perform spectral clustering from features, or affinity matrix, and return cluster labels.  Parameters

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

Training instances to cluster, or similarities / affinities between instances if affinity='precomputed'. If a sparse matrix is provided in a format other than csr_matrix, csc_matrix, or coo_matrix, it will be converted into a sparse csr_matrix.
yIgnored

Not used, present here for API consistency by convention.    Returns

labelsndarray of shape (n_samples,)

Cluster labels.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

 Examples using sklearn.cluster.SpectralClustering

  Comparing different clustering algorithms on toy datasets


fit(X, y=None) [source]

Perform spectral clustering from features, or affinity matrix.  Parameters

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

Training instances to cluster, or similarities / affinities between instances if affinity='precomputed'. If a sparse matrix is provided in a format other than csr_matrix, csc_matrix, or coo_matrix, it will be converted into a sparse csr_matrix.
yIgnored

Not used, present here for API consistency by convention.    Returns
 self


fit_predict(X, y=None) [source]

Perform spectral clustering from features, or affinity matrix, and return cluster labels.  Parameters

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

Training instances to cluster, or similarities / affinities between instances if affinity='precomputed'. If a sparse matrix is provided in a format other than csr_matrix, csc_matrix, or coo_matrix, it will be converted into a sparse csr_matrix.
yIgnored

Not used, present here for API consistency by convention.    Returns

labelsndarray of shape (n_samples,)

Cluster labels.


get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.


set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


class sklearn.cluster.SpectralCoclustering(n_clusters=3, *, svd_method='randomized', n_svd_vecs=None, mini_batch=False, init='k-means++', n_init=10, n_jobs='deprecated', random_state=None) [source]

Spectral Co-Clustering algorithm (Dhillon, 2001). Clusters rows and columns of an array X to solve the relaxed normalized cut of the bipartite graph created from X as follows: the edge between row vertex i and column vertex j has weight X[i, j]. The resulting bicluster structure is block-diagonal, since each row and each column belongs to exactly one bicluster. Supports sparse matrices, as long as they are nonnegative. Read more in the User Guide.  Parameters

n_clustersint, default=3

The number of biclusters to find.
svd_method{‘randomized’, ‘arpack’}, default=’randomized’

Selects the algorithm for finding singular vectors. May be ‘randomized’ or ‘arpack’. If ‘randomized’, use sklearn.utils.extmath.randomized_svd, which may be faster for large matrices. If ‘arpack’, use scipy.sparse.linalg.svds, which is more accurate, but possibly slower in some cases.
n_svd_vecsint, default=None

Number of vectors to use in calculating the SVD. Corresponds to ncv when svd_method=arpack and n_oversamples when svd_method is ‘randomized`.
mini_batchbool, default=False

Whether to use mini-batch k-means, which is faster but may get different results.
init{‘k-means++’, ‘random’, or ndarray of shape (n_clusters, n_features), default=’k-means++’

Method for initialization of k-means algorithm; defaults to ‘k-means++’.
n_initint, default=10

Number of random initializations that are tried with the k-means algorithm. If mini-batch k-means is used, the best initialization is chosen and the algorithm runs once. Otherwise, the algorithm is run for each initialization and the best solution chosen.
n_jobsint, default=None

The number of jobs to use for the computation. This works by breaking down the pairwise matrix into n_jobs even slices and computing them in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.  Deprecated since version 0.23: n_jobs was deprecated in version 0.23 and will be removed in 1.0 (renaming of 0.25).
random_stateint, RandomState instance, default=None

Used for randomizing the singular value decomposition and the k-means initialization. Use an int to make the randomness deterministic. See Glossary.    Attributes

rows_array-like of shape (n_row_clusters, n_rows)

Results of the clustering. rows[i, r] is True if cluster i contains row r. Available only after calling fit.
columns_array-like of shape (n_column_clusters, n_columns)

Results of the clustering, like rows.
row_labels_array-like of shape (n_rows,)

The bicluster label of each row.
column_labels_array-like of shape (n_cols,)

The bicluster label of each column.     References  Dhillon, Inderjit S, 2001. Co-clustering documents and words using bipartite spectral graph partitioning.  Examples >>> from sklearn.cluster import SpectralCoclustering
>>> import numpy as np
>>> X = np.array([[1, 1], [2, 1], [1, 0],
...               [4, 7], [3, 5], [3, 6]])
>>> clustering = SpectralCoclustering(n_clusters=2, random_state=0).fit(X)
>>> clustering.row_labels_
array([0, 1, 1, 0, 0, 0], dtype=int32)
>>> clustering.column_labels_
array([0, 0], dtype=int32)
>>> clustering
SpectralCoclustering(n_clusters=2, random_state=0)
 Methods
fit(X[, y]) Creates a biclustering for X.
get_indices(i) Row and column indices of the i’th bicluster.
get_params([deep]) Get parameters for this estimator.
get_shape(i) Shape of the i’th bicluster.
get_submatrix(i, data) Return the submatrix corresponding to bicluster i.
set_params(**params) Set the parameters of this estimator.
property biclusters_
Convenient way to get row and column indicators together. Returns the rows_ and columns_ members.

fit(X, y=None) [source]

Creates a biclustering for X.  Parameters

Xarray-like of shape (n_samples, n_features)

yIgnored

get_indices(i) [source]

Row and column indices of the i’th bicluster. Only works if rows_ and columns_ attributes exist.  Parameters

iint

The index of the cluster.    Returns

row_indndarray, dtype=np.intp

Indices of rows in the dataset that belong to the bicluster.
col_indndarray, dtype=np.intp

Indices of columns in the dataset that belong to the bicluster.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

get_shape(i) [source]

Shape of the i’th bicluster.  Parameters

iint

The index of the cluster.    Returns

n_rowsint

Number of rows in the bicluster.
n_colsint

Number of columns in the bicluster.

get_submatrix(i, data) [source]

Return the submatrix corresponding to bicluster i.  Parameters

iint

The index of the cluster.
dataarray-like of shape (n_samples, n_features)

The data.    Returns

submatrixndarray of shape (n_rows, n_cols)

The submatrix corresponding to bicluster i.     Notes Works with sparse matrices. Only works if rows_ and columns_ attributes exist.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


sklearn.cluster.SpectralCoclustering
class sklearn.cluster.SpectralCoclustering(n_clusters=3, *, svd_method='randomized', n_svd_vecs=None, mini_batch=False, init='k-means++', n_init=10, n_jobs='deprecated', random_state=None) [source]

Spectral Co-Clustering algorithm (Dhillon, 2001). Clusters rows and columns of an array X to solve the relaxed normalized cut of the bipartite graph created from X as follows: the edge between row vertex i and column vertex j has weight X[i, j]. The resulting bicluster structure is block-diagonal, since each row and each column belongs to exactly one bicluster. Supports sparse matrices, as long as they are nonnegative. Read more in the User Guide.  Parameters

n_clustersint, default=3

The number of biclusters to find.
svd_method{‘randomized’, ‘arpack’}, default=’randomized’

Selects the algorithm for finding singular vectors. May be ‘randomized’ or ‘arpack’. If ‘randomized’, use sklearn.utils.extmath.randomized_svd, which may be faster for large matrices. If ‘arpack’, use scipy.sparse.linalg.svds, which is more accurate, but possibly slower in some cases.
n_svd_vecsint, default=None

Number of vectors to use in calculating the SVD. Corresponds to ncv when svd_method=arpack and n_oversamples when svd_method is ‘randomized`.
mini_batchbool, default=False

Whether to use mini-batch k-means, which is faster but may get different results.
init{‘k-means++’, ‘random’, or ndarray of shape (n_clusters, n_features), default=’k-means++’

Method for initialization of k-means algorithm; defaults to ‘k-means++’.
n_initint, default=10

Number of random initializations that are tried with the k-means algorithm. If mini-batch k-means is used, the best initialization is chosen and the algorithm runs once. Otherwise, the algorithm is run for each initialization and the best solution chosen.
n_jobsint, default=None

The number of jobs to use for the computation. This works by breaking down the pairwise matrix into n_jobs even slices and computing them in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.  Deprecated since version 0.23: n_jobs was deprecated in version 0.23 and will be removed in 1.0 (renaming of 0.25).
random_stateint, RandomState instance, default=None

Used for randomizing the singular value decomposition and the k-means initialization. Use an int to make the randomness deterministic. See Glossary.    Attributes

rows_array-like of shape (n_row_clusters, n_rows)

Results of the clustering. rows[i, r] is True if cluster i contains row r. Available only after calling fit.
columns_array-like of shape (n_column_clusters, n_columns)

Results of the clustering, like rows.
row_labels_array-like of shape (n_rows,)

The bicluster label of each row.
column_labels_array-like of shape (n_cols,)

The bicluster label of each column.     References  Dhillon, Inderjit S, 2001. Co-clustering documents and words using bipartite spectral graph partitioning.  Examples >>> from sklearn.cluster import SpectralCoclustering
>>> import numpy as np
>>> X = np.array([[1, 1], [2, 1], [1, 0],
...               [4, 7], [3, 5], [3, 6]])
>>> clustering = SpectralCoclustering(n_clusters=2, random_state=0).fit(X)
>>> clustering.row_labels_
array([0, 1, 1, 0, 0, 0], dtype=int32)
>>> clustering.column_labels_
array([0, 0], dtype=int32)
>>> clustering
SpectralCoclustering(n_clusters=2, random_state=0)
 Methods
fit(X[, y]) Creates a biclustering for X.
get_indices(i) Row and column indices of the i’th bicluster.
get_params([deep]) Get parameters for this estimator.
get_shape(i) Shape of the i’th bicluster.
get_submatrix(i, data) Return the submatrix corresponding to bicluster i.
set_params(**params) Set the parameters of this estimator.
property biclusters_
Convenient way to get row and column indicators together. Returns the rows_ and columns_ members.

fit(X, y=None) [source]

Creates a biclustering for X.  Parameters

Xarray-like of shape (n_samples, n_features)

yIgnored

get_indices(i) [source]

Row and column indices of the i’th bicluster. Only works if rows_ and columns_ attributes exist.  Parameters

iint

The index of the cluster.    Returns

row_indndarray, dtype=np.intp

Indices of rows in the dataset that belong to the bicluster.
col_indndarray, dtype=np.intp

Indices of columns in the dataset that belong to the bicluster.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

get_shape(i) [source]

Shape of the i’th bicluster.  Parameters

iint

The index of the cluster.    Returns

n_rowsint

Number of rows in the bicluster.
n_colsint

Number of columns in the bicluster.

get_submatrix(i, data) [source]

Return the submatrix corresponding to bicluster i.  Parameters

iint

The index of the cluster.
dataarray-like of shape (n_samples, n_features)

The data.    Returns

submatrixndarray of shape (n_rows, n_cols)

The submatrix corresponding to bicluster i.     Notes Works with sparse matrices. Only works if rows_ and columns_ attributes exist.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

 Examples using sklearn.cluster.SpectralCoclustering

  A demo of the Spectral Co-Clustering algorithm

  Biclustering documents with the Spectral Co-clustering algorithm


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


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.


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

Applies transformers to columns of an array or pandas DataFrame. This estimator allows different columns or column subsets of the input to be transformed separately and the features generated by each transformer will be concatenated to form a single feature space. This is useful for heterogeneous or columnar data, to combine several feature extraction mechanisms or transformations into a single transformer. Read more in the User Guide.  New in version 0.20.   Parameters

transformerslist of tuples

List of (name, transformer, columns) tuples specifying the transformer objects to be applied to subsets of the data.
namestr

Like in Pipeline and FeatureUnion, this allows the transformer and its parameters to be set using set_params and searched in grid search.
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, array-like of bool, slice 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. Note that using this feature requires that the DataFrame columns input at fit and transform have identical order.
sparse_thresholdfloat, default=0.3

If the output of the different transformers contains sparse matrices, these will be stacked as a sparse matrix if the overall density is lower than this value. Use sparse_threshold=0 to always return dense. When the transformed output consists of all dense data, the stacked result will be dense, 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.
transformer_weightsdict, default=None

Multiplicative weights for features per transformer. The output of the transformer is multiplied by these weights. Keys are transformer names, values the weights.
verbosebool, default=False

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

transformers_list

The collection of fitted transformers as tuples of (name, fitted_transformer, column). fitted_transformer can be an estimator, ‘drop’, or ‘passthrough’. In case there were no columns selected, this will be the unfitted transformer. If there are remaining columns, the final element is a tuple of the form: (‘remainder’, transformer, remaining_columns) corresponding to the remainder parameter. If there are remaining columns, then len(transformers_)==len(transformers)+1, otherwise len(transformers_)==len(transformers).
named_transformers_Bunch

Access the fitted transformer by name.
sparse_output_bool

Boolean flag indicating whether the output of transform is a sparse matrix or a dense numpy array, which depends on the output of the individual transformers and the sparse_threshold keyword.      See also
make_column_transformer

Convenience function for combining the outputs of multiple transformer objects applied to column subsets of the original feature space.
make_column_selector

Convenience function for selecting columns based on datatype or the columns name with a regex pattern.    Notes The order of the columns in the transformed feature matrix follows the order of how the columns are specified in the transformers list. Columns of the original feature matrix that are not specified are dropped from the resulting transformed feature matrix, unless specified in the passthrough keyword. Those columns specified with passthrough are added at the right to the output of the transformers. Examples >>> import numpy as np
>>> from sklearn.compose import ColumnTransformer
>>> from sklearn.preprocessing import Normalizer
>>> ct = ColumnTransformer(
...     [("norm1", Normalizer(norm='l1'), [0, 1]),
...      ("norm2", Normalizer(norm='l1'), slice(2, 4))])
>>> X = np.array([[0., 1., 2., 2.],
...               [1., 1., 0., 1.]])
>>> # Normalizer scales each row of X to unit norm. A separate scaling
>>> # is applied for the two first and two last elements of each
>>> # row independently.
>>> ct.fit_transform(X)
array([[0. , 1. , 0.5, 0.5],
       [0.5, 0.5, 0. , 1. ]])
 Methods
fit(X[, y]) Fit all transformers using X.
fit_transform(X[, y]) Fit all transformers, transform the data and concatenate results.
get_feature_names() Get feature names from all transformers.
get_params([deep]) Get parameters for this estimator.
set_params(**kwargs) Set the parameters of this estimator.
transform(X) Transform X separately by each transformer, concatenate results.
fit(X, y=None) [source]

Fit all transformers using X.  Parameters

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

Input data, of which specified subsets are used to fit the transformers.
yarray-like of shape (n_samples,…), default=None

Targets for supervised learning.    Returns

selfColumnTransformer

This estimator

fit_transform(X, y=None) [source]

Fit all transformers, transform the data and concatenate results.  Parameters

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

Input data, of which specified subsets are used to fit the transformers.
yarray-like of shape (n_samples,), default=None

Targets for supervised learning.    Returns

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

hstack of results of transformers. sum_n_components is the sum of n_components (output dimension) over transformers. If any result is a sparse matrix, everything will be converted to sparse matrices.

get_feature_names() [source]

Get feature names from all transformers.  Returns

feature_nameslist of strings

Names of the features produced by transform.

get_params(deep=True) [source]

Get parameters for this estimator. Returns the parameters given in the constructor as well as the estimators contained within the transformers of the ColumnTransformer.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

property named_transformers_
Access the fitted transformer by name. Read-only attribute to access any transformer by given name. Keys are transformer names and values are the fitted transformer objects.

set_params(**kwargs) [source]

Set the parameters of this estimator. Valid parameter keys can be listed with get_params(). Note that you can directly set the parameters of the estimators contained in transformers of ColumnTransformer.  Returns
 self

transform(X) [source]

Transform X separately by each transformer, concatenate results.  Parameters

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

The data to be transformed by subset.    Returns

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

hstack of results of transformers. sum_n_components is the sum of n_components (output dimension) over transformers. If any result is a sparse matrix, everything will be converted to sparse matrices.


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

Applies transformers to columns of an array or pandas DataFrame. This estimator allows different columns or column subsets of the input to be transformed separately and the features generated by each transformer will be concatenated to form a single feature space. This is useful for heterogeneous or columnar data, to combine several feature extraction mechanisms or transformations into a single transformer. Read more in the User Guide.  New in version 0.20.   Parameters

transformerslist of tuples

List of (name, transformer, columns) tuples specifying the transformer objects to be applied to subsets of the data.
namestr

Like in Pipeline and FeatureUnion, this allows the transformer and its parameters to be set using set_params and searched in grid search.
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, array-like of bool, slice 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. Note that using this feature requires that the DataFrame columns input at fit and transform have identical order.
sparse_thresholdfloat, default=0.3

If the output of the different transformers contains sparse matrices, these will be stacked as a sparse matrix if the overall density is lower than this value. Use sparse_threshold=0 to always return dense. When the transformed output consists of all dense data, the stacked result will be dense, 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.
transformer_weightsdict, default=None

Multiplicative weights for features per transformer. The output of the transformer is multiplied by these weights. Keys are transformer names, values the weights.
verbosebool, default=False

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

transformers_list

The collection of fitted transformers as tuples of (name, fitted_transformer, column). fitted_transformer can be an estimator, ‘drop’, or ‘passthrough’. In case there were no columns selected, this will be the unfitted transformer. If there are remaining columns, the final element is a tuple of the form: (‘remainder’, transformer, remaining_columns) corresponding to the remainder parameter. If there are remaining columns, then len(transformers_)==len(transformers)+1, otherwise len(transformers_)==len(transformers).
named_transformers_Bunch

Access the fitted transformer by name.
sparse_output_bool

Boolean flag indicating whether the output of transform is a sparse matrix or a dense numpy array, which depends on the output of the individual transformers and the sparse_threshold keyword.      See also
make_column_transformer

Convenience function for combining the outputs of multiple transformer objects applied to column subsets of the original feature space.
make_column_selector

Convenience function for selecting columns based on datatype or the columns name with a regex pattern.    Notes The order of the columns in the transformed feature matrix follows the order of how the columns are specified in the transformers list. Columns of the original feature matrix that are not specified are dropped from the resulting transformed feature matrix, unless specified in the passthrough keyword. Those columns specified with passthrough are added at the right to the output of the transformers. Examples >>> import numpy as np
>>> from sklearn.compose import ColumnTransformer
>>> from sklearn.preprocessing import Normalizer
>>> ct = ColumnTransformer(
...     [("norm1", Normalizer(norm='l1'), [0, 1]),
...      ("norm2", Normalizer(norm='l1'), slice(2, 4))])
>>> X = np.array([[0., 1., 2., 2.],
...               [1., 1., 0., 1.]])
>>> # Normalizer scales each row of X to unit norm. A separate scaling
>>> # is applied for the two first and two last elements of each
>>> # row independently.
>>> ct.fit_transform(X)
array([[0. , 1. , 0.5, 0.5],
       [0.5, 0.5, 0. , 1. ]])
 Methods
fit(X[, y]) Fit all transformers using X.
fit_transform(X[, y]) Fit all transformers, transform the data and concatenate results.
get_feature_names() Get feature names from all transformers.
get_params([deep]) Get parameters for this estimator.
set_params(**kwargs) Set the parameters of this estimator.
transform(X) Transform X separately by each transformer, concatenate results.
fit(X, y=None) [source]

Fit all transformers using X.  Parameters

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

Input data, of which specified subsets are used to fit the transformers.
yarray-like of shape (n_samples,…), default=None

Targets for supervised learning.    Returns

selfColumnTransformer

This estimator

fit_transform(X, y=None) [source]

Fit all transformers, transform the data and concatenate results.  Parameters

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

Input data, of which specified subsets are used to fit the transformers.
yarray-like of shape (n_samples,), default=None

Targets for supervised learning.    Returns

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

hstack of results of transformers. sum_n_components is the sum of n_components (output dimension) over transformers. If any result is a sparse matrix, everything will be converted to sparse matrices.

get_feature_names() [source]

Get feature names from all transformers.  Returns

feature_nameslist of strings

Names of the features produced by transform.

get_params(deep=True) [source]

Get parameters for this estimator. Returns the parameters given in the constructor as well as the estimators contained within the transformers of the ColumnTransformer.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

property named_transformers_
Access the fitted transformer by name. Read-only attribute to access any transformer by given name. Keys are transformer names and values are the fitted transformer objects.

set_params(**kwargs) [source]

Set the parameters of this estimator. Valid parameter keys can be listed with get_params(). Note that you can directly set the parameters of the estimators contained in transformers of ColumnTransformer.  Returns
 self

transform(X) [source]

Transform X separately by each transformer, concatenate results.  Parameters

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

The data to be transformed by subset.    Returns

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

hstack of results of transformers. sum_n_components is the sum of n_components (output dimension) over transformers. If any result is a sparse matrix, everything will be converted to sparse matrices.

 Examples using sklearn.compose.ColumnTransformer

  Poisson regression and non-normal loss

  Tweedie regression on insurance claims

  Permutation Importance vs Random Forest Feature Importance (MDI)

  Column Transformer with Mixed Types

  Column Transformer with Heterogeneous Data Sources


fit(X, y=None) [source]

Fit all transformers using X.  Parameters

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

Input data, of which specified subsets are used to fit the transformers.
yarray-like of shape (n_samples,…), default=None

Targets for supervised learning.    Returns

selfColumnTransformer

This estimator


fit_transform(X, y=None) [source]

Fit all transformers, transform the data and concatenate results.  Parameters

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

Input data, of which specified subsets are used to fit the transformers.
yarray-like of shape (n_samples,), default=None

Targets for supervised learning.    Returns

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

hstack of results of transformers. sum_n_components is the sum of n_components (output dimension) over transformers. If any result is a sparse matrix, everything will be converted to sparse matrices.


get_feature_names() [source]

Get feature names from all transformers.  Returns

feature_nameslist of strings

Names of the features produced by transform.


get_params(deep=True) [source]

Get parameters for this estimator. Returns the parameters given in the constructor as well as the estimators contained within the transformers of the ColumnTransformer.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.


property named_transformers_
Access the fitted transformer by name. Read-only attribute to access any transformer by given name. Keys are transformer names and values are the fitted transformer objects.


set_params(**kwargs) [source]

Set the parameters of this estimator. Valid parameter keys can be listed with get_params(). Note that you can directly set the parameters of the estimators contained in transformers of ColumnTransformer.  Returns
 self


transform(X) [source]

Transform X separately by each transformer, concatenate results.  Parameters

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

The data to be transformed by subset.    Returns

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

hstack of results of transformers. sum_n_components is the sum of n_components (output dimension) over transformers. If any result is a sparse matrix, everything will be converted to sparse matrices.


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


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'])])