8000 Custom transformer for ColumnTransformer to access multiple columns with pandas input · scikit-learn scikit-learn · Discussion #25201 · GitHub
[go: up one dir, main page]

Skip to content

Custom transformer for ColumnTransformer to access multiple columns with pandas input #25201

Answered by glemaitre
wijayam asked this question in Q&A
Discussion options

You must be logged in to vote

Your code should be working. I will provide an example showing that it works

# %%
from sklearn.datasets import load_iris
from sklearn.utils import shuffle

iris = load_iris(as_frame=True)
X, y = iris.data, iris.target
y = iris.target_names[y]
X, y = shuffle(X, y, random_state=0)

# %%
from sklearn.base import BaseEstimator, TransformerMixin


class Multiplier(TransformerMixin, BaseEstimator):
    def __init__(self, column_name_1, columns_name_2):
        self.column_name_1 = column_name_1
        self.columns_name_2 = columns_name_2

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        return (X[self.column_name_1] * X[self.columns_name_2]).to_frame()


# %%

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
3 replies
@wijayam
Comment options

@glemaitre
Comment options

@wijayam
Comment options

Answer selected by wijayam
Comment options

You must be logged in to vote
1 reply
@cnesty
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
0