-
Notifications
You must be signed in to change notification settings - Fork 4
/
PartitionX_v1.m
53 lines (40 loc) · 1.17 KB
/
PartitionX_v1.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function [Xtrain,Xtest,WS,DS,WordTrainS,DocTrainS] = PartitionX_v1(X,TrainPercentage)
[wi,di,cc]=find(X);
wdi=find(X>0);
ii=zeros(sum(cc),1);
jj=zeros(sum(cc),1);
kk=zeros(sum(cc),1);
count=0;
for i=1:length(wdi)
ii(count+1:count+cc(i))=wi(i);
jj(count+1:count+cc(i))=di(i);
count=count+cc(i);
end
WS=ii;
DS=jj;
[P,N] = size(X);
TestLen = N;
dex = 1:N;
WordTrainS = true(size(DS));
DocTrainS = true(size(DS));
if TrainPercentage < 1.0
for ii = 1:TestLen
blockdex = find(DS==dex(ii));
if ~isempty(blockdex)
DocTrainS(blockdex)=false;
aa = randperm(length(blockdex));
TestStartLocation = max(round(length(blockdex)*TrainPercentage)+1,2);
if TestStartLocation==1
TestStartLocation=2;
end
WordTrainS(blockdex(aa(TestStartLocation:end))) = false;
% WordTrainS(blockdex(aa(ceil(length(blockdex)*0.2):end))) = false;
end
end
Xtrain = sparse(WS(WordTrainS),DS(WordTrainS),1,P,N);
Xtest = sparse(WS(~WordTrainS),DS(~WordTrainS),1,P,N);
DocTrainS = WordTrainS;
else
Xtrain = X;
Xtest = [];
end