哈雷双灯滑翔cvo:使用交叉验证和F1分数选择SVM参数

我需要跟踪 F1 分数,同时在 SVM 中调整 C & amp;Sigma,例如下面的代码跟踪准确性,我需要将其更改为 F1-Score,但我无法做到这一点......

%# read some training data
[labels,data] = libsvmread('./heart_scale');
%# grid of parameters
folds = 5;
[C,gamma] = meshgrid(-5:2:15, -15:2:3);
%# grid search, and cross-validation
cv_acc = zeros(numel(C),1);
    for i=1:numel(C)
cv_acc(i) = svmtrain(labels, data, ...
                sprintf('-c %f -g %f -v %d', 2^C(i), 2^gamma(i), folds));
end
%# pair (C,gamma) with best accuracy
[~,idx] = max(cv_acc);
%# now you can train you model using best_C and best_gamma
best_C = 2^C(idx);
best_gamma = 2^gamma(idx);
%# ...

我看到了以下两个链接

Retraining after Cross Validation with libsvm 10 fold cross-validation in one-against-all SVM (using LibSVM)

我明白,我必须首先在训练数据上找到最佳的 C 和 gamma / sigma 参数,然后使用这两个值做一个 LEE-ONE-OUT 交叉验证分类实验,所以我现在想要的是首先做一个网格搜索来调整 C & amp;sigma。请我更喜欢使用 MATLAB-SVM 而不是 LIBSVM。下面是我的 LEE-ONE-OUT 交叉验证分类代码。

... clc
 clear all
close all
a = load('V1.csv');
X = double(a(:,1:12));
Y = double(a(:,13));
% train data
datall=[X,Y];
A=datall;
n = 40;
ordering = randperm(n);
B = A(ordering, :);  
good=B; 
input=good(:,1:12);
target=good(:,13);
CVO = cvpartition(target,'leaveout',1);  
cp = clperf(target);                      %# init performance tracker
svmModel=[];
for i = 1:CVO.NumTestSets                                %# for each fold
trIdx = CVO.training(i);              
teIdx = CVO.test(i);                   
%# train an SVM model over training instances
svmModel = svmtrain(input(trIdx,:), target(trIdx), ...
       'Autoscale',true, 'Showplot',false, 'Method','ls', ...
      'BoxConstraint',0.1, 'Kernel_Function','rbf', 'RBF_Sigma',0.1);
%# test using test instances
pred = svmclify(svmModel, input(teIdx,:), 'Showplot',false);
%# evaluate and update performance object
cp = clperf(cp, pred, teIdx); 
end
%# get accuracy
accuracy=cp.CorrectRate*100
sensitivity=cp.Sensitivity*100
specificity=cp.Specificity*100
PPV=cp.PositivePredictiveValue*100
NPV=cp.NegativePredictiveValue*100
%# get confusion matrix
%# columns:actual, rows:predicted, last-row: unclified instances
cp.CountingMatrix
recallP = sensitivity;
recallN = specificity;
precisionP = PPV;
precisionN = NPV;
f1P = 2*((precisionP*recallP)/(precisionP + recallP));
f1N = 2*((precisionN*recallN)/(precisionN + recallN));
aF1 = ((f1P+f1N)/2);

我已经改变了 code,但我犯了一些错误,我得到的错误,

a = load('V1.csv');
X = double(a(:,1:12));
Y = double(a(:,13));
% train data
datall=[X,Y];
A=datall;
n = 40;
ordering = randperm(n);
B = A(ordering, :);  
good=B; 
inpt=good(:,1:12);
target=good(:,13);
k=10;
cvFolds = crossvalind('Kfold', target, k);   %# get indices of 10-fold CV
cp = clperf(target);                      %# init performance tracker
svmModel=[];
for i = 1:k 
    testIdx = (cvFolds == i);    %# get indices of test    instances
