📄 logitregkernel.m
字号:
function [Y_compute, Y_prob] = LogitRegKernel(para, X_train, Y_train, X_test, Y_test, num_class)
global temp_model_file preprocess;
Y_compute = zeros(size(Y_test)); Y_prob = zeros(size(Y_test));
if (num_class > 2)
error('LogitRegKernel: The class number is larger than 2!');
end;
class_set = GetClassSet(Y_train);
p = str2num(char(ParseParameter(para, {'-Kernel';'-KernelParam'; '-RegFactor'; '-PairFactor'; '-CostFactor'}, {'0';'0.05';'1';'1';'1'}, 1)));
kerneltype = p(1);
kernelpara = p(2);
regftr = p(3);
pairftr = p(4);
costfactor = p(5);
X_train_ext = [X_train ones(size(X_train, 1), 1)];
X_train_ext = X_train_ext(1:size(X_train_ext, 1), :);
Y_train = Y_train(1:size(Y_train, 1), :);
X_test_ext = [X_test ones(size(X_test, 1), 1)];
ConPair1 = []; ConPair2 = []; LabelPair = [];
if (preprocess.ConstraintAvailable == 1) & (preprocess.ShotAvailable == 1),
one_array = ones(sum(preprocess.constraintUsed == 1), 1);
ConPair1 = [preprocess.ConPair1(preprocess.constraintUsed == 1, :) one_array];
ConPair2 = [preprocess.ConPair2(preprocess.constraintUsed == 1, :) one_array];
LabelPair = preprocess.LabelPair(preprocess.constraintUsed == 1);
end;
X_ext = [X_train_ext; ConPair1; ConPair2];
Logit_Y_prob = zeros(size(X_test, 1), 1);
beta = [];
if (~isempty(X_train)),
% Convert the binary labels into +/-1
Y_train = (Y_train == class_set(1)) - (Y_train ~= class_set(1));
beta = ordinalKernel(Y_train, X_train_ext, LabelPair, ConPair1, ConPair2, kerneltype, kernelpara, regftr, pairftr, costfactor);
fid = fopen(temp_model_file, 'w');
if (fid > 0),
fprintf('Writing to %s .... \n', temp_model_file);
fprintf(fid, 'File: %s\n', preprocess.input_file);
fprintf(fid, 'N: %d\n', size(Y_train, 1));
fprintf(fid, '%d ', class_set); fprintf(fid, '\n');
format_str = '';
for i = 1:size(X_ext,2)+1, format_str = strcat(format_str, '%f,'); end;
format_str = strcat(format_str, '\n');
fprintf(fid, format_str, [beta X_ext]');
fclose(fid);
end;
else
fid = fopen(temp_model_file, 'r');
if (fid > 0),
fgets(fid);
line = fgetl(fid); num = sscanf(line, 'N: %d');
line = fgetl(fid); class_set = sscanf(line, '%d');
input = fscanf(fid, '%f,');
input = reshape(input, [], num)';
beta = input(:, 1); X_ext = input(:, 2:size(input, 2));
fclose(fid);
preprocess.ClassSet = class_set;
end;
end;
Logit_Y_prob = ordinalKernelPredict(beta, X_ext, X_test_ext, kerneltype, kernelpara);
% Y_prob = (exp(Logit_Y_prob) ./ (1 + exp(Logit_Y_prob))) .* (Logit_Y_prob >= 0) + (1 ./ (1 + exp(Logit_Y_prob))) .* (Logit_Y_prob < 0);
Y_prob = exp(Logit_Y_prob) ./ (1 + exp(Logit_Y_prob));
Y_compute = class_set(1) * (Logit_Y_prob >= 0) + class_set(2) * (Logit_Y_prob < 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -