/
base.py
44 lines (34 loc) · 1.48 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Base classes for all estimators."""
# Authors: Gael Varoquaux <gael.varoquaux@normalesup.org>
# Leandro Hermida <hermidal@cs.umd.edu>
# License: BSD 3 clause
from sklearn.base import TransformerMixin
class ExtendedTransformerMixin(TransformerMixin):
"""Mixin class for all transformers in scikit-learn."""
def fit_transform(self, 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
----------
X : array-like of shape (n_samples, n_features)
Input samples.
y : array-like of shape (n_samples,) or (n_samples, n_outputs), \
default=None
Target values (None for unsupervised transformations).
**fit_params : dict
Additional fit parameters.
Returns
-------
X_new : ndarray array of shape (n_samples, n_features_new)
Transformed array.
"""
# non-optimized default implementation; override when a better
# method is possible for a given clustering algorithm
if y is None:
# fit method of arity 1 (unsupervised transformation)
return self.fit(X, **fit_params).transform(X, **fit_params)
else:
# fit method of arity 2 (supervised transformation)
return self.fit(X, y, **fit_params).transform(X, **fit_params)