trainIdx = ~testIdx;   
C = 0.1:0.1:1; 
S = 0.1:0.1:1; 
fscores = zeros(numel(C), numel(S)); %// Pre-allocation
for c = 1:numel(C)   
for s = 1:numel(S)
    vals = crossval(@(XTRAIN, YTRAIN, XVAL, YVAL)(fun(XTRAIN, YTRAIN, XVAL, YVAL, C(c), S(c))),inpt(trainIdx,:),target(trainIdx));
    fscores(c,s) = mean(vals);
end
end
 end
[cbest, sbest] = find(fscores == max(fscores(:)));
C_final = C(cbest);
S_final = S(sbest);    

.......

功能……

.....
function fscore = fun(XTRAIN, YTRAIN, XVAL, YVAL, C, S)
svmModel = svmtrain(XTRAIN, YTRAIN, ...
   'Autoscale',true, 'Showplot',false, 'Method','ls', ...
  'BoxConstraint', C, 'Kernel_Function','rbf', 'RBF_Sigma', S);
   pred = svmclify(svmModel, XVAL, 'Showplot',false);
   cp = clperf(YVAL, pred)
   %# get accuracy
    accuracy=cp.CorrectRate*100
    sensitivity=cp.Sensitivity*100
    specificity=cp.Specificity*100
    PPV=cp.PositivePredictiveValue*100
    NPV=cp.NegativePredictiveValue*100
    %# get confusion matrix
    %# columns:actual, rows:predicted, last-row: unclified instances
    cp.CountingMatrix
    recallP = sensitivity;
    recallN = specificity;
    precisionP = PPV;
    precisionN = NPV;
    f1P = 2*((precisionP*recallP)/(precisionP + recallP));
    f1N = 2*((precisionN*recallN)/(precisionN + recallN));
    fscore = ((f1P+f1N)/2);
    end
1

所以基本想采取你的这一行:

svmModel = svmtrain(input(trIdx,:), target(trIdx), ...
       'Autoscale',true, 'Showplot',false, 'Method','ls', ...
      'BoxConstraint',0.1, 'Kernel_Function','rbf', 'RBF_Sigma',0.1);

把它放在一个循环中,改变你的'BoxConstraint''RBF_Sigma'参数,然后使用crossval输出参数迭代组合的 f1 分数。

您可以使用单个 for 循环,就像您的 libsvm 代码示例一样(即使用meshgrid1:numel(),这可能更快)或嵌套的 for 循环。

