Feature Transformation Techniques
Zuhaib Ashraf
Innovating Today, Shaping Tomorrow: AI Solutions For Every Field. Let's talk about Artificial intelligence| Machine Learning | Deep Learning | Computer Vision | AIOps | MLOps | GDSC AI/ML Lead
Introduction:
Data preprocessing is an important step in machine learning projects. Real-life data can be messy and unorganized, so we need to clean it up before using it in our models. This preprocessing step is crucial for getting good results. Feature Transformation is a technique we should always use, no matter what type of model we're working with. It helps us improve the data so our models can perform better.
Explain Feature transformation:
Feature transformation is a technique that we used to boost the performance of our machine learning algorithm with the help of mathematical formulas. We apply mathematical formulas on features to transform them in a form that directly boost the performance of machine learning algorithm.
How feature transformation increase the performance of machine learning algorithm?
The answer is, the distribution of our data is not normally distributed which has the very large impact on linear models like linear regression, logistic regression etc. Feature transformation technique used mathematical formulas to normalize the distribution. In that way, feature transformation boost the performance of machine learning algorithm.
Before applying feature transformation:
After applying feature transformation:
How normal distribution gives the boost to the performance of machine learning algorithm?
As we know statistics is the mother of machine learning, when a statistician see a normal distribution he sees a way of solving a particular problem in an easy way, this can also said same for the machine learning algorithm as the base of machine learning algorithm is statistics so, when we give normally distributed data to machine learning algorithm the calculation that the algorithm has to made became more easy, so ultimately it takes less time on training and give best accuracy.
Without applying feature transformation the accuracy:
After applying feature transformation:
As we can see the clear boost in the accuracy of logistic regression.
Types of function transformer:
There are three types of function transformation available in sklearn library,
Function Transformer:
In function transformer, there are multiple types of function transformer. The most commonly used are,
Log transformer:
In log transform, we apply log to every value of that particular column to make there distribution normal so that the performance of machine learning algorithm boosts.
Where to use?
How it works?
Sometimes some columns has large scale than other columns, when we apply log transform, it will convert its scale into the range of other data. In that way its distribution transformed into normal distribution.
Before applying log transformation:
Accuracy of machine learning model before log transformation:
Applying log transformation:
Results after applying log transform:
As we can clearly see the improvements after applying log transform.
Reciprocal transform 1/x:
In reciprocal transform, we apply reciprocal of every value of that particular column to make there distribution normal so that the performance of machine learning algorithm boosts.
When to use?
How it works?
It's important to note that the reciprocal transform may not be suitable for all types of data or all situations. It should be applied judiciously and with an understanding of the underlying data characteristics and the specific problem at hand. Additionally, it's crucial to handle potential issues that may arise, such as division by zero or close-to-zero values, which can impact the effectiveness of the reciprocal transform.
Applying reciprocal transform before and after:
领英推荐
square transform:
The square transform computes the square of each feature value. For a given feature value x, the square transform is calculated as x^2.
Where to Use:
The square transform can be useful in various scenarios:
How it Works:
When the square transform is applied to a feature, it has several effects on the data:
As with any feature transformation technique, the square transform should be applied thoughtfully and in consideration of the data characteristics and the specific problem at hand. It may not always be suitable or beneficial, and it's important to evaluate its impact on the data and model performance.
Applying square transform before and after:
Custom transfomer:
You can use this particular piece of code for making custom mathematical transformer.
Power Transformer:
Power transformer is used when the desired output is more Gaussian based. Power transform has two types:
Box-Cox transform:
Box-Cox require data to be strictly positive, it does not even accepts zero in the data. Formula on the basis of Box-Cox transforms works is,
The exponent here is a variable called λ that varies over the range of -5 to 5 and in the process of searching it examine all possible values of λ. Finally, we choose the optimal values (resulting in the best approximation to a normal distribution) for that particular feature.
Scope:
Applied only on values greater than zero (positive values only zero excluded)
Internal working techniques:
Distribution Before applying Box-Cox:
Algorithm Accuracy before applying Box-Cox:
Distribution after applying Box-Cox:
Algorithm Accuracy before applying Box-Cox:
Yeo-Johnson transform:
This transformation is the specialized form of Box-Cox transform, we can apply Yeo-Johnson transform as well on the negative values. Formula of Yeo-Johnson,
Distribution Before applying yeo-johnson
Algorithm Accuracy before applying Yeo-Johnson:
Distribution after applying Yeo-Johnson:
Algorithm Accuracy before applying Yeo-Johnson:
Conclusion:
When we are working with linear models it is necessary to normalize the scale of data for the better performance. The feature transformer technique has lots of variants available to do the task of normalizing the distribution, at the end of the day it totally depends on us what approach we will go for.