Exemple #1
0
    def fit(self, x, y=None):
        x = np.asfortranarray(x)
        _, self.n_features = x.shape
        # 1) Get all simple features
        simple = (SimpleFeature(e, index=i) for e, i in product(self.exponents, range(self.n_features)))
        simple = get_valid((s, s.transform(x)) for s in simple)
        # 2) Get all operator features
        operator = (OperatorFeature(s, op, operator_name=op_name) for (s, _), (op_name, op) in product(simple, self.operators.items()))
        operator = get_valid((o, o.transform(x)) for o in operator)
        # 3) Get all product features
        all_ = simple + operator

        if self.consider_products:
            combs = chain(product(operator, simple), combinations(simple, 2))
            prod = [ProductFeature(feat1, feat2) for (feat1, _) , (feat2, _) in combs]
            all_ += get_valid((p, p.transform(x)) for p in prod)

        all_ = get_valid(all_)
        feat_cls, features = zip(*[(c, np.array(f)) for c, f in all_])

        self._precomputed_features = np.array(list(features)).T  # speed up fit_transform
        self._precompute_hash = _hash(x)

        self.feat_cls = list(feat_cls)
        return self
Exemple #2
0
 def inner(x):
     key = _hash(x)
     if key not in cache:
         cache[key] = _hash(x)
     return cache[key]
Exemple #3
0
 def transform(self, x):
     if self._precompute_hash == _hash(x):
         return self._precomputed_features
     else:
         features = [c.transform(x) for c in self.feat_cls]
         return np.array(list(features)).T