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