机器学习练习--线性回归算法实现

机器学习练习代码

https://github.com/zlotus/Coursera_Machine_Learning_Exercises

在练习中的文件和函数

  • ex1 .m 引导你练习的脚本你(单变量)
  • ex1_multi.m 后来部分(多变量)练习
  • ex1data1.txt 线性回归单变量的数据组
  • exdata1.txt 线性回归多变量的数据组
  • submit.m 提交代码到服务器
  • warmUpExercise.m 在matlab中简单例子的函数
  • plotData.m 显示数据组的函数
  • computeCost.m 计算线性回归的成本的函数
  • gradientDescent.m 梯度下降的函数(单变量)
  • computeCostMulti.m 计算多变量线性回归的成本的函数
  • gradientDescentMulti.m 计算梯度下降的函数(多变量)
  • featureNormalize.m 规范化特征函数
  • normalEqn.m 计算正规方程函数

练习提示

在整个的练习中,你将使用代码ex1.m和ex1_multi.m.这些代码因一些问题共建立数据,并且调用你写的函数,你不需要修改仔仔的代码,你仅仅修改其他文件的函数,

对于这个这个程序的练习,第一部分的实现单变量线性回归的练习是必须做的,第二部分的练习是实现多变量的线性回归是可选的。

热身练习

返回5*5的单位矩阵

function A = warmUpExercise()
%WARMUPEXERCISE Example function in octave
%   A = WARMUPEXERCISE() is an example function that returns the 5x5 identity matrix

A = [];
% ============= YOUR CODE HERE ==============
% Instructions: Return the 5x5 identity matrix 
%               In octave, we return values by defining which variables
%               represent the return values (at the top of the file)
%               and then set them accordingly. 

A = eye(5);





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


end

根据数据画图


1      2      3

4      5      6

7      8      9

 X = data(:, 1);   // 读data里面的第一列,并且转置(1 4 7)代表的是1 1,2 1 ,3 1

y = data(:, 2);    // 读data里面的第一列,并且转置(2 5 8)代表的是1 2, 2 2, 3 2

plot(x, y, 'rx', 'MarkerSize', 10); % Plot the data
ylabel('Profit in $10,000s'); % Set the y-axis label
xlabel('Population of City in 10,000s'); % Set the x-axis label

plot(x, y, 'rx', 'MarkerSize', 10);

参数:

X--向量或者矩阵

Y--向量或者矩阵

“rx”--样式


‘MarkerSize--样式的大小

10--大小就10(键值对)

xlabel('Population of City in 10,000s');


theta = zeros(2, 1);




猜你喜欢

转载自blog.csdn.net/qq_29407397/article/details/80852436
今日推荐