Up to now, we already learn about regression, classification and clustering in our machine learning and pattern recognition post series. During this post, we will learn another powerful method in machine learning, which is kernel method, or also called kernel trick! Why do we use kernel trick? Some reasons are (1) we don’t need to think how to form a design matrix. Just imagine for example if our features are “words”, not number. How can we form design matrix for it? Using kernel method, we can just define our kernel, for example using hamming distance of our “words”, etc. (2) Kernel method provides us a way to project our data into much higher dimensional space, even equals to infinite dimensional space. We can take benefit of it so that our model performs better.
So, how to do kernel trick? We will demonstrate how to do that in our regularized regression. In our regularized regression using LSE we already talk here, we get the loss function to be minimized as follows.
where is design matrix, is vector of our training data , and is our regression parameter. From what we already talk here too, we already derive that our regression parameter as follows.
Our regression parameter above, in kernel trick, is usually mentioned as primal form. And in the kernel trick, we will try to modify it into so-called “dual form”. To do that, let’s modify our formula a little bit.
To find the dual form of regression parameter, we will plug our above into our loss function, then, instead of minimizing w.r.t , we will minimize our loss function w.r.t . Here we go.
We will define “kernel” . Thus, our derivation will be:
To minimize our loss function/error function , we take the first differential of it with respect to , and make it equals to zero. Here we go.
Voila! We already successfully derive parameter that minimizes our regression loss function in dual form. To make prediction of , we can just replug to our prediction formula:
See, now, we calculate our regression parameter and prediction using kernel , and we can define our kernel as what we want! As long it’s still reasonable. Awesome, right? 😀 Some popular kernels are fisher kernel, polynomial kernel and RBF (Radial Basis Fuction) kernel. For example, in RBF kernel, the equal feature space of the kernel has an infinite number of dimensions.
Remember! That kernel is a trick. So, we can use it in other machine learning methods, such as in Bayesian regression, SVM, and so on. In this post, we demonstrate to derive kernel formula, which is bringing our primal form to dual form, for our regularized regression problem. To do for other machine learning methods, we can derive with similar process we already demonstrate in this post.
This video below talks about how we can implement kernel trick in k-means clustering in python.