matlab 10折交叉验证实例,如何在不使用MATLAB中的内置函数的情况下进行10倍交叉验证?(How to do a 10-fold cross validation without using...

该博客讨论如何在MATLAB中不使用内置函数进行10折交叉验证,特别是针对数字识别任务。作者试图对500张图片样本(每种数字50张)进行训练和测试,但遇到了如何正确调整训练和测试集的问题。解答者提供了一个改进的循环结构,通过计算训练和测试的子集,避免了使用if语句。代码示例展示了如何生成训练和测试索引,以及如何在每个折叠中进行训练和验证。
摘要由CSDN通过智能技术生成

如何在不使用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()返回的训练有

...

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Matlab使用sklearn库可以方便地进行随机森林模型的构建和交叉验证,并使用feature_importances属性计算影响因素。具体步骤如下: 1. 准备数据集:将数据集拆分为训练集和测试集,其训练集用于进行交叉验证,测试集用于评估模型的性能。 2. 构建随机森林模型:使用sklearn库的RandomForestRegressor或RandomForestClassifier函数构建随机森林模型。其,可以设置的参数包括树的数量、每棵树随机选取的特征数量、叶节点最小样本数等。 3. 进行交叉验证使用sklearn库的KFold函数将训练集拆分为10份,进行10折交叉验证。在每一次交叉验证使用fit函数训练随机森林模型,并使用predict函数预测测试集的结果。 4. 计算影响因素:使用feature_importances属性可以计算每个特征对模型预测结果的影响程度。在进行交叉验证时,需要将每次训练得到的模型的影响因素加权平均,得到最终的影响因素。 5. 评估模型性能:使用测试集来评估模型的性能,可以计算出模型的准确率、精确率、召回率等指标。 下面是一个示例代码,用于演示如何在Matlab使用sklearn库构建随机森林模型,并进行10折交叉验证,并计算影响因素: ```matlab % 准备数据集 load fisheriris X = meas; y = species; rng(1); % 设置随机种子 cv = cvpartition(y,'HoldOut',0.3); Xtrain = X(cv.training,:); ytrain = y(cv.training,:); Xtest = X(cv.test,:); ytest = y(cv.test,:); % 构建随机森林模型 mdl = fitensemble(Xtrain,ytrain,'RandomForest',100,'Tree',... 'Type','Classification','PredictorNames',{'SepalLength','SepalWidth','PetalLength','PetalWidth'}); % 进行交叉验证 k = 10; % 设置折数 cv = cvpartition(size(Xtrain,1),'KFold',k); importance = zeros(size(Xtrain,2),1); for i = 1:k Xtrain_cv = Xtrain(cv.training(i),:); ytrain_cv = ytrain(cv.training(i)); Xtest_cv = Xtrain(cv.test(i),:); ytest_cv = ytrain(cv.test(i)); mdl_cv = fitensemble(Xtrain_cv,ytrain_cv,'RandomForest',100,'Tree',... 'Type','Classification','PredictorNames',{'SepalLength','SepalWidth','PetalLength','PetalWidth'}); yfit_cv = predict(mdl_cv,Xtest_cv); importance = importance + mdl_cv.FeatureImportance/k; end % 计算影响因素 importance = mdl.FeatureImportance; % 评估模型性能 yfit = predict(mdl,Xtest); accuracy = sum(strcmp(ytest,yfit))/length(ytest); ``` 在上述代码,我们使用fitensemble函数构建随机森林模型,并设置树的数量为100,随机选取的特征数量为默认值,即sqrt(p)。然后使用KFold函数将训练集拆分为10份,进行10折交叉验证。在每一次交叉验证使用fit函数训练随机森林模型,并使用predict函数预测测试集的结果。最后使用FeatureImportance属性计算每个特征的重要性,将每次训练得到的模型的影响因素加权平均得到最终的影响因素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值