forked from alex-ritchie/LSPCA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
center_data.m
28 lines (23 loc) · 1007 Bytes
/
center_data.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function [Xtrain,Xtest,Ytrain,Ytest] = center_data(Xtrain,Xtest,Ytrain,Ytest,type)
[ntrain,~] = size(Xtrain);
[ntest,~] = size(Xtest);
% properly normalize the training data
Xtrainmu = mean(Xtrain);
Xtrainstd = std(Xtrain);
Xtrainstd(Xtrainstd==0) = 1; %dont normalize variance of constant features (should probably be removed from data)
Xtrain = Xtrain - ones(ntrain, 1)*Xtrainmu;
Xtrain = Xtrain ./ (ones(ntrain, 1)*Xtrainstd);
%apply the same normalization to the testing data
Xtest = Xtest - ones(ntest, 1)*Xtrainmu;
Xtest = Xtest ./ (ones(ntest, 1)*Xtrainstd);
% normalize Y if doing regression
if strcmp(type, 'regression')
Ytrainmu = mean(Ytrain);
Ytrainstd = std(Ytrain);
Ytrainstd(Ytrainstd==0); %dont normalize variance of constant labels (should probably be removed from data)
Ytrain = Ytrain - ones(ntrain, 1)*Ytrainmu;
Ytrain = Ytrain ./ (ones(ntrain, 1)*Ytrainstd);
Ytest = Ytest - ones(ntest, 1)*Ytrainmu;
Ytest = Ytest ./ (ones(ntest, 1)*Ytrainstd);
end
end