機械学習-サンプルセットの選択(トレーニングセットとテストセットを含む)



Machine Learning Selection Sample Set Including Training Set



差出人:http://www.xuebuyuan.com/1409669.html

https://blog.csdn.net/bbbeoy/article/details/72967794



経験的リスクを最小限に抑える アルゴリズム 過剰適合の問題は、分類問題を行うときに一般的に使用される交差検定法によって与えられます。

1つ:単純な相互検証の手順は次のとおりです。



1.すべてのトレーニングデータからランダムに選択しますSトレーニングセットトレインとしてsをランダムに選択し、残りを テスト テストセットテストとして設定します。

2.テストセットをトレーニングすることにより、仮説関数またはモデルが取得されます。
3.テストセット内の各サンプルの仮説関数またはモデルに従って、トレーニングセットのクラスラベルが取得され、分類正解率が取得されます。

4.分類率が最大のモデルまたは仮説を選択します。

この方法は、ホールドアウト交差検定または単純交差検定と呼ばれます。テストセットとトレーニングセットが分離されているため、過剰適合の現象が回避されます



2:k分割交差検定

1.すべてのトレーニングセットSをk個の互いに素なサブセットに分割します。 Sのトレーニング例の数がmであるとすると、各サブセットにはm / kのトレーニング例があり、対応するサブセットは{s1、s2、...、sk}と呼ばれます。
2.分割されたサブセットから毎回、1つをテストセットとして、もう1つをトレーニングセットとして使用します。

3.トレーニングに基づいてモデルまたは仮説関数をトレーニングします。
4.このモデルをテストセットに配置して、分類率を取得します。

5. k回得られた分類率の平均値を、モデルまたは仮説関数の真の分類率として計算します。

この方法では、すべてのサンプルを最大限に活用します。しかし、計算は面倒であり、k回のトレーニングとk回のテストが必要です。


3:leave-one-leave-one-out相互検証

ワンリーブ法では、一度に1つのサンプルのみをテストセットとして残し、他のサンプルをトレーニングセットとして残します。 k個のサンプルがある場合、k回トレーニングし、k回テストする必要があります。

ワンショット計算が最も複雑ですが、サンプル使用率が最も高くなります。少量のサンプルに適しています。


交差検定インデックスを生成する

構文

インデックス = Crossvalind( 'Kfold'、N、K)K-fold cross
[トレーニング、テスト] = crossvalind( 'HoldOut'、N、P)
[Train、Test] = crossvalind( 'LeaveMOut'、N、M)相互検証のためにMメソッドを残します。デフォルトのMは1で、相互検証のために1つのメソッドを残します。
[トレーニング、テスト] = crossvalind( 'Resubstitution'、N、[P、Q])
[...] = crossvalind(Method、Group、...)
[...] = crossvalind(Method、Group、...、 'Classes'、C)
[...] = crossvalind(Method、Group、...、 'Min'、MinValue)

https://blog.csdn.net/NNNNNNNNNNNNY/article/details/45789323から部分的に複製


相互検証は、データサンプルをいくつかのサブセットにランダムに分割できるランダムラウンドロビン検証方法です。相互検証は、主に統計分析または機械学習アルゴリズムの一般化能力を評価するために使用されます。
機械学習アルゴリズムの一般化能力の最初の種類の評価では、ランダムに分割されたデータの一部をトレーニングサンプルとして選択し、別の部分をテストサンプルとして選択できます。具体的な実装プロセスは次のとおりです。

Data = rand(9,3) %Create a random matrix sample with dimension 9×3 indices = crossvalind('Kfold', 9, 3)% randomly divides the data sample into 3 parts for i = 1:3% loop 3 times, take the i part as the test sample, and the other two parts as the training sample test = (indices == i) train = ~test trainData = Data(train, :) testData = Data(test, :) end

Index data, that is, divided into three categories, the same number means that the corresponding number of rows is the same category:
image
Test data when i=3:
image
Corresponding train data (that is, inverse test):
image
testData (that is, the data of the row corresponding to ‘1’ in the test data)
image
trainData:
image

