collabFilter

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: 8 Imported by: 0

README

Memory-Based Collaborative Filtering (in Go)

To quote wikipedia, "this is used for making recommendations".

This uses a neighest-neighbor based algorithm to compare a users product selection with other users and recommends products for a selected uesr. Cosine similarity is used when there is an explicit rating system, and jaccard similarity is used for the binary case.


To use, download the package: go get github.com/timkaye11/goRecommend/collabFilter


Example

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

func main() {
	// User product matrix. 0 indicates products not viewed by user.
	// Uses cosine similarity.


	// arguments to **MakeRatingMatrix** are: data, nrows, ncols. 
	prefs := MakeRatingMatrix([]float64{
		2, 3, 4, 1, 5,
		3, 0, 3, 3, 0,
		4, 4, 1, 2, 3,
		2, 4, 0, 3, 4,
		3, 1, 3, 0, 4}, 5, 5)

	// Can also load/build matrix from a text file
	prefs := Load("path/to/file", "separator")


	// product titles <- column titles for prefs matrix
	products := []string{"Spiderman", "Big Momma's House", "Vanilla Sky", "Pacific Rim", "The Mask"}
	// gets recommendations for user 1 (second row) for un-rated products.
	prods, scores, err := GetRecommendations(prefs, 1, products)
	if err != nil {
		fmt.Println("WHAT!?")
	}
	fmt.Printf("\nRecommended Products are: %v, with scores: %v", prods, scores)

	// For a binary matrix, use the getBinaryRecommendations function in the exact same way.
	// Uses Jaccard Similarity to return confidence/probabality of user's recommendations
	binaryPrefs := MakeRatingMatrix([]float64{
		1, 1, 1, 1, 0,
		0, 1, 1, 0, 1,
		1, 1, 1, 1, 1,
		1, 1, 0, 0, 1,
		1, 0, 1, 1, 1}, 5, 5)
	// Returns recommended products for User ID 1 (second row) in descending order, w/ corresponding confidence/probability,
	// and error - if applicable.
	prods, scores, _ := GetBinaryRecommendations(binaryPrefs, 1, products)
	...


}

Documentation

Overview

User-item recommendation using nearest-neighrbor collaborative filtering in Go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CosineSim

func CosineSim(a, b []float64) float64

Cosine Similarity between two vectors Returns cos similarity on a scale from 0 to 1.

func DotProduct

func DotProduct(a, b []float64) (float64, error)

Find the dot product between two vectors

func GetBinaryRecommendations

func GetBinaryRecommendations(prefs *DenseMatrix, user int, products []string) ([]string, []float64, error)

Gets Recommendations for a user (row index) based on the prefs matrix. Uses cosine similarity for rating scale, and jaccard similarity if binary

func GetRecommendations

func GetRecommendations(prefs *DenseMatrix, user int, products []string) ([]string, []float64, error)

Gets Recommendations for a user (row index) based on the prefs matrix. Uses cosine similarity for rating scale, and jaccard similarity if binary

func Jaccard

func Jaccard(a, b []float64) float64

defined as A n B / A u B. Used for binary user/product matrices.

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. Already tested in ALS package

func MakeRatingMatrix

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

func NormSquared

func NormSquared(a []float64) float64

For cosine similarity. Returns sqrt of sum of squared elements.

Types

This section is empty.

Jump to

Keyboard shortcuts

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