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-07-20 19:31:45 +08:00
|
|
|
from ..utils import check_array
|
2014-12-29 21:56:02 +08:00
|
|
|
from ..utils.validation import check_is_fitted
|
2011-01-10 00:31:17 +08:00
|
|
|
|
2014-06-09 03:44:01 +08:00
|
|
|
import warnings
|
|
|
|
|
|
2011-05-17 09:52:06 +08:00
|
|
|
|
2011-01-20 11:54:20 +08:00
|
|
|
###############################################################################
|
|
|
|
|
# Mixin class for feature agglomeration.
|
2011-01-10 00:31:17 +08:00
|
|
|
|
2011-02-22 10:28:34 +08:00
|
|
|
class AgglomerationTransform(TransformerMixin):
|
2011-01-10 00:31:17 +08:00
|
|
|
"""
|
2011-01-23 06:37:20 +08:00
|
|
|
A class for feature agglomeration via the transform interface
|
2011-01-10 00:31:17 +08:00
|
|
|
"""
|
|
|
|
|
|
2014-04-15 06:57:07 +08:00
|
|
|
pooling_func = np.mean
|
|
|
|
|
|
|
|
|
|
def transform(self, X, pooling_func=None):
|
2011-01-10 00:31:17 +08:00
|
|
|
"""
|
|
|
|
|
Transform a new matrix using the built clustering
|
|
|
|
|
|
|
|
|
|
Parameters
|
2013-06-17 12:26:12 +08:00
|
|
|
----------
|
|
|
|
|
X : array-like, shape = [n_samples, n_features] or [n_features]
|
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 21:20:09 +08:00
|
|
|
pooling_func : callable, default=np.mean
|
2013-06-17 12:26:12 +08:00
|
|
|
This combines the values of agglomerated features into a single
|
2013-06-17 21:20:09 +08:00
|
|
|
value, and should accept an array of shape [M, N] and the keyword
|
|
|
|
|
argument `axis=1`, and reduce it to an array of size [M].
|
2013-06-17 12:26:12 +08:00
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
Y : array, shape = [n_samples, n_clusters] or [n_clusters]
|
|
|
|
|
The pooled values for each feature cluster.
|
2011-01-10 00:31:17 +08:00
|
|
|
"""
|
2014-12-29 21:56:02 +08:00
|
|
|
check_is_fitted(self, "labels_")
|
|
|
|
|
|
2014-04-15 06:57:07 +08:00
|
|
|
if pooling_func is not None:
|
2015-01-11 15:52:28 +08:00
|
|
|
warnings.warn("The pooling_func parameter is deprecated since 0.15 "
|
|
|
|
|
"and will be removed in 0.18. "
|
|
|
|
|
"Pass it to the constructor instead.",
|
|
|
|
|
DeprecationWarning)
|
2014-04-15 06:57:07 +08:00
|
|
|
else:
|
|
|
|
|
pooling_func = self.pooling_func
|
2014-07-20 19:31:45 +08:00
|
|
|
X = check_array(X)
|
2011-01-10 00:31:17 +08:00
|
|
|
nX = []
|
2012-08-27 00:29:29 +08:00
|
|
|
if len(self.labels_) != X.shape[1]:
|
|
|
|
|
raise ValueError("X has a different number of features than "
|
2012-12-17 06:28:10 +08:00
|
|
|
"during fitting.")
|
2012-08-27 00:29:29 +08:00
|
|
|
|
2011-01-10 00:31:17 +08:00
|
|
|
for l in np.unique(self.labels_):
|
2011-01-23 06:37:20 +08:00
|
|
|
nX.append(pooling_func(X[:, self.labels_ == l], axis=1))
|
2011-01-10 00:31:17 +08:00
|
|
|
return np.array(nX).T
|
|
|
|
|
|
|
|
|
|
def inverse_transform(self, Xred):
|
|
|
|
|
"""
|
|
|
|
|
Inverse the transformation.
|
|
|
|
|
Return a vector of size nb_features with the values of Xred assigned
|
|
|
|
|
to each group of features
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2013-06-17 12:26:12 +08:00
|
|
|
Xred : array-like, shape=[n_samples, n_clusters] or [n_clusters,]
|
2011-01-20 11:54:20 +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
|
|
|
|
|
-------
|
2013-06-17 12:26:12 +08:00
|
|
|
X : array, shape=[n_samples, n_features] or [n_features]
|
|
|
|
|
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
|
|
|
"""
|
2014-12-29 21:56:02 +08:00
|
|
|
check_is_fitted(self, "labels_")
|
|
|
|
|
|
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]
|