sklearn labelencoder, sklearn label encoder, sklearn label encoding, sklearn encoding, labelencoder, laber encoding, hot encoding, sklearn hotencoder, categorical values to numeric values, sklearn onehotencoding, one hot encoding, sklearn one hot encoding.
Sklearn labelencoder is a process of converting categorical values to numeric values so that machine learning models can understand the data and find hidden patterns. Although, there are various ways for categorical encoding and sklearn labelencoder is one of them. In this short article, we will learn how sklearn labelencoder works by taking various examples. Moreover, we will also compare sklearn labelencoder with sklearn one hot encoder.
|You may like:|
Machine learning full course with 30+ algorithms
What is sklearn module?
Sklearn, also known as Scikit-learn is probably the most useful library for machine learning in Python. The sklearn library contains a lot of efficient tools for machine learning and statistical modeling including classification(KNN, SVM, Decision trees), regression(linear regression, isolation forest, random forest), clustering(k-mean clustering), and dimensionality reduction(PCA). It also supports Python numerical and scientific libraries like NumPy and SciPy.
More importantly, it has various methods for data preprocessing including random state, data splitting, data encoding, and many more. In this article, we will focus on only one encoding method which is one-hot encoding.
How does sklearn labelencoder works?
Sklearn labelencoder converts the labels or categories into labels so that the machine learning model can understand the dataset. The label encoding assigns a new numeric value to each of the categories as shown below:
As you can see, the label encoder has assigned a specific value for each of the categories.
Examples of sklearn labelencoder
Now, we will take various examples of sklearn label encoder and will solve various examples. Here is what we are going to do in this section:
- Sklearn label encoding one column
- Sklearn label encoding multiple columns
Example 1: Sklearn label encoding on one column
Let us first import the dataset and then use the sklearn label encoding to convert categorical values to numeric ones.
# importing pandas import pandas as pd # importing dataset data = pd.read_excel('Label_Encoding.xlsx') # heading of data data.head()
As you can see, the output of the data is categorical. Now, we will use the sklearn labelencoder to convert these values into numeric values.
# Import sklearn labelencoder from sklearn import preprocessing # initializing sklearn labelencoder label_encoder = preprocessing.LabelEncoder() # encoding marrige column data['Marrige_Status']= label_encoder.fit_transform(data['Marrige_Status']) # printing data['Marrige_Status'].unique()
As you can see, there are only numeric values in the output column.
Example 2: sklearn label encoding multiple columns
Encoding multiple columns in sklearn is very much similar to a single column. Here we just need to specify the names of all columns.
# importing the dataset df = pd.read_csv("Placement_Data_Full_Class.csv") # data frame df.head()
As you can see, there are many columns with categorical values. Let us now apply the sklearn labelencoder to convert the data into numeric values.
# multiple columns cols = ['workex', 'status', 'hsc_s', 'degree_t'] # sklearn labelencoder df[cols] = df[cols].apply(preprocessing.LabelEncoder().fit_transform) # print df.head()
As you can see, we get the encoded dataset.
Label encoding assign each categorical value an integer value based on alphabetical order. In this short article, we learned how we can use the sklearn labelencoder to convert categorical values to numeric ones.
1 thought on “Sklearn labelencoder – Examples”