代码和作业说明下载
第一次作业是要我们实现一个Linear Regression。
需要完成下列的代码文件:
- warmUpExercise.m
- plotData.m
- computeCost.m
- gradientDescent.m
warmUpExercise.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| function A = warmUpExercise() A = []; A = eye(5); end
|
plotData.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| function plotData(x, y) figure; plot(x, y, 'rx', 'MarkerSize', 10); ylabel('Profit in $10,000s'); xlabel('Population of City in 10,000s'); end
|
computeCost.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| function J = computeCost(X, y, theta) m = length(y); J = 0; J = sum(((X * theta) - y) .^ 2) / (2 * m); end '''
## gradientDescent.m
'''matlab function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters) m = length(y); J_history = zeros(num_iters, 1); for iter = 1:num_iters theta = theta - alpha / m * X' * ((X * theta) - y); J_history(iter) = computeCost(X, y, theta); end end
|