scikit-learn:4.7. Pairwise metrics, Affinities and Kernels
參考:http://scikit-learn.org/stable/modules/metrics.html
The sklearn.metrics.pairwise submodule
implements utilities to evaluate pairwise distances(样本对的距离) or affinity of sets of samples(样本集的相似度)。
Distance metrics are functions d(a, b) such that d(a, b) < d(a, c) if
objects a and b are considered “more similar” than objects a and c.
Kernels are measures of similarity, i.e. s(a, b) > s(a, c) if
objects a and b are considered “more similar” than objects a and c.
1、Cosine similarity
向量点积的L2-norm:
if and
are
row vectors, their cosine similarity is
defined as:
This kernel is a popular choice
for computing the similarity of documents represented as tf-idf vectors.
2、Linear kernel
If x and y are column vectors, their linear kernel is:
(x, y) = x_transport
* y
3、Polynomial kernel
Conceptually, the polynomial kernels
considers not only the similarity between vectors under the same dimension, but also across dimensions. When used in machine learning algorithms, this allows to account for feature interaction.
The polynomial kernel is defined as:
4、Sigmoid kernel
defined as:
5、RBF kernel
defined as:
If the
kernel is known as the Gaussian kernel of variance .
6、Chi-squared kernel
defined as:
The chi-squared kernel is a very popular choice for training non-linear SVMs in computer
vision applications. It can be computed usingchi2_kernel and
then passed to an sklearn.svm.SVC with kernel="precomputed":
>>> from sklearn.svm import SVC
>>> from sklearn.metrics.pairwise import chi2_kernel
>>> X = [[0, 1], [1, 0], [.2, .8], [.7, .3]]
>>> y = [0, 1, 0, 1]
>>> K = chi2_kernel(X, gamma=.5)
>>> K
array([[ 1. , 0.36..., 0.89..., 0.58...],
[ 0.36..., 1. , 0.51..., 0.83...],
[ 0.89..., 0.51..., 1. , 0.77... ],
[ 0.58..., 0.83..., 0.77... , 1. ]]) >>> svm = SVC(kernel='precomputed').fit(K, y)
>>> svm.predict(K)
array([0, 1, 0, 1])
It can also be directly used as the kernel argument:
>>> svm = SVC(kernel=chi2_kernel).fit(X, y)
>>> svm.predict(X)
array([0, 1, 0, 1])
scikit-learn:4.7. Pairwise metrics, Affinities and Kernels的更多相关文章
- scikit learn 模块 调参 pipeline+girdsearch 数据举例:文档分类 (python代码)
scikit learn 模块 调参 pipeline+girdsearch 数据举例:文档分类数据集 fetch_20newsgroups #-*- coding: UTF-8 -*- import ...
- (原创)(四)机器学习笔记之Scikit Learn的Logistic回归初探
目录 5.3 使用LogisticRegressionCV进行正则化的 Logistic Regression 参数调优 一.Scikit Learn中有关logistics回归函数的介绍 1. 交叉 ...
- (原创)(三)机器学习笔记之Scikit Learn的线性回归模型初探
一.Scikit Learn中使用estimator三部曲 1. 构造estimator 2. 训练模型:fit 3. 利用模型进行预测:predict 二.模型评价 模型训练好后,度量模型拟合效果的 ...
- Scikit Learn: 在python中机器学习
转自:http://my.oschina.net/u/175377/blog/84420#OSC_h2_23 Scikit Learn: 在python中机器学习 Warning 警告:有些没能理解的 ...
- Scikit Learn
Scikit Learn Scikit-Learn简称sklearn,基于 Python 语言的,简单高效的数据挖掘和数据分析工具,建立在 NumPy,SciPy 和 matplotlib 上.
- Query意图分析:记一次完整的机器学习过程(scikit learn library学习笔记)
所谓学习问题,是指观察由n个样本组成的集合,并根据这些数据来预测未知数据的性质. 学习任务(一个二分类问题): 区分一个普通的互联网检索Query是否具有某个垂直领域的意图.假设现在有一个O2O领域的 ...
- Learning to Rank:Point-wise、Pair-wise 和 List-wise区别
机器学习的 ranking 技术——learning2rank,包括 pointwise.pairwise.listwise 三大类型. [Ref-1]给出的: <Point wise rank ...
- OpenShift实战(五):OpenShift容器监控Metrics
1.创建持久化metric pv卷 [root@master1 pv]# cat metrics.json apiVersion: v1 kind: PersistentVolume metadata ...
- 如何使用scikit—learn处理文本数据
答案在这里:http://www.tuicool.com/articles/U3uiiu http://scikit-learn.org/stable/modules/feature_extracti ...
随机推荐
- Thread stack overrun
ERROR 1436 (HY000): Thread stack overrun: 6448 bytes used of a 131072 byte stac k, and 128000 bytes ...
- Caffe RPN:把RPN网络layer添加到caffe基础结构中
在测试MIT Scene Parsing Benchmark (SceneParse150)使用FCN网络时候,遇到Caffe错误. 遇到错误:不可识别的网络层crop 网络层 CreatorRegi ...
- Masonry 原理一
Under the hood Auto Layout is a powerful and flexible way of organising and laying out your views. H ...
- BZOJ1013 + BZOJ1923 + POJ1830 (高斯消元)
三个题放在一起写了 主要是搞搞模板 在这里简述一下怎么写高斯消元 就和代数里学的加减消元学的一样 把矩阵化为上三角形形式 然后进行回代 同时枚举当前要消元的未知数和当前化简到哪一行了 然后从这一行往后 ...
- Oracle 11g 字符集修改
服务端字符集修改 1.确认服务端字符集 select userenv('language') from dual; 2.修改服务端字符集 首先以 DBA 身份登录 Oracle.Windows 系统下 ...
- vue的路由配置
路由,其实就是指向的意思,当我点击页面上的home按钮时,页面中就要显示home的内容,如果点击页面上的about 按钮,页面中就要显示about 的内容.Home按钮 => home 内容, ...
- Gym - 101670C Chessboard Dancing(CTU Open Contest 2017 找规律)
题目:链接 思路: 多画出几个情况就可以找出规律来了 Knight (当大于2的时候只要两种颜色相间出现就可以了) King(当大于等于3的时候,总可以用四种形式来补色,具体如下) Bishop(斜 ...
- Python,subprocess模块(补充)
1.subprocess模块,前戏 res = os.system('dir') 打印到屏幕,res为0或非0 os.popen('dir') 返回一个内存对象,相当于文件流 a = os.popen ...
- linux less-分屏上下翻页浏览文件内容
博主推荐:获取更多 linux文件内容查看命令 收藏:linux命令大全 less命令的作用与more十分相似,都可以用来浏览文字档案的内容,不同的是less命令允许用户向前或向后浏览文件,而more ...
- Vue2.0 Props双向绑定报错简易处理办法
在写项目的时候遇到了一个报错问题,虽然功能是正常运行,chrome的提示是:[Vue warn]: Avoid mutating a prop directly since the value wil ...