clj-recommendation
A clojure recommendation library. Written for educational purposes but might be useful to someone.
In beta and changing frequently at the moment.
Install as:
[clj-recommendation "0.1.33"]
Require:
(:require [clj-recommendation :as rec])
Usage
The library defines a few data structures that can act as recommenders. In general when querying the query should be in the same format as the underlying data structure used by the recommender. In theory it should be easy to produce recommenders with different underlying data (i.e. databases) by programming at the protocols defined in the library.
Recommenders
The function knn-recommender
will return a knnRecommender record
when supplied with data, a distance calculation method keyword and the
number of nearest neighbours to use in recommendations (k; default
5). The data should be a hash of hashes and attribute values should be
numerical. Allowable distance calculation keywords are :pearson,
:cosine, :euclidean and :manhattan. The functions nearest-neighbours
and recommend
can then be used to get nearest neighbours and
recommendations:
user> music-example
{"Bill" {"Blues Traveler" 2.0, "Broken Bells" 3.5, "Deadmau5" 4.0,
"Phoenix" 2.0, "Slightly Stoopid" 3.5, "Vampire Weekend" 3.0}, "Chan"
{"Blues Traveler" 5.0, "Broken Bells" 1.0, "Deadmau5" 1.0,
"Norah Jones" 3.0, "Phoenix" 5, "Slightly Stoopid" 1.0}, ...}
user> (def nh (knn-recommender music-example :pearson))
#'clj-recommendation.core-test/nh
user> (nearest-neighbours nh {"query" {"Blues Traveler" 3.5, "Broken Bells" 2.0,
"Norah Jones" 4.5, "Phoenix" 5.0, "Slightly Stoopid" 1.5, "The Strokes" 2.5,
"Vampire Weekend" 2.0}})
(["Veronica" 0.829385976226374] ["Chan" 0.8197822947299414] ["Jordyn"
0.7639748605475432] ["Hailey" 0.42008402520840293] ["Sam" 0.2818672605010608])
user> (recommend nh {"query" {"Blues Traveler" 3.5, "Broken Bells" 2.0,
"Norah Jones" 4.5, "Phoenix" 5.0, "Slightly Stoopid" 1.5, "The Strokes" 2.5,
"Vampire Weekend" 2.0}})
"Deadmau5"
user> (recommendations nh {"query" {"Broken Bells" 4.0, "Deadmau5" 1.0,
"Norah Jones" 4.0, "The Strokes" 4.0, "Vampire Weekend" 1.0}})
(["Phoenix" 5.0] ["Slightly Stoopid" 4.5])
>user
Both nearest-neighbour
and recommendations
return list of vectors
with the first element of each vector corresponding to the name of the
neighbour or recommendation and the second element the distance score
or the predicted value of the recommendation based on the query
attribute values, respectively. recommend
returns a single value.
For item-based recommendations similarity matrix records can be
defined using weighted-slope1-sim-matrix
or
adj-cosine-sim-matrix
. Both accept a nested hash of hashes of names
or classes and a hash of attributes. Attribute values should be
numerical. The helper function invert-hash
can be used to invert a
hash for use in a similarity matrix. The recommend
and predict
functions can then be used to provide recommendations and predictions
based on item similarity deviations:
user> (def wsm (weighted-slope1-sim-matrix (invert-hash music-example)))
#'clj-recommendation.core-test/wsm
user> (predict wsm "Blues Traveler" {"query" {"Broken Bells" 4.0,
"Deadmau5" 1.0, "Norah Jones" 4.0, "The Strokes" 4.0, "Vampire Weekend" 1.0}})
3.526315789473684
user> (recommend wsm {"query" {"Broken Bells" 4.0, "Deadmau5" 1.0, "Norah Jones"
4.0, "The Strokes" 4.0, "Vampire Weekend" 1.0}})
"Phoenix"
user> (recommendations recm {"query" {"Broken Bells" 4.0, "Deadmau5" 1.0, "Norah Jones"
4.0, "The Strokes" 4.0, "Vampire Weekend" 1.0}})
(["Phoenix" 3.8541666666666665] ["Blues Traveler" 3.526315789473684]
["Slightly Stoopid" 2.8541666666666665])
user>
License
Copyright © 2017 Jason Mulvenna
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.