前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >正则化线性回归,来研究具有不同偏差方差特性的模型。

正则化线性回归,来研究具有不同偏差方差特性的模型。

作者头像
裴来凡
发布于 2022-05-28 07:17:19
发布于 2022-05-28 07:17:19
55000
代码可运行
举报
运行总次数:0
代码可运行

ex5.m

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
%% Machine Learning Online Class
%  Exercise 5 | Regularized Linear Regression and Bias-Variance
%
%  Instructions
%  ------------
% 
%  This file contains code that helps you get started on the
%  exercise. You will need to complete the following functions:
%
%     linearRegCostFunction.m
%     learningCurve.m
%     validationCurve.m
%
%  For this exercise, you will not need to change any code in this file,
%  or any other files other than those mentioned above.
%

%% Initialization
clear ; close all; clc

%% =========== Part 1: Loading and Visualizing Data =============
%  We start the exercise by first loading and visualizing the dataset. 
%  The following code will load the dataset into your environment and plot
%  the data.
%

% Load Training Data
fprintf('Loading and Visualizing Data ...\n')

% Load from ex5data1: 
% You will have X, y, Xval, yval, Xtest, ytest in your environment
load ('ex5data1.mat');

% m = Number of examples
m = size(X, 1);

% Plot training data
plot(X, y, 'rx', 'MarkerSize', 10, 'LineWidth', 1.5);
xlabel('Change in water level (x)');
ylabel('Water flowing out of the dam (y)');

fprintf('Program paused. Press enter to continue.\n');
pause;

%% =========== Part 2: Regularized Linear Regression Cost =============
%  You should now implement the cost function for regularized linear 
%  regression. 
%

theta = [1 ; 1];
J = linearRegCostFunction([ones(m, 1) X], y, theta, 1);

fprintf(['Cost at theta = [1 ; 1]: %f '...
         '\n(this value should be about 303.993192)\n'], J);

fprintf('Program paused. Press enter to continue.\n');
pause;

%% =========== Part 3: Regularized Linear Regression Gradient =============
%  You should now implement the gradient for regularized linear 
%  regression.
%

theta = [1 ; 1];
[J, grad] = linearRegCostFunction([ones(m, 1) X], y, theta, 1);

fprintf(['Gradient at theta = [1 ; 1]:  [%f; %f] '...
         '\n(this value should be about [-15.303016; 598.250744])\n'], ...
         grad(1), grad(2));

fprintf('Program paused. Press enter to continue.\n');
pause;


%% =========== Part 4: Train Linear Regression =============
%  Once you have implemented the cost and gradient correctly, the
%  trainLinearReg function will use your cost function to train 
%  regularized linear regression.
% 
%  Write Up Note: The data is non-linear, so this will not give a great 
%                 fit.
%

%  Train linear regression with lambda = 0
lambda = 0;
[theta] = trainLinearReg([ones(m, 1) X], y, lambda);

%  Plot fit over the data
plot(X, y, 'rx', 'MarkerSize', 10, 'LineWidth', 1.5);
xlabel('Change in water level (x)');
ylabel('Water flowing out of the dam (y)');
hold on;
plot(X, [ones(m, 1) X]*theta, '--', 'LineWidth', 2)
hold off;

fprintf('Program paused. Press enter to continue.\n');
pause;


%% =========== Part 5: Learning Curve for Linear Regression =============
%  Next, you should implement the learningCurve function. 
%
%  Write Up Note: Since the model is underfitting the data, we expect to
%                 see a graph with "high bias" -- Figure 3 in ex5.pdf 
%

lambda = 0;
[error_train, error_val] = ...
    learningCurve([ones(m, 1) X], y, ...
                  [ones(size(Xval, 1), 1) Xval], yval, ...
                  lambda);

plot(1:m, error_train, 1:m, error_val);
title('Learning curve for linear regression')
legend('Train', 'Cross Validation')
xlabel('Number of training examples')
ylabel('Error')
axis([0 13 0 150])

fprintf('# Training Examples\tTrain Error\tCross Validation Error\n');
for i = 1:m
    fprintf('  \t%d\t\t%f\t%f\n', i, error_train(i), error_val(i));
end

fprintf('Program paused. Press enter to continue.\n');
pause;

%% =========== Part 6: Feature Mapping for Polynomial Regression =============
%  One solution to this is to use polynomial regression. You should now
%  complete polyFeatures to map each example into its powers
%

p = 8;

% Map X onto Polynomial Features and Normalize
X_poly = polyFeatures(X, p);
[X_poly, mu, sigma] = featureNormalize(X_poly);  % Normalize
X_poly = [ones(m, 1), X_poly];                   % Add Ones

% Map X_poly_test and normalize (using mu and sigma)
X_poly_test = polyFeatures(Xtest, p);
X_poly_test = bsxfun(@minus, X_poly_test, mu);
X_poly_test = bsxfun(@rdivide, X_poly_test, sigma);
X_poly_test = [ones(size(X_poly_test, 1), 1), X_poly_test];         % Add Ones

