2013-06-25

Machine Learning 第八波编程作业(完)——Anomaly Detection and Recommender Systems

仅列出核心代码:

1.estimateGuassian.m

mu = mean(X)';
X2 = (X - ones(m, 1)*mu').^2;
sigma2 = mean(X2);

2.selectThreshold.m

cvPredictions = (pval < epsilon);
tp = sum((cvPredictions == 1) & (yval == 1));
fp = sum((cvPredictions == 1) & (yval == 0));
fn = sum((cvPredictions == 0) & (yval == 1));
prec = tp/(tp + fp);
rec = tp/(tp + fn);
F1 = 2*prec*rec/(prec + rec);

3.cofiCostFunc.m

X1 = (X*Theta'- Y).*R;
reg1 = (sum(sum(X.^2)) + sum(sum(Theta.^2)))*lambda/2;
J = sum(sum((X1).^2))/2 + reg1;

X_grad = X1*Theta + lambda*X;
Theta_grad = X1'*X + lambda*Theta;

课程地址:https://www.coursera.org/course/ml

 

2013-06-18

Machine Learning 第七波编程作业——K-means Clustering and Principal Component Analysis

仅列出核心代码:

1.findClosestCentroids.m

m = size(X, 1);
len = zeros(K, 1);
for i = 1:m
    for j = 1:K
        len(j) = norm(X(i, :) - centroids(j, :))^2;
    end
    [~, idx(i)] = min(len);
end

2.computeCentroids.m

for k = 1:K
    ind = find(idx == k);
    centroids(k, :) = mean(X(ind, :));
end

3.pca.m

Sigma = X'*X/m;
[U,S,~] = svd(Sigma);

4.projectData.m

Z = X * U(:, 1:K);

5.recoverData.m

X_rec = Z * U(:, 1:K)';

课程地址:https://www.coursera.org/course/ml

2013-06-11

Machine Learning 第六波编程作业——Support Vector Machines

仅列出核心代码:

1.gaussianKernel.m

sim = exp(-sum((x1 - x2).^2) /(2*sigma^2));

2.dataset3Params.m

TD =  [0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30];
pre_err = zeros(length(TD));
for i = 1:length(TD)
    for j = 1:length(TD)
        C = TD(i);
        sigma = TD(j);
        model= svmTrain(X, y, C, @(x1, x2) gaussianKernel(x1, x2, sigma));
        predictions = svmPredict(model, Xval);
        pre_err(i, j) = mean(double(predictions ~= yval));
    end
end
mm = min(min(pre_err));
[ind_C, ind_sigma] = find(pre_err == mm);
C = TD(ind_C);
sigma = TD(ind_sigma);

3.processEmail.m

for i = 1:length(vocabList)
v = strcmp(str, vocabList(i));
    if v==1
        word_indices = [word_indices ; i];
    end
end

4.emailFeatures.m

x(word_indices) = 1;

课程地址:https://www.coursera.org/course/ml

2013-06-03

Machine Learning 第五波编程作业 – Regularized Linear Regression and Bias/Variance

仅列出核心代码:

1.linearRegCostFunction.m

h = X * theta;
J = (X * theta - y).' * (X * theta - y) / (2*m)...
    +(lambda/(2*m)) * sum(theta(2:end).^2);
grad = grad(:);
grad(1) = (X(:, 1).' * (h - y)) /m;

grad(2:end) = (X(:, 2:end).' * (h - y)) /m ...
+ (lambda/m) * theta(2:end);

2.learningCurve.m

for i = 1:m
    Xi = X(1:i, :);
    yi = y(1:i);
    lambda = 1;
    [theta] = trainLinearReg(Xi, yi, lambda);
    lambda = 0;
    % For train error, make sure you compute it on the training subset
    [error_train(i), ~] = linearRegCostFunction(Xi, yi, theta, lambda);
    % For validation error, compute it over the entire cross validation set
    [error_val(i), ~] = linearRegCostFunction(Xval, yval, theta, lambda);
end

3.polyFeatures.m

for i =1:p
    X_poly(:, i) = X(:, 1).^i;
end

4.validationCurve.m

for i = 1:length(lambda_vec)
    [theta] = trainLinearReg(X, y, lambda_vec(i));
    % For train error, make sure you compute it on the training subset
    [error_train(i), ~] = linearRegCostFunction(X, y, theta, 0);
    % For validation error, compute it over the entire cross validation set
    [error_val(i), ~] = linearRegCostFunction(Xval, yval, theta, 0);
end

课程地址:https://www.coursera.org/course/ml