1

I have written a code that can add 3 new columns into a NumPy array, using function transformer(1 st column is element-wise +, 2nd is element-wise *, 3rd is element-wise /. Just need to know if in this way I can add new features to an existing dataset

import numpy as np
from sklearn.preprocessing import FunctionTransformer
def col_add(x):
    x1 = x[:, 0] + x[:, 1]
    x2 = x[:, 0] * x[:, 1]
    x3 = x[:, 0] / x[:, 1]
    return np.c_[x, x1, x2, x3]

col_adder = FunctionTransformer(col_add) arr = np.array([[2, 7], [4, 9], [3, 5]]) arr array([[2, 7], [4, 9], [3, 5]]) col_adder.transform(arr) # will add 3 columns array([[ 2. , 7. , 9. , 14. , 0.28571429], [ 4. , 9. , 13. , 36. , 0.44444444], [ 3. , 5. , 8. , 15. , 0.6 ]])

Multivac
  • 2,959
  • 2
  • 9
  • 26
  • You asked if this can add new features, then provided code that shows that indeed it has added new features. What exactly is the question? – Ben Reiniger Dec 16 '21 at 16:37

1 Answers1

1

Scikit-learn FeatureUnion is what you are looking for:

import numpy as np
from sklearn.preprocessing import FunctionTransformer
from sklearn.pipeline import FeatureUnion
from sklearn.datasets import load_iris

X, _ = load_iris(return_X_y = True)

def col_add(x): x1 = x[:, 0] + x[:, 1] x2 = x[:, 0] * x[:, 1] x3 = x[:, 0] / x[:, 1] return np.c_[x, x1, x2, x3]

transformer = FunctionTransformer(col_add) f = FeatureUnion([("t",transformer)]).fit(X)

Xnew = f.transform(X)

Multivac
  • 2,959
  • 2
  • 9
  • 26