vectorizer

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2024 License: MIT Imports: 2 Imported by: 0

README

Vectorizer

Go Reference Go Tests

Vectorizer is a generic compatible module, which provides an efficient way to manage and manipulate sparse vectors. It supports operations such as applying values to vectors, calculating magnitudes, and computing cosine similarity.

Features

  • Sparse Vector Representation: Efficiently store vectors with non-zero values only.
  • Vector Operations: Includes methods for scaling, normalizing, and calculating cosine similarity.

Prerequisites

  • Go 1.23.0 or above

Installation

go get -u github.com/softwarespot/vectorizer

Usage

A basic example of using Vectorizer.

package main

import (
	"fmt"

	"github.com/softwarespot/vectorizer"
)

func main() {
	// Create a vectorizer with the type "string"
   	vz := vectorizer.New[string](128)

	vec1 := vectorizer.NewVector[string]()
	vz.ApplyTo(vec1, "Event 1", 1)
	vz.ApplyTo(vec1, "Event 2", 2)
	vz.ApplyTo(vec1, "Event 3", 3)

	vec2 := vectorizer.NewVector[string]()
	vz.ApplyTo(vec2, "Event 2", 2)
	vz.ApplyTo(vec2, "Event 3", 3)

	fmt.Println(vec1.CosineSimilarity(vec2)) // outputs: 0.9636241116594316
}

License

The code has been licensed under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Vector

type Vector[T comparable] struct {
	// contains filtered or unexported fields
}

Vector represents a sparse vector.

func NewVector

func NewVector[T comparable]() *Vector[T]

NewVector creates a new empty Vector.

func (*Vector[T]) CosineSimilarity

func (v *Vector[T]) CosineSimilarity(v2 *Vector[T]) (float64, error)

CosineSimilarity calculates the cosine similarity between two vectors. It returns an error if either vector has zero magnitude.

func (*Vector[T]) Normalize

func (v *Vector[T]) Normalize()

Normalize scales the vector to have a magnitude of 1. If the vector has zero magnitude, it remains unchanged.

func (*Vector[T]) Scale

func (v *Vector[T]) Scale(scalar float64)

Scale multiplies all elements of the vector by a scalar value.

type Vectorizer

type Vectorizer[T comparable] struct {
	// contains filtered or unexported fields
}

Vectorizer is a structure that manages dimensions for vectors.

func New

func New[T comparable](size int) *Vectorizer[T]

New creates a new Vectorizer with an initial size.

func (*Vectorizer[T]) ApplyTo

func (vz *Vectorizer[T]) ApplyTo(v *Vector[T], key T, value float64)

ApplyTo applies a value to the vector at the specified key.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL