2、 Linear Regression 线性回归 [Stanford - Machine Learning]
来源:互联网 发布:python写网络爬虫 编辑:程序博客网 时间:2024/05/10 22:33
线性回归就是在(features+1)维空间中通过梯度下降的方法找到一条直线,使这条直线尽量匹配所有的点(每一个点是一个样本,当然可以自己构建样本如:长*宽=面积)
3、Feature Scaling:对训练样本X,因为不同样本代表的信息不同,大小差距很大,一般要进行缩放或者归一化
4、有了以上三个函数就可以求出theta了,也就是求出了我们的直线,最后让我们实现它,并预测一个样本
线性回归的知识点:(图片看不清可以在新标签打开或者保存)
·
线性回归如何编程实现:(Matlab)
1、Cost Function函数:可以用来观察每次的Cost是否下降(把每一次梯度下降递归的结果保存起来,作图就可以观察到了),便于选择合适的训练速率alpha
function J = computeCost(X, y, theta)m = length(y); % number of training examplesJ=sum((X*theta-y).^2) /(2*m);end
2、Gradient descent函数:循环调用,不断修正theta的值,直到收敛
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)m = length(y); % number of training examplesJ_history = zeros(num_iters, 1);for iter = 1:num_iters theta = theta - X'*(X*theta-y) * alpha/m; % Save the cost J in every iteration J_history(iter) = computeCostMulti(X, y, theta);endend
3、Feature Scaling:对训练样本X,因为不同样本代表的信息不同,大小差距很大,一般要进行缩放或者归一化
function [X_norm, mu, sigma] = featureNormalize(X)mu = mean(X); % 保存平均值 后面预测新的样本时用到,因为新的样本也要先Normalize处理sigma = std(X,0,1); % 保存标准差 后面预测新的样本时用到X_norm = (X-repmat(mean(X),length(X),1))./repmat(sigma,length(X),1);end
4、有了以上三个函数就可以求出theta了,也就是求出了我们的直线,最后让我们实现它,并预测一个样本
% Load Datadata = load('ex1data2.txt');X = data(:, 1:2); y = data(:, 3);m = length(y);% Scale features and set them to zero mean[X mu sigma] = featureNormalize(X);% Add intercept term to XX = [ones(m, 1) X];% Choose some alpha valuealpha = 1;num_iters = 50;% Init Theta and Run Gradient Descent theta = zeros(3, 1);[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);% Plot the convergence graphfigure;plot(1:numel(J_history), J_history, '-b', 'LineWidth', 2);xlabel('Number of iterations');ylabel('Cost J');% Display gradient descent's resultfprintf('Theta computed from gradient descent: \n');fprintf(' %f \n', theta);fprintf('\n');% Estimate the price of a 1650 sq-ft, 3 br houseXX = [1650 3];XX=[ones(1, 1) (XX - mu)./sigma];price = XX *theta ;fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ... '(using gradient descent):\n $%f\n'], price);
0 0
- 2、 Linear Regression 线性回归 [Stanford - Machine Learning]
- stanford machine learning, linear regression
- [笔记]机器学习(Machine Learning) - 01.线性回归(Linear Regression)
- 【Stanford Machine Learning】Lecture 2--Linear Regression with Multiple Variables
- Stanford Machine Learning: (1). Linear Regression
- Stanford Machine Learning Note1 - Linear Regression
- Coursera《machine learning》--(2)单变量线性回归(Linear Regression with One Variable)
- ANDREW Ng教授的机器学习(Machine Learning)学习笔记(2)-- 线性回归模型(Linear regression model)
- 【Machine Learning】单参数线性回归 Linear Regression with one variable
- Machine Learning - II. Linear Regression with One Variable单变量线性回归 (Week 1)
- Machine Learning 线性回归( Linear Regression) Andrew Ng 课程练习 Matlab Script 详细解析
- Stanford Machine Learning 公开课笔记(1) Linear Regression
- Stanford Machine Learning 公开课笔记(1) Linear Regression
- 【machine learning】linear regression
- Stanford机器学习---第2讲. 多变量线性回归 Linear Regression with multiple variable
- Linear Regression 线性回归
- linear regression 线性回归
- 线性回归 Linear Regression
- Android Studio 快捷键
- netty5.0之EventLoop
- 瞬间
- java中的基本数据类型和引用数据类
- ziti
- 2、 Linear Regression 线性回归 [Stanford - Machine Learning]
- 进程和线程的区别
- c++写nodejs的扩展模块
- linux ls 只显示文件或者文件夹
- Lamda表达式的一个有意思的例子
- Daikon与NGUI教程学习笔记
- Hibernate的十大罪状
- NYOJ 1104 劝你别做【简单题】
- iOS运行时之二:关联对象