首页 > 编程语言 >相似度算法

相似度算法

时间:2024-10-28 15:45:39浏览次数:3  
标签:10.5 temp data List 22.2 算法 相似 0.13793103448275873

package com.rongyi.platform.game.web.data;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* 参考:https://blog.csdn.net/weixin_73733267/article/details/135144512
* https://cloud.tencent.com/developer/article/1399322
*/
public class Compare {

// 初始化数据
public static List<Double> init(List<Double> data){

// int[] arr = {100};
// int targetLength = findClosestValue(arr,data.size());
return initLength(data,100);
}

// 找最接近的数
public static int findClosestValue(int[] arr, int target) {
int[] temp = new int[arr.length];
for (int i = 0;i < arr.length;i++) {
temp[i] = Math.abs(arr[i] - target);
}
int finalTemp = temp[0];
int index = 0;
for (int i = 1;i < temp.length;i++) {
if(temp[i] < finalTemp){
finalTemp = temp[i];
index = i;
}
}
return arr[index];
}

// 标准模型的初始化 TODO
public static List<Double> initLength(List<Double> data1,int targetLength){
// 扩展
data1 = extendGtLength(data1,targetLength);

// 裁剪
data1 = extendEqLength(data1,targetLength);
System.out.println("扩展后的数据:" + data1);
if(data1.size() != targetLength){
throw new RuntimeException("扩展裁剪有问题");
}
// 归一化
MinMaxScaler minMaxScaler = new MinMaxScaler(data1);
return minMaxScaler.scale();
}

/**
* 把数组data扩展到data的长度大于length。
*/
private static List<Double> extendGtLength(List<Double> data,int length){
if(data.size() >= length){
return minuteBetweenLength(data,length);
}
// 数据扩充
List<Double> temp = new ArrayList<>();
// if(data.size() * 2 <= length){
// 扩1倍
for (int i = 0; i < data.size(); i++) {
temp.add(data.get(i));
if(i < data.size() - 1){
temp.add((data.get(i + 1) + data.get(i)) / 2);
}
}
// }else if((data.size() * 1.5 <= length)){
// // 扩0.5倍
// for (int i = 0; i < data.size(); i++) {
// temp.add(data.get(i));
// if (i != 0 && i % 2 == 0) {
// temp.add((data.get(i + 1) + data.get(i)) / 2);
// }
// }
// }else{
// // 扩0.25倍
// for (int i = 0; i < data.size(); i++) {
// temp.add(data.get(i));
// if (i != 0 && i % 4 == 0) {
// temp.add((data.get(i + 1) + data.get(i)) / 2);
// }
// }
// }
if(temp.size() >= length){
return temp;
}
System.out.println("扩" + temp);
return extendGtLength(temp,length);
}

// 缩减到length长度。
private static List<Double> minuteBetweenLength(List<Double> data,int length){
if(data.size() < length){
throw new RuntimeException("不符合缩减条件");
}
if(data.size() >= length && data.size() < length * 2){
return data;
}
int x = data.size() / length;
// 缩减
List<Double> temp = new ArrayList<>();
for(int i = 0; i < data.size();i = i + x){
temp.add(data.get(i));
}
return minuteBetweenLength(temp, temp.size());
}

// 缩减到length长度。
private static List<Double> extendEqLength(List<Double> data,int length){
if(data.size() >= length && data.size() * 2 < length){
throw new RuntimeException("不符合extendEqLength条件");
}
if(data.size() == length){
return data;
}
// 需要减少的数量
int elementsToRemove = data.size() -length;

// 每间隔x,忽略此数据
int x = data.size() / elementsToRemove;
List<Double> temp = new ArrayList<>();
temp.add(data.get(0));
int removeNum = 0;
for (int i = 1; i < data.size();i++){
if(!(i % x == 0)){
temp.add(data.get(i));
}else if(removeNum<elementsToRemove){
removeNum++;
}else{
temp.add(data.get(i));
}
}
if(temp.size() < length){
temp.add(data.get(data.size() - 1));
}
if(temp.size() > length){
temp.remove(data.get(data.size() - 1));
}
return temp;
}

public static void main(String[] args) {
List<Double> data1 = Arrays.asList(10.1,10.5,11.2,10.8,12.0,12.5,11.5,12.5,13.5,13.1,14.5,14.3,15.8,16.0,17.0,16.5,16.5,17.1,16.5,15.2,14.8,14.0,14.5,13.5,12.5,11.1,11.5,11.2,11.8,11.0,10.5,10.5);
List<Double> list1 = init(data1);
System.out.println("归一化1后:" + JSON.toJSONString(list1));

// List<Double> data2 = Arrays.asList(33.3,35.6,38.4,37.3,38.4,39.5,40.5,42.6,41.6,43.6,44.6,50.0,48.5,46.5,44.5,42.5,42.5,42.0,41.0,38.5,37.5,36.5,36.5,34.5,36.5,37.5,34.5,33.5,33.5,31.5,30.5);
List<Double> data2 = Arrays.asList(50.0,48.5,46.5,44.5,42.5,42.5,42.0,41.0,38.5,37.5,36.5,36.5,34.5,36.5,37.5,34.5,33.5,33.5,31.5,30.5,33.3,35.6,38.4,37.3,38.4,39.5,40.5,42.6,41.6,43.6,44.6);
List<Double> list2 = init(data2);
System.out.println("归一化2后:" + JSON.toJSONString(list2));
System.out.println("长度:" + data2.size());
double distance = EuclideanDistance.calculateEuclideanDistance(list1, list2);
System.out.println("欧式距离: " + distance);
System.out.println("欧式距离相似度: " + (100- distance));

}

// public static void main1(String[] args) {
// List<Double> data1 = Arrays.asList(10.1,10.5,11.2,10.8,12.0,12.5,11.5,10.5,10.5,10.1,10.5,10.5,10.8,13.0,13.0,11.5,10.5,10.1,10.5,11.2,10.8,12.0,11.5,10.5,10.5,10.1,10.5,11.2,10.8,13.0,12.5,11.5,10.5,10.5);
// List<Double> list1 = init(data1);
// System.out.println("归一化1后:" + JSON.toJSONString(list1));
// List<Double> data2 = Arrays.asList(20.8,22.2,25.0,27.5,30.0,33.2,27.4,25.0,23.0,22.2,20.8,22.2,25.0,27.5,27.5,30.0,33.2,26.4,23.3,22.2,22.0,22.2,25.0,27.5,30.0,33.2,27.4,23.3,22.2,20.8,22.2,25.0,27.5,30.0);
// // List<Double> data2 = Arrays.asList(33.2,26.4,23.3,22.2,22.01,22.2,25.01,27.5,30.01,33.2,27.4,23.3,22.2,20.8,22.2,25.01,27.5,30.01,28.4,27.8,24.6,23.5,22.1,26.5,24.6,26.5,27.5,28.5,29.4,30.2,30.6,30.5,30.5,30.5);
// List<Double> list2 = init(data2);
// System.out.println("归一化2后:" + JSON.toJSONString(list2));
// System.out.println("长度:" + data2.size());
// double distance = EuclideanDistance.calculateEuclideanDistance(list1, list2);
// System.out.println("欧式距离: " + distance);
// System.out.println("欧式距离相似度: " + (10- distance));
//
// }

// public static void main22(String[] args) {
// List<Double> data1 = Arrays.asList(10.1,10.5,11.2,10.8,12.0,12.5,11.5,10.5,10.5,10.1,10.5,10.5,10.8,13.0,13.0,11.5,10.5,10.1,10.5,11.2,10.8,12.0,11.5,10.5,10.5,10.1,10.5,11.2,10.8,13.0,12.5,11.5,10.5,10.5,10.1,10.5,11.2,10.8,12.0,12.5,11.5,10.5,10.5,10.1,10.5,10.5,10.8,13.0,13.0,11.5);
// List<Double> list1 = init(data1);
// System.out.println("归一化1后:" + JSON.toJSONString(list1));
// // List<Double> data2 = Arrays.asList(20.8,22.2,25.0,27.5,30.0,33.2,27.4,25.0,23.0,22.2,20.8,22.2,25.0,27.5,27.5,30.0,33.2,26.4,23.3,22.2,22.0,22.2,25.0,27.5,30.0,33.2,27.4,23.3,22.2,20.8,22.2,25.0,27.5,30.0,20.8,22.2,25.0,27.5,30.0,33.2,27.4,25.0,23.0,22.2,20.8,22.2,25.0,27.5,27.5,30.0);
// // List<Double> data2 = Arrays.asList(33.2,26.4,23.3,22.2,22.01,22.2,25.01,27.5,30.01,33.2,27.4,23.3,22.2,21.2,22.2,25.01,27.5,30.01,28.4,27.8,24.6,23.5,22.1,26.5,24.6,26.5,27.5,28.5,29.4,30.2,30.6,30.5,30.5,30.5,33.2,26.4,23.3,22.2,22.01,22.2,25.01,27.5,30.01,33.2,27.4,23.3,22.2,21.2,22.2,25.01);
// List<Double> data2 = Arrays.asList(33.2,26.4,23.3,22.2,15.01,22.2,25.01,26.5,30.01,33.2,27.4,14.3,22.2,21.2,22.2,25.01,27.5,30.01,35.4,28.8,24.6,23.5,22.1,26.5,24.6,26.5,27.5,28.5,29.4,25.2,30.6,30.5,30.5,39.5,33.2,26.4,23.3,22.2,22.01,22.2,25.01,27.5,30.01,33.2,30.4,23.3,22.2,21.2,30.2,25.01);
// List<Double> list2 = init(data2);
// System.out.println("归一化2后:" + JSON.toJSONString(list2));
// System.out.println("长度:" + data2.size());
// double distance = EuclideanDistance.calculateEuclideanDistance(list1, list2);
// System.out.println("欧式距离: " + distance);
// System.out.println("欧式距离相似度: " + (100- distance));
//
// }

private static double process(double distance){
double sim = 100 - distance;
if(sim < 10){
return 0.0;
}
if(sim > 90){
return 100;
}
return 10 * (sim-10) / 8;
}

public List<Double> pre(List<Double> data1,int length){

return null;
}

public static void main2(String[] args) {
String str = "33.2 26.4 23.3 22.2 22.01 22.2 25.01 27.5 30.01 33.2 27.4 23.3 22.2 20.8 22.2 25.01 27.5 30.01 28.4 27.8 24.6 23.5 22.1 26.5 24.6 26.5 27.5 28.5 29.4 30.2 30.6 30.5 30.5 30.5";
String replace = str.replace(" ", ",");
System.out.println("结构:" + replace);
}

public static void main22(String[] args) {
String str= "0.0,0.0,0.0,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.379310344827586,0.379310344827586,0.379310344827586,0.24137931034482793,0.24137931034482793,0.6551724137931035,0.6551724137931035,0.6551724137931035,0.8275862068965517,0.8275862068965517,0.8275862068965517,0.48275862068965525,0.48275862068965525,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.0,0.0,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.24137931034482793,0.24137931034482793,1.0,1.0,1.0,1.0,1.0,1.0,0.48275862068965525,0.48275862068965525,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.0,0.0,0.0,0.13793103448275873,0.13793103448275873,0.379310344827586,0.379310344827586,0.379310344827586,0.24137931034482793,0.24137931034482793,0.24137931034482793,0.6551724137931035,0.6551724137931035,0.48275862068965525,0.48275862068965525,0.48275862068965525,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.0,0.0,0.0,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.379310344827586,0.379310344827586,0.379310344827586,0.24137931034482793,0.24137931034482793,0.24137931034482793,0.24137931034482793,1.0,1.0,1.0,1.0,0.8275862068965517,0.8275862068965517,0.8275862068965517,0.8275862068965517,0.48275862068965525,0.48275862068965525,0.48275862068965525,0.48275862068965525,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873,0.13793103448275873";
String replace = str.replace(",", " ");
System.out.println("结构:" + replace);
}
}

