兩個數組相識度算法 tanimoto

在日常程序中可能會出現對比兩個數組相識程度,我這裏是看到一些資料後根據lire程序提取的計算兩個數組相識度的算法 tanimoto

度量兩個集合之間的相似程度的方法。

A=[1,2,3,4]

B=[1,2,5]

C = A & B = [1,2]

T = Nc / ( Na + Nb -Nc) = len(c) / ( len(a) + len(b) - len(c)) = 2 / (4+3-2) = 0.4

可以用戶計算用戶之間的相似程度



public void getDistance(int[] t, int[] s) {
		double Result = 0;
		if ((t.length == s.length)) {			
			double Temp1 = 0;
			double Temp2 = 0;
			double TempCount1 = 0, TempCount2 = 0, TempCount3 = 0;
			for (int i = 0; i < t.length; i++) {
				Temp1 += t[i];
				Temp2 += s[i];
			}
			if (Temp1 == 0 || Temp2 == 0)
				Result = 100;
			if (Temp1 == 0 && Temp2 == 0)
				Result = 0;
			if (Temp1 > 0 && Temp2 > 0) {
				for (int i = 0; i < tImg.length; i++) {
					TempCount1 += (t[i] / Temp1) * (s[i] / Temp2);
					TempCount2 += (s[i] / Temp2) * (s[i] / Temp2);
					TempCount3 += (t[i] / Temp1) * (t[i] / Temp1);
				}
				Result = (100 - 100 * (TempCount1 / (TempCount2 + TempCount3 - TempCount1))); // Tanimoto
			}			
		}
	}

網上有人說這幾個都可以計算數組的相識度,但是我沒有嘗試過(tanimoto和歐氏距離、Cosine和皮爾遜係數 )




發佈了54 篇原創文章 · 獲贊 7 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章