% Map X_poly_val and normalize (using mu and sigma)
X_poly_val = polyFeatures(Xval, p);
X_poly_val = bsxfun(@minus, X_poly_val, mu);
X_poly_val = bsxfun(@rdivide, X_poly_val, sigma);
X_poly_val = [ones(size(X_poly_val, 1), 1), X_poly_val];           % Add Ones

fprintf('Normalized Training Example 1:\n');
fprintf('  %f  \n', X_poly(1, :));

fprintf('\nProgram paused. Press enter to continue.\n');
pause;



%% =========== Part 7: Learning Curve for Polynomial Regression =============
%  Now, you will get to experiment with polynomial regression with multiple
%  values of lambda. The code below runs polynomial regression with 
%  lambda = 0. You should try running the code with different values of
%  lambda to see how the fit and learning curve change.
%

lambda = 3;
[theta] = trainLinearReg(X_poly, y, lambda);

% Plot training data and fit
figure(1);
plot(X, y, 'rx', 'MarkerSize', 10, 'LineWidth', 1.5);
plotFit(min(X), max(X), mu, sigma, theta, p);
xlabel('Change in water level (x)');
ylabel('Water flowing out of the dam (y)');
title (sprintf('Polynomial Regression Fit (lambda = %f)', lambda));

figure(2);
[error_train, error_val] = ...
    learningCurve(X_poly, y, X_poly_val, yval, lambda);
plot(1:m, error_train, 1:m, error_val);

title(sprintf('Polynomial Regression Learning Curve (lambda = %f)', lambda));
xlabel('Number of training examples')
ylabel('Error')
axis([0 13 0 100])
legend('Train', 'Cross Validation')

fprintf('Polynomial Regression (lambda = %f)\n\n', lambda);
fprintf('# Training Examples\tTrain Error\tCross Validation Error\n');
for i = 1:m
    fprintf('  \t%d\t\t%f\t%f\n', i, error_train(i), error_val(i));
end

fprintf('Program paused. Press enter to continue.\n');
pause;

%% =========== Part 8: Validation for Selecting Lambda =============
%  You will now implement validationCurve to test various values of 
%  lambda on a validation set. You will then use this to select the
%  "best" lambda value.
%

[lambda_vec, error_train, error_val] = ...
    validationCurve(X_poly, y, X_poly_val, yval);

close all;
plot(lambda_vec, error_train, lambda_vec, error_val);
legend('Train', 'Cross Validation');
xlabel('lambda');
ylabel('Error');

fprintf('lambda\t\tTrain Error\tValidation Error\n');
for i = 1:length(lambda_vec)
  fprintf(' %f\t%f\t%f\n', ...
            lambda_vec(i), error_train(i), error_val(i));
end

