## Label Encoding in Machine Learning: Simplifying Categorical Variables

As machine learning algorithms become increasingly popular, we are faced with the challenge of dealing with various types of data, including categorical variables. Categorical variables are variables that take on a limited set of values, such as gender (male or female), color (red, blue, green), or geographic location (USA, Europe, Asia).

These variables cannot be used directly in machine learning algorithms, as they require numerical values. This is where label encoding comes into play.

### What is Label Encoding?

Label encoding is a method of encoding categorical variables so that they can be used as input for machine learning models.

The process of label encoding involves assigning integer values to the different categories in a variable. For example, if we have a categorical variable “animal” with categories “dog,” “cat,” and “bird,” we can label encode these categories as follows:

- “dog” 0
- “cat” 1
- “bird” 2

By assigning integer values to the categories, we can now represent the categorical variable in a numerical format, making it easier to use as an input for machine learning algorithms.

## Performing Label Encoding in Python

Python offers several libraries for performing label encoding, including pandas and scikit-learn’s preprocessing module. Let’s take a look at how we can use these libraries to perform label encoding on a pandas DataFrame.

### Importing necessary libraries and modules

We first need to import the necessary libraries and modules. This includes pandas and sklearn.preprocessing’s LabelEncoder() function.

```
import pandas as pd
from sklearn.preprocessing import LabelEncoder
```

### Using apply() method to perform label encoding

Now, let’s create a sample pandas DataFrame with a categorical variable “fruit” that contains different fruit types:

`df = pd.DataFrame({'fruit': ['apple', 'banana', 'orange', 'apple', 'orange']})`

fruit | |
---|---|

0 | apple |

1 | banana |

2 | orange |

3 | apple |

4 | orange |

We can now use the apply() method to perform label encoding on the “fruit” column:

```
le = LabelEncoder()
df['fruit_encoded'] = df['fruit'].apply(le.fit_transform)
```

fruit | fruit_encoded | |
---|---|---|

0 | apple | 0 |

1 | banana | 1 |

2 | orange | 2 |

3 | apple | 0 |

4 | orange | 2 |

The apply() method applies the LabelEncoder() function to each value in the “fruit” column, and returns an encoded value for each category in a new column “fruit_encoded.”

## Conclusion

Label encoding is a simple and effective method for encoding categorical variables in machine learning. By assigning integer values to different categories, we can represent categorical variables in a numerical format, making it easier to use as an input for machine learning algorithms.

Python offers several libraries for performing label encoding, including pandas and scikit-learn’s preprocessing module. The apply() method can be used to perform label encoding on pandas DataFrames.

With label encoding, we can simplify the process of working with categorical data and improve the accuracy of our machine learning models.

## 3) Example of Label Encoding in Pandas DataFrame

As discussed earlier, label encoding is a popular technique used to convert categorical data into numerical data that can be used effectively in machine learning models. In this section, we will explore an example of label encoding in pandas DataFrame.

Suppose we have a dataset containing information about basketball players such as name, position, team, and nationality. Let’s create a pandas DataFrame to store this data.

### Creating a pandas DataFrame for basketball player data

```
import pandas as pd
data = {'Name': ['LeBron James', 'Stephen Curry', 'Kawhi Leonard', 'Kobe Bryant', 'Kevin Durant', 'Russell Westbrook'],
'Position': ['SF', 'PG', 'SF', 'SG', 'SF', 'PG'],
'Team': ['Lakers', 'Warriors', 'Clippers', 'Lakers', 'Nets', 'Wizards'],
'Nationality': ['USA', 'USA', 'USA', 'USA', 'USA', 'USA']}
df = pd.DataFrame(data)
print(df)
```

### Output:

```
Name Position Team Nationality
0 LeBron James SF Lakers USA
1 Stephen Curry PG Warriors USA
2 Kawhi Leonard SF Clippers USA
3 Kobe Bryant SG Lakers USA
4 Kevin Durant SF Nets USA
5 Russell Westbrook PG Wizards USA
```

As we can see, the Position, Team, and Nationality columns contain categorical data. To use this data in machine learning models, we need to convert it into numerical data using label encoding.

### Performing label encoding on multiple columns using apply() method

To demonstrate how label encoding works, let’s apply it to the Position, Team, and Nationality columns using the apply() method of pandas DataFrame.

```
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df['Position'] = le.fit_transform(df['Position'])
df['Team'] = le.fit_transform(df['Team'])
df['Nationality'] = le.fit_transform(df['Nationality'])
print(df)
```

### Output:

```
Name Position Team Nationality
0 LeBron James 1 1 0
1 Stephen Curry 0 2 0
2 Kawhi Leonard 1 0 0
3 Kobe Bryant 2 1 0
4 Kevin Durant 1 3 0
5 Russell Westbrook 0 4 0
```

As we can see, the categorical data has been converted into numerical data using label encoding. The Position column has been converted to 0, 1, and 2, each representing a different position.

The Team column has been converted to 0, 1, 2, 3, and 4, each representing a different team. The Nationality column has been converted to 0, representing the USA nationality.

Now, this pandas DataFrame with label encoded data can be used in machine learning models.

## 4) Interpretation of Label Encoding in Pandas DataFrame

Now that we have seen an example of label encoding using pandas DataFrame, let’s discuss how it works. Label encoding assigns integer values to each unique category in the data.

Each unique category is assigned a unique integer value, starting from 0. Let’s take an example to understand it better.

Suppose we have a categorical variable ‘Pet’ that contains three categories: ‘dog’, ‘cat’, and ‘bird’. Label encoding would assign the following integer values to these categories:

- ‘dog’ 0
- ‘cat’ 1
- ‘bird’ 2

As we can see, each category has been assigned a unique integer value.

This way, we can represent the categorical variable ‘Pet’ in numerical format. The next step is to perform label encoding on the DataFrame using sklearn’s LabelEncoder.

We can apply LabelEncoder to Pandas DataFrame using the apply() method, as we demonstrated earlier. LabelEncoder fit_transform() function is applied to each column to handle the transformation.

Any given unique value for a categorical variable, the fit() method finds it and labels the value with an integer value. The transform() method applies the label we got to the feature or input data in question.

In the output of label encoded Pandas DataFrame, each unique categorical data point gets a unique integer value. Therefore, a categorical variable with N unique categories would be encoded with N different integer values starting from 0.

## Conclusion

In this article, we have learned about how label encoding works to convert categorical data into numerical data. We explored an example of label encoding using pandas DataFrame with multiple categorical variables, and how the apply() method can be used to perform label encoding on pandas DataFrame.

With this understanding of label encoding, we can convert categorical data into numerical data that can be used in machine learning models. Label encoding is a crucial technique that helps transform data to numerical format for efficient use in machine learning algorithms.

The process of assigning unique integer values to each categorical value enables the transformation of categorical variables into numerical format, making it easier to use as an input for machine learning models. With pandas DataFrame and scikit-learn’s preprocessing module, label encoding can be easily performed.

Categorical data is simplified with this method and facilitates analysis through machine learning models. In a nutshell, label encoding has a significant role in enhancing the effectiveness of machine learning models.