《Pattern Recognition and Machine Learning》:第一章,introduction(第一节:polynomial curve fitting)
2015-09-07 10:51
579 查看
开学了,以前学好多东西都忘了,趁此机会把未看的经典《Pattern Recognition and Machine Learning》看一下,回忆知识点,加深理解,但只记录新的理解。
1.1节讲了一个例子,polynomial curve fitting。
1)使用root-mean-square(RMS) error比使用sum of squared errors好:
RMS不受样本数的影响:不同样本数得出的两个errors可以简单比较。
RMS的error和target y的数量级是一样的,因为square之后进行了root操作。
2)防止over-fitting的方法:
limit the number of parameters,减少模型复杂度。
adopting Bayesian approach,有先验,对少量数据适用。
regularization,注意 w0 一般不包含在regularizer内。
validation,分割数据集并单独验证参数泛化能力。
下面给出一个拟合多项式的例子:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201911/07/9e27ef3603bb56276871caf3da2a5fd0)
提供部分数据:
1.1节讲了一个例子,polynomial curve fitting。
1)使用root-mean-square(RMS) error比使用sum of squared errors好:
RMS不受样本数的影响:不同样本数得出的两个errors可以简单比较。
RMS的error和target y的数量级是一样的,因为square之后进行了root操作。
2)防止over-fitting的方法:
limit the number of parameters,减少模型复杂度。
adopting Bayesian approach,有先验,对少量数据适用。
regularization,注意 w0 一般不包含在regularizer内。
validation,分割数据集并单独验证参数泛化能力。
下面给出一个拟合多项式的例子:
import pandas as pd import numpy as np import scipy as sp import matplotlib.pyplot as plt from sklearn.pipeline import Pipeline from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures #RMS def rmse(y_test, y_true): return sp.sqrt(sp.mean((y_test - y_true) ** 2)) # the sum of the squares of the errors def sse(y_test, y_true): return ((y_test - y_true) ** 2).sum() / 2.0 # load training and test datasets data = pd.read_csv('fitting.csv') X = data.id.values y = data.dingdanshu.values X = np.array(X) y = np.array(y) X = X[2:20] y = y[2:20] #print X #print y # plot scatter of original data plt.scatter(X, y) degree = [1, 2, 3, 4, 5] for d in degree: clf = Pipeline([ ('poly', PolynomialFeatures(degree=d)), ('linear', LinearRegression(fit_intercept=False)) ]) clf.fit(X[:, np.newaxis], y) y_test = clf.predict(X[:, np.newaxis]) print(clf.named_steps['linear'].coef_) print('rmse=%.2f, sse=%.2f, clf.score=%.2f' % (rmse(y_test, y), sse(y_test, y), clf.score(X[:, np.newaxis], y)) ) # plot fit line of y_test plt.plot(X, y_test, linewidth=2) plt.grid() plt.legend(['1', '2', '3','4','5'], loc='upper right') plt.show() ''' 对5次多项式求 每个点处的曲率 aa=clf.named_steps['linear'].coef_ for i in range(20): y1=aa[1]+aa[2]*2*i+aa[3]*3*i*i+aa[4]*4*i*i*i+aa[5]*5*i*i*i*i y2=aa[2]*2+aa[3]*6*i+aa[4]*12*i*i+aa[5]*20*i*i*i r = (y2*y2)/((1+y1*y1)*(1+y1*y1)*(1+y1*y1)) print i, "---", y1, "---", y2, "---", r '''结果如图所示:
提供部分数据:
id | dingdanshu |
0 | 153880 |
1 | 246449 |
2 | 178882 |
3 | 120823 |
4 | 82616 |
5 | 60869 |
6 | 48609 |
7 | 39028 |
8 | 32532 |
9 | 28355 |
10 | 25058 |
11 | 22341 |
12 | 20822 |
13 | 19471 |
14 | 17555 |
15 | 16965 |
16 | 15134 |
17 | 14305 |
18 | 12716 |
19 | 11684 |
20 | 11274 |
21 | 10270 |
22 | 9139 |
23 | 8210 |
24 | 7848 |
25 | 6906 |
相关文章推荐
- 程序员必备:技术面试准备手册
- 2015年吉林省赛 Pin Pin Pin(矩阵快速幂)
- Linux内核监控模块-3-系统调用的截获
- test
- android moveTo、lineTo、quadTo、cubicTo、arcTo详解(转)
- 第5回 二弟呀,面子工程很重要
- iOS Wi-Fi 描述文件:关于“自动加入”和单一连接密码设置
- SOAPUI测试步骤(二)---流量控制(Conditional Goto)
- 码农们可以优越,并且应该优越
- auto layout
- android moveTo、lineTo、quadTo、cubicTo、arcTo详解(转)
- 训练样本集的制作
- 第一次启动判断
- C++的File类文件操作
- python导入模块的方法
- 移动互联网的未知魅力
- 第5回 二弟呀,面子工程很重要
- 第5回 二弟呀,面子工程很重要
- SOAPUI测试步骤(一)——断言测试(Assertion TestStep)
- ptree数据结构分析