分类器交叉验证java_使用交叉验证和F1分数选择SVM参数
我需要在SVM中调整C&Sigma时跟踪F1分数,例如以下代码跟踪准确度,我需要将其更改为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);
%# ...
我看过以下两个链接
我知道我必须首先在训练数据中找到最佳的C和gamma / sigma参数,然后使用这两个值进行LEAVE-ONE-OUT交叉验证分类实验,所以我现在想要的是先做一个网格 - 调整C&sigma的研究 . 请我更喜欢使用MATLAB-SVM而不是LIBSVM . 以下是我的LEAVE-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 = classperf(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 = svmclassify(svmModel, input(teIdx,:), 'Showplot',false);
%# evaluate and update performance object
cp = classperf(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: unclassified 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);
我已经改变了代码,但我犯了一些错误,我收到了错误,
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 = classperf(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 = svmclassify(svmModel, XVAL, 'Showplot',false);
cp = classperf(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: unclassified 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
分类器交叉验证java_使用交叉验证和F1分数选择SVM参数相关推荐
- 分类器交叉验证java_使用交叉验证的KNN分类器
首先,您需要准确定义您的任务 . F.ex给出R ^(MxN)中的图像I,我们希望将I分类为包含面部的图像或没有面部的图像 . 我经常使用像素分类器,其任务类似于:对于图像,我决定每个像素是面像素还是 ...
- 机器学习之选择小样本交叉验证训练模型并使用精确率、召回率、F1分数和AUC值、画出ROC曲线评估
题目如下 (1)读入aviation数据集,设置MEMBER_NO为索引列:(4分) (2)剔除重复值.缺失值.(4分) (3)随机抽取500样本,切片特征X和标签Y:(4分) 因为调参很慢,所以这里 ...
- 交叉验证方法汇总【附代码】(留一法、K折交叉验证、分层交叉验证、对抗验证、时间序列交叉验证)
目录 交叉验证是什么? 留一验证(LOOCV,Leave one out cross validation ) LOOCC代码 验证集方法 验证集方法代码 K折交叉验证(k-fold cross va ...
- ssl 客户端证书验证_SSL客户端身份验证:这是信任的问题
ssl 客户端证书验证 [编者注:本文介绍了如何为Domino 4.6和4.6.1设置SSL客户机认证.] 互联网上最新的行业流行词是SSL. 但是SSL真正需要提供什么? 安全套接字层(SSL)是一 ...
- Python使用matplotlib可视化两个时间序列的交叉相关性图、交叉相关图显示了两个时间序列之间的滞后性(Cross Correlation plot)
Python使用matplotlib可视化两个时间序列的交叉相关性图.交叉相关图显示了两个时间序列之间的滞后性(Cross Correlation plot) 目录
- 伤感网络验证系统_可验证云数据库架构与设计
区块链的发展到了一个关键阶段.向左走,是一眼望不到尽头的公链和交易所.向右走,是一脸茫然的探寻:区块链如何和古典互联网行业相结合.就像文章<货币.区块链和社交扩展性>所阐述的根本原理,区块 ...
- 域的非验证方式还原与验证方式还原的区别
救急:一台服务器要重装系统,怎么份域用户信息 在Windows2000中,备份与恢复Active Directory是一项非常重要的工作.在NT中,所有有关用户和企业配置方面的信息都存储在注册表中,因 ...
- ASP.NET MVC如何实现自定义验证(服务端验证+客户端验证)
ASP.NET MVC通过Model验证帮助我们很容易的实现对数据的验证,在默认的情况下,基于ValidationAttribute的声明是验证被使用,我们只需要将相应的ValidationAttri ...
- 怎么看python环境变量配置是否好了验证图片_简述验证Anaconda是否安装成功的两种方式和Anaconda环境变量配置过程...
加个"星标",每天一起快乐的学习 今 日 鸡 汤 闻道欲来相问讯,西楼望月几回圆. /1 前言/ 大家好,我是 Rocky0429,今天我给大家介绍一下验证Anaconda是否安装 ...
- php ci提交表单验证,ci表单验证代码
概述 这是只考虑php对表单数据的接收处理.至于js部分以前会结合validate来讲解下 在解释 CodeIgniter 的数据验证处理之前,让我们先描述一下一般的情况: 一个表单显示了. 你填写并 ...
最新文章
- C++类为什么使用private?------封装性
- Autism Course of Yale University Fred Volkman 2
- linux zlib简介
- 交换机的4种网络结构方式你知道是什么吗?
- Linux GDB调试
- html中字段是日期控件,jQuery日历插件datepicker用法详解
- 转: CentOS 安装 SVN1.8 客户端
- python符号表_根据第一个字符分割列表-Python
- idea 2017 常用图标
- 最简单的DLL导出函数隐藏方式
- android Paddle 视频字幕识别TTS语音
- linux下c语言读写文件操作,Linux下C语言之文件操作
- 以太网转DMX模块核心模块 支持ArtNet sACN DMX RDM
- fastjson 是反射吗_6种超声检测灵敏度,你都知道吗?
- 手机lightroom预设位置
- 1号店详情页(共5页)
- 银屑病推荐益生菌摄入(持续更新中)
- html转盘游戏,使用Javascript和CSS3实现一个转盘小游戏(实战篇)
- 夜来风雨声,“网络”知多少?
- Howler.js:用于现代Web的音频库