C = [0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30, 100, 300] %// you must choose your own set of values for the parameters that you want to test. You can either do it this way by explicitly typing out a list
S = 0:0.1:1 %// or you can do it this way using the : operator
fscores = zeros(numel(C), numel(S)); %// Pre-allocation
for c = 1:numel(C)   
    for s = 1:numel(S)
        vals = crossval(@(XTRAIN, YTRAIN, XVAL, YVAL)(fun(XTRAIN, YTRAIN, XVAL, YVAL, C(c), S(c)),input(trIdx,:),target(trIdx));
        fscores(c,s) = mean(vals);
    end
end
%// Then establish the C and S that gave you the bet f-score. Don't forget that c and s are just indexes though!
[cbest, sbest] = find(fscores == max(fscores(:)));
C_final = C(cbest);
S_final = S(sbest);

现在我们只需要定义函数fun。文档对fun有这样的说法:

fun 是具有两个输入的函数的函数句柄,即 X 的训练子集 XTRAIN 和 X 的测试子集 XTEST,如下所示:

testval = fun(XTRAIN,XTEST)每次调用时,fun 都应该使用 XTRAIN 来拟合模型,然后使用该拟合模型返回在 XTEST 上计算的一些标准 testval。

所以fun需要:

输出单个 f-score

将 X 和 Y 的训练和测试集作为输入。请注意,这些都是您实际训练集的子集!将它们更像是您训练集的训练和验证子集。还要注意,crossval 将为您拆分这些集!

在训练子集上训练分类器(使用循环中的当前CS参数)

在测试(或验证)子集上运行新的分类器

计算并输出一个性能指标(在你的情况下,你想要 f1 分数)

您会注意到fun不能接受任何额外的参数,这就是为什么我将它包装在一个匿名函数中,以便我们可以传递当前的CS值。(即上面的所有@(...)(fun(...))东西。这只是将我们的六个参数fun“转换”为所需的 4 个参数

function fscore = fun(XTRAIN, YTRAIN, XVAL, YVAL, C, S)
   svmModel = svmtrain(XTRAIN, YTRAIN, ...
       'Autoscale',true, 'Showplot',false, 'Method','ls', ...
      'BoxConstraint', C, 'Kernel_Function','rbf', 'RBF_Sigma', S);
   pred = svmclify(svmModel, XVAL, 'Showplot',false);
   CP = clperf(YVAL, pred)
   fscore = ... %// You can do this bit the same way you did earlier
end
0

我发现了target(trainIdx)的唯一问题。这是一个行向量,所以我只是用target(trainIdx)替换了target(trainIdx),这是一个列向量。

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(168)
C Event:C# 事件继承(one x event)
上一篇
漏电保护器c20和c32:C++17和C++ 20库兼容性(c++17 vs c++20)
下一篇

相关推荐

  • linux切换到超级用户:```shecho 'Hello World' > hello_world.txt```

    Linux切换到超级用户的方法有两种:使用su命令:…

    2023-03-24 09:03:15
    0 70 84
  • cvt变速箱结构图解:CVT变速箱的工作原理

    CVT变速箱是一种无级变速箱,它的结构由两个部分组成:输入轴和输出轴。输入轴由发动机驱动,输出轴将变速箱的输出转移到车轮上。输入轴上安装有一个可变的滑轮,它可以改变输入轴的传动比,从而改变变速箱的输出比。另外,输入轴上还安装有一个液压系统,它可以控制滑轮的位置,从而改变输入轴的传动比。…

    2023-04-02 04:00:04
    0 85 81
  • android cursor遍历 title = row[0] value = row[1] dic[titl

    示例示例Android中使用Cursor遍历数据库的步骤如下:获取一个Cursor对象,通过的()方法来获取;…

    2023-05-06 13:38:40
    0 51 15
  • cv树洞柒夜事件CV树洞中的秘密与惊奇

    cv树洞柒夜事件是一个有趣的计算机视觉活动,它提供了一种新的方式来利用计算机视觉技术来探索和发现景观。它始于2020年7月,由一群热爱计算机视觉的研究者和开发者发起,他们希望通过使用机器学习技术来探索景观中的精彩之处。cv树洞柒夜事件的目标是使用机器学习技术来探索景观中的精彩之处,并利用这些发现来改善景观设计。参与者需要使用机器学习技术来探索景观,并利用这些发现来改善景观设计。参与者可以使用Python,OpenCV,TensorFlow等技术来完成任务,并将其发布在GitHub上。…

    2023-01-09 12:42:42
    0 31 47
  • javascript:void什么意思:无效的JavaScript函数

    javascript:void是一个JavaScript语句,它的作用是返回undefined的值。它可以用来替代不需要返回值的函数或表达式,也可以在页面中添加一个空链接,使其不会跳转到其他页面。…

    2023-04-13 05:37:41
    0 67 89
  • win10怎么cmd打开设置:如何在Windows 10中使用CMD打开设置

    打开“开始”菜单,点击搜索框,输入cmd,然后按下回车键。在弹出的命令提示符窗口中,输入以下代码:start ms-:…

    2023-06-07 01:59:36
    0 79 39
  • java hashcode方法:Unlocking the Power of Java Hashcode for Maximum

    Java中的hashCode()方法是Object类中定义的一个方法,它的作用是返回一个对象的哈希码值。哈希码是一个整数,它是根据对象的地址或者存储在对象中的某些值来计算出来的。…

    2023-05-11 09:16:15
    0 18 56
  • java protected关键字:使用protected关键字保护类成员的优点

    示例示例关键字是java中的修饰符,它可以修饰类、变量和方法。修饰类:修饰的类只能在同一个包内被访问,如果子类继承了修饰的父类,则子类可以在不同包内访问父类的成员。…

    2023-04-18 04:45:37
    0 31 55

发表评论

登录 后才能评论

评论列表(53条)