Pregunta

I am using Accord.NET in F# for the first time and I am having problems creating the function to calculate the distance for KNN.

Here is my code

static member RunKNN =
    let inputs = MachineLearningEngine.TrainingInputClass
    let outputs = MachineLearningEngine.TrainingOutputClass

    let knn = new KNearestNeighbors<int*int*int*int>(1,inputs,outputs,null)
    let input = 1,1,1,1
    knn.Compute(input)

When I swap out the null for a function like this

let distanceFunction = fun (a:int,b:int,c:int,d:int)
                           (e:int,f:int,g:int,h:int)
                           (i:float) ->
                           0 

I get an exception like this:

*Error 1 This expression was expected to have type System.Func<(int * int * int * int),(int * int * int * int),float> but here has type int * int * int * int -> int * int * int * int -> float -> int*

So far, the only article I found close to my problem is this one. Apparently, there is a problem with how F# and C# handle delegates?

I posted this same question on the Google group for Accord.NET here.

Thanks in advance

¿Fue útil?

Solución

Declare the distance function like this:

let distanceFunction (a:int,b:int,c:int,d:int) (e:int,f:int,g:int,h:int) =  
  0.0

(it takes two tuples in input and returns a float), and then create a delegate from it:

let distanceDelegate = 
  System.Func<(int * int * int * int),(int * int * int * int),float>(distanceFunction)

Passing this delegate to Accord.NET should do the trick.

Otros consejos

I would guess you should use the tuple form like so

let distanceFunction = fun ((a:int,b:int,c:int,d:int),
                           (e:int,f:int,g:int,h:int),
                           (i:float)) ->
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top