问题描述
Each row is a sample containing two numbers x and y, where x is the input feature and y is its corresponding response. Notice that, x and y are splited by ‘\t’.
For example, x is 0.025817872 and y is 1.072778871 in
0.025817872 1.072778871
给定一个文件data,有x,y两组数据,进行多项式拟合。很简单的一个问题,基本是学习python的numpy库一些函数的使用和plt的画图。
然后关于多项式拟合的求解问题,在第一次长达40页的作业中写了无数次,无论是最小化MSE还是MLE(极大似然估计)都算过了,这里就懒得贴公式了。贴一下之前看到的推导的图:
Numpy的笔记
numpy
赛高!之前用numpy
也就是做信号处理和打打CTF比赛会用,还真没有系统的去看它的函数和具体的参数。这次感觉学了很多没见过的方法,做个笔记。
- 几种数据类型:array,list,matrix,这三个数据类型挺容易混淆的,反正python里面最容易出bug的就是各种各样的奇怪的类型不匹配错误,反正慢慢
print(type(x))
看一看,转换类型什么的还是不难调的。1
2
3
4
5import numpy
numpy.mat(x) #转matrix类型
numpy.array(x) #转array类型
numpy.flatten(x) #顾名思义,把多维的array摊开
numpy.newaxis #扩充维度 numpy
数组和矩阵的生成,线性方程求解1
2
3
4
5numpy.linalg(a,b) #解线性方程,直接ax=b,随便两个矩阵
numpy.tile(list,[a,b])#往横纵方向分别拉伸a倍和b倍
numpy.zeros([a,b],type)#生成类型为type的全0矩阵
a=np.zeros([10,10],float)#10*10 float
np.linspace(-1, 1, 30) #生成(-1,1)内的点列,第三个参数是划分粒度- 推荐一个很好用的
numpy
读取数据的方法1
2
3data = np.loadtxt(f)#载入数据,f为文件读取路径
x = data[:, 0]#取第一列
y = data[:, 1]#取第二列,类推
数据以及源码
这里就直接放下源代码,注释写的应该很清楚了,plt画图后面有时间再整,很好用,基本和matlab的功能没什么差别。
- 实验数据data
- 源代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error
# 多项式拟合函数
# m代表拟合多项式的最高次数
# n为数据点的个数
# x为数据x轴方向上的向量(x1,x2,...,xn),y为数据y轴方向上的向量(y1,y2,...,yn)
# 拟合目标:最小化MSE,即min∑(y-head - y)^2的参数矩阵W
# 关于W的方程(X' * X) * W = X' * y
# X是n*(m+1)矩阵,第i行为1,xi,xi^2,...,xn^m
# X'是X转置, lamba为正则项(默认为0),防止过拟合
def polynthic_fit(m, n, x, y, lamda=0):
print("The number of polynths is ", m, " the length of data set is ", n)
m_list = np.arange(m+1) # (0,1,2...,m)
m_list = m_list[np.newaxis, :] # 扩充至二维
e = np.tile(m_list, [n, 1]) # 扩展为目标维数矩阵
X = np.power(x, e) # 扩展为x
Xt = X.transpose()
a = np.matmul(Xt, X) + lamda*np.identity(m+1) # a=X'*X+lamdaI(m+1)
b = np.matmul(Xt, y) # b=X'*y
W = np.linalg.solve(a, b) # aW = b <=> (X.T * X) * W = X.T * T,解线性方程组
# print(W) # 参数矩阵
# 生成拟合的数据点 y-head=X*W
y_head = np.matmul(X, W)
return W, y_head
def draw_fitting(xn, yn, W, m):
# 图像绘制
plt.figure(1, figsize=(8, 5))
p = np.poly1d(W.flatten()[::-1])
print(p)
x = np.linspace(-1, 1, 30)
y = p(x)
# 拟合出来的m次多项式曲线
plt.plot(x, y, 'g', linewidth=3)
# 原数据散点图
plt.scatter(xn, yn, color='b', marker='o', edgecolors='b',
s=100, linewidth=3, label="training data")
plt.tick_params(labelsize=15)
# 自定义x,y轴的精度
plt.xticks(np.linspace(-1, 1, 5))
plt.yticks(np.linspace(0, 1, 3))
# plt.ylim(-1.5, 1.5)#设置坐标轴范围
# 曲线风格,label样式
font = {'family': 'Times New Roman', 'size': 20}
plt.xlabel('x', font)
plt.ylabel('y', font, rotation='horizontal')
plt.title(str(m)+'-times-fitting', font)
plt.savefig(str(m)+'_times_fitting.png', dpi=400)
def file_read(f):
data = np.loadtxt(f)
x = data[:, 0]
y = data[:, 1]
return x, y
if __name__ == "__main__":
x, y = file_read('data')
times = 2
# np.mat(y).transpose()转为矩阵并且规格为m*1
W, y_head = polynthic_fit(times, len(x), np.mat(
x).transpose(), np.mat(y).transpose())
print('拟合的多项式系数如下:\n', np.array(W))
draw_fitting(x, y, np.array(W), times)
print('MSE : ', mean_squared_error(y_head, y))
拟合结果及图像
实际结果
1
2
3
4
5
6
7times 1
0.4308 x + 1
MSE : 0.009405599894797913
times 2
-0.1422 x ^2+ 0.3861 x + 1.03
MSE : 0.008083110784509665
- Judging from the results of the image, the fitting effect of the two is good. From the quantitative point of view of MSE, the second-order fitting effect is better, but there may be over-fitting phenomenon. Generally, in machine learning, in order for the function to be generalized in general, functions are required to be as simple as possible. Therefore, when the primary and secondary fitting effects are almost as good, we prefer to use the primary polynomial model.Judging from the results of the image, the fitting effect of the two is good. From the quantitative point of view of MSE, the second-order fitting effect is better, but there may be over-fitting phenomenon. Generally, in machine learning, in order for the function to be generalized in general, functions are required to be as simple as possible. Therefore, when the primary and secondary fitting effects are almost as good, we prefer to use the primary polynomial model.