【VC++开源代码栏目提醒】:网学会员VC++开源代码为您提供Weka[31] crossValidation源代码分析 - 综合课件参考,解决您在Weka[31] crossValidation源代码分析 - 综合课件学习中工作中的难题,参考学习。
Weka31 crossValidation源
代码分析 作者Koala/屈伟 Weka学习论坛里的人也帮我下载过一些论文而群主也希望我建设群论坛而我一开始看到的就是cross validation感觉介绍的有点简单了这里我只是再深一点而已。
我还是Ng的介绍为主。
在看到学习算法的时候我们都是以最小化经验误差为目标比如有方法梯度下降牛顿法lagrange乘子法坐标上升法都是我blog里提到过的方法了。
如果我们用下面的步骤来得到模型 1. 在数据集上训练多个模型。
2. 选择训练误差最小的模型。
下面是说明这个问题的一幅图它的原意倒和这没什么关系直接去看Pattern recognition and machine learning第一章。
这幅图大概意思就是模型复杂到一定
程序可以表示
学习到概念之后再使用更复杂模型那么它的训练误差会下降而测试误差会提高。
这幅图其实还有一个比较深的意义那就是你可以通过它来选择合适的模型不要一看测试误差高就加样本。
然后有一个代替它的方法是hold-out cross validation或是被称为simple cross validation。
1. 把数据集分为两部分一部分为训练集比如70的数据和测试数据集比如30。
测试数据集也被称为hold-out cross validation set。
2. 在训练集上训练多个模型。
3. 每一个模型在测试数据集上得到一个分类误差值选择分类误差最小的模型。
通常测试集的大小在数据集的1/4到1/3之间。
一般30是一个t典型的选择。
这样做的原因是如果只是以最小化经验误差为目标那么最终选择的就是过拟合的模型。
但用这种方法也有一个缺点就是它浪费了30的数据就算我们最后选择出我们认为合理的模型再用全部的数据进行训练只不能保证这个模型是最好的。
如果训练样本充分那倒没什么如果训练样本不足而模型与模型训练所需的样本也是不一样的不太清楚如何表述可以看一下learning theory就是求的最小需要样本。
再说明白一点在样本不充足的情况下或不知道是不是充足的情况下模型A在70的数据上比模型B好不能说在100的数据集上模型A一定比模型B好。
接下来的方法是k-fold cross validation这个算法就不写伪
代码了马上看真
代码一个典型的选择是k10它与上面的方法相比它只留下了1/k的数据但是我们也需要训练k次比以前多了k次其实也不完全是这样就算是划分训练测试集也不可能只测一次。
还有就是Pattern recognition and machine learning提到的A further problem with techniques such as cross-validation that use separate data to assess performance is that we might have multiple complexity parameters for a single model for instance there might be several regularization parameters. Exploring combinations of setting for such parameters could in the worst case require a number of training runs that is exponential in the number of parameters. 在数据实在太少的情况下就要用一种特殊的cross validation方法了是leave-one-out cross validation就是每次训练只留下一个样本不训练用它来测试。
在听Ng的
课程时他的学生提了一个很有意思的
问题在Learning Theory这个还是比较重要的被Ng视为是否入门的标准之一中不是可以从样本数求出来一个模型学习所需要的样本数在一定条件下为什么还要用model selection这一类方法呢 下面翻译一下大意Ng的回答 It turns out that when you’re proving learning theory bounds very often the bounds will be extremely loose because you’re sort of proving the worse case upper bound that holds true even for very bad – what is it – so the bounds that I proved just now right That holds true for absolutely any probability distribution over training examples right So just assume the training examples we’ve drawn iid from some distribution script d and the bounds I proved hold true for absolutely any probability distribution over script d. And chances are whatever real life distribution you get over you know houses and their prices or whatever is probably not as bad as the very worse one you could’ve gotten okay And so it turns out that if you actually plug in the constants of learning theory bounds you often get extremely large numbers. 你在证明学习理论的边界时通常边界都是异常loose因为你在证的都是比较糟糕的上界也就是在很坏的时候都成立的边界这些就是训练样本无论服从何从概率分布都成立。
现实中的数据也许不会和最坏的情况一样。
当你将常量加入学习理论的边界时像算法时间空间分析时忽略所有常量你通常都会得到一个非常大的值。
Take logistic regression – logistic regression you have ten parameters and 0.01 error and with 95 percent probability. How many training examples do I need If you actually plug in actual constants into the text for learning theory bounds you often get extremely pessi
mistic estimates with the number of examples you need. You end up with some ridiculously large numbers. You would need 10000 training examples to fit ten parameters. So a good way to think of these learning theory bounds is – and this is why also when I write papers on learning theory bounds I quite often use big-O notation to just absolutely just ignore the constant factors because the bounds seem to be very loose. 以logistic regression为例你有10个参数和在95的概率下错误率小于0.01。
我需要多少样本如果你将常量代入边界你会得到一个非常悲观的估计你会得到一个大的可笑的值比如10000个训练样本来拟合10个参数。
所以一个好的方式来理解这些学习边界是忽略常量因为边界非常loose。
There are some attempts to use these bounds to give guidelines as to what model to choose and so on. But I personally tend to use the bounds – again intuition about – for example what are the number of training examples you need grows linearly in the number of parameters or what are your grows x dimension in number of parameters whether it goes quadratic – parameters So it’s quite often the shape of the bounds. The fact that the number of training examples – the fact that some complexity is linear in the
VC dimension that’s sort of a useful intuition you can get from these theories. But the actual magnitude of the bound will tend to be much looser than will hold true for a particular problem you are working on. 有一些关于用这些边界来指导选择哪一种模型的方法但我个人倾向于用这些边界——再强调一下直观的——比如你所需要的样本数是与参数的个数是线性关系还是二次关系。
所以通常都是这些关系给你了一个直观的认识。
而它得到出的边界比真实的一些你正在处理的特殊问题要loose的多。
代码在classifiers.Evaluation类中 / Performs a stratified if class is nominal cross-validation for a classifier on a set of instances. Now performs a deep copy of the classifier before each call to buildClassifier just in case the classifier is not initialized properly. / public void crossValidateModelClassifier classifier Instances data int numFolds Random random throws Exception // Make a copy of the data we can reorder data new Instancesdata data.randomizerandom if data.classAttribute.isNominal data.stratifynumFolds // Do the folds for int i 0 i numFolds i Instances train data.trainCVnumFolds i random setPriorstrain Classifier copiedClassifier Classifier.makeCopyclassifier copiedClassifier.buildClassifiertrain Instances test data.testCVnumFolds i evaluateModelcopiedClassifier test m_NumFolds numFolds Randomize很简单 public void randomizeRandom random for int j numInstances - 1 j 0 j-- swapj random.nextIntj 1 Randomize注意它是从后向前打乱的这样写的确简单点。
public void stratifyint numFolds if numFolds