2015-12-06 101 views
0

我有以下python Numpy函數;它可以取X,一個任意數量的列和行的數組,並輸出由最小二乘函數預測的Y值。F#庫或.Net編號相當於Python Numpy函數

什麼是Math.Net相當於這樣的功能呢?

這裏是Python代碼:

newdataX = np.ones([dataX.shape[0],dataX.shape[1]+1]) 
    newdataX[:,0:dataX.shape[1]]=dataX 

    # build and save the model 
    self.model_coefs, residuals, rank, s = np.linalg.lstsq(newdataX, dataY) 
+1

它怎麼說,一半的東西你問Ø在這個.NET領域,這個虛擬大廳顯示器會被投票嗎? 我字面上*有*將這種態度歸因於我是否轉到.NET。 – bordeo

回答

0

您可以從.NET中使用pythonnet(C#下面的代碼被複制從GitHub)調用numpy的。可以將它們轉換爲界面上的Python列表,但這會降低某些情況下的性能。

https://github.com/pythonnet/pythonnet/tree/develop

static void Main(string[] args) 
{ 
    using (Py.GIL()) { 
    dynamic np = Py.Import("numpy"); 
    dynamic sin = np.sin; 
    Console.WriteLine(np.cos(np.pi*2)); 
    Console.WriteLine(sin(5)); 
    double c = np.cos(5) + sin(5); 
    Console.WriteLine(c); 
    dynamic a = np.array(new List<float> { 1, 2, 3 }); 
    dynamic b = np.array(new List<float> { 6, 5, 4 }, Py.kw("dtype", np.int32)); 
    Console.WriteLine(a.dtype); 
    Console.WriteLine(b.dtype); 
    Console.WriteLine(a * b); 
    Console.ReadKey(); 
    } 
} 

輸出:

1.0 
-0.958924274663 
-0.6752620892 
float64 
int32 
[ 6. 10. 12.] 

這裏張貼在github例如使用F#:

https://github.com/pythonnet/pythonnet/issues/112

open Python.Runtime 
open FSharp.Interop.Dynamic 
open System.Collections.Generic 

[<EntryPoint>] 
let main argv = 
    //set up for garbage collection? 
    use gil = Py.GIL() 

    //----- 
    //NUMPY 
    //import numpy 
    let np = Py.Import("numpy") 

    //call a numpy function dynamically 
    let sinResult = np?sin(5) 

    //make a python list the hard way 
    let list = new Python.Runtime.PyList() 
    list.Append(new PyFloat(4.0)) 
    list.Append(new PyFloat(5.0)) 

    //run the python list through np.array dynamically 
    let a = np?array(list) 
    let sumA = np?sum(a) 

    //again, but use a keyword to change the type 
    let b = np?array(list, Py.kw("dtype", np?int32)) 
    let sumAB = np?add(a,b) 

    let SeqToPyFloat (aSeq : float seq) = 
     let list = new Python.Runtime.PyList() 
     aSeq |> Seq.iter(fun x -> list.Append(new PyFloat(x))) 
     list 

    //Worth making some convenience functions (see below for why) 
    let a2 = np?array([|1.0;2.0;3.0|] |> SeqToPyFloat) 

    //-------------------- 
    //Problematic cases: these run but don't give good results 
    //make a np.array from a generic list 
    let list2 = [|1;2;3|] |> ResizeArray 
    let c = np?array(list2) 
    printfn "%A" c //gives type not value in debugger 

    //make a np.array from an array 
    let d = np?array([|1;2;3|]) 
    printfn "%A" d //gives type not value in debugger 

    //use a np.array in a function 
    let sumD = np?sum(d) //gives type not value in debugger 
    //let sumCD = np?add(d,d) // this will crash 

    //can't use primitive f# operators on the np.arrays without throwing an exception; seems 
    //to work in c# https://github.com/tonyroberts/pythonnet //develop branch 
    //let e = d + 1 

    //----- 
    //NLTK 
    //import nltk 
    let nltk = Py.Import("nltk") 
    let sentence = "I am happy" 
    let tokens = nltk?word_tokenize(sentence) 
    let tags = nltk?pos_tag(tokens) 

    let taggedWords = nltk?corpus?brown?tagged_words() 
    let taggedWordsNews = nltk?corpus?brown?tagged_words(Py.kw("categories", "news")) 
    printfn "%A" taggedWordsNews 

    let tlp = nltk?sem?logic?LogicParser(Py.kw("type_check",true)) 
    let parsed = tlp?parse("walk(angus)") 
    printfn "%A" parsed?argument 

    0 // return an integer exit code 
+0

bordeo想在.net中使用特定的numpy方法,因此我的答案提供了一個解決方案。我沒有看到從.net調用c/python代碼的任何問題,這很像是在定製高性能本地代碼或使用C++/cli時進行調試。 @ TheInnerLight - 您的評論和可能會降低看起來偏向於託管的.NET解決方案。 – denfromufa

+1

原則上,我個人對於非託管的.NET解決方案沒有任何問題(雖然看起來確實是一種類型安全的解決方案),但問題特指Math.NET數值和F#。您的代碼示例使用C#。 F#(至少開箱即用)沒有任何「動態」等價物,所以我認爲這些問題需要在答案中解決。 – TheInnerLight

+0

@TheInnerLight我用A#的F#例子更新了我的答案 – denfromufa

3

我認爲你正在尋找此頁面上的功能:http://numerics.mathdotnet.com/api/MathNet.Numerics.LinearRegression/MultipleRegression.htm

您有幾種選擇來解決:

  • 正常公式:MultipleRegression.NormalEquations(x, y)

  • QR分解:MultipleRegression.QR(x, y)

  • SVD:MultipleRegression.SVD(x, y)

普通方程是快,但不太穩定的數值,而SVD是最穩定的數值,但最慢的。

唯一的「時髦」的一部分,現在與pythonnet正在通過numpy的數組: