%i,j,p
%i
%The rec_id of a recording in the test set. Only include predictions for recordings in the test set.
%j
%The species/class #. For each rec_id, there should be 19 lines for species 0 through 18.
%p
%Your classifier's prediction about the probability that species j is present in rec_id i. This must be in the range [0,1].


load('birds20130709.mat');

ixtrain = find(J == 0);
ixtest = find(J == 1);

z=a(ixtest,:);
[bags, labs, bagid] = getbags(z);



C = 19;
predictions = nan(length(bagid), 19);


u = milvector([],'e')*scalem([],'variance')*loglc2*classc;

%For each class, train a classifier
for i=1:C
    a = changelablist(a,i+1);  %Bird 1 is lablist 2, etc
    x = a(ixtrain, :);
    z = a(ixtest,:);
    
    w = x*u;
    out = z*w;
    
    try
        testc(out,'auc')
    catch
    end
            
    predictions(:,i) = out(:,1);
end






%Write everything to a CSV file


fid = fopen('pred20130709.csv', 'w');
fprintf(fid, '%s,%s,%s\n', 'rec_id', 'species', 'probability');

for i=1:length(bagid)
        
    for j=1:C
        if (i==length(bagid) && j==C)
            fprintf(fid, '%d,%d,%f', bagid(i), j-1, predictions(i,j));
        else
            fprintf(fid, '%d,%d,%f\n', bagid(i), j-1, predictions(i,j));
        end
    end
end
fclose(fid);
