Dear Sir, I have made such a transformer, below given is an example that generates 3 new columns, from existing 2 columns of a numpy array , first column is for element wise addition, second is for element wise multiplication and third is for element wise division .
>>> 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 ]]) >>> So in this way a function transformer can be used to add new features generated from existing columns ? On Fri, Jun 18, 2021 at 4:15 PM Manprit Singh <manpritsingh...@gmail.com> wrote: > Dear sir , > > Just need to know if I can use a function transformer to generate new > columns in the data set . > > Just see the below written pipeline > > num_pipeline = Pipeline([('imputer', SimpleImputer(strategy="median")), > ('attribs_adder', column_adder), > ('std_scaler', StandardScaler()), > ]) > This pipeline is for numerical attributes in the dataset, firstly it will > treat all mising values in the data set using SimpleImputer , then i have > made a function to add three more columns in the existing data, i have made > a function transformer with this function and then StandardScaler . > > The columns being added are generated from existing columns (by element > wise division of two columns) . So Using a function transformer is ok ? >
_______________________________________________ scikit-learn mailing list scikit-learn@python.org https://mail.python.org/mailman/listinfo/scikit-learn