# AlgorithmStar 度量 计算组件 *AlgorithmStar* 本文将会基于 AlgorithmStar 1.44 以及以上的版本来演示,度量 计算 组件 的使用! ## 目录 [TOC]  ## 获取到依赖库 在这里我们直接通过 maven 来将依赖库文件导入到我们的项目中,只需要在 `pom.xml` 中输入下面的依赖坐标即可! ```xml <dependencies> <!-- Binding using the adapter of log4j2 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.20.0</version> <!--<scope>provided</scope>--> </dependency> <!-- Log4j2 log facade --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.20.0</version> <!--<scope>provided</scope>--> </dependency> <!-- Log4j2 log real surface --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.20.0</version> <!--<scope>provided</scope>--> </dependency> <dependency> <groupId>io.github.BeardedManZhao</groupId> <artifactId>algorithmStar</artifactId> <version>1.44</version> </dependency> </dependencies> ``` ## 度量计算组件 计算实例 距离算法主要就是针对两个坐标之间的距离进行研究与计算,著名的欧几里德为例,距离算法的使用如下所示,其它计算组件的使用方式与欧几里得相同,其它组件在下面也有介绍。 | 计算组件类型 | 支持版本 | 功能 | |--------------------------------------------------------------------------------------------------|------|-------------| | io.github.beardedManZhao.algorithmStar.algorithm.distanceAlgorithm.EuclideanMetric | v1.0 | 计算欧几里得距离 | | io.github.beardedManZhao.algorithmStar.algorithm.distanceAlgorithm.CanberraDistance | v1.0 | 计算堪培拉距离 | | io.github.beardedManZhao.algorithmStar.algorithm.distanceAlgorithm.ChebyshevDistance | v1.0 | 计算切比雪夫距离 | | io.github.beardedManZhao.algorithmStar.algorithm.distanceAlgorithm.CosineDistance | v1.0 | 计算向量余弦度量 | | io.github.beardedManZhao.algorithmStar.algorithm.distanceAlgorithm.HausdorffDistance | v1.0 | 计算豪斯多夫距离 | | io.github.beardedManZhao.algorithmStar.algorithm.distanceAlgorithm.ManhattanDistance | v1.0 | 计算曼哈顿距离 | | io.github.beardedManZhao.algorithmStar.algorithm.distanceAlgorithm.MinkowskiDistance | v1.0 | 计算闵可夫斯基距离 | | io.github.beardedManZhao.algorithmStar.algorithm.distanceAlgorithm.StandardizedEuclideanDistance | v1.0 | 计算标准化欧几里得度量 | ### 距离计算代表 - 欧几里德距离计算组件 此计算组件所计算的数值会随着数据的区别增大而增大,它返回的是距离! ```java package top.lingyuzhao; import io.github.beardedManZhao.algorithmStar.algorithm.distanceAlgorithm.EuclideanMetric; import io.github.beardedManZhao.algorithmStar.core.AlgorithmStar; import io.github.beardedManZhao.algorithmStar.core.VectorFactory; import io.github.beardedManZhao.algorithmStar.operands.coordinate.FloatingPointCoordinates; import io.github.beardedManZhao.algorithmStar.operands.vector.IntegerVector; /** * @author zhao */ public final class MAIN { public static void main(String[] args) { // 构建两个向量对象 分别是 [ 1 2 3 4 5 6 7 8 9 10 ] 以及 [ 1 2 3 4 5 6 7 8 90 10 ] final VectorFactory vectorFactory = AlgorithmStar.vectorFactory(); final IntegerVector integerVector1 = vectorFactory.parseVector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); final IntegerVector integerVector2 = vectorFactory.parseVector(1, 2, 3, 4, 5, 6, 7, 8, 90, 10); // 获取到计算组件 这个计算组件是 德式度量 计算组件 final EuclideanMetric<?, FloatingPointCoordinates<?>> euclideanMetric = EuclideanMetric.getInstance("euclideanMetric"); // 打印一下两个向量 System.out.println(integerVector1); System.out.println(integerVector2); // 构建一个 AS 库计算对象 我们只需要把 计算组件 和 被计算的操作数 传递给它就可以啦 final AlgorithmStar<?, ?> instance = AlgorithmStar.getInstance(); // 获取到 第一个向量与第一个向量的距离 不需要担心 toArray 的性能问题,其获取到的是向量对象中的引用 // TODO 因为我们在计算的时候是不需要进行修改的操作,因此我们可以直接获取到引用,请注意,如果需要修改的话,请使用 copyToNewArray 方法 final double trueDistance1 = instance.getTrueDistance(euclideanMetric, integerVector1.toArray(), integerVector1.toArray()); System.out.println(trueDistance1); // 获取到 第一个向量与第二个向量的距离 final double trueDistance2 = instance.getTrueDistance(euclideanMetric, integerVector1.toArray(), integerVector2.toArray()); System.out.println(trueDistance2); } } ``` 下面是计算结果 ``` [ 1 2 3 4 5 6 7 8 9 10 ] [ 1 2 3 4 5 6 7 8 90 10 ] 0.0 81.0 ``` ### 相似度系数计算代表 - 向量余弦距离计算组件 此计算操作中返回的是两个被比较对象的相似度,其越是接近1,就越是相似,在下面我们进行了一个演示。 ```java package top.lingyuzhao; import io.github.beardedManZhao.algorithmStar.algorithm.distanceAlgorithm.CosineDistance; import io.github.beardedManZhao.algorithmStar.core.AlgorithmStar; import io.github.beardedManZhao.algorithmStar.core.VectorFactory; import io.github.beardedManZhao.algorithmStar.operands.vector.IntegerVector; /** * @author zhao */ public final class MAIN { public static void main(String[] args) { // 构建两个向量对象 分别是 [ 1 2 3 4 5 6 7 8 9 10 ] 以及 [ 1 2 3 4 5 6 7 8 90 10 ] final VectorFactory vectorFactory = AlgorithmStar.vectorFactory(); final IntegerVector integerVector1 = vectorFactory.parseVector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); final IntegerVector integerVector2 = vectorFactory.parseVector(1, 2, 3, 4, 5, 6, 7, 8, 90, 10); // 获取到计算组件 这个计算组件是 向量余弦距离 计算组件 final CosineDistance<IntegerVector> cos = CosineDistance.getInstance("cos"); // 打印一下两个向量 System.out.println(integerVector1); System.out.println(integerVector2); // 构建一个 AS 库计算对象 final AlgorithmStar<?, ?> instance = AlgorithmStar.getInstance(); // 获取到 第一个向量与第一个向量的距离 不需要担心 toArray 的性能问题,其获取到的是向量对象中的引用 // TODO 因为我们在计算的时候是不需要进行修改的操作,因此我们可以直接获取到引用,请注意,如果需要修改的话,请使用 copyToNewArray 方法 final double trueDistance1 = instance.getTrueDistance(cos, integerVector1.toArray(), integerVector1.toArray()); System.out.println(trueDistance1); // 获取到 第一个向量与第二个向量的距离 final double trueDistance2 = instance.getTrueDistance(cos, integerVector1.toArray(), integerVector2.toArray()); System.out.println(trueDistance2); } } ``` 下面是计算结果 ``` [ 1 2 3 4 5 6 7 8 9 10 ] [ 1 2 3 4 5 6 7 8 90 10 ] 1.0664819944598338 0.6443030653556969 ``` ### 其它计算组件 在上面的例子中,我们演示的就是两种计算组件的代表,实际上,每一个计算组件都有其特别之处,在 AS机器学习库中,这类计算组件都属于 `io.github.beardedManZhao.algorithmStar.algorithm.distanceAlgorithm.DistanceAlgorithm` 类的子类,因此想要使用更多计算组件,您可以修改上面例子中的 “获取计算组件” 操作! ==还未更新== ------ ***操作记录*** 作者:[algorithmStar](https://www.lingyuzhao.top//index.html?search=23 "algorithmStar") 操作时间:2025-01-24 19:13:25 星期五 【时区:UTC 8】 事件描述备注:保存/发布 中国 天津 [](如果不需要此记录可以手动删除,每次保存都会自动的追加记录)