fprintf('Program paused. Press enter to continue.\n');
pause;
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-05-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 图像处理与模式识别研究所 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
使用协同过滤来构建电影推荐系统。
%% Machine Learning Online Class % Exercise 8 | Anomaly Detection and Collaborative Filtering % % Instructions % ------------ % % This file contains code that helps you get started on the % exercise. You will need to complete the following functions:
裴来凡
2022/05/28
3230
使用协同过滤来构建电影推荐系统。
实现线性回归,并看到线性回归对数据的作用。
ex1.m %% Machine Learning Online Class - Exercise 1: Linear Regression % Instructions % ------------ % % This file contains code that helps you get started on the % linear exercise. You will need to complete the following functions % in this exerics
裴来凡
2022/05/28
4330
实现线性回归,并看到线性回归对数据的作用。
机器学习作业5-偏差和方差
模型训练完成,怎么直观的观察模型的好坏呢? 观察训练集的方差和交叉验证的方差,可以大致判断是欠拟合还是过拟合
公号sumsmile
2021/03/12
4400
机器学习作业5-偏差和方差
基于支持向量机的电子邮件垃圾邮件分类。
ex6.m %% Machine Learning Online Class % Exercise 6 | Support Vector Machines % % Instructions % ------------ % % This file contains code that helps you get started on the % exercise. You will need to complete the following functions: % % gau
裴来凡
2022/05/28
5790
基于支持向量机的电子邮件垃圾邮件分类。
逻辑回归和神经网络来识别手写数字。
ex3.m %% Machine Learning Online Class - Exercise 3 | Part 1: One-vs-all % Instructions % ------------ % % This file contains code that helps you get started on the % linear exercise. You will need to complete the following functions % in this exeri
裴来凡
2022/05/28
2420
逻辑回归和神经网络来识别手写数字。
实现神经网络的反向传播算法,并将其应用于手写数字识别任务。
ex4.m %% Machine Learning Online Class - Exercise 4 Neural Network Learning % Instructions % ------------ % % This file contains code that helps you get started on the % linear exercise. You will need to complete the following functions % in this
裴来凡
2022/05/28
3450
实现神经网络的反向传播算法,并将其应用于手写数字识别任务。
实现逻辑回归,并将其应用于两个不同的数据集。
ex2.m %% Machine Learning Online Class - Exercise 2: Logistic Regression % % Instructions % ------------ % % This file contains code that helps you get started on the logistic % regression exercise. You will need to complete the following functions
裴来凡
2022/05/28
6720
实现逻辑回归,并将其应用于两个不同的数据集。
Stanford 机器学习练习 Part 1 Linear Regression
warmUpExercise.m function A = warmUpExercise() %WARMUPEXERCISE Example function in octave % A = WARMUPEXERCISE() is an example function that returns the 5x5 identity matrix A = []; % ============= YOUR CODE HERE ============== % Instructions: Return
xindoo
2021/01/21
5350
全逻辑回归和神经网络识别手写数字。
ex3_nn.m %% Machine Learning Online Class - Exercise 3 | Part 2: Neural Networks % Instructions % ------------ % % This file contains code that helps you get started on the % linear exercise. You will need to complete the following functions %
裴来凡
2022/05/28
2110
全逻辑回归和神经网络识别手写数字。
python实现线性回归之岭回归
上述式子中w为长度为n的向量,不包括偏置项的系数 θ0,θ是长度为n+1的向量,包括偏置项系数θ0;m为样本数,n为特征数。
西西嘛呦
2020/08/26
1.7K0
python实现线性回归之岭回归
实现K-means聚类算法并将其应用于压缩图像。
%% Machine Learning Online Class % Exercise 7 | Principle Component Analysis and K-Means Clustering % % Instructions % ------------ % % This file contains code that helps you get started on the % exercise. You will need to complete the following funct
裴来凡
2022/05/28
3700
实现K-means聚类算法并将其应用于压缩图像。
python实现线性回归之lasso回归
Lasso回归于岭回归非常相似,它们的差别在于使用了不同的正则化项。最终都实现了约束参数从而防止过拟合的效果。但是Lasso之所以重要,还有另一个原因是:Lasso能够将一些作用比较小的特征的参数训练为0,从而获得稀疏解。也就是说用这种方法,在训练模型的过程中实现了降维(特征筛选)的目的。
西西嘛呦
2020/08/26
3.6K0
python实现线性回归之lasso回归
python实现线性回归之弹性网回归
恰好为岭回归罚函数和Lasso罚函数的一个凸线性组合.当α=0时,弹性网回归即为岭回归;当 α=1时,弹性网回归即为Lasso回归.因此,弹性网回归兼有Lasso回归和岭回归的优点,既能达到变量选择的目的,又具有很好的群组效应。
西西嘛呦
2020/08/26
1.7K0
python实现线性回归之弹性网回归
逐渐增加样本训练模型实现误差最小且误差值接近1.41%的最小P(误差)值。
Q1_final.m clear all; close all; clc; %% Set-Up: given parameters and validation data % Given parameters n = 2; % number of feature dimensions N_train = [10;100;1000]; % number of training samples N_val = 10000; % nu
裴来凡
2022/05/28
9440
逐渐增加样本训练模型实现误差最小且误差值接近1.41%的最小P(误差)值。
python实现线性回归之简单回归
代码来源:https://github.com/eriklindernoren/ML-From-Scratch
西西嘛呦
2020/08/26
5230
python实现线性回归之简单回归
机器学习笔记之正则化的线性回归的岭回归与Lasso回归
正则化是用来防止过拟合的方法。在最开始学习机器学习的课程时,只是觉得这个方法就像某种魔法一样非常神奇的改变了模型的参数。
Jetpropelledsnake21
2021/01/21
1.1K0
机器学习第5天:多项式回归与学习曲线
将多项式化为多个单项的,也就是将x的平方和x两个项分离开,然后单独给线性模型处理,求出参数,最后再组合在一起,很好理解,让我们来看一下代码
Nowl
2024/01/18
1450
机器学习第5天:多项式回归与学习曲线
【机器学习 | 回归问题】超越直线:释放多项式回归的潜力 —— 详解线性回归与非线性 (含详细案例、源码)
🙋‍♂️声明:本人目前大学就读于大二,研究兴趣方向人工智能&硬件(虽然硬件还没开始玩,但一直很感兴趣!希望大佬带带)
计算机魔术师
2023/10/09
6480
线性回归模型使用技巧
线性回归是统计学中最基础且广泛使用的预测模型之一。它通过找到最佳拟合直线(或超平面)来描述因变量(目标变量)与自变量(预测因子)之间的关系。本文将探讨线性回归的核心理论,常见问题,如何避免这些错误,并提供一个实践案例及代码示例。
Jimaks
2024/05/14
1990
机器学习中的线性回归
线性回归是机器学习领域中最简单而有效的模型之一。它用于建立自变量(输入)和因变量(输出)之间的线性关系。在实际应用中,线性回归广泛用于预测、分析和建模。让我们深入了解线性回归的基本原理和应用。
GeekLiHua
2025/01/21
1000
推荐阅读
相关推荐
使用协同过滤来构建电影推荐系统。
更多 >
领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文