Java实现DTW

1、DTW类

Dtw.java

-----------------------------------------------------------------

public class Dtw {

public double getMin(double a, double b, double c) {
double min = a;
if (b > a)
min = a;
else if (c > b) {
min = b;
} else {
min = c;
}
return min;
}

public double getDistance(double[] seqa, double[] seqb) {
double distance = 0;
int lena = seqa.length;
int lenb = seqb.length;
double[][] c = new double[lena][lenb];
for (int i = 0; i < lena; i++) {
for (int j = 0; j < lenb; j++) {
c[i][j] = 1;
}
}
for (int i = 0; i < lena; i++) {
for (int j = 0; j < lenb; j++) {
double tmp = (seqa[i] - seqb[j]) * (seqa[i] - seqb[j]);
if (j == 0 && i == 0)
c[i][j] = tmp;
else if (j > 0)
c[i][j] = c[i][j - 1] + tmp;
if (i > 0) {
if (j == 0)
c[i][j] = tmp + c[i - 1][j];
else
c[i][j] = tmp + getMin(c[i][j - 1], c[i - 1][j - 1], c[i - 1][j]);
}
}
}
distance = c[lena - 1][lenb - 1];
return distance;
}

}


2、测试

Test.java

-----------------------------------------------------------------

public class Test {
public static void main(String[] args) {
Dtw dtw = new Dtw();
double[] x = { 3, 5, 6, 7, 7, 1 };
double[] y = { 3, 6, 6, 7, 8, 1, 1 };
double[] z = { 2, 5, 7, 7, 7, 7, 2 };
System.out.println(dtw.getDistance(x, y));
System.out.println(dtw.getDistance(x, z));
}
}


Java实现DTW时间序列算法可以使用第三方库,例如tslearn和jmotif。以下是使用tslearn库实现DTW算法的步骤: 1. 安装tslearn库:可以使用pip install tslearn命令进行安装。 2. 导入tslearn库:在Java代码中导入tslearn库。 3. 加载时间序列数据:将需要比较的时间序列数据加载到Java程序中。 4. 计算DTW距离:使用tslearn库中的dtw方法计算两个时间序列之间的DTW距离。 5. 输出结果:将计算结果输出到控制台或者文件中。 以下是一个简单的Java程序示例,演示如何使用tslearn库实现DTW算法: ``` import java.util.ArrayList; import java.util.List; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; import org.apache.commons.math3.ml.distance.DistanceMeasure; import org.apache.commons.math3.ml.distance.EuclideanDistance; import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation; import org.apache.commons.math3.stat.descriptive.moment.Mean; import org.apache.commons.math3.stat.descriptive.rank.Percentile; import org.apache.commons.math3.stat.descriptive.rank.Median; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; import org.apache.commons.math3.stat.descriptive.rank.Max; import org.apache.commons.math3.stat.descriptive.rank.Min; import org.apache.commons.math3.stat.descriptive.rank.Sum; import org.apache.commons.math3.stat.descriptive.rank.Product; import org.apache.commons.math3.stat.descriptive.rank.Variance; import org.apache.commons.math3.stat.descriptive.rank.Skewness; import org.apache.commons.math3.stat.descriptive.rank.Kurtosis; import org.apache.commons.math3.stat.descriptive.moment.GeometricMean; import org.apache.commons.math3.stat.descriptive.moment.HarmonicMean; import org.apache.commons.math3.stat.descriptive.moment.SecondMoment; import org.apache.commons.math3.stat.descriptive.moment.ThirdMoment; import org.apache.commons.math3.stat.descriptive.moment.FourthMoment; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovariance; import org.apache.commons.math3.stat.descriptive.moment.VectorialMean;import org.apache.commons.math3.stat.descriptive.moment.VectorialProduct; import org.apache.commons.math3.stat.descriptive.moment.VectorialSummaryStatistics; import org.apache.commons.math3.stat.descriptive.moment.VectorialVariance; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewness; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosis; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrix; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVector; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMoment; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMoment; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMoment; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMoment; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceMatrixValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanVectorValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCentralMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialFourthMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialThirdMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSecondMomentValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialProductValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialMeanValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialVarianceValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialSkewnessValue; import org.apache.commons.math3.stat.descriptive.moment.VectorialKurtosisValue; import org.apache.commons.math3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值