k-fold cross validation (k-fold crossValidation):
In machine learning, the data set A is divided into a training set (training set) B and a test set (test set) C. In the case of insufficient sample size, in order to make full use of the data set to test the effect of the algorithm,The data set A is randomly divided into k packets, one of which is used as the test set each time, and the remaining k-1 packets are used as the training set for training.
In matlab, you can use:
indices=crossvalind('Kfold',x,k)
To achieve the operation of random subcontracting, where x is an N-dimensional column vector (N is the number of elements in the data set A, regardless of the specific content of x, only need to be able to represent the size of the data set), k is the packet to be divided into The total number, the output indices is an N-dimensional column vector,The value corresponding to each element is the number of the package to which the unit belongs (that is, the elements in the column vector are integer random numbers from 1 to k),Using this vector, the data set can be divided by loop control.

example:

対応する出力:
生成されたランダム行列データ:
[M,N]=size(data)//The data set is an M*N matrix, where each row represents a sample indices=crossvalind('Kfold', data(1:M,N), 10)//random subcontracting for k=1:10//Cross-validation k=10, 10 packets take turns as the test set test = (indices == k) //Get the corresponding unit number of the test set element in the data set train = ~test//The number of train set elements is the number of non-test elements train_data=data(train,:)//The data of the train sample is divided from the data set train_target=target(:,train)//Get the test target of the sample set, in this case is the actual classification of the train sample test_data=data(test,:)//test sample set test_target=target(:,test)//The actual classification of test ........... end [m n]=size(data) %data is the sample set. One observation sample per line indices = crossvalind('Kfold',m,10)% produces 10 folds, that is, equal proportions of 1-10 in indices for i=1:10 test=(indices==i)% logical judgment, select a fold as the test set every cycle train=~test% take the complement of test as the training set, that is, the remaining 9 folds data_train=data(trian,:) The numbers obtained above% are all logical values, use the label_train=label(train,:) selected with the sample set %label is the sample category label, and also select the corresponding training set data_test=data (test,:) %Similarly select the samples and labels of the test set label_test=label(test,:) end groups=ismenber(label,1) %label is the sample category label, generates a logical matrix groups, 1 is used for logical judgment and screening [train, test] = crossvalind('holdOut', groups)% classify groups, the default ratio is 1:1, that is, P=0.5 クロスバリンド関数

①インデックス= crossvalind( 'Kfold'、N、K):
このコマンドは、N個の観測サンプルのK個のフォールド(フォールドを意味し、「レイヤー」を意味し、英語でより鮮やかに感じます)のリストを返します。ラベルには、同じ(またはほぼ同じ)比率の1-K値が含まれており、サンプルをK個の反発サブセットに分割します。 Kフォールドクロスチェックでは、K-1フォールドがトレーニングに使用され、残りの1つがテストに使用されます。このプロセスはK回ループし、そのたびにテストセットとして異なるフォールドを選択します。 Kのデフォルト値は5です。手順:

[m,n]=size(data) [train,test]=crossvalind('LeaveMOut',m,10) svmStruct = svmtrain(data(train,:),groups(train)) classes = svmclassify(svmStruct,data(test,:)) cp=classperf(groups) cr=cp.CorrectRate ②[トレーニング、テスト] = crossvalind( 'HoldOut'、N、P):
このコマンドは、論理値のラベルベクトルを返し、テストセットとしてN個の観測サンプルからP * Nサンプルをランダムに選択(または近似)します。したがって、Pは0-1である必要があり、デフォルト値は0.5です。手順:
③[トレーニング、テスト] = crossvalind( 'LeaveMOut'、N、M): 
このコマンドは、論理値のラベルベクトルを返し、N個の観測サンプルからM個のサンプルをテストセットとしてランダムに選択します。 Mのデフォルト値は1です。ループでLeaveMOutを使用しても、相補セットが生成されることを保証できないこと、つまり、各ループのランダムな選択が独立していることを保証できないことに注意してください。補完を使用する場合は、Kfoldコマンドを使用します。手順:
|_+_|
 

④[Train、Test] = crossvalind( 'Resubstitution'、N、[P、Q]):

この機能は②の特殊なケースです。入れたくないときP * N残りをトレーニングセットとして使用する場合
この機能を使用する場合は、
Qスケールを指定し、を選択しますQ * Nトレーニングセットとして。 2セットの選択
交差を最小化する原理。