标签:10.5,temp,data,List,22.2,算法,相似,0.13793103448275873
From: https://www.cnblogs.com/maohuidong/p/18510780

相关文章

  • 算法定制视频分析网关车辆违停在园区场景中的应用
    针对园区内车辆违停的监控挑战,算法定制视频分析网关能够与园区现有的监控体系无缝整合,利用人工智能技术对监控系统进行智能化升级,从而实现全天候自动监测车辆违停情况,有效解决了园区在车辆管理上的难题。通过在园区的禁止停车区、主要道路和停车场等关键位置安装监控摄像头,并将......
  • 算法转发和unidbg
    利用python库实现frida附加方式实现frida#-*-coding:UTF-8-*-importfrida,sysjsCode="""Java.perform(function(){varRequestUtil=Java.use('com.dodonew.online.http.RequestUtil');RequestUtil.encodeDesMap.overload(&......
  • 数据结构与算法——树与二叉树
    树与二叉树1.树的定义与相关概念树的示例:树的集合形式定义Tree=(K,R)元素集合:K={ki|0<=i<=n,n>=0,ki∈ElemType}(n为树中结点数,n=0则树为空,n>0则为非空树)对于一棵非空树,关系R满足下列条件:1.有且仅有一个结点没有前驱,称为根结点。2.处根结点外,其余每个结点有且仅有一个前......
  • 算法学习笔记3:图论
    图论拓扑序列有向无环图一定存在拓扑序列,通过入度为0来判断该点是否可以加入队列。强连通分量定义:在有向图G中,如果两个顶点u,v间有一条从u到v的有向路径,同时还有一条从v到u的有向路径,则称两个顶点强连通。如果有向图G的每两个顶点都强连通,称G是一个强连通图。有向非强连通图......
  • 算法学习笔记1:数据结构
    数据结构并查集一种树形的数据结构,近乎O(1)的时间复杂度。又一次理解了并查集用来维护额外信息的作用,可以用来记录集合中的元素个数,也可以维护节点到根节点之间的距离,可能还有别的,然后在进行路径压缩的时候修改需要维护的额外信息。主要构成pre[]数组、find()、join()......
  • 算法学习笔记2:搜索
    搜索BFS我的理解:基础的bfs本质上也是动态规划,dist[i,j]表示到达(i,j)转移的最小次数。由于动态规划的无后效性,就是当前状态确定后,不需要管之前的状态转移。bfs是一层一层搜的,搜索的相当于是一个状态,第一个搜到的就是最优的。比如最简单的走迷宫,每个点只会走一次,那么第一......
  • 机器学习中的算法—背包问题
    原文链接:机器学习中的算法—背包问题–每天进步一点点背包问题是一种资源分配算法,是一种非常典型的问题,是对资源分配最大化的体现。倘若有n件物品,其中每件物品都有属于自己的质量和价值,现在仅有一个背包,但是背包最大的承载量为w,因此需要试图在这个背包里装一些物品,使得物品的......
  • 【计算机专业毕设选题推荐】基于协同过滤算法的的儿童图书推荐系统的设计与实现 【附
    ✍✍计算机编程指导师⭐⭐个人介绍:自己非常喜欢研究技术问题!专业做Java、Python、小程序、安卓、大数据、爬虫、Golang、大屏等实战项目。⛽⛽实战项目:有源码或者技术上的问题欢迎在评论区一起讨论交流!⚡⚡Java实战|SpringBoot/SSMPython实战项目|Django微信小程......
  • 紫微斗数算法的实现流程
    题外话我想了又想大凡能够修炼成绝世高手的都是“魔鬼”。只有魔鬼才会纯粹的“敢贪,敢嗔,敢痴”。你我都困在了敢字。程序猿拿起拿锋利的刀,解构世间的一切吧!最近看西游有感而发。“联系是普遍存在的,规律是客观存在的”,那能不能用程序来解构命运的客观存在?那就来试试吧!​ 紫微......
  • SMO算法 公式推导
    ......