給你多組資料集,例如給你很多房子的面積、房子距離市中心的距離、房子的價格,然後再給你一組面積、 距離,讓你預測房價。這類問題稱為回歸問題。
回歸問題(Regression) 是給定多個自變量、一個因變量以及代表它們之間關系的一些訓練樣本,來确定它們的關系。其中最簡單的一類是線性回歸(Linear Regression)。
線性回歸函數的形式如下:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5iYlNWYiJWYwUzMykTYkJ2NihTY0UWYjBTY3EGZzYGOi9CX0JXZ252bj91Ztl2Lc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
(1)
θj是我們要求的系數。接下來介紹一下求θ 的兩種方法,梯度下降(Gradient Descent)和正規方程(Normal Rquation )。
1. 梯度下降法
描述:梯度下降法(Gradient descent)是一個一階最優化算法,通常也稱為最速下降法。 要使用梯度下降法找到一個函數的局部極小值,必須向函數上目前點對應梯度(或者是近似梯度)的反方向的規定步長距離點進行疊代搜尋。如果相反地向梯度正方向疊代進行搜尋,則會接近函數的局部極大值點;這個過程則被稱為梯度上升法。
公式:
(2)
其中,J(θ) 稱為代價函數(Cost Function )或損失函數(Loss Function), 用來量度預測結果和标準結果之間的誤差,常見的有交叉熵,均方誤差,平均絕對值誤差等。在這裡使用均方誤差。α是學習速率,取值自定,一般取比較小的數,如0.03
(3)
( hθ(x) 是x經過待求的函數得出的結果,y(i) 是資料集中的結果)
公式(2)的旨在求出最小的θj,把代價函數J(θ)降到最小。它的原理是θ不停地疊代,減去θ對應的代價函數在x的偏導,如果偏導是正的,那麼J(θ)在該方向單增,減去這個正數後θ變小,J(θ)也會跟着變小;反之,如果偏導是負的,J(θ)單減,原θ減負數,θ變大,J(θ)減小。無論怎樣,J(θ)都朝着減小的方向變化。值得注意的是,如果學習速率α偏大,那麼θ在做差的話可能減過頭甚至得到的新J(θ)比原來還要大,而如果學習速率α偏小,那麼花費的時間會變長。
(結合圖像更直覺)
梯度下降算法是一個不斷疊代的過程,需要不斷重複公式(2),直到J(θ)符合預期誤差或者達到足夠的疊代次數。
具體步驟如下:
step0: 初始化α,θ(任意值)和疊代次數;
step1:利用公式(3) ,求J(θ);
step2:利用公式(2),本次疊代的新θ;
step3:重複step1 - step2
2. 正規方程
利用正規方程可以直接計算計算θ,前提是(XTX)必須可逆
3. matlab實作
3.1 初始化參數
data = load('ex1data1.txt');
X = data(:, 1); y = data(:, 2);
m = length(y); % number of training examples
plotData(X, y)
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1); % initialize fitting parameters
% Some gradient descent settings
iterations = 1500;
alpha = 0.01;
3.2 計算代價函數
function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression
% J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
% parameter for linear regression to fit the data points in X and y
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly
J = 0;
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
% You should set J to the cost.
predictions = X*theta;
sqrErrors = (predictions - y).^2;
J = 1/(2*m) * sum(sqrErrors);
% =========================================================================
end
3.3 梯度下降并疊代
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
x = X(:,2);
for iter = 1:num_iters
% ====================== YOUR CODE HERE ======================
% Instructions: Perform a single gradient step on the parameter vector
% theta.
%
% Hint: While debugging, it can be useful to print out the values
% of the cost function (computeCost) and gradient here.
%
J = alpha * (1/m) * (X * theta - y)' ;
theta(1) = theta(1) - J * ones(m,1);
theta(2) = theta(2) - J * x;
% ============================================================
% Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta);
end
end
3.4 繪圖
% Grid over which we will calculate J
theta0_vals = linspace(-10, 10, 100);
theta1_vals = linspace(-1, 4, 100);
% initialize J_vals to a matrix of 0's
J_vals = zeros(length(theta0_vals), length(theta1_vals));
% Fill out J_vals
for i = 1:length(theta0_vals)
for j = 1:length(theta1_vals)
t = [theta0_vals(i); theta1_vals(j)];
J_vals(i,j) = computeCost(X, y, t);
end
end
% Because of the way meshgrids work in the surf command, we need to
% transpose J_vals before calling surf, or else the axes will be flipped
J_vals = J_vals';
% Surface plot
figure;
surf(theta0_vals, theta1_vals, J_vals)
xlabel('\theta_0'); ylabel('\theta_1');
% Contour plot
figure;
% Plot J_vals as 15 contours spaced logarithmically between 0.01 and 100
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20))
xlabel('\theta_0'); ylabel('\theta_1');
hold on;
plot(theta(1), theta(2), 'rx', 'MarkerSize', 10, 'LineWidth', 2);
coursera機器學習筆記-多元線性回歸,normal equation
#對coursera上Andrew Ng老師開的機器學習課程的筆記和心得: #注:此筆記是我自己認為本節課裡比較重要.難了解或容易忘記的内容并做了些補充,并非是課堂詳細筆記和要點: #标記為
Stanford機器學習筆記-1.線性回歸
Content: 1. Linear Regression 1.1 Linear Regression with one variable 1.1.1 Gradient descent algorit ...
Andrew Ng機器學習課程筆記--week1(機器學習介紹及線性回歸)
title: Andrew Ng機器學習課程筆記--week1(機器學習介紹及線性回歸) tags: 機器學習, 學習筆記 grammar_cjkRuby: true --- 之前看過一遍,但是總是模 ...
Python機器學習筆記:不得不了解的機器學習面試知識點(1)
機器學習崗位的面試中通常會對一些常見的機器學習算法和思想進行提問,在平時的學習過程中可能對算法的理論,注意點,差別會有一定的認識,但是這些知識可能不系統,在回答的時候未必能在短時間内答出自己的認識,因 ...
cs229 斯坦福機器學習筆記(一)-- 入門與LR模型
版權聲明:本文為部落客原創文章,轉載請注明出處. https://blog.csdn.net/Dinosoft/article/details/34960693 前言 說到機器學習,非常多人推薦的學習資 ...
機器學習筆記5-Tensorflow進階API之tf.estimator
前言 本文接着上一篇繼續來聊Tensorflow的接口,上一篇中用較低層的接口實作了線性模型,本篇中将用更進階的API--tf.estimator來改寫線性模型. 還記得之前的文章
【TensorFlow篇】--Tensorflow架構初始,實作機器學習中多元線性回歸
一.前述 TensorFlow是谷歌基于DistBelief進行研發的第二代人工智能學習系統,其命名來源于本身的運作原理.Tensor(張量)意味着N維數組,Flow(流)意味着基于資料流圖的計算,T ...
Python機器學習筆記:sklearn庫的學習
網上有很多關于sklearn的學習教程,大部分都是簡單的講清楚某一方面,其實最好的教程就是官方文檔. 官方文檔位址:https://scikit-learn.org/stable/ (可是官方文檔非常 ...
Python機器學習筆記:不得不了解的機器學習知識點(2)
之前一篇筆記: Python機器學習筆記:不得不了解的機器學習知識點(1) 1,什麼樣的資料集不适合用深度學習? 資料集太小,資料樣本不足時,深度學習相對其它機器學習算法,沒有明顯優勢. 資料集沒有局 ...
随機推薦
Tensorflow二分類處理dense或者sparse(文本分類)的輸入資料
這裡做了一些小的修改,感謝谷歌rd的幫助,使得能夠統一處理dense的資料,或者類似文本分類這樣sparse的輸入資料.後續會做進一步學習優化,比如如何多線程處理. 具體如何處理sparse 主要是使 ...
月曆元件的使用,bootstrap-datetimepicker
官方文檔:http://www.bootcss.com/p/bootstrap-datetimepicker/ .html