method_to_transform¶
-
gtda.mapper.method_to_transform(cls, method_name)[source]¶ Wrap a class to add a
transformmethod as an alias to an existing method.An example of use is for classes possessing a
scoremethod such as kernel density estimators and anomaly/novelty detection estimators, allow for these estimators are to be used as steps in a pipeline.Note that 1D array outputs are reshaped into 2D column vectors before being returned by the new
transform.- Parameters
cls (object) – Class to be wrapped. If method_name is not one of its methods,
transformalways returnsNone.method_name (str) – Name of the method in cls to which
transformwill be an alias. The fist argument of this method (afterself) becomes theXinput fortransform.
- Returns
wrapped_cls – New class inheriting from
sklearn.base.TransformerMixin, so that bothtransformandfit_transformare available. Its name is the name of cls prepended with'Extended'.- Return type
object
Examples
>>> import numpy as np >>> from sklearn.neighbors import KernelDensity >>> from gtda.mapper import method_to_transform >>> X = np.random.random((100, 2)) >>> kde = KernelDensity()
Extend
KernelDensityto give it atransformmethod as an alias ofscore_samples(up to output shape). The new class is instantiated with the same parameters as the original one.>>> ExtendedKDE = method_to_transform(KernelDensity, 'score_samples') >>> extended_kde = ExtendedKDE() >>> Xt = kde.fit(X).score_samples(X) >>> print(Xt.shape) (100,) >>> Xt_extended = extended_kde.fit_transform(X) >>> print(Xt_extended.shape) (100, 1) >>> np.array_equal(Xt, Xt_extended.flatten()) True