定制 ankane/ngt 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ankane/ngt

最新稳定版本:v0.2.0

Composer 安装命令:

composer require ankane/ngt

包简介

High-speed approximate nearest neighbors for PHP

README 文档

README

NGT - high-speed approximate nearest neighbors - for PHP

Build Status

Installation

Run:

composer require ankane/ngt

Add scripts to composer.json to download the shared library:

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

And run:

composer install

On Mac, also install OpenMP:

brew install libomp

NGT is not available for Windows

Getting Started

Prep your data

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

Create an index

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

Insert objects

$index->batchInsert($objects);

Search the index

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

Save the index

$index->save($path);

Load an index

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

Get an object by id

$index->object($id);

Insert a single object

$index->insert($object);

Remove an object by id

$index->remove($id);

Build the index

$index->buildIndex();

Full Example

$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");
}

Index Options

Defaults shown below

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
);

Credits

This library is modeled after NGT’s Python API.

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

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

统计信息

  • 总下载量: 2.26k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2022-11-19