私はいくつかのデータを持っており、共通の比率を維持する小さなグループに分割したいと思います。私は2つの配列の入力を受け取り、サイズ比を計算し、いくつのグループを分けることができるかを教えてくれる関数を書いた(すべてのグループが同じサイズの場合)、ここに関数がある:Python関数でデータを分割するときの比率を維持する
def cross_validation_group(train_data, test_data):
import numpy as np
from calculator import factors
test_length = len(test_data)
train_length = len(train_data)
total_length = test_length + train_length
ratio = test_length/float(total_length)
possibilities = factors(total_length)
print possibilities
print possibilities[len(possibilities)-1] * ratio
super_count = 0
for i in possibilities:
if i < len(possibilities)/2:
pass
else:
attempt = float(i * ratio)
if attempt.is_integer():
print str(i) + " is an option for total size with " + str(attempt) + " as test size and " + str(i - attempt) + " as train size! This is with " + str(total_length/i) + " folds."
else:
pass
folds = int(raw_input("So how many folds would you like to use? If no possibilities were given that would be sufficient, type 0: "))
if folds != 0:
total_size = total_length/folds
test_size = float(total_size * ratio)
train_size = total_size - test_size
columns = train_data[0]
columns= len(columns)
groups = np.empty((folds,(test_size + train_size),columns))
i = 0
a = 0
b = 0
for j in range (0,folds):
test_size_new = test_size * (j + 1)
train_size_new = train_size * j
total_size_new = (train_size + test_size) * (j + 1)
cut_off = total_size_new - train_size
p = 0
while i < total_size_new:
if i < cut_off:
groups[j,p] = test_data[a]
a += 1
else:
groups[j,p] = train_data[b]
b += 1
i += 1
p += 1
return groups
else:
print "This method cannot be used because the ratio cannot be maintained with equal group sizes other than for the options you were givens"
私の質問は、各グループが同じ量を持っていることを確認するために繰り返すのではなく、折り返しの数と関数を変更する関数への3番目の入力適切な比率で、それはちょうど良い比率を持っていますが、サイズが異なりますか? @JamesHolderness
ため
添加は、だからあなたの方法はほぼ完璧ですが、ここで問題の1つである:9つの折り目と長さ357と143との
、これは返すリストです:
[(39, 16), (39, 16), (39, 16), (39, 16), (39, 16), (39, 16), (39, 16), (39, 16), (39, 16)]
今すぐ列を追加すると、次のようになります。351 144
3 51は357よりも小さいので問題ありませんが、143より大きいため144は機能しません!その理由は、357と143は配列の長さなので、その配列の144番目の行は存在しません...
さまざまなトレーニングセットと相互検証することを意味しますか?これは統計的にはまあまあですか?それは実際には通常行われますか? – thebigdog
はい、これは相互検証のためのものです。いいえ、これは、トレーニングデータにないテストデータに何かが存在するかどうかを確認するために、テストセットとトレーニングセットの類似性をテストすることになっています。通常、クロスバリデーションは1つのトレーニングセットでのみ実行され、2つのアレイではなく、トレーニングアレイとトレーニングアレイに列を与えて、そうすることができます。 –
2つの配列の大きさがmとnで、mのnで割った既約分数がp/qの場合、m = k * pとn = k * q '。一度あなたが 'k'を持っていれば、それの任意の[パーティション](http://en.wikipedia.org/wiki/Partition_(number_theory))は要素の比率を保持する元のデータの分割につながります。あなたが私が精緻化する必要があるかどうかを教えてください。 – Jaime