Encoding categorical data
In some data set there may be categorical data. For example we can consider following data set.
Country Age Salary
USA 35 $62,000
UK 25 $58,000
Canada 40 $69,000
USA 40 $78,000
UK 30 $64,000
Canada 30 $61,000
If we consider the country column, it can be categorized by country. We are going to categorize it by giving USA=1, UK=2 and Canada=3.
* Note: Since we are going to build mathematical model, we can't deal with data like row values in Country column. Therefore we convert them to numerical values.
Country Age Salary
1 35 $62,000
2 25 $58,000
3 40 $69,000
1 40 $78,000
2 30 $64,000
3 30 $61,000
Now we have a categorized data set. But still we have a problem. The problem is while USA=1 Canada takes 3 then if we put these values in a mathematical calculation, Canada will take higher value than USA although it should not be like that(according to this eg). How to prevent this in ML? The solution is Dummy Encoding or dummy variables.
Dummy Encoding
In dummy variables concept, the categorical column is replaced with new columns corresponding to the number of categories. For eg: The Country column will be replaced with USA,UK and Canada columns.
USA UK Canada Age Salary
1 0 0 35 $62,000
0 1 0 25 $58,000
0 0 1 40 $69,000
1 0 0 40 $78,000
0 1 0 30 $64,000
0 0 1 30 $61,000
Encoding in Python
# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
X[:, 0] = labelencoder_X.fit_transform(X[:, 0])
onehotencoder = OneHotEncoder(categorical_features = [0])
X = onehotencoder.fit_transform(X).toarray()
Encoding in R
# Encoding categorical data
dataset$Country = factor(dataset$Country,
levels = c("USA", "UK", "Canada"),
labels = c(1,2,3))
Country Age Salary
USA 35 $62,000
UK 25 $58,000
Canada 40 $69,000
USA 40 $78,000
UK 30 $64,000
Canada 30 $61,000
If we consider the country column, it can be categorized by country. We are going to categorize it by giving USA=1, UK=2 and Canada=3.
* Note: Since we are going to build mathematical model, we can't deal with data like row values in Country column. Therefore we convert them to numerical values.
Country Age Salary
1 35 $62,000
2 25 $58,000
3 40 $69,000
1 40 $78,000
2 30 $64,000
3 30 $61,000
Now we have a categorized data set. But still we have a problem. The problem is while USA=1 Canada takes 3 then if we put these values in a mathematical calculation, Canada will take higher value than USA although it should not be like that(according to this eg). How to prevent this in ML? The solution is Dummy Encoding or dummy variables.
Dummy Encoding
In dummy variables concept, the categorical column is replaced with new columns corresponding to the number of categories. For eg: The Country column will be replaced with USA,UK and Canada columns.
USA UK Canada Age Salary
1 0 0 35 $62,000
0 1 0 25 $58,000
0 0 1 40 $69,000
1 0 0 40 $78,000
0 1 0 30 $64,000
0 0 1 30 $61,000
Encoding in Python
# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
X[:, 0] = labelencoder_X.fit_transform(X[:, 0])
onehotencoder = OneHotEncoder(categorical_features = [0])
X = onehotencoder.fit_transform(X).toarray()
Encoding in R
# Encoding categorical data
dataset$Country = factor(dataset$Country,
levels = c("USA", "UK", "Canada"),
labels = c(1,2,3))
Comments
Post a Comment