官术网_书友最值得收藏!

  • Deep Learning By Example
  • Ahmed Menshawy
  • 335字
  • 2021-06-24 18:52:41

Learning model coefficients

In order to estimate the coefficients of our model, we need to fit the data with a regression line that gives a similar answer to the actual sales. To get a regression line that best fits the data, we will use a criterion called least squares. So, we need to find a line that minimizes the difference between the predicted value and the observed (actual) one. In other words, we need to find a regression line that minimizes the sum of squared residuals (SSresiduals). Figure 2 illustrates this:

Figure 2: Fitting the data points (sample of TV ads) with a regression line that minimizes the sum of the squared residuals (difference between the predicted and observed value)

The following are the elements that exist in Figure 2:

  • Black dots represent the actual or observed values of x (TV ad) and y (sales)
  • The blue line represents the least squares line (regression line)
  • The red line represents the residuals, which are the differences between the predicted and the observed (actual) values

So, this is how our coefficients relate to the least squares line (regression line):

  • beta0 is the intercept, which is the value of y when x =0
  • beta1 is the slope, which represents the change in y divided by the change in x

Figure 3 presents a graphical explanation of this:

Figure 3: The relation between the least squares line and the model coefficients

Now, let's go ahead and start to learn these coefficients using Statsmodels:

# To use the formula notation below, we need to import the module like the following
import statsmodels.formula.api as smf
# create a fitted model in one line of code(which will represent the least squares line)
lm = smf.ols(formula='sales ~ TV', data=advertising_data).fit()
# show the trained model coefficients
lm.params

Output:

Intercept    7.032594
TV 0.047537
dtype: float64

As we mentioned, one of the advantages of linear regression models is that they are easy to interpret, so let's go ahead and interpret the model.

主站蜘蛛池模板: 姜堰市| 平塘县| 宁德市| 嵊泗县| 九寨沟县| 进贤县| 丰都县| 旬阳县| 西贡区| 彩票| 武强县| 昌平区| 稷山县| 武强县| 香港 | 锡林郭勒盟| 威海市| 昆明市| 南雄市| 阳新县| 农安县| 宽甸| 云阳县| 南阳市| 武邑县| 陇南市| 万盛区| 苏尼特右旗| 日喀则市| 吐鲁番市| 琼海市| 伊金霍洛旗| 敦煌市| 永顺县| 库尔勒市| 集贤县| 罗平县| 镇巴县| 巴彦淖尔市| 分宜县| 邻水|