2011-01-10 00:31:17 +08:00
|
|
|
"""
|
2011-01-23 18:36:07 +08:00
|
|
|
Feature agglomeration. Base classes and functions for performing feature
|
2011-01-10 00:31:17 +08:00
|
|
|
agglomeration.
|
|
|
|
|
"""
|
2011-01-20 11:54:20 +08:00
|
|
|
# Author: V. Michel, A. Gramfort
|
2011-01-10 00:31:17 +08:00
|
|
|
# License: BSD 3 clause
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
2011-10-21 22:02:11 +08:00
|
|
|
|
2011-02-22 10:28:34 +08:00
|
|
|
from ..base import TransformerMixin
|
2014-12-29 21:56:02 +08:00
|
|
|
from ..utils.validation import check_is_fitted
|
2017-11-08 00:27:34 +08:00
|
|
|
from scipy.sparse import issparse
|
2011-01-10 00:31:17 +08:00
|
|
|
|
2011-01-20 11:54:20 +08:00
|
|
|
###############################################################################
|
|
|
|
|
# Mixin class for feature agglomeration.
|
2011-01-10 00:31:17 +08:00
|
|
|
|
2017-11-08 00:27:34 +08:00
|
|
|
|
2011-02-22 10:28:34 +08:00
|
|
|
class AgglomerationTransform(TransformerMixin):
|
2011-01-10 00:31:17 +08:00
|
|
|
"""
|
2021-09-01 01:04:29 +08:00
|
|
|
A class for feature agglomeration via the transform interface.
|
2011-01-10 00:31:17 +08:00
|
|
|
"""
|
|
|
|
|
|
2015-10-20 16:22:10 +08:00
|
|
|
def transform(self, X):
|
2011-01-10 00:31:17 +08:00
|
|
|
"""
|
2021-09-01 01:04:29 +08:00
|
|
|
Transform a new matrix using the built clustering.
|
2011-01-10 00:31:17 +08:00
|
|
|
|
|
|
|
|
Parameters
|
2013-06-17 12:26:12 +08:00
|
|
|
----------
|
2021-09-01 01:04:29 +08:00
|
|
|
X : array-like of shape (n_samples, n_features) or \
|
|
|
|
|
(n_samples, n_samples)
|
2011-01-10 00:31:17 +08:00
|
|
|
A M by N array of M observations in N dimensions or a length
|
|
|
|
|
M array of M one-dimensional observations.
|
|
|
|
|
|
2013-06-17 12:26:12 +08:00
|
|
|
Returns
|
|
|
|
|
-------
|
2020-06-07 22:07:47 +08:00
|
|
|
Y : ndarray of shape (n_samples, n_clusters) or (n_clusters,)
|
2013-06-17 12:26:12 +08:00
|
|
|
The pooled values for each feature cluster.
|
2011-01-10 00:31:17 +08:00
|
|
|
"""
|
2019-08-14 04:09:07 +08:00
|
|
|
check_is_fitted(self)
|
2014-12-29 21:56:02 +08:00
|
|
|
|
2020-11-21 05:50:32 +08:00
|
|
|
X = self._validate_data(X, reset=False)
|
2019-05-09 20:41:56 +08:00
|
|
|
if self.pooling_func == np.mean and not issparse(X):
|
2017-11-08 00:27:34 +08:00
|
|
|
size = np.bincount(self.labels_)
|
|
|
|
|
n_samples = X.shape[0]
|
|
|
|
|
# a fast way to compute the mean of grouped features
|
|
|
|
|
nX = np.array(
|
|
|
|
|
[np.bincount(self.labels_, X[i, :]) / size for i in range(n_samples)]
|
|
|
|
|
)
|
|
|
|
|
else:
|
2019-05-09 20:41:56 +08:00
|
|
|
nX = [
|
|
|
|
|
self.pooling_func(X[:, self.labels_ == l], axis=1)
|
2019-01-08 06:32:34 +08:00
|
|
|
for l in np.unique(self.labels_)
|
|
|
|
|
]
|
2017-11-08 00:27:34 +08:00
|
|
|
nX = np.array(nX).T
|
|
|
|
|
return nX
|
2011-01-10 00:31:17 +08:00
|
|
|
|
|
|
|
|
def inverse_transform(self, Xred):
|
|
|
|
|
"""
|
2021-09-01 01:04:29 +08:00
|
|
|
Inverse the transformation and return a vector of size `n_features`.
|
2011-01-10 00:31:17 +08:00
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2019-10-03 08:54:32 +08:00
|
|
|
Xred : array-like of shape (n_samples, n_clusters) or (n_clusters,)
|
2021-09-01 01:04:29 +08:00
|
|
|
The values to be assigned to each cluster of samples.
|
2011-01-10 00:31:17 +08:00
|
|
|
|
2011-12-23 02:34:33 +08:00
|
|
|
Returns
|
|
|
|
|
-------
|
2020-06-07 22:07:47 +08:00
|
|
|
X : ndarray of shape (n_samples, n_features) or (n_features,)
|
2021-09-01 01:04:29 +08:00
|
|
|
A vector of size `n_samples` with the values of `Xred` assigned to
|
2011-01-20 11:54:20 +08:00
|
|
|
each of the cluster of samples.
|
2011-01-10 00:31:17 +08:00
|
|
|
"""
|
2019-08-14 04:09:07 +08:00
|
|
|
check_is_fitted(self)
|
2014-12-29 21:56:02 +08:00
|
|
|
|
2014-01-06 21:45:40 +08:00
|
|
|
unil, inverse = np.unique(self.labels_, return_inverse=True)
|
2013-06-17 12:32:23 +08:00
|
|
|
return Xred[..., inverse]
|