divik.feature_extraction
module¶
Unsupervised feature extraction methods
- class divik.feature_extraction.HistogramEqualization(n_bins=256, n_jobs=-1)[source]¶
Equalize histogram of the features to increase contrast
Based on https://github.com/scikit-image/scikit-image/blob/master/skimage/exposure/exposure.py#L187-L223
- Parameters
- n_binsint, default 256
Number of bins for histogram equalization.
- n_jobsint, default -1
Number of CPU cores to use during equalization
- Attributes
- cdf_array
Values of cumulative distribution function for all the features
- bins_array
Bin centers for all the features
Methods
fit_transform
(X[, y])Fit to data, then transform it.
get_params
([deep])Get parameters for this estimator.
set_params
(**params)Set the parameters of this estimator.
fit
transform
- fit_transform(X, y=None, **fit_params)¶
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
- Parameters
- Xarray-like of shape (n_samples, n_features)
Input samples.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None
Target values (None for unsupervised transformations).
- **fit_paramsdict
Additional fit parameters.
- Returns
- X_newndarray array of shape (n_samples, n_features_new)
Transformed array.
- get_params(deep=True)¶
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)¶
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 divik.feature_extraction.KneePCA(whiten=False, refit=False)[source]¶
Principal component analysis (PCA) with knee method
PCA with automated components selection based on knee method over cumulative explained variance. Remaining components are discarded.
- Parameters
- whitenbool, optional (default False)
When True (False by default) the
pca_.components_
vectors are multiplied by the square root of n_samples and then divided by the singular values to ensure uncorrelated outputs with unit component-wise variances.Whitening will remove some information from the transformed signal (the relative variance scales of the components) but can sometime improve the predictive accuracy of the downstream estimators by making their data respect some hard-wired assumptions.
- refitbool, optional (default False)
When
True
(False
by default) thepca_
is re-fit with the smaller number of components. This could reduce memory footprint, but requires training fitting PCA.
- Attributes
- pca_PCA
Fit PCA estimator.
- n_components_int
The number of selected components.
Methods
fit
(X[, y])Fit the model from data in X.
fit_transform
(X[, y])Fit to data, then transform it.
get_params
([deep])Get parameters for this estimator.
Transform data back to its original space.
set_params
(**params)Set the parameters of this estimator.
transform
(X[, y])Apply dimensionality reduction to X.
- fit(X, y=None)[source]¶
Fit the model from data in X.
- Parameters
- Xarray-like, shape (n_samples, n_features)
Training vector, where
n_samples
is the number of samples andn_features
is the number of features.- Y: Ignored.
- Returns
- selfobject
Returns the instance itself.
- fit_transform(X, y=None, **fit_params)¶
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
- Parameters
- Xarray-like of shape (n_samples, n_features)
Input samples.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None
Target values (None for unsupervised transformations).
- **fit_paramsdict
Additional fit parameters.
- Returns
- X_newndarray array of shape (n_samples, n_features_new)
Transformed array.
- get_params(deep=True)¶
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.
- inverse_transform(X)[source]¶
Transform data back to its original space.
In other words, return an input X_original whose transform would be X.
- Parameters
- Xarray-like, shape (n_samples, n_components)
New data, where
n_samples
is the number of samples andn_components
is the number of components.
- Returns
- X_original array-like, shape (n_samples, n_features)
Notes
If whitening is enabled, inverse_transform will compute the exact inverse operation, which includes reversing whitening.
- set_params(**params)¶
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, y=None)[source]¶
Apply dimensionality reduction to X.
X is projected on the first principal components previously extracted from a training set.
- Parameters
- Xarray-like, shape (n_samples, n_features)
New data, where
n_samples
is the number of samples andn_features
is the number of features.
- Returns
- X_newarray-like, shape (n_samples, n_components)
Examples
>>> import numpy as np >>> from divik.feature_extraction import KneePCA >>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]) >>> pca = KneePCA(refit=True) >>> pca.fit(X) KneePCA(refit=True) >>> pca.transform(X)
- class divik.feature_extraction.LocallyAdjustedRbfSpectralEmbedding(distance='euclidean', n_components=2, random_state=None, eigen_solver=None, n_neighbors=None, n_jobs=1)[source]¶
Spectral embedding for non-linear dimensionality reduction.
Forms an affinity matrix given by the specified function and applies spectral decomposition to the corresponding graph laplacian. The resulting transformation is given by the value of the eigenvectors for each data point.
Note : Laplacian Eigenmaps is the actual algorithm implemented here.
- Parameters
- distance{‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘cityblock’,
- ‘correlation’, ‘cosine’, ‘dice’, ‘euclidean’, ‘hamming’, ‘jaccard’,
- ‘kulsinski’, ‘mahalanobis’, ‘atching’, ‘minkowski’, ‘rogerstanimoto’,
- ‘russellrao’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘yule’}
Distance measure, defaults to
euclidean
. These are the distances supported by scipy package.- n_componentsinteger, default: 2
The dimension of the projected subspace.
- random_stateint, RandomState instance or None, optional, default: None
A pseudo random number generator used for the initialization of the lobpcg eigenvectors. If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by
np.random
. Used whensolver
==amg
.- 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.
- n_neighborsint, defaultmax(n_samples/10 , 1)
Number of nearest neighbors for nearest_neighbors graph building.
- n_jobsint, optional (default = 1)
The number of parallel jobs to run. If
-1
, then the number of jobs is set to the number of CPU cores.
References
A Tutorial on Spectral Clustering, 2007 Ulrike von Luxburg http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.165.9323
On Spectral Clustering: Analysis and an algorithm, 2001 Andrew Y. Ng, Michael I. Jordan, Yair Weiss http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.19.8100
Normalized cuts and image segmentation, 2000 Jianbo Shi, Jitendra Malik http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.160.2324
- Attributes
- embedding_array, shape = (n_samples, n_components)
Spectral embedding of the training matrix.
Methods
fit
(X[, y])Fit the model from data in X.
fit_transform
(X[, y])Fit the model from data in X and transform X.
get_params
([deep])Get parameters for this estimator.
save
(destination)Save embedding to a directory
set_params
(**params)Set the parameters of this estimator.
transform
- fit(X, y=None)[source]¶
Fit the model from data in X.
- Parameters
- Xarray-like, shape (n_samples, n_features)
Training vector, where n_samples is the number of samples and n_features is the number of features.
- Y: Ignored.
- Returns
- selfobject
Returns the instance itself.
- fit_transform(X, y=None)[source]¶
Fit the model from data in X and transform X.
- Parameters
- Xarray-like, shape (n_samples, n_features)
Training vector, where n_samples is the number of samples and n_features is the number of features.
- Y: Ignored.
- Returns
- X_newarray-like, shape (n_samples, n_components)
- get_params(deep=True)¶
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.
- save(destination)[source]¶
Save embedding to a directory
- Parameters
- destinationstr
Directory to save the embedding.
- set_params(**params)¶
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.