function [y_predict,Metric] = Pairwise_kNNv2(X_train,X_test,y_train,y_test,k,u,numLabel,theta)
%PAIRWISE_KNNV2 此处显示有关此函数的摘要
%   Compare approach with MLKNN
%   此处显示详细说明
    %basic functions called by CLIM
    addpath('basic_functions');
    %optimization function
    addpath('minFunc');
    addpath('minFunc/compiled');

    write_file.file_id = 1;%standard output (the screen)
    write_file.head_str = '   ';
    write_file.verbose = 1;%with outputs
    
    num_train = size(X_train,1);
    num_test = size(X_test,1);
    % X_train_aug = X_train;
    % X_test_aug = X_test;
    % for r = 1:numel(numLabel)-1
    %     rs = sum(numLabel(1:r-1))+1;
    %     re = sum(numLabel(1:r));
    %     y_train_r = y_train(:,rs:re);
    %     y_test_r = y_test(:,rs:re);
    %     for s = r+1:numel(numLabel)
    %         num_dim = numLabel(r)+numLabel(s);
    %         ss = sum(numLabel(1:s-1))+1;
    %         se = sum(numLabel(1:s));
    %         y_train_s = y_train(:,ss:se);
    %         y_test_s = y_test(:,ss:se);
    %         y_train_rs = [y_train_r,y_train_s];
    %         y_test_rs = [y_test_r,y_test_s];
    %         [X_train_stats,X_test_stats,X_train_idx_rs,X_test_idx_rs] = kNN_stats(X_train,y_train_rs,X_test,k,numLabel([r,s]));
    %         % X_train_stats = normalize(k-X_train_stats);
    %         % X_test_stats = normalize(k-X_test_stats);
    %         % X_train_aug = [X_train_aug,X_train_stats];
    %         % X_test_aug = [X_test_aug,X_test_stats];
    %     end
    % end
    [y_predict_real,~] = firstPre(X_train,y_train,numLabel,2^-3,write_file);
    [X_train_stats_all,kNN_train_idx_all]...
        = kNN_stats(y_predict_real,y_predict_real,y_train,k,numLabel);
    [X_test_stats_all,kNN_test_idx_all]...
        = kNN_stats(X_train,X_test,y_train,k,numLabel);
    num_dim = sum(numLabel,1);
    prior = zeros(num_dim,1);
    nprior = zeros(num_dim,1);
    for l = 1:num_dim
        temp_Cl = sum(y_train(:,l)==ones(num_train,1));
        prior(l,1) = (u+temp_Cl)/(2*u+num_train);
        nprior(l,1) = 1-prior(l,1);
    end
    temp_Ci = zeros(num_dim,k+1);
    temp_nCi = zeros(num_dim,k+1);
    for x = 1:num_train
        temp = zeros(1,num_dim);
        neighbor_labels = [];
        for j = 1:k
            neighbor_labels = [neighbor_labels, y_train(kNN_train_idx_all(x,j),:)'];
        end
        for j = 1:num_dim
            temp(1,j) = sum(neighbor_labels(j,:)==ones(1,k));
        end
        for j = 1:num_dim
            if (y_train(x,j)==1)
                temp_Ci(j,temp(j)+1) = temp_Ci(j,temp(j)+1)+1;
            else
                temp_nCi(j,temp(j)+1) = temp_nCi(j,temp(j)+1)+1;
            end
        end
    end
    for i = 1:num_dim
        temp1 = sum(temp_Ci(i,:));
        temp2 = sum(temp_nCi(i,:));
        for j = 1:k+1
            post(i,j) = (u+temp_Ci(i,j))/(u*(k+1)+temp1);
            npost(i,j) = (u+temp_nCi(i,j))/(u*(k+1)+temp2);
        end
    end
    % computing outputs
    outputs = zeros(num_dim,num_test);
    for t = 1:num_test
        temp = zeros(1,num_dim);
        neighbor_labels = [];
        for j = 1:k
            neighbor_labels = [neighbor_labels, y_train(kNN_test_idx_all(t,j),:)'];
        end
        for j = 1:num_dim
            temp(1,j) = sum(neighbor_labels(j,:)==ones(1,k));
        end
        for j = 1:num_dim
            prob_in = prior(j)*post(j,temp(1,j)+1);
            prob_out = nprior(j)*npost(j,temp(1,j)+1);
            if (prob_in+prob_out==0)
                outputs(j,t) = prior(j);
            else
                outputs(j,t) = prob_in/(prob_in+prob_out);
            end
        end
    end
    y_predict = outputs';
    y_predict_b = y_predict;
    y_predict_b(y_predict_b>=theta) = 1;
    y_predict_b(y_predict_b<theta) = 0;
    Metric = MDML_metric(y_predict,y_test,y_predict_b,numLabel);
end

