One-Hot Encoding with DictVectorizer
Tags: tutorial, machinelearning, datascience, data
Published: 2023-06-22 11:20:48

## Introduction


Usually, datasets used for training machine learning models contain feature columns with various data types, one of which is categorical features, these features are non-numerical. 
Examples include:
- Name (with values like "Kelvin", "Jonathan")
- Gender (with values like "male", "female")
- Country (with values like "Nigeria", "USA")


In many cases, categorical features are represented as strings, and most machine learning algorithms cannot process strings unless we convert them to numerical values.


There are various methods to deal with categorical variables, one of which is **One-Hot encoding**.
This article will guide you on how to implement one-hot encoding with DictVectorizer.


## Table of content

1. [Prerequisites](#chapter-1)
2. [What is One-Hot Encoding?](#chapter-2)
3. [What is DictVectorizer?](#chapter-3)
4. [Implementing One-Hot encoding](#chapter-4)
5. [Conclusion](#chapter-5)

### Prerequisites <a name="chapter-1"></a>

- Basic understanding of Python
- Basic understanding of data science libraries e.g Pandas, Numpy, Scikit learn
- Jupyter notebook to test try codes yourself
- Basic understanding of Machine learning  

### What is One-Hot Encoding? <a name="chapter-2"></a>




One hot encoding is a method used for converting categorical variables to numerical values.

One-hot encoding assigns binary features to unique categorical values. If a value is present in an observation, its corresponding feature is set to 1; otherwise, it is set to 0.
For example: 


![One hot encoding image by author](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1sqrtic4y2ow059d97dj.png)


In the above diagram, the original data has a column called "Country" that contains the following values: "NIGERIA", "USA", "JAPAN", and "TOGO".


One-hot encoding created four new binary columns, one for each unique category, with a value of 1 indicating that the category is present and a value of 0 indicating that it is not.






### What is DictVectorizer? <a name="chapter-3"></a>




As implied by its name, DictVectorizer is a class that transforms lists of feature-value mappings(Python dict objects) into vectors.

### Implementing one-hot encoding <a name="chapter-4"></a>

Now that you have a grasp of One-Hot encoding and DictVectorizer, let's dive into putting it into practice. 

To implement one-hot encoding, tabular data with categorical features is needed, hence a [Kaggle](https://www.kaggle.com/) dataset will be used in this guide, follow this [link](https://www.kaggle.com/datasets/prathamtripathi/drug-classification?select=drug200.csv) to download the dataset used in this guide. 

The following steps will guide you on using DictVectorizer to implement one hot encoding.




#### 1. Read and process the data


```
# import libraries 
import pandas as pd
import numpy as np
# Read the data
data = pd.read_csv("drug200.csv")
# select categorical data 
columns = ["Sex", "BP", "Cholesterol"]
# select only the first 10 rows
categorical=data[columns].iloc[:11]
categorical
```


- The above code imports necessary data science libraries needed to read and process the data 


- The code also reads the CSV (comma-separated values) data into a pandas DataFrame and selects the columns with categorical features. 


Here is the output of the code 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amje8mqicew1ezg4zi8i.png)


  


#### 2. Convert categorical features to a list of dictionaries




```
categorical_dict = categorical.to_dict(orient="records")
categorical_dict
```


Here is the output of the code.




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/byta92zmr7th8tye20b7.png)






#### 3. Initiate an instance of DictVectorizer class




```
from sklearn.feature_extraction import DictVectorizer
dv = DictVectorizer(sparse = False)


```


Here's a breakdown of what the code does:


- `from sklearn.feature_extraction import DictVectorizer:` 
This line imports the `DictVectorizer` class from the `sklearn.feature_extraction` module. 


- `dv = DictVectorizer(sparse=False):` 
This line creates an instance of the DictVectorizer class and assigns it to the variable `dv`.


- The `sparse=False ` argument is passed to the `DictVectorizer` constructor, indicating that the resulting matrix representation should be a dense numpy array rather than sparse. 




#### 4. Fit and transform the dictionary.


```
dv.fit(categorical_dict)
transformed_data=dv.transform(categorical_dict)
transformed_data
```


Here is the result of the above code.




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iqibghkq6llfy1t6iu3d.png)




We successfully One-Hot encoded the categorical data with DictVectorizer. 


To explore more, let's check how the categorical data were represented on the transformed data.


```
dv.get_feature_names()
```
Here is the output of the code 




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/40eagxrou4aega4ixf5m.png)


Furthermore, here is a diagram of how the column/feature names are stored on the transformed data. 




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/in940mwuwxwf6740nv0r.png)


### Conclusion <a name="chapter-5"></a>


DictVectorizer is a way of performing One-Hot encoding on categorical data, it takes a list of dictionaries and transforms them to numpy arrays. 


DictVectorizer is easy to implement and makes machine learning model deployment simpler. 


I would recommend utilizing DictVectorizer to transform your categorical data into numerical representations for your future machine learning projects.


Thanks for reading this article. If you have any further questions or would like to connect, feel free to reach out to me on [Twitter](https://twitter.com/cyber_holics) and on  [LinkedIn](https://www.linkedin.com/in/victor-oshimua-4b2945214/). I appreciate your engagement and look forward to staying connected.