# Octave解析机器学习算法

## 监督学习

### 回归

``````% COST function.
% It shows how accurate our model is based on current model parameters.
function [cost] = cost_function(X, y, theta, lambda)
% Input:
% X - input features - (m x n) matrix.
% theta - our model parameters - (n x 1) vector.
% y - a vector of correct output - (m x 1) vector.
% lambda - regularization parameter.
%
% Output:
% cost - number that represents the cost (error) of our model with specified parameters theta.
%
% Where:
% m - number of training examples,
% n - number of features.

% Get the size of the trainging set.
m = size(X, 1);

% Get the difference between predictions and correct output values.
differences = hypothesis(X, theta) - y;

% Calculate regularization parameter.
% Remember that we should not regularize the parameter theta_zero.
theta_cut = theta(2:end, 1);
regularization_param = lambda * (theta_cut' * theta_cut);

% Calculate current predictions cost.
cost = (1 / 2 * m) * (differences' * differences + regularization_param);
end
``````
``````% FEATURE NORMALIZE function.
% Normalizes the features in X. Returns a normalized version of X where the mean value of
% each feature is 0 and the standard deviation is 1.
function [X_normalized, mu, sigma] = feature_normalize(X)
X_normalized = X;
mu = zeros(1, size(X_normalized, 2));
sigma = zeros(1, size(X_normalized, 2));

% Get average values for each feature (column) in X.
mu = mean(X_normalized);

% Calculate the standard deviation for each feature.
sigma = std(X_normalized);

% Subtract mean values from each feature (column) of every example (row)
% to make all features be spread around zero.
X_normalized = X_normalized - mu;

% Normalize each feature values for each example so that all features
% are close to [-1:1] boundaries.
X_normalized = X_normalized ./ sigma;
end
``````