Linear regression - How to do Testing and training data.
14 views (last 30 days)
Show older comments
I'm trying to read from the testing and training data. But I can't make the code to do that. I'm new to linear regression. I try to implement some code from Matwork but no success yet.
clc;
clear all;
%x=xlsread('Data Mar-26-14 Time 1533-21.txt');
%x = fileread('Data Mar-26-14 Time 1533-21.txt');
myDir = uigetdir; %gets directory
myFiles = dir(fullfile(myDir,'*.txt')); %gets all text files in struct
%x = fileread('*.txt'); % File Name XXX.txt
%x = textscan(data, '%f%f' , 'HeaderLines',5); % To skip 5 Header line
ytrain=x(:,end); % Target variable
xtrain=zscore(x(:,1:end-1));% Normalized Predictors
p=length(x);
xtrain=[ones(length(xtrain),1) xtrain]; % one is added for calculation of biases.
xtest=xtrain;
ytest=ytrain;
%compute cost and gradient
iter=1000; % No. of iterations for weight updation
theta=zeros(size(xtrain,2),1); % Initial weights
alpha=0.1 % Learning parameter
[J grad h th]=cost(theta,xtrain,ytrain,alpha,iter) % Cost funtion
ypred=xtest*th; %target prediction
% probability calculation
[hp]=sigmoid(ypred); % Hypothesis Function
ypred(hp>=0.5)=1;
ypred(hp<0.5)=0;
% Decision Boundary
syms x1 x2
fnn=th(1)+th(2)*x1+th(3)*x2-0.5
figure
hold on
scatter(xtest(ytest==1,2),xtest(ytest==1,3),'b+','linewidth',5.0)
scatter(xtest(ytest==0,2),xtest(ytest==0,3),'r','linewidth',5.0)
h1=ezplot(fnn)
set(h1,'color','r')
legend('Pos class','Neg. class','Decision boundary')
xlim([-2 2])
ylim([-2 2])
function fscore=cmpt_multiclass_fisher(X,y)
uniclass = unique(y);
for k = 1:2
idxk = find( y==uniclass(k) ); nk = length(idxk);
Xk = X(idxk,:);
num(k,:) = nk.*( mean(Xk) - mean(X) ).^2;
den(k,:) = nk.*(var(Xk) );
end
fscore = sum(num) ./ sum(den);
end
0 Comments
Answers (0)
See Also
Categories
Find more on Linear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!