One Hot Encoding of datasets in Python
Sometimes in datasets, we encounter columns that contain numbers of no specific order of preference. The data in the column usually denotes a category or value of the category and also when the data in the column is label encoded. This confuses the machine learning model, to avoid this the data in the column should be One Hot encoded.
One Hot Encoding –
It refers to splitting the column which contains numerical categorical data to many columns depending on the number of categories present in that column. Each column contains “0” or “1” corresponding to which column it has been placed.
For example :
Consider the data where fruits and their corresponding categorical value and prices are given.
|FRUIT||CATEGORICAL VALUE OF FRUIT||PRICE|
The output after one hot encoding the data is given as follows,
Below is the Implementation in Python –
The following example is the data of zones and credit scores of customers, the zone is a categorical value which needs to be one hot encoded.
To one hot encode the zone column –
The output contains 5 columns, one column for the price, and the remaining 4 columns representing the 4 zones.
One hot encoder only takes numerical categorical values, hence any value of string type should be label encoded before one-hot encoded.
The below example has the data of geography and gender of the customers which has to be label encoded first.
Label encoding the data –
One Hot Encoding Gender and Geography Columns –
The output contains 5 columns, 2 columns representing the gender, male and female, and the remaining 3 columns representing the countries France, Germany, and Spain.
- The one hot encoder does not accept 1-dimensional array or a pandas series, the input should always be 2 Dimensional.
- The data passed to the encoder should not contain strings.
Python Example for Beginners
Two Machine Learning Fields
There are two sides to machine learning:
- Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
- Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.
Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes
Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!
Latest end-to-end Learn by Coding Recipes in Project-Based Learning:
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Google –> SETScholars