大家好,又见面了,我是你们的朋友全栈君。
目标:希望通过的矩阵运算就能得出矩阵向量中两两之间的欧式距离
欧氏距离公式:
-
一般而言,我们常见的欧式距离计算公式如下:
- a,b 对应的是两组不同的向量
- d i s t ( a , b ) = ( a 1 − b 1 ) 2 + ( a 2 − b 2 ) 2 + ⋅ ⋅ ⋅ ( a n − b n ) 2 dist(a,b)=\sqrt{(a_1-b_1)^{2}+(a_2-b_2)^{2}+···(a_n-b_n)^{2}} dist(a,b)=(a1−b1)2+(a2−b2)2+⋅⋅⋅(an−bn)2
-
事实上对于上面的公式如果我们通过向量的角度来考虑,就会变成是下列形式:
- a,b 对应的是两组不同的向量
- d i s t ( a , b ) = d o t ( a , a ) − 2 ∗ d o t ( a , b ) + d o t ( b , b ) dist(a,b) = \sqrt{dot(a,a)-2*dot(a,b)+dot(b,b)} dist(a,b)=dot(a,a)−2∗dot(a,b)+dot(b,b)
假设有下列矩阵 A A A:
-
A = [ a 1 a 2 a 3 b 1 b 2 b 3 ] {A}= \left[{\begin{array}{}{a{_1}}&{a{_2}}&{a{_3}}\\{b{_1}}&{b{_2}}&{b{_3}}\end{array}}\right] A=[a1b1a2b2a3b3]
-
为了凑出上面的公式:
-
先计算出 d o t ( A , A ) dot(A,A) dot(A,A) -> A A T {AA^T} AAT,
- A ‾ = [ a 1 a 2 a 3 b 1 b 2 b 3 ] [ a 1 b 1 a 2 b 2 a 3 b 3 ] = [ ( a 1 ) 2 + ( a 1 ) 2 + ( a 1 ) 2 ( a 1 ) ( b 1 ) + ( a 2 ) ( b 2 ) + ( a 3 ) ( b 3 ) ( a 1 ) ( b 1 ) + ( a 2 ) ( b 2 ) + ( a 3 ) ( b 3 ) ( b 1 ) 2 + ( b 2 ) 2 + ( b 3 ) 2 ] \overline{A} = \left[{\begin{array}{}{a{_1}}&{a{_2}}&{a{_3}}\\{b{_1}}&{b{_2}}&{b{_3}}\end{array}}\right] \left[{\begin{array}{}{a{_1}}&{b{_1}} \\ {a{_2}}&{b{_2}} \\ {a{_3}}&{b{_3}} \end{array}}\right] = \left[{\begin{array}{}{(a{_1})^2+(a{_1})^2+(a{_1})^2}&{(a{_1})(b{_1})+(a{_2})(b{_2})+(a{_3})(b{_3})} \\{(a{_1})(b{_1})+(a{_2})(b{_2})+(a{_3})(b{_3})} & {(b{_1})^2+(b{_2})^2+(b{_3})^2} \end{array}}\right] A=[a1b1a2b2a3b3]⎣⎡a1a2a3b1b2b3⎦⎤=[(a1)2+(a1)2+(a1)2(a1)(b1)+(a2)(b2)+(a3)(b3)(a1)(b1)+(a2)(b2)+(a3)(b3)(b1)2+(b2)2+(b3)2]
-
取 A ‾ \overline{A} A对角线:
- A ‾ . d i a g ( ) \overline{A}.diag() A.diag() = [ ( a 1 ) 2 + ( a 2 ) 2 + ( a 3 ) 2 ( b 1 ) 2 + ( b 2 ) 2 + ( b 3 ) 2 ] \left[{\begin{array}{}{(a{_1})^2+(a{_2})^2+(a{_3})^2} & {(b{_1})^2+(b{_2})^2+(b{_3})^2}\end{array}}\right] [(a1)2+(a2)2+(a3)2(b1)2+(b2)2+(b3)2]
-
对对角线矩阵进行一些变换
- A ‾ 1 \overline{A}{_1} A1 = [ ( a 1 ) 2 + ( a 2 ) 2 + ( a 3 ) 2 ( b 1 ) 2 + ( b 2 ) 2 + ( b 3 ) 2 ] T \left[{\begin{array}{}{(a{_1})^2+(a{_2})^2+(a{_3})^2} & {(b{_1})^2+(b{_2})^2+(b{_3})^2}\end{array}}\right]^T [(a1)2+(a2)2+(a3)2(b1)2+(b2)2+(b3)2]T [ 1 1 ] \left[{\begin{array}{}1 & 1\end{array}}\right] [11]
- = [ ( a 1 ) 2 + ( a 2 ) 2 + ( a 3 ) 2 ( a 1 ) 2 + ( a 2 ) 2 + ( a 3 ) 2 ( b 1 ) 2 + ( b 2 ) 2 + ( b 3 ) 2 ( b 1 ) 2 + ( b 2 ) 2 + ( b 3 ) 2 ] \left[{\begin{array}{}{(a{_1})^2+(a{_2})^2+(a{_3})^2} & {(a{_1})^2+(a{_2})^2+(a{_3})^2} \\ {(b{_1})^2+(b{_2})^2+(b{_3})^2} & {(b{_1})^2+(b{_2})^2+(b{_3})^2} \end{array}}\right] [(a1)2+(a2)2+(a3)2(b1)2+(b2)2+(b3)2(a1)2+(a2)2+(a3)2(b1)2+(b2)2+(b3)2]
- A ‾ 2 \overline{A}{_2} A2 = [ 1 1 ] T \left[{\begin{array}{}1 & 1\end{array}}\right]^T [11]T [ ( a 1 ) 2 + ( a 2 ) 2 + ( a 3 ) 2 ( b 1 ) 2 + ( b 2 ) 2 + ( b 3 ) 2 ] \left[{\begin{array}{}{(a{_1})^2+(a{_2})^2+(a{_3})^2} & {(b{_1})^2+(b{_2})^2+(b{_3})^2}\end{array}}\right] [(a1)2+(a2)2+(a3)2(b1)2+(b2)2+(b3)2]
- = [ ( a 1 ) 2 + ( a 2 ) 2 + ( a 3 ) 2 ( b 1 ) 2 + ( b 2 ) 2 + ( b 3 ) 2 ( a 1 ) 2 + ( a 2 ) 2 + ( a 3 ) 2 ( b 1 ) 2 + ( b 2 ) 2 + ( b 3 ) 2 ] \left[{\begin{array}{}{(a{_1})^2+(a{_2})^2+(a{_3})^2} & {(b{_1})^2+(b{_2})^2+(b{_3})^2} \\ {(a{_1})^2+(a{_2})^2+(a{_3})^2} & {(b{_1})^2+(b{_2})^2+(b{_3})^2} \end{array}}\right] [(a1)2+(a2)2+(a3)2(a1)2+(a2)2+(a3)2(b1)2+(b2)2+(b3)2(b1)2+(b2)2+(b3)2]
-
经过了上面的处理,我们就可以得出上述的公式了
- d i s t ( A ) = A ‾ 1 + A ‾ 2 − 2 A ‾ dist(A) = {\overline{A}{_1}} + {\overline{A}{_2}} – {\overline{2A}} dist(A)=A1+A2−2A
-
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/150907.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...