深度学习(多变量线性回归)

来源:互联网 发布:下载淘宝网app 编辑:程序博客网 时间:2024/05/20 18:41

今天完成了多变量线性回归的编程练习,除了训练参数theta以外,还要训练学习速率alpha。数据下载地址

%x数据有两个属性:x(1)是房子的大小,x(2)是房子卧室的个数%y数据是房子的价格clear;clc;%% 导入数据x=load('ex3x.dat');y=load('ex3y.dat');%% 对x数据进行标准化处理x = [ones(size(x,1),1),x];meanx = mean(x);%求均值sigmax = std(x);%求标准偏差x(:,2) = (x(:,2)-meanx(2))./sigmax(2);x(:,3) = (x(:,3)-meanx(3))./sigmax(3);%% 初始化参数figure;itera_num = 100;                               %迭代次数sample_num =size(x,1);                         %样本的个数alpha = [0.01,0.03,0.1,0.3,1,1.3];             %定义6种学习速率(3倍间隔)plotstyle ={'b','r','g','k','b--','r--'};      %定义6种曲线的颜色for k=1:length(alpha)    theta=zeros(size(x,2),1);                  %初始化参数    Jtheta =zeros(itera_num, 1);               %每一个迭代次数下的代价值    for i=1:itera_num        Jtheta(i)=(1/(2*sample_num)).*(x*theta-y)'*(x*theta-y);        grad=(1/sample_num).*x'*(x*theta-y);        theta=theta-alpha(k).*grad;    end    plot(1:100,Jtheta(1:100),char(plotstyle{k}),'LineWidth', 2)%此处一定要通过char函数来转换    hold on;    if alpha(k)==1        theta_result=theta;    endend legend('alpha=0.01','alpha=0.03','alpha=0.1','alpha=0.3','alpha=1','alpha=1.3');xlabel('Number of iterations');ylabel('Cost function');title('comparison result');%% 预测结果price=theta_result'*[1,(1650-meanx(2))/sigmax(2),(3-meanx(3)/sigmax(3))]';fprintf('当房子大小为1650时,卧室个数3时,房价为%f\n',price);

运行结果为:
多变量线性回归结果

0 0