- Deep Learning By Example
- Ahmed Menshawy
- 322字
- 2021-06-24 18:52:41
Using the model for prediction
Let's say we have unseen data of TV ad spending and that we want to know their corresponding impact on the sales of the company. So, we need to use the learned model to do that for us. Let's suppose that we want to know how much sales will increase from $50000 of TV advertising.
Let's use our learned model coefficients to make such a calculation:
y = 7.032594 + 0.047537 x 50
# manually calculating the increase in the sales based on $50k
7.032594 + 0.047537*50000
Output:
We can also use Statsmodels to make the prediction for us. First, we need to provide the TV ad value in a pandas DataFrame since the Statsmodels interface expects it:
# creating a Pandas DataFrame to match Statsmodels interface expectations
new_TVAdSpending = pd.DataFrame({'TV': [50000]})
new_TVAdSpending.head()
Output:

Now, we can go ahead and use the predict function to predict the sales value:
# use the model to make predictions on a new value
preds = lm.predict(new_TVAdSpending)
Output:
array([ 9.40942557])
Let's see how the learned least squares line looks. In order to draw the line, we need two points, with each point represented by this pair: (x, predict_value_of_x).
So, let's take the minimum and maximum values for the TV ad feature:
# create a DataFrame with the minimum and maximum values of TV
X_min_max = pd.DataFrame({'TV': [advertising_data.TV.min(), advertising_data.TV.max()]})
X_min_max.head()
Output:

Let's get the corresponding predictions for these two values:
# predictions for X min and max values
predictions = lm.predict(X_min_max)
predictions
Output:
array([ 7.0658692, 21.12245377])
Now, let's plot the actual data and then fit it with the least squares line:
# plotting the acutal observed data
advertising_data.plot(kind='scatter', x='TV', y='sales')
#plotting the least squares line
plt.plot(new_TVAdSpending, preds, c='red', linewidth=2)
Output:
Extensions of this example and further explanations will be explained in the next chapter.
- Dreamweaver CS3 Ajax網(wǎng)頁設(shè)計(jì)入門與實(shí)例詳解
- Practical Data Analysis
- 21小時(shí)學(xué)通AutoCAD
- 3D Printing with RepRap Cookbook
- 城市道路交通主動控制技術(shù)
- 分布式多媒體計(jì)算機(jī)系統(tǒng)
- iClone 4.31 3D Animation Beginner's Guide
- Ceph:Designing and Implementing Scalable Storage Systems
- JavaScript典型應(yīng)用與最佳實(shí)踐
- 大數(shù)據(jù)驅(qū)動的設(shè)備健康預(yù)測及維護(hù)決策優(yōu)化
- 中國戰(zhàn)略性新興產(chǎn)業(yè)研究與發(fā)展·工業(yè)機(jī)器人
- 氣動系統(tǒng)裝調(diào)與PLC控制
- Learning ServiceNow
- 數(shù)據(jù)要素:全球經(jīng)濟(jì)社會發(fā)展的新動力
- Advanced Deep Learning with Keras