OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
ExclusionTransformer.py
Go to the documentation of this file.
1 from ._CustomTransformer import _CustomTransformer
2 
3 import numpy as np
4 
5 
7  '''
8  Exclude certain columns from being transformed by the given transformer.
9  The passed in transformer should be a transformer class, and exclude_slice can
10  be any object which, when used to slice a numpy array, will give the
11  appropriate columns which should be excluded. So, for example:
12  - slice(1)
13  - slice(-3, None)
14  - slice(1,None,2)
15  - np.array([True, False, False, True])
16  etc.
17  '''
18  def __init__(self, exclude_slice, transformer, transformer_args=[], transformer_kwargs={}):
19  self.transformer = transformer(*transformer_args, **transformer_kwargs)
20  self.excl = exclude_slice
21 
22  def _fit(self, X, *args, **kwargs):
23  cols = np.arange(X.shape[1])
24  cols = [c for c in cols if c not in cols[self.excl]]
25  self.transformer.fit(X[:, cols])
26  self.keep = cols
27 
28  def _transform(self, X, *args, **kwargs):
29  Z = np.zeros_like(X)
30  Z[:, self.keep] = self.transformer.transform(X[:, self.keep])
31  Z[:, self.excl] = X[:, self.excl]
32  return Z
33 
34  def _inverse_transform(self, X, *args, **kwargs):
35  Z = np.zeros_like(X)
36  Z[:, self.keep] = self.transformer.inverse_transform(X[:, self.keep])
37  Z[:, self.excl] = X[:, self.excl]
38  return Z
def __init__(self, exclude_slice, transformer, transformer_args=[], transformer_kwargs={})