ankane/ngt

为PHP提供的高速近似最近邻算法

v0.2.0 2024-06-03 03:03 UTC

This package is not auto-updated.

Last update: 2024-09-23 04:18:05 UTC


README

NGT - 为PHP提供的高速近似最近邻算法

Build Status

安装

运行

composer require ankane/ngt

将脚本添加到composer.json中,以下载共享库

    "scripts": {
        "post-install-cmd": "Ngt\\Vendor::check",
        "post-update-cmd": "Ngt\\Vendor::check"
    }

然后运行

composer install

在Mac上,还需要安装OpenMP

brew install libomp

NGT不支持Windows

入门指南

准备数据

$objects = [
    [1, 1, 2, 1],
    [5, 4, 6, 5],
    [1, 2, 1, 2]
];

创建索引

$index = new Ngt\Index($dimensions);

插入对象

$index->batchInsert($objects);

搜索索引

$index->search($query, size: 3);

保存索引

$index->save($path);

加载索引

$index = Ngt\Index::load($path);

通过id获取对象

$index->object($id);

插入单个对象

$index->insert($object);

通过id删除对象

$index->remove($id);

构建索引

$index->buildIndex();

完整示例

$dim = 10;
$objects = [];
for ($i = 0; $i < 100; $i++) {
    $object = [];
    for ($j = 0; $j < $dim; $j++) {
        $object[] = rand(0, 100);
    }
    $objects[] = $object;
}

$index = new Ngt\Index($dim);
$index->batchInsert($objects);

$query = $objects[0];
$result = $index->search($query, size: 3);

foreach ($result as $res) {
    print($res['id'] . ', ' . $res['distance'] . "\n");
}

索引选项

以下为默认选项

use Ngt\DistanceType;
use Ngt\ObjectType;

new Ngt\Index(
    $dimensions,
    edgeSizeForCreation: 10,
    edgeSizeForSearch: 40,
    distanceType: DistanceType::L2,  // L1, L2, Hamming, Angle, Cosine, NormalizedAngle, NormalizedCosine, Jaccard
    objectType: ObjectType::Float    // Float, Float16, Integer
);

致谢

此库基于NGT的Python API

历史

查看变更日志

贡献

鼓励每个人帮助改进此项目。以下是一些您可以提供帮助的方式

开始开发

git clone https://github.com/ankane/ngt-php.git
cd ngt-php
composer install
composer test