Skip to content

Instantly share code, notes, and snippets.

@sudipto80
Created January 21, 2016 08:56
Show Gist options
  • Save sudipto80/7002e66350ca7b2a7551 to your computer and use it in GitHub Desktop.
Save sudipto80/7002e66350ca7b2a7551 to your computer and use it in GitHub Desktop.
Collaborative Filtering
// Learn more about F# at http://fsharp.org
// See the 'F# Tutorial' project for more help.
//Comprehensive coverage of Collaborative Filtering Techniques
//http://www.hindawi.com/journals/aai/2009/421425/
module confusion =
let TP (matches : int [] []) =
matches |> Array.mapi( fun i j -> matches.[i].[i]) |> Array.sum
//True Positive
let TP_for (thisOne : int) (matches : int [] []) =
matches.[thisOne].[thisOne]
//False positive for
let FP_for (thisOne : int) (matches : int [] []) =
let all = [for i in 0 .. matches.Length-1 -> matches.[i].[thisOne]]
let allSum = all |> List.sum
allSum - (TP_for thisOne matches)
//False negative for
let FN_for (thisOne : int) (matches : int [] []) =
let all = [for i in 0 .. matches.[thisOne].Length - 1 -> matches.[thisOne].[i]]
let allSum = all |> List.sum
allSum - matches.[thisOne].[thisOne]
//True negativs. All other animals that are
let TN_for (thisOne : int) (matches : int [] []) =
let rows = [0 .. matches.Length - 1] |> List.filter (fun t -> t <> thisOne)
let cols = [0 .. matches.[0].Length - 1 ] |> List.filter (fun t -> t <> thisOne)
let carts = rows |> List.collect (fun x -> cols |> List.map (fun y -> x, y))
carts |> List.map (fun t -> matches.[fst t].[snd t]) |> List.sum
//True Positive Rate and Sensitivity are same thing
let TPR (thisOne : int) (matches : int [] []) =
float (TP_for thisOne matches) / float( (TP_for thisOne matches + FN_for thisOne matches))
//Total Positives
let P (thisOne : int) (matches : int [] []) =
TP_for thisOne matches + FN_for thisOne matches
//Total Negatives
let N (thisOne : int) (matches : int [] []) =
FP_for thisOne matches + TN_for thisOne matches
//Specificity
let SPC (thisOne : int) (matches : int [] []) =
float ( TN_for thisOne matches )/ float ( N thisOne matches )
//Precision of Positive Predictive Value
let PPV (thisOne : int) (matches : int [] []) =
float (TP_for thisOne matches )/float (TP_for thisOne matches + FP_for thisOne matches)
let ``Precision``(thisOne : int) (matches : int [] []) =
PPV thisOne matches
//Negative Predictive value
let NPV (thisOne : int) (matches : int [] []) =
float (TN_for thisOne matches) / float ( TN_for thisOne matches + FN_for thisOne matches)
//Fall out or False Position Rate
let FPR (thisOne : int) (matches : int [] []) =
float (FP_for thisOne matches) / float (N thisOne matches)
//False Discovery Rate
let FDR (thisOne : int) (matches : int [] []) =
FP_for thisOne matches
//False Negative Rate or "miss" rate
let FNR (thisOne : int) (matches : int [] []) =
float (FN_for thisOne matches) / float( P thisOne matches )
//F1
let F1(thisOne : int) (matches : int [] []) =
2. * float (TP_for thisOne matches) /
( 2. * float (TP_for thisOne matches) + float( FP_for thisOne matches )+ float (FN_for thisOne matches))
//Matthews correlation coefficient (MCC)
let MCC (thisOne : int) (matches : int [] []) =
let numerator = TP_for thisOne matches * TN_for thisOne matches -
FP_for thisOne matches * FN_for thisOne matches
let A = float (TP_for thisOne matches + FP_for thisOne matches)
let B = float (TP_for thisOne matches + FN_for thisOne matches)
let C = float (TN_for thisOne matches + FP_for thisOne matches)
let D = float (TN_for thisOne matches + FN_for thisOne matches)
let denominator = sqrt (A * B * C * D)
float numerator / denominator
//Accuracy
let ACC (thisOne : int) (matches : int[][] ) =
let numerator = float (TP_for thisOne matches + TN_for thisOne matches )
let denominator = float (P thisOne matches + N thisOne matches)
numerator/denominator
//Sensitity for a given instance
let ``Sensitivity for`` (thisOne : int) (matches : int [] []) =
TPR thisOne matches
//Specificity for a given instance
let ``Specificity for`` (thisOne: int)(matches: int[][]) =
SPC thisOne matches
let ``Informedness``(thisOne:int) (matches:int[][]) =
``Sensitivity for`` thisOne matches + ``Specificity for`` thisOne matches - 1.
let ``Markedness``(thisOne:int) (matches:int[][]) =
Precision thisOne matches + NPV thisOne matches - 1.
let LRPlus (thisOne : int) (matches : int [] []) =
float (TPR thisOne matches ) / float (FPR thisOne matches)
let LRMinus (thisOne : int) (matches : int [] []) =
float (FNR thisOne matches) / float (SPC thisOne matches)
module recEval =
//Mean Absolute Error
let mae (ratings:float list)(predictions:float list) =
(List.zip ratings predictions |> List.sumBy (fun t -> abs (fst t - snd t)))/float ratings.Length
//Normalized Mean Absolute Error
let nmae (ratings:float list)(predictions:float list) =
let rMax = ratings |> List.max
let rMin = ratings |> List.min
(mae ratings predictions )/(rMax - rMin)
//root mean squared error
let rmse(ratings:float list)(predictions:float list) =
sqrt( ( List.zip ratings predictions |> List.map (fun t -> fst t - snd t) |> List.sum )/(float predictions.Length ))
let private den (index:int)(alpha:float)=
2.0 ** ((float (index+1) - 1.0)*(alpha - 1.0))
let halfLife (ratings:float list)(d:float)(alpha:float)=
ratings
|> List.mapi(fun index rating -> max (rating - d) 0.0 / den index alpha)
|> List.sum
let spearmanRank()=0.0
module collabFilter =
let commonItemIndices (ratings:(float list)list)(a:int)(u:int)=
List.zip ratings.[a] ratings.[u] |> List.mapi (fun i r -> if fst r <> 0.0 && snd r <> 0.0 then i else -1 )
|> List.filter ( fun t -> t <> -1)
let mu_au (ratings:(float list)list)(a:int)(u:int)=
let com = commonItemIndices ratings a u
if com.Length <> 0 then
let mu_a = com |> List.map (fun index -> ratings.[a].[index]) |> List.average
let mu_u = com |> List.map (fun index -> ratings.[u].[index]) |> List.average
(mu_a,mu_u)
else
(0.0,0.0)
let SimiDot (ratings :(float list)list)(i:int)(j:int)=
let li = ratings |> List.map(fun rating -> rating.[i])
let lj = ratings |> List.map(fun rating -> rating.[j])
let num = List.zip li lj |> List.sumBy (fun item -> fst item * snd item)
let d1 = li |> List.sumBy (fun item -> item * item )
let d2 = lj |> List.sumBy (fun item -> item * item )
if d1 = 0.0 || d2 = 0.0 then 0.0 else num / (sqrt d1 * sqrt d2)
let SimuDot (ratings :(float list)list) (a:int)(u:int)=
let num = List.zip ratings.[a] ratings.[u]
|> List.sumBy (fun item -> fst item * snd item)
let d1 = ratings.[a] |> List.sumBy (fun item -> item * item )
let d2 = ratings.[u] |> List.sumBy (fun item -> item * item )
if d1 = 0.0 || d2 = 0.0 then 0.0 else num / (sqrt d1 * sqrt d2)
//User-User similarity using Pearson's Correlation Coefficient
let Simu (ratings:(float list)list) (a:int)(u:int)=
let common = commonItemIndices ratings a u
let averages = mu_au ratings a u
let ra = fst averages
let ru = snd averages
let num = common |> List.sumBy (fun t -> (ratings.[a].[t] - ra)*(ratings.[u].[t] - ru))
let d1 = common |> List.sumBy (fun t -> (ratings.[a].[t] - ra)** 2.0)
let d2 = common |> List.sumBy (fun t -> (ratings.[u].[t] - ru)** 2.0)
if d1 = 0.0 || d2 = 0.0 then 0.0 else num / ((sqrt d1) * (sqrt d2 ))
let rBari (ratings:(float list)list)(itemIndex:int)=
let nonZeroRatings = ratings |> List.map (fun rating -> rating.[itemIndex])
|> List.filter (fun t -> t <> 0.0)
nonZeroRatings |> List.average
let Simi(ratings:(float list)list) (i:int)(j:int)=
let ri_bar = rBari ratings i
let rj_bar = rBari ratings j
let common = commonItemIndices ratings i j
let num = common |> List.sumBy (fun t -> (ratings.[i].[t] - ri_bar)*(ratings.[j].[t] - rj_bar))
let d1 = common |> List.sumBy (fun t -> (ratings.[i].[t] - ri_bar)** 2.0)
let d2 = common |> List.sumBy (fun t -> (ratings.[i].[t] - rj_bar)** 2.0)
if d1 = 0.0 || d2 = 0.0 then 0.0 else num / ((sqrt d1) * (sqrt d2 ))
//Average rating for all other rated items by the user
//except the item "except"
let rBaru(u:float list)(except:int)=
let filtered = u |> List.mapi(fun i j -> if i <> except then j else 0.0)
|> List.filter(fun t -> t <> 0.0)
float ( List.sum filtered ) / float filtered.Length
//User User CF - basic
let Predictu(ratings:(float list)list)(a:int)(i:int) =
let rb = rBaru ratings.[a] i
let neighborIndices = ratings
|> List.mapi(fun index rating -> if rating.[i] <> 0.0 then index else -1)
|> List.filter(fun index -> index <> -1)
let neighbors = neighborIndices
|> List.map (fun index -> ratings.[index])
let gaps = neighbors |> List.map (fun neighbor -> neighbor.[i] - rBaru neighbor i)
let simis = neighborIndices |> List.map (fun index -> Simu ratings a index)
let num = List.zip gaps simis |> List.sumBy (fun t -> fst t * snd t)
let den = simis |> List.sumBy (fun t -> abs t)
if den <> 0.0 then
let div = num / den
let predicted = rb + div
//Sometimes the value can be beyond the range.
//so having a 7 is same as 5.0 in practice
//so is having a -1 which is same as zero
if predicted > 5.0 then 5.0 elif predicted < 1.0 then 1.0 else predicted
else
0.0 //We don't know what it is.
//Item based collaborative filtering - basic
let Predicti (ratings:(float list)list)(userIndex:int)(itemIndex:int)=
let rated = ratings.[userIndex]
|> List.mapi (fun i t -> if t <> 0.0 then i else -1)
|> List.filter (fun k -> k <> -1)
let num = rated |> List.sumBy (fun i -> ratings.[userIndex].[i] * SimiDot ratings itemIndex i)
let den = rated |> List.sumBy ( fun i -> abs (Simi ratings itemIndex i) )
let predicted = num / den
//Predicting something as bad as -1.34 is same as predicting it as 1
//Similarly predicting something as good as 7.5 is same as predicting it as 5
//on a 1-5 rating scale.
//Other than that the ranking might
if predicted < 0.0 then 1. elif predicted > 5. then 5. else predicted
let stddev(list:float list)=
sqrt (List.fold (fun acc elem -> acc + (float elem - List.average list) ** 2.0 ) 0.0 list / float list.Length)
let zscore (ratings:(float list)list)(userIndex:int)(itemIndex:int)=
let rBar = rBaru ratings.[userIndex] itemIndex
let sigma = stddev ratings.[userIndex]
ratings.[userIndex].[itemIndex] - rBar / sigma
//Variation of the User User CF using Z-Score
let PredictuZ(ratings:(float list)list)(a:int)(i:int) =
let rb = rBaru ratings.[a] i
let neighborIndices = ratings
|> List.mapi(fun index rating -> if rating.[i] <> 0.0 then index else -1)
|> List.filter(fun index -> index <> -1)
let neighbors = neighborIndices
|> List.map (fun index -> ratings.[index])
let gaps = neighbors |> List.map (fun neighbor -> zscore ratings a i)
let simis = neighborIndices |> List.map (fun index -> Simu ratings a index)
let num = List.zip gaps simis |> List.sumBy (fun t -> fst t * snd t)
let den = simis |> List.sumBy (fun t -> abs t)
if den <> 0.0 then
let div = num / den
let predicted = rb + div * stddev ratings.[a]
//Sometimes the value can be beyond the range.
//so having a 7 is same as 5.0 in practice
//so is having a -1 which is same as zero
if predicted > 5.0 then 5.0 elif predicted < 1.0 then 1.0 else predicted
else
0.0 //Else we don't know
//Non personalized baseline predictor
let baseline (ratings:(float list)list) =
let mu = ratings |> List.map ( fun ra -> [for i in 0 .. ra.Length - 1 -> ra.[i]]
|> List.filter (fun t -> t <> 0.0)
|> List.average)
|> List.average
let mutable bu = ratings |> List.sumBy (fun rating -> [for i in 0 .. rating.Length - 1 -> rating.[i]]
|> List.filter (fun ri -> ri <> 0.0)
|> List.sumBy (fun ri -> ri - mu))
bu <- bu / float ratings.[0].Length
let mutable bi = ratings |> List.sumBy (fun ra -> [for i in 0 .. ra.Length - 1 -> ra.[i]]
|> List.filter (fun t -> t <> 0.0)
|> List.sumBy (fun z -> z - bu - mu))
bi <- bi / float ratings.Length
mu + bu + bi
open collabFilter
open recEval
[<EntryPoint>]
let main argv =
let ratings = [[4.;0.;5.;5.];[4.;2.;1.;0.];[3.;0.;2.;4.];[4.;4.;0.;0.];[2.;1.;3.;5.]]
// 0 1
// 1 3
// 2 1
// 3 2
// 3 3
let ratingsML = [[5.0;3.0;4.0;3.0;3.0];
[4.0;0.0;0.0;0.0;0.0];
[0.0;0.0;0.0;0.0;0.0];
[0.0;0.0;0.0;0.0;0.0];
[4.0;3.0;0.0;0.0;0.0];
[4.0;0.0;0.0;0.0;0.0];
[0.0;0.0;0.0;5.0;0.0];
[0.0;0.0;0.0;0.0;0.0];
[0.0;0.0;0.0;0.0;0.0];
[4.0;0.0;0.0;4.0;0.0];
[0.0;0.0;0.0;0.0;0.0];
[0.0;0.0;0.0;5.0;0.0];
[3.0;3.0;0.0;5.0;1.0];
[0.0;0.0;0.0;0.0;0.0];
[1.0;0.0;0.0;0.0;0.0];
[5.0;0.0;0.0;5.0;0.0];
[4.0;0.0;0.0;0.0;0.0];
[5.0;0.0;0.0;3.0;0.0];
[0.0;0.0;0.0;4.0;0.0];
[3.0;0.0;0.0;0.0;0.0];
[5.0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;4.0;3.0];
[4.0;0.0;0.0;4.0;0.0];
[5.0;0.0;0.0;0.0;0.0];
[4.0;0.0;0.0;0.0;0.0];
[0.0;0.0;0.0;0.0;0.0];
[5.0;3.0;0.0;2.0;0.0];
[4.0;3.0;0.0;0.0;3.0];
[0.0;0.0;0.0;0.0;0.0]]
let p3 = PredictuZ ratingsML 4 2
let pre01 = Predicti ratings 0 1
let pre13 = Predicti ratings 1 3
let pre21 = Predicti ratings 2 1
let pre32 = Predicti ratings 3 2
let pre33 = Predicti ratings 3 3
printfn " Item - Item Collaborative Filtering "
printfn "pre01 = %A" pre01
printfn "pre13 = %A" pre13
printfn "pre21 = %A" pre21
printfn "pre32 = %A" pre32
printfn "pre33 = %A" pre33
let pre01 = Predictu ratings 0 1
let pre13 = Predictu ratings 1 3
let pre21 = Predictu ratings 2 1
let pre32 = Predictu ratings 3 2
let pre33 = Predictu ratings 3 3
printfn " User - User Collaborative Filtering "
printfn "pre01 = %A" pre01
printfn "pre13 = %A" pre13
printfn "pre21 = %A" pre21
printfn "pre32 = %A" pre32
printfn "pre33 = %A" pre33
let z = halfLife ratings.[3] 3.5 5.0
// let pre01 = Predictu ratings 0 2
let pre01z = PredictuZ ratings 0 2
let pre11 = PredictuZ ratings 3 2
let pre112 = Predictu ratings 3 2
// let zb = baseline ratings
// let simu04 = Predictu ratings 2 1
// let pre01 = Predicti ratings 2 1
// let z = Predicti ratings 0 1
// let simi03 = Simi ratings 1 2
//
let pre33 = Predicti ratings 3 0
// let pre21 = Predictu ratings 2 1
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment