如何在F#

时间:2015-12-06 01:50:46

标签: f#

我在Python中有以下简单的类实现。我想复制F#中的功能......但我不确定如何......

class MyClass(object):

    def __init__(self):
        pass

    def addEvidence(self,dataX,dataY):

        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)

    def query(self,points):

        return (self.model_coefs[:-1] * points).sum(axis = 1) + self.model_coefs[-1]

因此,在伪代码中,类看起来像这样:

Class()

    self.model_coefs = []
    self.residuals = []
    self.rank = 0

    self.addEvidence(dataX,dataY):

         x, y, z = f(dataX,dataY)

         self.model_coefs <- x
         self.residuals <- y
         self.rank <- z

    self.query(points)

         return ([self.model_coefs[i] * points[i] for i in points].sum()) + self.model_coefs[self.model_coefs.length]

如果有一种F-sharpy方法可以做到这一点,那很好 - 事实是,实体是一种带有功能包装设计的数据仓,所以我不确定它是怎么回事完成功能......

无论如何,这是我到目前为止的尝试(我现在正在跳过线性回归..)

namespace Learners

type LinearRegressionLearner() = 
    member this.ModelCoefficients  : int [] = [|0;0|]
    member this.Residuals : int[] = [||]
    member this.Rank = 0

    member this.addEvidence dataX dataY = "expletive"
    member this.query (points:int[]) = 
           points |> Array.iteri(fun i x -> x * this.ModelCoefficients.[i])

我在这里收到错误:points |> Array.iteri(fun i x -> x * this.ModelCoefficients.[i]) ...我猜this.ModelCoefficients.[i]是一个单位而且不匹配x,这是一个整数?

1 个答案:

答案 0 :(得分:3)

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Lab31Frame { public static void main( String[] args ) { JFrame myFrame = new JFrame( "Lab 3: Question 2" ); // create an instance of Panel and add to frame Lab31Panel myPanel = new Lab31Panel(); myFrame.add( myPanel ); // set up functionality of frame myFrame.setSize( 500, 310 ); myFrame.setVisible( true ); myFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); }//end main } // end class(与所有Array.iteri函数一样)需要一个返回iter的函数 - 你的函数返回unit。也许你想要Array.mapi而不是?