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 ]])