Python Common Least squares (OLS) for polynomial-fitting

Source: Internet
Author: User
Tags statsmodels

Multivariate function fitting. such as TV and radio prices, the impact of multiple sales, at this time there are two independent variables.

Python solution:

ImportNumPy as NPImportPandas as PD#import Statsmodels.api as SM #方法一ImportStatsmodels.formula.api as SMF#Method TwoImportMatplotlib.pyplot as Plt fromMpl_toolkits.mplot3dImportAXES3DDF= Pd.read_csv ('Http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv', index_col=R) X= df[['TV','Radio']]y= df['Sales']#est = sm. OLS (Y, Sm.add_constant (X)). Fit () #方法一EST = smf.ols (formula='Sales ~ TV + Radio', DATA=DF). Fit ()#Method Twoy_pred =est.predict (X) df['sales_pred'] =y_predPrint(DF)Print(Est.summary ())#regression ResultsPrint(Est.params)#coefficientFig=plt.figure () Ax= Fig.add_subplot (111, projection='3d')#ax = axes3d (Fig)Ax.scatter (x['TV'], x['Radio'], Y, c='b', marker='o') Ax.scatter (x['TV'], x['Radio'], y_pred, c='R', marker='+') Ax.set_xlabel ('X Label') Ax.set_ylabel ('Y Label') Ax.set_zlabel ('Z Label') plt.show ()

The results and parameters of the fitting are printed out, and the result function is:

F (Sales) =β0 +Β1*[TV] +β2*[radio]

F (Sales) = 2.9211 + 0.0458 * [TV] + 0.188 * [radio]

In the figure, in the sales direction, the blue point is the original sales actual value, and the red point is the value calculated by the Fit function. In fact, the error is not big, some of the data below.

Also can be quasi-unity meta-function;

ImportNumPy as NPImportPandas as PDImportStatsmodels.formula.api as SMFImportMatplotlib.pyplot as Plt fromMpl_toolkits.mplot3dImportAXES3DDF= Pd.read_csv ('Http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv', index_col=0) X= df['TV']y= df['Sales']est= Smf.ols (formula='Sales ~ TV', Data=DF). Fit () y_pred=est.predict (X)Print(est.summary ()) FIG=plt.figure () Ax= Fig.add_subplot (111) Ax.scatter (X, y, c='b') Ax.plot (X, y_pred, C='R') plt.show ()

Python Common Least squares (OLS) for polynomial-fitting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.