2
$\begingroup$

I have written the following code for encoding categorical features of the dataframe( named 't') -

from sklearn.compose import ColumnTransformer

categorical_columns = ['warehouse_ID', 'Product_Type','month', 'is_weekend', 'is_warehouse_closed']

transformer = ColumnTransformer(transformers= [('le',preprocessing.LabelEncoder()  ,categorical_columns)],remainder= 'passthrough')

Train_transform = transformer.fit_transform(t) 

But it is showing this error -

TypeError: fit_transform() takes 2 positional arguments but 3 were given

$\endgroup$
0

1 Answer 1

0
$\begingroup$

You don't even necessarily need the LabelEncoder()

One simple approach using a minimal example is:

from sklearn.compose import ColumnTransformer
from sklearn import preprocessing
import pandas as pd

categorical_columns = ['warehouse_ID', 'Product_Type','month', 'is_weekend', 'is_warehouse_closed']

t = pd.DataFrame([['CategoryString'] * len(categorical_columns)], columns=categorical_columns) # create dummy t; to be removed
display(t)

t = pd.DataFrame({col: t[col].astype('category').cat.codes for col in categorical_columns}, index=t.index)

display(t)

More approaches, incl. how to use LabelEncoder() for multiple columns, can already be found here

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.