Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One Hot Encoding a 2 categorical variable

For variables with two categories, do they need to be One Hot Encoded? In my dataset I have a binary variable as either 1 or 0. Do I need to transform that variable in a pipeline for my model or do I leave it as is?

variable = np.array([0,0,0,1,0,1,0]).reshape(-1,1)
ohc = OneHotEncoder()
ohc.fit(variable)
like image 715
Jack Armstrong Avatar asked Apr 21 '26 19:04

Jack Armstrong


1 Answers

If your variable is already binary (only two classes: 0 and 1), you can say that this variable is already One Hot Encoded, so you don't need to to OneHotEncoder again with Sklearn function.

Moreover, in general terms, if you binary variable is categorical, you have to transform it to numerical using LabelEncoder. Anyway, in your example, your variable was already numerical.

like image 175
Alex Serra Marrugat Avatar answered Apr 30 '26 07:04

Alex Serra Marrugat