sklearn.metrics.pairwise
子模塊工具的實用程序,以評估成對距離或樣品集的近似關(guān)系。
該模塊包含距離度量和內(nèi)核。這里對兩者進行了簡要總結(jié)。
距離度量函數(shù)d(a, b)
,如果對象a
和b
被認(rèn)為比對象a
和c
更相似 ,則d(a, b) < d(a, c)
。兩個完全相同的對象的距離為零。最受歡迎的例子之一是歐幾里得距離。要成為“真實”指標(biāo),它必須滿足以下四個條件:
1. d(a, b) >= 0, for all a and b
2. d(a, b) == 0, if and only if a = b, positive definiteness
3. d(a, b) == d(b, a), symmetry
4. d(a, c) <= d(a, b) + d(b, c), the triangle inequality
核是相似度的量度,如果對象a
和b
被視為比對象a
和c
更為“相似” ,那么s(a, b) > s(a, c)
,內(nèi)核還必須是正半定數(shù)。
在距離度量和相似性度量之間進行轉(zhuǎn)換的方法有很多種,例如核。設(shè)D
距離,S
為內(nèi)核:
S = np.exp(-D * gamma)
,其中一個選擇 gamma
的試探法是1 / num_features
S = 1. / (D / np.max(D))
X
的行向量和Y
的行向量之間的距離可以使用pairwise_distances
進行計算。如果省略Y
,則計算X
行向量的成對距離。同樣, pairwise.pairwise_kernels
可用于使用不同的內(nèi)核函數(shù)計算X
以及Y
核。有關(guān)更多詳細信息,請參見API參考。
>>> import numpy as np
>>> from sklearn.metrics import pairwise_distances
>>> from sklearn.metrics.pairwise import pairwise_kernels
>>> X = np.array([[2, 3], [3, 5], [5, 8]])
>>> Y = np.array([[1, 0], [2, 1]])
>>> pairwise_distances(X, Y, metric='manhattan')
array([[ 4., 2.],
[ 7., 5.],
[12., 10.]])
>>> pairwise_distances(X, metric='manhattan')
array([[0., 3., 8.],
[3., 0., 5.],
[8., 5., 0.]])
>>> pairwise_kernels(X, Y, metric='linear')
array([[ 2., 7.],
[ 3., 11.],
[ 5., 18.]])
cosine_similarity
計算向量的L2正則化點積。也就是說,如果 和 是行向量,它們的余弦相似度 定義為:
之所以稱為余弦相似度,是因為歐幾里得(L2)正則化將向量投影到單位球體上,然后它們的點積就是向量表示的點之間的角度的余弦值。
該內(nèi)核是用于計算以tf-idf向量表示的文檔的相似度的普遍選擇。cosine_similarity
接受 scipy.sparse
矩陣。(請注意,sklearn.feature_extraction.text
中的tf-idf函數(shù)可以生成規(guī)范的向量,在這種情況下,cosine_similarity
等效于linear_kernel
,只是速度較慢。)
參考文獻:
C.D. Manning, P. Raghavan和 H. Schütze (2008). 信息檢索簡介.劍橋大學(xué)出版社。 https://nlp.stanford.edu/IR-book/html/htmledition/the-vector-space-model-for-scoring-1.html
linear_kernel
函數(shù)計算線性核,是在 degree=1
和coef0=0
(同質(zhì)化)情況下的polynomial_kernel
的特例。如果x
和y
是列向量,則它們的線性核為:
polynomial_kernel
函數(shù)計算兩個向量之間的度數(shù)多項式核。多項式核代表兩個向量之間的相似性。從概念上講,多項式內(nèi)核考慮的不僅是相同維度下向量之間的相似性,還考慮跨維度的。當(dāng)在機器學(xué)習(xí)算法中使用時,這可以解決特征交互問題。
多項式內(nèi)核定義為:
其中:
x
,y
是輸入向量d
是內(nèi)核維度如果 則說該內(nèi)核是同質(zhì)的。
sigmoid_kernel
函數(shù)計算兩個向量之間的sigmoid核。sigmoid核也稱為雙曲線正切或多層感知器(因為在神經(jīng)網(wǎng)絡(luò)領(lǐng)域,它經(jīng)常被用作神經(jīng)元激活函數(shù))。它定義為:
其中
x
,y
是輸入向量rbf_kernel
函數(shù)計算兩個向量之間的徑向基函數(shù)(RBF)內(nèi)核。該內(nèi)核定義為:
其中x
和y
是輸入向量。如果 則該核稱為方差的高斯核 。
laplacian_kernel
函數(shù)是徑向基函數(shù)內(nèi)核的一個變體,定義為:
其中x
和y
是輸入矢量, 是輸入向量之間的曼哈頓距離。
它已被證明在應(yīng)用于無噪聲數(shù)據(jù)的機器學(xué)習(xí)中很有用。例如機器學(xué)習(xí)中的量子力學(xué)。
卡方核是在計算機視覺應(yīng)用中訓(xùn)練非線性SVM非常受歡迎的選擇??梢允褂?a rel="external nofollow" target="_blank" style="text-decoration: none; color: #1e6bb8; word-wrap: break-word; font-weight: bold; border-bottom: 1px solid #1e6bb8;">chi2_kernel
計算,然后將 kernel="precomputed"
傳遞給 sklearn.svm.SVC
:
>>> 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.36787944, 0.89483932, 0.58364548],
[0.36787944, 1. , 0.51341712, 0.83822343],
[0.89483932, 0.51341712, 1. , 0.7768366 ],
[0.58364548, 0.83822343, 0.7768366 , 1. ]])
>>> svm = SVC(kernel='precomputed').fit(K, y)
>>> svm.predict(K)
array([0, 1, 0, 1])
它也可以直接用作kernel
參數(shù):
>>> svm = SVC(kernel=chi2_kernel).fit(X, y)
>>> svm.predict(X)
array([0, 1, 0, 1])
卡方核由下式給出
假定數(shù)據(jù)為非負數(shù),通常將其用L1范數(shù)標(biāo)準(zhǔn)化為1。通過與卡方距離的連接來合理化歸一化,卡方距離是離散概率分布之間的距離。
卡方核最常用于可視化詞匯的直方圖(袋)。
參考文獻:
Zhang, J. and Marszalek, M. and Lazebnik, S. and Schmid, C. Local features and kernels for classification of texture and object categories: A comprehensive study International Journal of Computer Vision 2007 https://research.microsoft.com/en-us/um/people/manik/projects/trade-off/papers/ZhangIJCV06.pdf
更多建議: