吳恩達 MachineLearning Week6

吳恩達 MachineLearning Week6

第六週知識點總結

  1. 應將數據分割爲訓練集(training set)/交叉驗證集(cross validation set)/測試集(test set)三個部分。
    訓練集用於訓練數據,驗證集用於肯定模型選定的參數維度,是否過擬合等,測試集用來最終測驗模型效果。
  2. 模型的參數維度越小,越容易過擬合,體如今交叉驗證集偏差(cross validation error)會很大但可能會形成過擬合。
    通常隨着參數維度的逐漸增長,訓練集偏差(train error)會愈來愈大,但泛化效果會越好,交叉驗證即偏差會減少。最終兩個偏差值會愈來愈接近並收斂。
  3. 對於過擬合或者高偏差的解決方法通常有以下幾種
    • 更多的訓練集 —— 解決過擬合
    • 更少的參數維度 —— 解決過擬合
    • 更多的參數維度 —— 解決高偏差
    • 增大lambda —— 解決過擬合
    • 減少lambda —— 解決高偏差
  4. 有些時候可能會有偏斜數據問題(Skewed data)。如癌症發病率爲 0.5% 若是預測模型對全部病人都預測未得癌症則該模型也能有99.5的正確率。這顯然是不合適的。因而引入了以下幾個量
    • Precision = true positive / (true positive + false positive)
    • Recall = true positive / (true positive + false negatvie)
    • Fscore = 2 * ( P * R ) / (P + R)

其中 true positive 表示當實際得病,預測得病。False positive 表示實際未得病,預測值得病。
false negative 表示實際得病,預測未得病。True negative 表示實際未得病,預測未得病。
Precision 越高說明預測精度越高,預測得病的得病機率很高,但這樣會致使低 Recall 值,便可能會漏診。
把得病的預測未得病的。最後用一個 Fscore 值來評價預測模型值越高越好。測試

課後做業代碼

linearRegCostFunction.m

function [J, grad] = linearRegCostFunction(X, y, theta, lambda)
%LINEARREGCOSTFUNCTION Compute cost and gradient for regularized linear
%regression with multiple variables
%   [J, grad] = LINEARREGCOSTFUNCTION(X, y, theta, lambda) computes the
%   cost of using theta as the parameter for linear regression to fit the
%   data points in X and y. Returns the cost in J and the gradient in grad

% Initialize some useful values
m = length(y); % number of training examples

% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost and gradient of regularized linear
%               regression for a particular choice of theta.
%
%               You should set J to the cost and grad to the gradient.
%


theta_without1 = theta(2:end , :);

J =  sum((X * theta - y) .^ 2) / ( 2 * m) + sum(lambda * theta_without1 .^ 2 /( 2 * m)) ;

theta_without1 = theta;
theta_without1(1) = 0;

grad = X' * (X * theta - y) / m +  lambda * theta_without1 / m;




% =========================================================================

grad = grad(:);

end

learningCurve.m

function [error_train, error_val] = ...
    learningCurve(X, y, Xval, yval, lambda)
%LEARNINGCURVE Generates the train and cross validation set errors needed
%to plot a learning curve
%   [error_train, error_val] = ...
%       LEARNINGCURVE(X, y, Xval, yval, lambda) returns the train and
%       cross validation set errors for a learning curve. In particular,
%       it returns two vectors of the same length - error_train and
%       error_val. Then, error_train(i) contains the training error for
%       i examples (and similarly for error_val(i)).
%
%   In this function, you will compute the train and test errors for
%   dataset sizes from 1 up to m. In practice, when working with larger
%   datasets, you might want to do this in larger intervals.
%

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

% You need to return these values correctly
error_train = zeros(m, 1);
error_val   = zeros(m, 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Fill in this function to return training errors in
%               error_train and the cross validation errors in error_val.
%               i.e., error_train(i) and
%               error_val(i) should give you the errors
%               obtained after training on i examples.
%


 for i = 1:m
     theta = trainLinearReg(X(1:i , :) , y(1:i) , lambda);
     error_train(i) = linearRegCostFunction(X(1:i , :) , y(1:i) , theta , 0);
     error_val(i) = linearRegCostFunction(Xval , yval , theta , 0);
 end




% -------------------------------------------------------------

% =========================================================================

end

polyFeatures.m

function [X_poly] = polyFeatures(X, p)
%POLYFEATURES Maps X (1D vector) into the p-th power
%   [X_poly] = POLYFEATURES(X, p) takes a data matrix X (size m x 1) and
%   maps each example into its polynomial features where
%   X_poly(i, :) = [X(i) X(i).^2 X(i).^3 ...  X(i).^p];
%


% You need to return the following variables correctly.
X_poly = zeros(numel(X), p);

% ====================== YOUR CODE HERE ======================
% Instructions: Given a vector X, return a matrix X_poly where the p-th
%               column of X contains the values of X to the p-th power.
%
%
m = numel(X);

X1 = X(:);

disp(X1);
for i = 1:p
  for j = 1:m

    X_poly(j,i) = X1(j)^i;
  end
end





% =========================================================================

end

validationCurve.m

function [lambda_vec, error_train, error_val] = ...
    validationCurve(X, y, Xval, yval)
%VALIDATIONCURVE Generate the train and validation errors needed to
%plot a validation curve that we can use to select lambda
%   [lambda_vec, error_train, error_val] = ...
%       VALIDATIONCURVE(X, y, Xval, yval) returns the train
%       and validation errors (in error_train, error_val)
%       for different values of lambda. You are given the training set (X,
%       y) and validation set (Xval, yval).
%

% Selected values of lambda (you should not change this)
lambda_vec = [0 0.001 0.003 0.01 0.03 0.1 0.3 1 3 10]';

% You need to return these variables correctly.
error_train = zeros(length(lambda_vec), 1);
error_val = zeros(length(lambda_vec), 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Fill in this function to return training errors in
%               error_train and the validation errors in error_val. The
%               vector lambda_vec contains the different lambda parameters
%               to use for each calculation of the errors, i.e,
%               error_train(i), and error_val(i) should give
%               you the errors obtained after training with
%               lambda = lambda_vec(i)
%



for i = 1:length(lambda_vec)
    lambda = lambda_vec(i);
    theta = trainLinearReg(X, y, lambda);
    error_train(i) = linearRegCostFunction(X , y , theta , 0);
    error_val(i) = linearRegCostFunction(Xval , yval , theta , 0);
end







% =========================================================================

end
相關文章
相關標籤/搜索