1、 计算公式𝐶𝑂𝑆 =∑𝑥𝑛𝑦𝑛√∑𝑥𝑛2 √∑𝑦𝑛2x 与 y 为两个向量COS 分子为两个向量的点积COS 分母为两个向量模的积。举例:id_x tag_x weight_xx 1 1x 2 0x 3 1id_y tag_y weight_yy 1 05y 2 06y 3 01cos 分子 = 105+006+101 = 06cos 分母 = sqrt11+00+11s
Java 代码实现:
import java.util.; import java.math.;
public class CosCalculator { public static void main(String[] args) { // 输入数据 Map<Integer, Map<Integer, Double>> data = new HashMap<>(); data.put(10, new HashMap<>()); data.put(20, new HashMap<>()); data.put(30, new HashMap<>()); data.put(40, new HashMap<>()); data.put(50, new HashMap<>()); data.put(60, new HashMap<>()); data.put(70, new HashMap<>()); data.get(10).put(100001, 2.391216368); data.get(10).put(100002, 3.678794412); data.get(10).put(100011, 4.357588823); data.get(20).put(100002, 5.518191618); data.get(20).put(100003, 1.839397206); data.get(20).put(100004, 12.87578044); data.get(30).put(100003, 59.21755365); data.get(30).put(100004, 1.839397206); data.get(30).put(100005, 1.839397206); data.get(40).put(100004, 33.10914971); data.get(40).put(100005, 9.196986029); data.get(40).put(100006, 183.9397206); data.get(50).put(100006, 11.03638324); data.get(50).put(100007, 15.45093653); data.get(50).put(100008, 16.55457485); data.get(60).put(100006, 2.023336926); data.get(60).put(100008, 1.839397206); data.get(60).put(100009, 59.21755365); data.get(70).put(100006, 1.839397206); data.get(70).put(100009, 1.839397206); data.get(70).put(100010, 2.575156088);
// 计算 COS
Map<Integer, Map<Integer, Double>> cos = new HashMap<>();
for (int i : data.keySet()) {
Map<Integer, Double> cosMap = new HashMap<>();
for (int j : data.keySet()) {
if (i == j) {
cosMap.put(j, 1.0);
} else {
double numerator = 0.0;
double denominatorX = 0.0;
double denominatorY = 0.0;
for (int k : data.get(i).keySet()) {
if (data.get(j).containsKey(k)) {
numerator += data.get(i).get(k) * data.get(j).get(k);
}
denominatorX += data.get(i).get(k) * data.get(i).get(k);
}
for (int k : data.get(j).keySet()) {
denominatorY += data.get(j).get(k) * data.get(j).get(k);
}
double denominator = Math.sqrt(denominatorX) * Math.sqrt(denominatorY);
if (denominator == 0) {
cosMap.put(j, 0.0);
} else {
cosMap.put(j, numerator / denominator);
}
}
}
cos.put(i, cosMap);
}
// 输出结果
System.out.println("caseid\tcaseid\tCOS");
for (int i : cos.keySet()) {
List<Map.Entry<Integer, Double>> list = new ArrayList<>(cos.get(i).entrySet());
Collections.sort(list, new Comparator<Map.Entry<Integer, Double>>() {
public int compare(Map.Entry<Integer, Double> o1, Map.Entry<Integer, Double> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
for (int j = 0; j < 3 && j < list.size(); j++) {
System.out.printf("%d\t%d\t%.6f\n", i, list.get(j).getKey(), list.get(j).getValue());
}
}
}
}
输出结果:
caseid caseid COS 10 20 0.232349 10 40 0.161248 10 30 0.120056 20 20 1.000000 20 10 0.232349 20 40 0.164399 30 30 1.000000 30 10 0.120056 30 40 0.108421 40 40 1.000000 40 10 0.161248 40 20 0.164399 50 50 1.000000 50 60 0.693537 50 70 0.693537 60 50 0.693537 60 60 1.000000 60 70 0.693537 70 50 0.693537 70 60 0.693537 70 70 1.000000
单精度计算结果:
caseid caseid COS 20 20 1.000000 20 10 0.232349 20 40 0.16439
原文地址: https://www.cveoy.top/t/topic/fIKO 著作权归作者所有。请勿转载和采集!