1
$\begingroup$

I have a column named Street that has 2 values: Paved and Gravel. Here is what print(train[binary_columns[0]].unique().tolist()) gives me:

['Pave', 'Grvl']

I want to encode these values in binary like this:

df['Street'] = df['Street'].replace(['Pave', 'Grvl'], [1, 0])

But I wonder if this is a good idea. Wouldn't the computer interpret this as Pave > Grvl? How does the computer differentiate between binary and integer encoding?

$\endgroup$

2 Answers 2

2
$\begingroup$

Your categorical variable has two levels, so there is no actual difference between dummy-coding vs. simply entering the variable into the analysis. That is, to dummy code you would create one new variable with two values but your original variable is already one variable with two values. Dummy-coding is important for variables with more than two possible values. So, in this case the computer won't consider Pave > Grvl.

But if you have more than two variables then you should use dummy variables.

For your data, you can use pandas.get_dummies() or sklearn's one hot encoder to achieve your result.

$\endgroup$
3
  • $\begingroup$ So then when am I allowed to use binary encoding? Because that same problem would arise even for variables like gender, which makes a lot of data scientists depend on binary encoding. $\endgroup$ Jun 13, 2019 at 6:24
  • $\begingroup$ Sorry I got mixed up, have edited. $\endgroup$
    – bkshi
    Jun 13, 2019 at 6:34
  • $\begingroup$ I already know how to do OHE. My concern was whether the computer will interpret it as Pave > Grvl. Thank you. $\endgroup$ Jun 13, 2019 at 6:38
0
$\begingroup$
  1. How to encode?

sklearn.preprocessing provides various classes for this purpose, LabelBinarizer is one of them.

  1. Wouldn't the computer interpret this as Pave > Grvl?

Consider an example, where people prefer Paved house in comparison to Graveled. Then their is a relationship between values and hence it should be treated as something like you have mentioned, other wise it should be independent values(refer next answer).

  1. How does the computer differentiate between binary and integer encoding?

As I mentioned above, if the categorical values have some relationship(as mentioned above), then in such a case it should be integer values(0,1,2 and so on), otherwise it should be binary. Binary representation will help us in presenting as an independent value to ML model (however it doesn't make much sense in this case as you just have 2 values). But consider an example where a feature have more than 2 categorical values. If they all are independent then it should be represented as binary value i.e in the form of OneHotEncoding(refer sklearn.preprocessing classes).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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