相当于Python Numpy函数的F#library或.Net数字

2024-10-02 08:24:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的python Numpy函数;它可以获取X,一个具有任意数量的列和行的数组,并输出由最小二乘函数预测的Y值。在

什么是数学.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)

Tags: and函数代码buildnumpy数量netmodel
2条回答

您可以使用pythonnet从.NET调用numpy(下面的C代码是从GITHUB复制的):

pythonnet现在唯一“时髦”的部分就是传递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();
  }
}

输出:

^{pr2}$

以下是使用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

我想您正在寻找这个页面上的函数:http://numerics.mathdotnet.com/api/MathNet.Numerics.LinearRegression/MultipleRegression.htm

您有几个选项需要解决:

  • 正规方程:MultipleRegression.NormalEquations(x, y)

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

  • 高级副总裁:MultipleRegression.SVD(x, y)

法方程速度较快,但数值稳定性较差,而SVD数值稳定性最好,但速度最慢。在

相关问题 更多 >

    热门问题