如何在不使用MATLAB中的内置函数的情况下进行10倍交叉验证?(How to do a 10-fold cross validation without using built-in function in MATLAB?)
我尝试进行10倍交叉验证而不使用内置函数来训练和识别0-9的数字我有500张图片的样本(每个数字50个训练和测试。)我尝试实现答案MATLAB:10折交叉验证不使用现有功能和其他网站,但它没有那么多帮助。 主要是因为我是MATLAB的新手所以我不太了解我应该做些什么来调整它。 这是迄今为止的代码。
c=zeros(10,size(x,2),size(x,3));
K=10;
k=10;
test= 1:50/K;
for fold =1:K
if(test(1)~=1)
train = x(1:test(1)-1,:,:);
if (test(5) ~=50)
train=[train ; x(test(end):50,:,:)];
end
else
train = x(test(1):50,:,:);
end
test = test+ones(1,50/K)*50/K;
end
for i =0:9
test=test+50/K*ones(1,5);
c(i+1,:,:)=cal_likelihood(x(1+i*50:50+i*50,:,:),50/k*(k-1));
end
变量说明
x是500x28x28的双倍,它保留所有500位数字的图片。
test是一个测试集。
火车是训练集。
为了进行10次交叉验证,我需要更改训练集
第1折:1:5用于测试,6:45用于火车
第2次折叠:6:10进行测试,1:5 11:50进行火车等等
问题是我不知道如何将训练集从一组转移到另一组,如从6:45到1:5和11:50。 或者我可以写一个比这更好的循环吗?
PSS。 如果有人回答这个问题,请不要介意500x28x28 double实际意味着什么。
I try to do a 10 folds cross validation without using built-in function to train and recognize digit from 0-9 I have sample of 500 picture(50 for each digit to train and test.) I try implement the answer MATLAB: 10 fold cross Validation without using existing functions and other websites but it didn't help that much. Mostly because I'm new to MATLAB so I don't know much about what I should do to tweak it. This is the code I have so far.
c=zeros(10,size(x,2),size(x,3));
K=10;
k=10;
test= 1:50/K;
for fold =1:K
if(test(1)~=1)
train = x(1:test(1)-1,:,:);
if (test(5) ~=50)
train=[train ; x(test(end):50,:,:)];
end
else
train = x(test(1):50,:,:);
end
test = test+ones(1,50/K)*50/K;
end
for i =0:9
test=test+50/K*ones(1,5);
c(i+1,:,:)=cal_likelihood(x(1+i*50:50+i*50,:,:),50/k*(k-1));
end
Variable explanation
x is the 500x28x28 double where it keeps all 500 digit picture.
test is a test set.
train is a training set.
In order to do 10 folds cross validation I need to change training set like
1st fold : 1:5 for test,6:45 for train
2nd fold : 6:10 for test,1:5 11:50 for train and so on
The problem is I don't know how to shift the training set from one set to another like from 6:45 to 1:5 and 11:50. or Can I write a better loop than this?
PSS. If someone who answer this don't mind What does 500x28x28 double actually mean.
原文:https://stackoverflow.com/questions/39591173
更新时间:2019-09-12 14:55
最满意答案
有几种方法可以写出来,其中一些比其他方法更容易理解。 Matlab非常适合写入,而1:3等表达式求值为[1,2,3] ,表达式1:0求值为空集。 因此,在不必使用if语句的情况下生成集合非常简单。
我开始循环:
samples_per_digit=50;
block_sze=samples_per_digit/K;
for fold =1:K
test_ind = 1+(fold-1)*block_sze:fold*block_sze;
train_ind = [1:(fold-1)*block_sze, (fold*block_sze+1):samples_per_digit];
for i=0:9
train=x(train_ind+i*samples_per_digit,:,:);
test=x(test_ind+i*samples_per_digit,:,:);
% Perform training and validation in here for this fold of the digit i
您可以验证test_ind和train_ind是否与您需要的培训和验证块的子集相对应。 只有在最里面的循环中,这些转化为对应于数字图像的矩阵,使用i的值来计算偏移量。 当然,如果你愿意,你可以交换循环的顺序,计算一个数字的所有折叠。 这一切都取决于您希望如何存储结果。
There are a few ways you could write this, some of which are easier to understand than others. Matlab is quite nice to write in as while expressions such as 1:3 evaluate to [1,2,3], the expression 1:0 evaluates to the empty set. So, it is very straightforward to generate the sets without having to use if statements.
I'd start off the loop as:
samples_per_digit=50;
block_sze=samples_per_digit/K;
for fold =1:K
test_ind = 1+(fold-1)*block_sze:fold*block_sze;
train_ind = [1:(fold-1)*block_sze, (fold*block_sze+1):samples_per_digit];
for i=0:9
train=x(train_ind+i*samples_per_digit,:,:);
test=x(test_ind+i*samples_per_digit,:,:);
% Perform training and validation in here for this fold of the digit i
You can verify that test_ind and train_ind correspond to the subsets of blocks of training and validation that you need. It is only in the innermost loop that these translate to the matrices corresponding to the digit images, using the value of i to compute the offset. Of course, if you wish, you can swap the order of the loops, computing all of the folds for a single digit. It all depends on how you wish to store your results.
2016-09-20
相关问答
它应该类似于优化函数,其中来自拟合函数fun的返回值应该表明它适合数据的程度。 正如文档所述, fun有两个参数,一个是训练数据集XTRAIN ,另一个是测试数据集XTEST 。 如果您的数据X包含一列已知结果X(:,1)和其他列X(:, 2:end) ,并使用XTRAIN训练您的数据,那么您的返回值可能与总和一样简单拟合模型的平方误差: testval = sum( (model(XTEST(:, 2:end)) - XTEST(:, 1)).^2 );
其中model(XTEST(:, 2:
...
您可以简单地使用crossvalind('Kfold', Group, K) ,其中Group是包含每个观察的类标签的向量。 这将导致每个组按比例丰富的集合。 You can simply use crossvalind('Kfold', Group, K), where Group is the vector containing the class label for each observation. This will lead to sets where each group is pr
...
没有可重复的例子有点困难,但我认为最后一行的返回共享是原因。 为了便于阅读,我重新格式化了您的代码 library(caret)
library(C5.0)
library(irr)
folds = createFolds(mdd.cohort1$edmsemmancomprej, k=10)
str(folds)
mdd.cohort1_train = mdd.cohort1[-folds$Fold01,]
mdd.cohort1_test = mdd.cohort1[folds$F
...
关于为每个子集执行代码,您可以将拟合放在循环中并存储结果,例如 % Sample the data
parm = [AT];
n = length(parm);
k = 10; % how many parts to use
allix = randperm(n); % all data indices, randomly ordered
numineach = ceil(n/k); % at least one part must have this ma
...
这是我对这个交叉验证的要求。 我使用魔法创建虚拟数据(10),我也随机创建标签。 想法在跟随,我们得到我们的数据和标签,并将它们与随机列结合起来。 考虑下面的虚拟代码。 >> data = magic(4)
data =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> dataRowNumber = size(data,1)
data
...
有几种方法可以写出来,其中一些比其他方法更容易理解。 Matlab非常适合写入,而1:3等表达式求值为[1,2,3] ,表达式1:0求值为空集。 因此,在不必使用if语句的情况下生成集合非常简单。 我开始循环: samples_per_digit=50;
block_sze=samples_per_digit/K;
for fold =1:K
test_ind = 1+(fold-1)*block_sze:fold*block_sze;
train_ind = [1:(fold-1
...
在libsvm中使用'-v 10'选项..它将为您进行交叉验证... use the '-v 10' option in libsvm.. it will do the cross validation for you...
据我所知,Weka(和其他评估方法)中的交叉验证仅用于估计泛化误差。 也就是说,(隐式)假设是您希望将学习的模型与您未提供给Weka的数据(也称为“验证集”)一起使用。 因此,您获得的模型将对整个数据进行培训。 在交叉验证期间,它会训练和评估许多不同的模型(在您的情况下为10),以估计学习模型的概括程度。 您实际上并未看到这些模型 - 它们仅在内部使用。 未评估显示的模型。 As far as I know, the cross-validation in Weka (and the other
...
cvi是一个列表对象,所以cvi[-1]和cvi[1]是列表对象,然后你尝试使用列表对象得到x[cvi[-1]] ,这是没有意义的,因为列表对象可以是包含数字,字符,日期和其他列表的复杂对象。 订阅带有单个方括号的列表始终返回一个列表。 使用双方括号来获得成分,在这种情况下是向量。 > cvi[1] # this is a list with one element
$V1
[1] 101 78 231 82 211 239 20 201 294 276 181 168 207 240
...
关于第一个问题。 将"CrossVal"设置为"on"并将训练后的模型提取到crossval()函数都旨在实现同样的目标。 你可以使用其中一种,这取决于你。 kFoldLoss()是一个函数per-se,不包含在“CrossVal”标志中。 它将交叉验证的模型作为输入。 无论您是使用fitcsvm()的“CrossVal”标志还是使用正确的crossval()函数对此模型进行交叉验证。 必须使用此功能是否要评估错误率。 关于第二个问题,简短的回答是肯定的。 您必须使用fitcsvm()返回的训练有
...