Previously, we saw how we can combine data from different sources into a unified dataframe.
Now, we have a lot of columns that have different types of data.
Our goal is to transform the data into a machine-learning-digestible format.
All machine learning algorithms are based on mathematics.
So, we need to convert all the columns into numerical format. Before that, let's see all the different types of data we have.
Taking a broader perspective, data is classified into numerical and categorical data:
Numerical: As the name suggests, this is numeric data that is quantifiable.
Categorical: The data is a string or non-numeric data that is qualitative in nature.
Numerical data is further divided into the following:
Discrete: To explain in simple terms, any numerical data that is countable is called discrete.
For example: the number of people in a family or the number of students in a class.
Discrete data can only take certain values (such as1,2,3,4,etc).
Continuous: Any numerical data that is measurable is called continuous.
For example: the height of a person or the time taken to reach a destination.
Continuous data can take virtually any value (for example, 1.25, 3.8888, and 77.1276).
Categorical data is further divided into the following:
Ordered: Any categorical data that has some order associated with it is called ordered categorical data.
For example: movie ratings (excellent, good, bad, worst) and feedback (happy, not bad, bad).
You can think of ordered data as being something you could mark on a scale.
Nominal: Any categorical data that has no order is called nominal categorical data.
Examples include gender and country.
Handling Categorical Data
There are some algorithms that can work well with categorical data, such as decision trees. But most machine learning algorithms cannot operate directly with categorical data. These algorithms require the input and output both to be in numerical form. If the output to be predicted is categorical, then after prediction we convert them back to categorical data from numerical data.
Let's discuss some key challenges that we face while dealing with categorical data:
High cardinality: Cardinality means uniqueness in data. The data column, in this case, will have a lot of different values. A good example is User ID – in a table of 500 different users, the User ID column would have 500 unique values.
Rare occurrences: These data columns might have variables that occur very rarely and therefore would not be significant enough to have an impact on the model.
Frequent occurrences: There might be a category in the data columns that occurs many times with very low variance, which would fail to make an impact on the model.
Won’t fit: This categorical data, left unprocessed, won’t fit our model.
Encoding:
To address the problems associated with categorical data, we can use encoding. This is the process by which we convert a categorical variable into a numerical form.
Replacing :
This is a technique in which we replace the categorical data with a number. This is a simple replacement and does not involve much logical processing.
Here is an example of how we replace categorical data with a number.