X=data(:,1);
y=data(:,2);
pos0=find(y==0);
pos1=find(y==1);
x_pos=1:size(y);
hold on
plot(X(pos0,:),y(pos0,:),'ro','LineWidth',2,'MarkerSize',7);
plot(X(pos1,:),y(pos1,:),'k+','LineWidth',2,'MarkerSize',7);
%一元线性回归的w和b只是标量,可以直接用求导后的形式表示出来
x_u=sum(X,1)/m;
w=sum(y.*(X-x_u))/(sum(X.*X)-1/m*(sum(X,1)*sum(X,1)));
b=1/m*(sum(y-w.*X));
%做预测
y_hat=w*X+b;plot(X,y_hat,'g-','LineWidth',2,'MarkerSize',7);
y_0=find(y_hat<0.5);
y_1=find(y_hat>=0.5);
y_hat2=y_hat;
y_hat2(y_0)=0;
y_hat2(y_1)=1;
accuracy=size(find(y_hat2==y),1)/m;
一元线性回归的测试数据(Copy_of_data.mat)如下链接:https://download.****.net/download/qq_34911780/10313609