在R中为svm编写自定义内核 [英] Write custom kernel for svm in R

查看:90
本文介绍了在R中为svm编写自定义内核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在R中使用e1071软件包的svm()函数.我是这个软件包的新手,我想知道是否有可能在svm()中编写您自己的可调用内核.我看到已经预加载了几个内核,但是我没有看到余弦相似性内核.

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.

或者,R中是否还有另一个软件包允许您使用余弦相似性内核运行SVM?

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

推荐答案

坏消息是e1071当前不支持它.许多年前对此进行了讨论 https://stat .ethz.ch/pipermail/r-help/2002-July/023299.html .

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||>

因此您不必实施自定义内核,只需标准化数据并运行常规的线性内核SVM.换句话说-计算(样本方式)常规欧几里德范数,并将每个样本除以其自己的范数.然后运行线性SVM,结果等同于对原始数据运行余弦内核.

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.

如果您想与定制机器学习模型一起进行研究,R可能不是走的路(因为它是应用现有技术的工具,而不是精心设计的开发系统-如果您想要在R中自定义一些东西,您基本上必须进入C ++级别).相反,您可能需要考虑使用python和众多库(例如scikit-learn + pykernels),它们为您提供了更大的灵活性.

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.

这篇关于在R中为svm编写自定义内核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