ALS

package
v0.0.0-...-ed64787 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2014 License: MIT Imports: 9 Imported by: 2

README

Alternating Least Squares (ALS)

Uses the ALS algorithm outlined in this article for the explicit case. For the implicit case, the algorithm is outlined here

Relies on Skelter John's matrix.go package for some matrix functionality.

Usage

  1. Install go get github.com/timkaye11/goRecommend/ALS

  2. Download the dependencies

    • If GPM is not installed, then do: brew install gpm
    • Get the dependencies: gpm install
  3. Run Code

import "github.com/timkaye11/goRecommend/ALS"

func main() {
	// For this instance, cols indicate product ID ; rows indicate user ID. Remember indexing starts at 0. 
	Q := MakeRatingMatrix([]float64{
		5, 5, 5, 0, 1,
		0, 0, 0, 4, 1,
		1, 2, 3, 3, 1,
		2, 0, 4, 1, 0,
		5, 2, 0, 1, 0}, 5, 5)

	// OR load in through a text file
	// Q := Load("path/to/file", "separator") // where separator can be a comma, tally, tab, etc...

	// Train a model with 5 factors, 10 iterations, and a lambda value of 0.01.
	// 10 iterations is usually enough to reach convergence, and a lambda val of 0.01 is acceptable.
	n_factors := 5
	n_iterations := 10
	lambda := 0.01

	// Train Model Using Explicit ALS. This means that users rated each product on a scale
	// where 0 indicates not rated
	// Prints out the final error value.
	Qhat := Train(Q, n_factors, n_iterations, lambda)
	fmt.Println(Qhat)

	// Get Prediction for a user/product pair.
	fmt.Println(Predict(Qhat, 2, 1))

	// Get top - N recommended products based off of Qhat for a given user ID.
	// Args: Original user/product matrix, trained model, N, product names.
	// Returns []string of length N & error - if applicable
	// If Product Names is nil, then returns top indices for each user. Returns in descending order.

	products := []string{"Macy Gray", "The Black Keys", "Spoon", "A Tribe Called Quest", "Kanye West"}
	fmt.Println(GetTopNRecommendations(Q, Qhat, userID, n, products))

	// Implicit. Can do 'GetTopNRecommendations' in implicit case too. 
	R := TrainImplicit(Q, 5, 10, 0.01)
	fmt.Println(Predict(R, 1, 1))

}

Documentation

Overview

Alternating Least Squares recommendation algorithm in Go !

Index

Constants

This section is empty.

Variables

View Source
var (
	NA = math.NaN()
)

Functions

func GetTopNRecommendations

func GetTopNRecommendations(Q, Qhat *DenseMatrix, user, n int, products []string) ([]string, error)

looks at the model generated by ALS and makes a user/product prediction Returns best n recommendations for a user index in the matrix. If products is nil, returns top indices. Else returns names of top products.

func Load

func Load(path, sep string) *DenseMatrix

read file with separator and load into a matrix. If user/product ID's start at 1, set first product/user at row/col index 0.

func MakeRatingMatrix

func MakeRatingMatrix(ratings []float64, rows, cols int) *DenseMatrix

Wrapper for MakeDenseMatrix. Returns rating matrix

func Predict

func Predict(Qhat *DenseMatrix, user, product int) (float64, error)

Returns recommended value for a given user-product indices. Error if out of range.

func Train

func Train(Q *DenseMatrix, n_factors, iterations int, lambda float64) (*DenseMatrix, float64)

Params: the user/product matrix, number of factors for recommendation, iterations, and lambda value for ALS. Returns the trained matrix with predictions for 0 valued entries, and the final error calculation (float64)

func TrainImplicit

func TrainImplicit(R *DenseMatrix, n_factors, iterations int, lambda float64) *DenseMatrix

Params: the rating matrix, number of factors, number of iterations, and lambda for building recommendation matrix. Returns the confidence matrix on a scale from 0 to 1.

Types

This section is empty.

Jump to

Keyboard shortcuts

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