計算距離的需求有兩種:
一種是給定一個特征集合X,然后計算Pairwise距離矩陣,那么可使用D=pdist(X,distance)的方式;
另一種是給定兩個對應的特征集合X和Y,然后計算X與Y對應的距離信息,使用D=pdist2(X,Y,distance)的方式;
需注意,2011版本以前的Matlab是沒有pdist2.m文件的,而早期的pdist2.m文件中的距離計算方式也比較少,所以建議使用最新的Matlab版本,很重要。
其中,distance的定義有如下幾種:
歐幾里德距離Euclidean distance(‘euclidean’)
歐氏距離雖然很有用,但也有明顯的缺點。
一:它將樣品的不同屬性(即各指標或各變量)之間的差別等同看待,這一點有時不能滿足實際要求。
二:它沒有考慮各變量的數量級(量綱),容易犯大數吃小數的毛病。所以,可以先對原始數據進行規范化處理再進行距離計算。
標准歐幾里德距離Standardized Euclidean distance(‘seuclidean’)
相比單純的歐氏距離,標准歐氏距離能夠有效的解決上述缺點。注意,這里的V在許多Matlab函數中是可以自己設定的,不一定非得取標准差,可以依據各變量的重要程度設置不同的值,如knnsearch函數中的Scale屬性。
馬哈拉諾比斯距離Mahalanobis distance(‘mahalanobis’)
where C is the covariance matrix.
馬氏距離是由印度統計學家馬哈拉諾比斯(P. C. Mahalanobis)提出的,表示數據的協方差距離。它是一種有效的計算兩個未知樣本集的相似度的方法。與歐式距離不同的是它考慮到各種特性之間的聯系(例如:一條關於身高的信息會帶來一條關於體重的信息,因為兩者是有關聯的)並且是尺度無關的(scale-invariant),即獨立於測量尺度。
如果協方差矩陣為單位矩陣,那么馬氏距離就簡化為歐式距離,如果協方差矩陣為對角陣,則其也可稱為正規化的歐氏距離.
馬氏優缺點:
1)馬氏距離的計算是建立在總體樣本的基礎上的,因為C是由總樣本計算而來,所以馬氏距離的計算是不穩定的;
2)在計算馬氏距離過程中,要求總體樣本數大於樣本的維數。
3)協方差矩陣的逆矩陣可能不存在。
曼哈頓距離(城市區塊距離)City block metric(‘cityblock’)
Notice that the city block distance is a special case of the Minkowski metric, where p=1.
閔可夫斯基距離Minkowski metric(‘minkowski’)
Notice that for the special case of p = 1, the Minkowski metric gives the city block metric, for the special case of p = 2, the Minkowski metric gives the Euclidean distance, and for the special case of p = ∞, the Minkowski metric gives the Chebychev distance.
閔可夫斯基距離由於是歐氏距離的推廣,所以其缺點與歐氏距離大致相同。
切比雪夫距離Chebychev distance(‘chebychev’)
Notice that the Chebychev distance is a special case of the Minkowski metric, where p = ∞.
夾角余弦距離Cosine distance(‘cosine’)
與Jaccard距離相比,Cosine距離不僅忽略0-0匹配,而且能夠處理非二元向量,即考慮到變量值的大小。
相關距離Correlation distance(‘correlation’)
Correlation距離主要用來度量兩個向量的線性相關程度。
漢明距離Hamming distance(‘hamming’)
兩個向量之間的漢明距離的定義為兩個向量不同的變量個數所占變量總數的百分比。
傑卡德距離Jaccard distance(‘jaccard’)
Jaccard距離常用來處理僅包含非對稱的二元(0-1)屬性的對象。很顯然,Jaccard距離不關心0-0匹配,而Hamming距離關心0-0匹配。
Spearman distance(‘spearman’)
1、pdist函數
調用格式:Y=pdist(X,’metric’)
說明:用 ‘metric’指定的方法計算 X 數據矩陣中對象之間的距離。’
X:一個m×n的矩陣,它是由m個對象組成的數據集,每個對象的大小為n。
metric’取值如下:
‘euclidean’:歐氏距離(默認);
‘seuclidean’:標准化歐氏距離;
‘mahalanobis’:馬氏距離;
‘cityblock’:布洛克距離;
‘minkowski’:明可夫斯基距離;
‘cosine’: 夾角余弦
‘correlation’: 相關距離
‘spearman'
‘hamming’: 漢明距離
‘jaccard’: 傑卡德距離& 傑卡德相似系數
‘chebychev’:Chebychev距離
2、pdist2函數
D = pdist2(X,Y)
D = pdist2(X,Y,distance)
D = pdist2(X,Y,'minkowski',P)
D = pdist2(X,Y,'mahalanobis',C)
D = pdist2(X,Y,distance,'Smallest',K)
D = pdist2(X,Y,distance,'Largest',K)
[D,I] = pdist2(X,Y,distance,'Smallest',K)
[D,I] = pdist2(X,Y,distance,'Largest',K)
clc;clear;
x = rand(4,3)
y = rand(1,3)
md1 = pdist2(x,y,'Euclidean');
md2 = pdist2(x,y,'seuclidean');
md3 = pdist2(x,y,'mahalanobis');
md4 = pdist2(x,y,'cityblock');
md5 = pdist2(x,y,'minkowski',p);
md6 = pdist2(x,y,'chebychev');
md7 = pdist2(x,y,'cosine');
md8 = pdist2(x,y,'correlation');
md9 = pdist2(x,y,'hamming');
md10 = pdist2(x,y,'jaccard');
md11 = pdist2(x,y,'spearman');
D1=[d1,md1],D2=[d2,md2],D3=[d3,md3]
D4=[d4,md4],D5=[d5,md5],D6=[d6,md6]
D7=[d7,md7],D8=[d8,md8]
md9,md10,md11
運行結果如下:
x =
0.5225 0.6382 0.6837
0.3972 0.5454 0.2888
0.8135 0.0440 0.0690
0.6608 0.5943 0.8384
y =
0.5898 0.7848 0.4977
D1 =
0.2462 0.2462
0.3716 0.3716
0.8848 0.8848
0.3967 0.3967
D2 =
0.8355 0.8355
1.5003 1.5003
3.1915 3.1915
1.2483 1.2483
D3 =
439.5074 439.5074
437.5606 437.5606
438.3339 438.3339
437.2702 437.2702
D4 =
0.3999 0.3999
0.6410 0.6410
1.3934 1.3934
0.6021 0.6021
D5 =
0.2147 0.2147
0.3107 0.3107
0.7919 0.7919
0.3603 0.3603
D6 =
0.1860 0.1860
0.2395 0.2395
0.7409 0.7409
0.3406 0.3406
D7 =
0.0253 0.0253
0.0022 0.0022
0.3904 0.3904
0.0531 0.0531
D8 =
1.0731 1.0731
0.0066 0.0066
1.2308 1.2308
1.8954 1.8954
md9 =
1
1
1
1
md10 =
1
1
1
1
md11 =
1.5000
0.0000
1.5000
2.0000
3、mahal函數
調用格式:D2=mahal(Y,X)
D2(I) = (Y(I,:)-MU) * SIGMA^(-1) * (Y(I,:)-MU)',
例子:
x = mvnrnd([0;0], [1 .9;.9 1], 100);
y = [1 1;1 -1;-1 1;-1 -1];
MahalDist = mahal(y,x)
sqEuclidDist = sum((y - repmat(mean(x),4,1)).^2, 2)
plot(x(:,1),x(:,2),'b.',y(:,1),y(:,2),'ro')
4、squareform函數
計算上三角矩陣和方形矩陣之間的距離矩陣
調用格式:
Z = squareform(Y)如果Y是由pdist函數得到的向量,翻轉Y為一個對稱方矩格式,
則Z(i,j)表示為原數據上i和j間的距離。
詳見 matlab help squareform