- Building Machine Learning Systems with Python
- Luis Pedro Coelho Willi Richert Matthieu Brucher
- 329字
- 2021-07-23 17:11:18
Starting with a simple straight line
Let's assume for a second that the underlying model is a straight line. Then the challenge is how to best put that line into the chart so that it results in the smallest approximation error. SciPy's polyfit() function does exactly that. Given data x and y and the desired order of the polynomial (a straight line has an order of 1), it finds the model function that minimizes the error function defined earlier:
fp1 = np.polyfit(x, y, 1)
The polyfit() function returns the parameters of the fitted Model function, fp1:
>>> print("Model parameters: %s" % fp1)
Model parameters: [ 2.59619213 989.02487106]
This means the best straight line fit is the following function:
f(x) = 2.59619213 * x + 989.02487106
We then use poly1d() to create a model function from the model parameters:
>>> f1 = np.poly1d(fp1)
>>> print(error(f1, x, y))
317389767.34
We can now use f1() to plot our first trained model. We have already implemented plot_web_traffic in a way that lets us easily add additional models to plot. In addition, we pass a list of models, of which we currently have only one:
plot_web_traffic(x, y, [f1])
This will produce the following plot:

It seems like the first four weeks are not that far off, although we can clearly see that there is something wrong with our initial assumption that the underlying model is a straight line. Also, how good or how bad actually is the error of 319,531,507.008 ?
The absolute value of the error is seldom of use in isolation. However, when comparing two competing models, we can use their errors to judge which one of them is better. Although our first model clearly is not the one we would use, it serves a very important purpose in the workflow. We will use it as our baseline until we find a better one. Whatever model we come up with in the future, we will compare it against the current baseline.
- Linux KVM虛擬化架構(gòu)實(shí)戰(zhàn)指南
- 現(xiàn)代辦公設(shè)備使用與維護(hù)
- 計(jì)算機(jī)應(yīng)用與維護(hù)基礎(chǔ)教程
- 單片機(jī)原理及應(yīng)用系統(tǒng)設(shè)計(jì)
- 電腦維護(hù)365問(wèn)
- 計(jì)算機(jī)維修與維護(hù)技術(shù)速成
- Mastering Adobe Photoshop Elements
- OUYA Game Development by Example
- 計(jì)算機(jī)組裝維修與外設(shè)配置(高等職業(yè)院校教改示范教材·計(jì)算機(jī)系列)
- Managing Data and Media in Microsoft Silverlight 4:A mashup of chapters from Packt's bestselling Silverlight books
- 數(shù)字媒體專業(yè)英語(yǔ)(第2版)
- STM32自學(xué)筆記
- 基于網(wǎng)絡(luò)化教學(xué)的項(xiàng)目化單片機(jī)應(yīng)用技術(shù)
- USB應(yīng)用分析精粹:從設(shè)備硬件、固件到主機(jī)端程序設(shè)計(jì)
- FPGA實(shí)戰(zhàn)訓(xùn)練精粹