Coursera - linear regression - notes
cost function的表达式
batch gradient descent
1.**simultaneously update all theta**
2.每次都使用 所有都数据
Update equation的表达式,
就是求导之后的
这里,
, X0 = 1
Intercept term (theta 0)
给X增加一列,全部为1, 把theta0也看成一个feature
画图
画红xxx
plot(x,y,'rx','MarkerSize',10); ylabel('somethingYlable'); xlabel('xLable');
画线
% 如果不用hold on 画布上之前的图就会消失 hold on; % keep previous plot visible plot(X(:,2), X*theta, '-') legend('Training data', 'Linear regression') hold off % don't overlay any more plots on this figure
改良之后的 动态展示绘图 和 cost展示
if mod(iter,200) == 0 hold on; % keep previous plot visible plot(X(:,2), X*theta, '-'); hold off % don't overlay any more plots on this figure current_J = computeCost(X, y, theta) end
plotPractice.m
matlab contour api
contour(Z,v)
draws a contour plot of matrix Z
with contour lines at the data values specified in the monotonically increasing vector v
contour(theta0_vals, theta1_vals, J_vals,[5 10 20 30 50 70 100 300])%logspace(-2, 3, 10)
figure; surf(theta0_vals, theta1_vals, J_vals) xlabel('\theta_0'); ylabel('\theta_1');
figure 是创建一个新图
theta0_vals 是 x
theta1_vals是 y
J_vals是 长为theta0 宽为 theta1的数组矩阵
logspace(-2, 3, 10)
貌似是10的-2次方到3次方,10个间隔,
如果是(-5,5,11) 那就是严格按照10的x次方间隔
matlab矩阵
可以用逗号分隔,也可以用空格分隔
分号分隔column
standard deviation 标准差 均方差
标准差是方差的算术平方根
Feature Normalization
feature scaling
两步
1.减去平均值
2.除以标准差(均方差)
it’s helpful to plot J for several learning rates on the same figure.
with a ‘hold on’ command between plots
可以在一张图上画多个cost曲线,来对比 learning rate
plot(1:numel(J_history), J_history, '-b', 'LineWidth', 2);
x为训练次数,从1到numel, y为cost
numel: 计算元素个数
Normal Equations
no "loop until convergence"
exact solution of linear regression
% 如果不用hold on 画布上之前的图就会消失 hold on; % keep previous plot visible plot(X(:,2), X*theta, '-') legend('Training data', 'Linear regression') hold off % don't overlay any more plots on this figure