Space In HIndi

 


Pages

Popular

LINEAR REGRESSION EXP-3

 EXPERIMENT NO3 :LINEAR REGRESSION

 

clear all;
clc;
%Load Data from Text file
%data=load('data.txt');
data=[166 54;195 82;200 72;260 72;265 90;335 124;370 94;450 118;300 105;400 110];
%Define x and y
x=data(:,1);
y=data(:,2);
%Plot the data
plot(x,y,'r*')
xlabel('Number of Pages') %Set x axis label
ylabel('Cost of Book') %Set y axis label
% count the no of data points
m=length(x);
%add columns of all ones(intercept term)to x
X=[ones(m,1) x];
%calculate theta
%theta1=(pinv(X'*X))
%theta2=(pinv(X'*X))*X'
theta=(pinv(X'*X))*X'*y
a=X*theta
%Plot the fitted Equation we got from regression
hold on %This keeps our previous plot training data visible
plot(X(:,2),X*theta,'g-')
legend('Training Data','Linear Regression');
hold off % Dont put any more plot on this figure

test_data=[110 55;105 54];
%% Mean Square Error
predicted_values=[ones(2,1) test_data(:,1)]*theta
mse1=sqrt(mean((predicted_values-test_data(:,2)).^2))

OUTPUT
theta =
33.4221
0.1995



No comments: