目标:
- 多项式曲线拟合
- 多元回归
- 插值
Simple Linear Regression:
假设我们有数据集(xi,yi),并且x和y是线性相关的。
Linear Regression Formulation:
定义一个误差平方和SSE并将线性回归模型带入SSE。
Solving Least-squares Problem:
要优化SSE,当其相对于每个参数的梯度等于零时最小化。
Least-squares Solution(最小二乘法求解):
假设存在N个数据点
一、多项式曲线拟合
Polynomial Curve Fitting: polyfit():
给出数据x和y,求出优化后的参数
x =[-1.2 -0.5 0.3 0.9 1.8 2.6 3.0 3.5];
y =[-15.6 -8.5 2.2 4.5 6.6 8.2 8.9 10.0];
fit = polyfit(x,y,1); % 求优化的参数
xfit = [x(1):0.1:x(end)];
yfit = fit(1)*xfit + fit(2);
plot(x,y,'ro',xfit,yfit);
set(gca,'FontSize',14);
legend(2,'data points','best-fit');
Are ? and ? Linearly Correlated?
判断x和y是否具有线性相关性,如果没有线性相关性,则不能很好地描述他们的关系。
所以使用下面方法进行判断:
- scatter(): scatterplot 画出数据的散点图
- corrcoef(): correlation 使用此函数来判定是否具有相关性,coefficient, −1 ≤ ? ≤ 1,当越接近1,说明正相关,反之负相关,为0没有相关性。
x =[-1.2 -0.5 0.3 0.9 1.8 2.6 3.0 3.5];
y =[-15.6 -8.5 2.2 4.5 6.6 8.2 8.9 10.0];
scatter(x,y);
box on; axis square;
corrcoef(x,y)
ans =
1.0000 0.9202
0.9202 1.0000
x和y所对应的点如图中所示,得出的结果x和y的相关性值为0.9202.可以发现线性相关性很高。
Higher Order Polynomials:
x =[-1.2 -0.5 0.3 0.9 1.8 2.6 3.0 3.5];
y =[-15.6 -8.5 2.2 4.5 6.6 8.2 8.9 10.0];
figure('Position', [50 50 1500 400]);
for i=1:3
subplot(1,3,i);
p = polyfit(x,y,i);
xfit = x(1):0.1:x(end);
yfit = polyval(p,xfit);
plot(x,y,'ro',xfit,yfit);
set(gca,'FontSize',14); ylim([-17, 11]);
legend(4,'Data points','Fitted curve');
end
二、多元回归
如果存在多个特征,也就是多个变量时。
Multiple Linear Regression: regress()
load carsmall;
y = MPG;
x1 = Weight; x2 = Horsepower;
X = [ones(length(x1),1) x1 x2];
b = regress(y,X);
x1fit = min(x1):100:max(x1);
x2fit = min(x2):10:max(x2);
[X1FIT,X2FIT]=meshgrid(x1fit,x2fit);
YFIT=b(1)+b(2)*X1FIT+b(3)*X2FIT;
scatter3(x1,x2,y,'filled');
hold on;
mesh(X1FIT,X2FIT,YFIT);
hold off;
xlabel('Weight'); ylabel('Horsepower'); zlabel('MPG');
view(50,10);
[b,bint,r,rint,stats]=regress(y,X);
Curve Fitting Toolbox: cftool()
>> cftool()
三、插值
常用的插值方法:
- 分段线性插值
- 分段三次多项式插值
- 三次样条插值
求插值的函数:
Linear Interpolation: interp1() :线性插值
x = linspace(0, 2*pi, 40); x_m = x;
x_m([11:13, 28:30]) = NaN; y_m = sin(x_m);
plot(x_m, y_m,'ro', 'MarkerFaceColor', 'r');
xlim([0, 2*pi]); ylim([-1.2, 1.2]); box on;
set(gca, 'FontName', 'symbol', 'FontSize', 16);
set(gca, 'XTick', 0:pi/2:2*pi);
set(gca, 'XTickLabel', {'0', 'p/2', 'p', '3p/2', '2p'});
m_i = ~isnan(x_m);
y_i = interp1(x_m(m_i), ...
y_m(m_i), x);
hold on;
plot(x,y_i,'-b', ...
'LineWidth', 2);
hold off;
Spline Interpolation: spline():样条插值
m_i = ~isnan(x_m);
y_i = spline(x_m(m_i), y_m(m_i), x);
hold on; plot(x,y_i,'-g', 'LineWidth', 2); hold off;
h = legend('Original', 'Linear', 'Spline');
set(h,'FontName', 'Times New Roman');
What Are Splines?
Cubic Spline vs. Hermite Polynomial
x = -3:3; y = [-1 -1 -1 0 1 1 1]; t = -3:.01:3;
s = spline(x,y,t); p = pchip(x,y,t);
hold on; plot(t,s,':g', 'LineWidth', 2);
plot(t,p,'--b', 'LineWidth', 2);
plot(x,y,'ro', 'MarkerFaceColor', 'r');
hold off; box on; set(gca, 'FontSize', 16);
h = legend(2,'Original', 'Spline', 'Hermite');
2D Interpolation: interp2()
xx = -2:.5:2; yy = -2:.5:3;
[X,Y] = meshgrid(xx,yy);
Z = X.*exp(-X.^2-Y.^2);
surf(X,Y,Z); hold on;
plot3(X,Y,Z+0.01,'ok',...
'MarkerFaceColor','r')
xx_i = -2:.1:2; yy_i = -2:.1:3;
[X_i,Y_i] = meshgrid(xx_i,yy_i);
Z_i = interp2(xx,yy,Z,X_i,Y_i);
surf(X_i,Y_i,Z_i); hold on;
plot3(X,Y,Z+0.01,'ok',...
'MarkerFaceColor','r')
2D Interpolation Using Spline
xx = -2:.5:2; yy = -2:.5:3; [X,Y] = meshgrid(xx,yy);
Z = X.*exp(-X.^2-Y.^2); xx_i = -2:.1:2; yy_i = -2:.1:3;
[X_i,Y_i] = meshgrid(xx_i,yy_i);
Z_c = interp2(xx,yy,Z,X_i,Y_i,'cubic');
surf(X_i,Y_i,Z_c); hold on;
plot3(X,Y,Z+0.01,'ok', 'MarkerFaceColor','r'); hold off;
Interpolation vs Regression:
Interpolation :
- 求函数逼近的过程
- 拟合遍历所有已知点。
Regression:
- 求最佳拟合曲线的过程
- 拟合一般不通过数据点。
完结撒花:这次课程已经结束了。感谢老师的课程。我认为是一门很好地课程。我学到了很多。但是,个人认为,本课程的内容还不够详细。所以,接下来会配合看一本书。很高兴学完了这门课程。笔记不是很完美。希望对看到的人能有一些小的帮助。这个笔记主要还是作为个人使用的。感谢!!!