Skip to content

Instantly share code, notes, and snippets.

View sudipto80's full-sized avatar
🎯
Focusing

SUDIPTA MUKHERJEE sudipto80

🎯
Focusing
View GitHub Profile
@sudipto80
sudipto80 / User_User.fs
Last active January 21, 2016 02:05
User User Collaborative Filtering 1
//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
//The following function finds the common item indices
let commonItemIndices (ratings:(float list)list)(a:int)(u:int)=
@sudipto80
sudipto80 / SimuDot.fs
Created January 21, 2016 02:17
Cosine Similarity
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)
let ratings = [[4.;0.;5.;5.];[4.;2.;1.;0.];[3.;0.;2.;4.];[4.;4.;0.;0.];[2.;1.;3.;5.]]
SimuDot ratings 0 1
@sudipto80
sudipto80 / Errors.fs
Created January 21, 2016 08:41
Mean Squarred Errors
//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)
@sudipto80
sudipto80 / trueRank.fs
Created January 21, 2016 08:44
True rank
//Calculates the standard deviation
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 trueRanks = [1.;2.;3.;4.;5.;5.;7.;8.;9.;10.]
let predictedRanks = [1.;2.;3.;4.;6.;7.;5.;8.;10.;9.]
let uBar = trueRanks |> List.average
let upBar = predictedRanks |> List.average
@sudipto80
sudipto80 / baseline.fs
Created January 21, 2016 08:55
base line predictor
//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))
@sudipto80
sudipto80 / CollaborativeFiltering.fs
Created January 21, 2016 08:56
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
@sudipto80
sudipto80 / tf-idf.fs
Created January 22, 2016 01:50
tf idf
let sentence1 = "this is a a sample"
let sentence2 = "this is another another example example example"
let word = "example"
let numberOfDocs = 2.
let tf1 = sentence1.Split ' ' |> Array.filter ( fun t -> t = word)
|> Array.length
let tf2 = sentence2.Split ' ' |> Array.filter ( fun t -> t = word)
|> Array.length
@sudipto80
sudipto80 / IRLib.fs
Created January 22, 2016 08:18
IR Lib
namespace IRLib
module asymSimilarity =
let private getABCD (first :string list)(second : string list) =
let all = Set.union (first |> Set.ofList) (second |> Set.ofList) |> Set.toList
let firstMatches = all |> List.map (fun t -> first |> List.contains t )
let secondMatches = all |> List.map (fun t -> second |> List.contains t )
@sudipto80
sudipto80 / similarCookies.fs
Created January 22, 2016 08:19
Similar Cookies
let biscuitA = ["refined wheat flour";"sugar";"edible vegetable oil";
"edible coconut products";"invert syrup";"milk solids";
"edible starch";"raising agent";"edible common salt";
"baking powder";"solbake";"emulsifier"]
let biscuitB = ["refined wheat flour";"cocoa powder";"suger";"cocoa butter";
"dextrose";"lecithin";"vanillin";"edible vegetable oil";
"raising agent";"cocoa solids";"edible common salt";"emulsifier"]
let biscuitC = ["refined wheat flour";"suger";"cocoa solids";
@sudipto80
sudipto80 / rose_And_potato.fs
Created January 22, 2016 08:21
Rose And Potato
open System.IO
let image = System.Drawing.Image.FromFile(@"C:\personal\rose.jpg")
let image2 = System.Drawing.Image.FromFile(@"C:\personal\potato.jpg")
let ms = new MemoryStream()
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg)
let bytes = ms.ToArray() |> Array.map int |> List.ofArray
let ms2 = new MemoryStream()
image2.Save(ms2,System.Drawing.Imaging.ImageFormat.Jpeg)