matlab中自帶的計算距離矩陣的函數有兩個pdist和pdist2。前者計算一個向量自身的距離矩陣,后者計算兩個向量之間的距離矩陣。基本調用形式如下:
D = pdist(X)
D = pdist2(X,Y)
這兩個函數都提供多種距離度量形式,非常方便,還可以調用自己編寫的距離函數。
需要注意的是:pdist2返回是n*n的距離矩陣,pdist則返回距離矩陣的下三角串聯形式。
下面是具體的介紹:
一、pdist
Pairwise distance between pairs of objects
Syntax
D = pdist(X)
D = pdist(X,distance)
Description
D = pdist(X)
計算 X 中各對行向量的相互距離(X是一個m-by-n的矩陣). 這里 D 要特別注意,D 是一個長為m(m–1)/2的行向量.可以這樣理解 D 的生成:首先生成一個 X 的距離方陣,由於該方陣是對稱的,且對角線上的元素為0,所以取此方陣的下三角元素,按照Matlab中矩陣的按列存儲原則,此下三角各元素的索引排列即為(2,1), (3,1), ..., (m,1), (3,2), ..., (m,2), ..., (m,m–1).可以用命令 squareform(D) 將此行向量轉換為原距離方陣.(squareform函數是專門干這事的,其逆變換是也是squareform。)
D = pdist(X,distance) 使用指定的距離.distance可以取下面圓括號中的值,用紅色標出!
Metrics
Given an m-by-n data matrix X, which is treated as m (1-by-n) row vectors x1, x2, ..., xm, the various distances between the vector xs and xt are defined as follows:
歐幾里德距離Euclidean distance('euclidean')
d 2 s,t =(x s x t )(x s x t ) ′
Notice that the Euclidean distance is a special case of the Minkowski metric, where p = 2.
歐氏距離雖然很有用,但也有明顯的缺點。
一:它將樣品的不同屬性(即各指標或各變量)之間的差別等同看待,這一點有時不能滿足實際要求。
二:它沒有考慮各變量的數量級(量綱),容易犯大數吃小數的毛病。所以,可以先對原始數據進行規范化處理再進行距離計算。
標准歐幾里德距離Standardized Euclidean distance('seuclidean')
d 2 s,t =(x s x t )V 1 (x s x t ) ′
where V is the n-by-n diagonal matrix whose jth diagonal element is S(j)2, where S is the vector of standard deviations.
相比單純的歐氏距離,標准歐氏距離能夠有效的解決上述缺點。注意,這里的V在許多Matlab函數中是可以自己設定的,不一定非得取標准差,可以依據各變量的重要程度設置不同的值,如knnsearch函數中的Scale屬性。
馬哈拉諾比斯距離Mahalanobis distance('mahalanobis')
d 2 s,t =(x s x t )C 1 (x s x t ) ′
where C is the covariance matrix.
馬氏距離是由印度統計學家馬哈拉諾比斯(P. C. Mahalanobis)提出的,表示數據的協方差距離。它是一種有效的計算兩個未知樣本集的相似度的方法。與歐式距離不同的是它考慮到各種特性之間的聯系(例如:一條關於身高的信息會帶來一條關於體重的信息,因為兩者是有關聯的)並且是尺度無關的(scale-invariant),即獨立於測量尺度。
如果協方差矩陣為單位矩陣,那么馬氏距離就簡化為歐式距離,如果協方差矩陣為對角陣,則其也可稱為正規化的歐氏距離.
馬氏優缺點:
1)馬氏距離的計算是建立在總體樣本的基礎上的,因為C是由總樣本計算而來,所以馬氏距離的計算是不穩定的;
2)在計算馬氏距離過程中,要求總體樣本數大於樣本的維數。
3)協方差矩陣的逆矩陣可能不存在。
曼哈頓距離(城市區塊距離)City block metric('cityblock')
d s,t =∑ j=1 n ∣ ∣ x s j x t j ∣ ∣
Notice that the city block distance is a special case of the Minkowski metric, where p=1.
閔可夫斯基距離Minkowski metric('minkowski')
d s,t =∑ j=1 n ∣ ∣ x s j x t j ∣ ∣ p p
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')
d s,t =max j ∣ ∣ x s j x t j ∣ ∣
Notice that the Chebychev distance is a special case of the Minkowski metric, where p = ∞.
夾角余弦距離Cosine distance('cosine')
d s,t =1x s x t ′ ∥x s ∥ 2 ∥x t ∥ 2
與Jaccard距離相比,Cosine距離不僅忽略0-0匹配,而且能夠處理非二元向量,即考慮到變量值的大小。
相關距離Correlation distance('correlation')
d s,t =1x s x t ′ (x s x s ˉ ˉ ˉ )(x s x s ˉ ˉ ˉ ) ′ √ (x t x t ˉ ˉ ˉ )(x t x t ˉ ˉ ˉ ) ′ √
Correlation距離主要用來度量兩個向量的線性相關程度。
漢明距離Hamming distance('hamming')
d s,t =(#(x s j ≠x t j ) n )
兩個向量之間的漢明距離的定義為兩個向量不同的變量個數所占變量總數的百分比。
傑卡德距離Jaccard distance('jaccard')
d s,t =#[(x s j ≠x t j )∩((x s j ≠0)∪(x t j ≠0))] #[(x s j ≠0)∪(x t j ≠0)]
Jaccard距離常用來處理僅包含非對稱的二元(0-1)屬性的對象。很顯然,Jaccard距離不關心0-0匹配,而Hamming距離關心0-0匹配。
Spearman distance('spearman')
d s,t =1(r s r s ˉ ˉ ˉ )(r t r t ˉ ˉ ˉ ) ′ (r s r s ˉ ˉ ˉ )(r s r s ˉ ˉ ˉ ) ′ √ (r t r t ˉ ˉ ˉ )(r t r t ˉ ˉ ˉ ) ′ √
where
rsj is the rank of xsj taken over x1j, x2j, ...xmj, as computed by tiedrank
rs and rt are the coordinate-wise rank vectors of xs and xt, i.e., rs = (rs1, rs2, ... rsn)
r s ˉ ˉ ˉ =1 n ∑ j r s j =n+1 2
r t ˉ ˉ ˉ =1 n ∑ j r t j =n+1 2
二、pdist2
Pairwise distance between two sets of observations
Syntax
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)
Description
這里 X 是 mx-by-n 維矩陣,Y 是 my-by-n 維矩陣,生成 mx-by-my 維距離矩陣 D。
[D,I] = pdist2(X,Y,distance,'Smallest',K)
生成 K-by-my 維矩陣 D 和同維矩陣 I,其中D的每列是原距離矩陣中最小的元素,按從小到大排列,I 中對應的列即為其索引號。注意,這里每列各自獨立地取 K 個最小值。
例如,令原mx-by-my 維距離矩陣為A,則 K-by-my 維矩陣 D 滿足 D(:,j)=A(I(:,j),j).
此外,
MATLAB 距離計算
判別分析時,通常涉及到計算兩個樣本之間的距離,多元統計學理論中有多種距離計算公式。MATLAB中已有對應函數,可方便直接調用計算。距離函數有:pdist, pdist2, mahal, squareform, mdscale, cmdscale
主要介紹pdist2 ,其它可參考matlab help
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)
練習:
2種計算方式,一種直接利用pdist計算,另一種按公式(見最后理論)直接計算。
% distance
clc;clear;
x = rand(4,3)
y = rand(1,3)
for i =1:size(x,1)
for j =1:size(y,1)
a = x(i,:); b=y(j,:);
% Euclidean distance
d1(i,j)=sqrt((a-b)*(a-b)');
% Standardized Euclidean distance
V = diag(1./std(x).^2);
d2(i,j)=sqrt((a-b)*V*(a-b)');
% Mahalanobis distance
C = cov(x);
d3(i,j)=sqrt((a-b)*pinv(C)*(a-b)');
% City block metric
d4(i,j)=sum(abs(a-b));
% Minkowski metric
p=3;
d5(i,j)=(sum(abs(a-b).^p))^(1/p);
% Chebychev distance
d6(i,j)=max(abs(a-b));
% Cosine distance
d7(i,j)=1-(a*b')/sqrt(a*a'*b*b');
% Correlation distance
ac = a-mean(a); bc = b-mean(b);
d8(i,j)=1- ac*bc'/(sqrt(sum(ac.^2))*sqrt(sum(bc.^2)));
end
end
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
基本理論公式如下: