【问题标题】:What is the numpy equivalent of predicting values after fitting a polynomial model via least squares regression?通过最小二乘回归拟合多项式模型后预测值的 numpy 等价物是多少?
【发布时间】:2019-12-30 03:16:22
【问题描述】:

假设我想通过最小二乘回归拟合度数为 d 的多项式模型。我在python中学到了两种方法。一个使用numpy,另一个使用sklearn。在我拟合模型并获得系数后,在sklearn 中预测测试数据的值,我可以这样做:

from sklearn.linear_model import LinearRegression 
model = LinearRegression()
model.fit(x_train, y_train) # Fitting on Training Data
model.predict(20) #One value in test data is 20

在我使用以下方法拟合模型后,model.predict()numpy 等效项是什么:

import numpy.polynomial.polynomial as poly
np_model = poly.polyfit(x_train, y_train, d)

【问题讨论】:

    标签: python numpy scikit-learn linear-regression


    【解决方案1】:

    我使用 numpy.polyval,文档位于 https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyval.html - 这是一个图形多项式拟合器作为使用 polyval 的示例。

    import numpy, matplotlib
    import matplotlib.pyplot as plt
    
    xData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.0, 6.6, 7.7, 0.0])
    yData = numpy.array([1.1, 20.2, 30.3, 40.4, 50.0, 60.6, 70.7, 0.1])
    
    polynomialOrder = 2 # example quadratic
    
    # curve fit the test data
    fittedParameters = numpy.polyfit(xData, yData, polynomialOrder)
    print('Fitted Parameters:', fittedParameters)
    
    modelPredictions = numpy.polyval(fittedParameters, xData)
    absError = modelPredictions - yData
    
    SE = numpy.square(absError) # squared errors
    MSE = numpy.mean(SE) # mean squared errors
    RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
    Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))
    print('RMSE:', RMSE)
    print('R-squared:', Rsquared)
    
    print()
    
    
    ##########################################################
    # graphics output section
    def ModelAndScatterPlot(graphWidth, graphHeight):
        f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
        axes = f.add_subplot(111)
    
        # first the raw data as a scatter plot
        axes.plot(xData, yData,  'D')
    
        # create data for the fitted equation plot
        xModel = numpy.linspace(min(xData), max(xData))
        yModel = numpy.polyval(fittedParameters, xModel)
    
        # now the model as a line plot
        axes.plot(xModel, yModel)
    
        axes.set_xlabel('X Data') # X axis data label
        axes.set_ylabel('Y Data') # Y axis data label
    
        plt.show()
        plt.close('all') # clean up after using pyplot
    
    graphWidth = 800
    graphHeight = 600
    ModelAndScatterPlot(graphWidth, graphHeight)
    

    【讨论】:

    • 对了,将多项式阶数(在代码的顶部)设置为 7 以查看过拟合的丑陋示例。
    猜你喜欢
    • 1970-01-01
    • 2015-05-15
    • 2012-07-14
    • 2015-02-12
    • 2019-03-29
    • 1970-01-01
    • 2020-12-06
    • 2018-01-15
    • 2017-12-11
    相关资源
    最近更新 更多