简体   繁体   中英

Write custom kernel for svm in R

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM