You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VB.NET中使用NDtw库实现时间序列DTW计算

如何在VB.NET中使用NDtw NuGet包比较时间序列?

问题描述

我希望使用NDtw NuGet包比较两个简单时间序列,以下是可正常运行的F#示例代码:

open NDtw

// Example comparison of 2 small datasets
let exampleDtw = new Dtw(
    [| 1.; 3.; 7.; 8.; 11.;  6.; 6.; |],
    [| 3.; 6.; 3.; 7.; 13.; 12.; 7.; |])

// Get the coordinate path used for the best match between the datasets
let output = exampleDtw.GetCost()

printf "%A" output

但我按照这个格式编写的VB.NET代码无法运行:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim dist As Int16

    Dim dtw As New Dtw([| 1.; 3.; 7.; 8.; 11.;  6.; 6.; |],[| 3.; 6.; 3.; 7.; 13.; 12.; 7.; |])

    dist = dtw.GetCost()

    TextBox3.Text = dist.ToString

End Sub

修正后的代码及说明

修正后的VB.NET代码

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' 声明正确的类型存储距离结果
    Dim dist As Double

    ' 使用VB.NET标准数组语法初始化时间序列
    Dim series1 As Double() = New Double() {1.0, 3.0, 7.0, 8.0, 11.0, 6.0, 6.0}
    Dim series2 As Double() = New Double() {3.0, 6.0, 3.0, 7.0, 13.0, 12.0, 7.0}

    ' 初始化Dtw实例
    Dim dtw As New Dtw(series1, series2)

    ' 获取计算后的距离值
    dist = dtw.GetCost()

    ' 显示结果(可选格式化输出)
    TextBox3.Text = dist.ToString("F2")
End Sub

关键修改点

  • 数组语法适配:VB.NET不支持F#的[| ... |]数组语法,必须使用New Double() {元素}的标准初始化方式。
  • 类型匹配修正:Dtw.GetCost()方法返回的是Double类型的浮点距离值,原代码中用Int16接收会导致类型转换错误,需改为Double。
  • 可选格式化:用ToString("F2")可以将结果格式化为保留两位小数的字符串,提升可读性。

关于SeriesVariable类的使用

如果需要自定义序列配置(比如指定距离计算规则),可以用SeriesVariable包装数组后传入:

' 使用SeriesVariable包装时间序列
Dim seriesVar1 As New SeriesVariable(series1)
Dim seriesVar2 As New SeriesVariable(series2)
Dim dtw As New Dtw(seriesVar1, seriesVar2)

基础的时间序列比较场景,直接传入Double()数组的重载即可满足需求。


内容的提问来源于stack exchange,提问作者Jamie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 19:07:04