【问题标题】:How to add prediction to polynomial regression如何将预测添加到多项式回归
【发布时间】:2023-04-10 10:09:01
【问题描述】:

是否可以从 sklean 库中添加预测功能?又该怎么做呢?

def monomial(a,b):
    return lambda x : a * math.pow(x,b)

返回形成所需阶多项式的单项式列表

def polyList(order):
    return [monomial(1,i) for i in range(0,order+1)]

返回给定输入的函数总和

def evaluate(functionList, x):
    return sum([f(x) for f in functionList])

返回加权和,即w0f0 + w1f1 +...

def weightedSum(w,F):
    if(len(w) != len(F)):
        raise Exception("Function/weight size mismatch")
    else:
        return lambda x:sum([w[i]*F[i](x) for i in range(0,len(w))])
############

在这里,我们用权重的最大似然估计拟合给定阶数的多项式。

def polyTrain(x,y,order): 
    #Initialize the weight vector and design matrix
    w = [1 for i in range(0,order)]
    F = polyList(order)
    design = [[f(i) for f in F] for i in x]
    #Convert them to numpy arrays
    w = numpy.asarray(w)
    design = numpy.asarray(design)
    #We solve Ax=b, [x values x 3][coefficients]T = [yvalues]
    pinv = numpy.linalg.pinv(design)
    t = numpy.asarray(y).T
    #We know that the ML estimates for w are w* = pinv(design)y.T
    w = numpy.dot(pinv,t)
    return weightedSum(w,F)

【问题讨论】:

    标签: python python-3.x scikit-learn regression


    【解决方案1】:

    如果你定义一个可以处理你想要的所有逻辑的类会更好。不过,如果你想 要编写完全符合 scikit-learn 中使用的 fit-transform-predict 协议的代码,您需要 从某个基础子类化 scikit-learn 的类,例如BaseEstimator、TransformerMixin、BaseRegressor。

    Numpy 提供了非常方便的函数vander,它可以极大地帮助你 当您使用多项式时。

    让我们定义一个类。

    class PolyRegressor:  # I omit subclassing for now. 
    
        def __init__(self, weights=None):
            self.weights = np.array(weights) if weights is not None else None
    
        @property
        def order(self):
            return len(self.weights) if self.weights is not None else 0
    
        def evaluate(self, x):
            return np.dot(np.vander(x, self.order), self.weights[:, np.newaxis]).ravel()
    
        def fit(self, X, y=None):
            self.weights = (np.linalg.pinv(np.vander(X, self.order)) @ y[:, np.newaxis]).ravel()
    
        def predict(self, X):
            if self.weights is not None: 
                return self.evaluate(X)
            else:
                raise Exception("Model wasn't fitted. Fit model first. ")
    
        def fit_predict(self, X, y=None):
            self.fit(X, y)
            return self.predict(X)
    
    
    reg = PolyRegressor()
    
    reg.weights = np.array([1,2,3])  # we implicitly define order = 2 here, e.g. 3 + 2x + 1x^2
    
    reg.evaluate(np.array([5])) # testing
    

    array([38]) # 输出

    reg.fit_predict(np.random.rand(10), np.random.rand(10) * 5)
    

    数组([2.55922997, 1.81433623, 2.29153779, 1.78458414, 1.75961514, 2.59770317、2.65122647、1.81313616、2.61993941、2.63325695])

    根据您的需要采用代码。希望对您有所帮助...

    【讨论】:

      猜你喜欢
      • 2019-07-29
      • 2012-12-03
      • 2019-01-24
      • 2018-10-19
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多