node.js - 找到在NodeJS中找到对象列表的欧几里得距离的最佳方法
问题描述
我有一个如下的对象列表,需要针对列表中的所有项目找到 test_obj 的欧几里德距离。
object_list = [ [1,2,3],
[10,20,30],
[15,25,35],
[20,30,40] ]
test_obj = [15,25,35]
我发现 numpy.linalg.norm() 在 python 中找到它。在 NodeJS 中是否有任何类似的方法或实用程序可以做同样的事情?
解决方案
Node.js 有一个模块可以完成此任务:euclidean-distance。
然后我们可以使用 array.map() 来获取从 test_obj 到 object_list 中每个项目的距离(反之亦然!),如下所示:
const distance = require('euclidean-distance')
object_list = [ [1,2,3],
[10,20,30],
[15,25,35],
[20,30,40] ]
test_obj = [15,25,35]
let distances = object_list.map(obj => distance(test_obj, obj));
console.log("Distances:", distances);
推荐阅读
- c# - 使用 Nominatim API 的反向地理编码不起作用
- php - 使用 laravel 连接查询
- hyperledger-composer - 订阅 Composer v0.19.12 上的事件
- javascript - 在 RxDb 中转储单个文档
- ruby-on-rails - Rspec 测试退出
- excel - 带有 Application.OnTime 的宏仅在打开工作表时有效
- python - 使用一个值在另一个输入字段 Tkinter Python 中使用
- ruby-on-rails - Rails 中公共目录中的文件 - Errno::ENOENT(没有这样的文件或目录 @ rb_sysopen
- .net - 按顺序从一串值中更改多个 ComboBoxes 的 SelectedIndex
- javascript - 如何让脚本标签仅在 Firefox 上有效?