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 / 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 / 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 / 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 / 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 / Pearsons.fs
Created January 21, 2016 01:40
Pearsons Coefficient
let x = [1.;2.;3.;4.;5.]
let y = [1.;2.;2.;3.;4.]
let z = [3;4]
let x_bar = List.average x
let y_bar = List.average y
let numerator =
List.zip x y
|> List.sumBy (fun item -> (fst item - x_bar)*(snd item - y_bar))
@sudipto80
sudipto80 / rmse.fs
Created January 20, 2016 05:41
Root mean squared error
//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 ))
rmse [1.3;4.5] [4.1;0.41] |> Dump
@sudipto80
sudipto80 / BFS.cs
Created January 19, 2016 12:35
BFS
Dictionary<string,List<string>> graph =
new Dictionary<string,List<string>>();
graph.Add("A",new List<string>(){"B","C","D"});
graph.Add("B",new List<string>(){"A","E","F"});
graph.Add("E",new List<string>(){"B","G"});
List<string> visited = new List<string>();
Queue<string> qu = new Queue<string>();
@sudipto80
sudipto80 / BinarySearch.cs
Created January 19, 2016 12:34
Binary Search
void Main()
{
BinarySearch(new int[]{1,3,4,5,9,10},1).Dump();
}
int BinarySearch(int[] array,int x)
{
int low = 0;
int high = array.Length - 1;
@sudipto80
sudipto80 / JollyJumper.c
Created January 13, 2016 11:23
Jolly Jumper
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int jolly( int nums[], int length)
{
//Pre-conditions
if(length <= 0 )
return 0;
int i;
@sudipto80
sudipto80 / Infix.cs
Created January 7, 2016 10:53
Infix Evaluation using Stack
string ops = "+-*/";
string vals = "1234567890";
Stack<char> opStack = new Stack<char>();
Stack<int> valStack = new Stack<int>();
void Main()
{
string expr = "(1 + ((2+3)*(4*5)))";
foreach (char c in expr)