scikit-learn:4.7. Pairwise metrics, affinities and kernels

Source: Internet
Author: User
Tags svm rbf kernel

Reference: http://scikit-learn.org/stable/modules/metrics.html


The sklearn.metrics.pairwise Submodule implements utilities to evaluate pairwise distances (distance from sample pairs) or affinity o F Sets of samples (similarity of sample set).

Distance Metrics is Functions d (A, &NBSP; b)  such that  d (A,   b)  <  d (A,  c)  if objects a &NBSP;AND&NBSP; b a &NBSP;AND&NBSP; c .&NBSP;

Kernels is measures of similarity, I.e. s (A, &NBSP; b)  >   s (A,  c)  if objects a &NBSP;AND&NBSP; b  are considered "more similar" than objects  a &NBSP;AND&NBSP; c .&NBSP;


1, cosine similarity

L2-norm of the vector dot product:

If and is row vectors, their cosine similarity is defined as:


This kernel was 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 underthe same dimension, but AL So across dimensions. When the used in machine learning algorithms, the 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_kernelAnd then passed to anSklearn.svm.SVC withkernel= "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])


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

scikit-learn:4.7. Pairwise metrics, affinities and kernels

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.