Write custom kernel for svm in R

对着背影说爱祢 提交于 2019-12-12 04:35:13

问题


I'm looking to use the svm() function of the e1071 package in R. I am new to this package and I was wondering if it is possible to write your own custom kernel callable in svm(). I see that there are several kernels pre-loaded, but I don't see a cosine similarity kernel, which is what I need.

Alternatively, is there another package in R allowing you to run SVM with cosine similarity kernel?


回答1:


The bad news is it is currently not supported in e1071. There was a discussion many years ago about it https://stat.ethz.ch/pipermail/r-help/2002-July/023299.html.

The good news is that cosine similarity kernel is defined as

K(x, y) = <x, y> / (||x|| ||y||) = <x / ||x||, y / ||y||>

so you do not have to implement a custom kernel, just normalize your data and run regular linear kernel SVM. In other words - compute (sample-wise) regular euclidean norms and divide each sample by its own norm. Then run linear SVM, the result is equivalent to running cosine kernel on raw data.

If you want to coduct research with customized machine learning models, R is probably not the way to go (as it is rather a tool for applying existing techniques than a well designed development system - if you want something custom in R you basically have to go to C++ level). Instead you might want to consider python and numerous libraries (such as scikit-learn + pykernels) which give you much more flexibility.



来源:https://stackoverflow.com/questions/42633265/write-custom-kernel-for-svm-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!