数千のドキュメントを含む一連のドキュメントがあります。数は違うが固定の3セットに分けたい。どのようにすればよいでしょうか?スクリプト bash/python/java または参照へのリンクをいただければ幸いです。
4 に答える
1
ドキュメントをランダムに取得できる場合は、Python で " set
"に対してリスト スライスを使用するだけです。
set_of_documents = ... #somehow build the set
list_of_documents = list(set_of_documents)
training = list_of_documents[:ntraining]
development = list_of_documents[-ndev:]
test = list_of_documents[ntraining:-ndev]
于 2012-08-17T14:12:07.890 に答える
1
docs={'doc1','doc2','doc3','doc4','doc5','doc6','doc7','doc8','doc9','doc10','doc11','doc12'}
training=set()
test=set()
dev=set()
lis=list(docs)
print(lis)
try:
for i in range(0,len(lis),3):
print(lis[i])
training.add(lis[i])
test.add(lis[i+1])
dev.add(lis[i+2])
except IndexError:
pass
print(training,test,dev)
**output:**
{'doc10', 'doc3', 'doc8', 'doc7'} {'doc1', 'doc11', 'doc9', 'doc4'} {'doc2', 'doc12', 'doc6', 'doc5'}
于 2012-08-17T14:22:55.053 に答える
1
documents
データを含む配列と、partitions
各リストに入れるドキュメントの数を指定する配列を指定します。
import random
def partition_docs(documents, partitions):
if len(documents) != sum(partitions):
raise ValueError("Need exactly %d documents for these partitions, have %d" % (sum(partitions), len(documents)))
random.shuffle(documents)
results = []
start = 0
for num in partitions:
results.append(documents[start:start+num])
start += num
return results
説明:random.shuffle
ドキュメントのリストを完全にランダム化します。次に、連続したスライスを取得して、それらを個別のリストとして返すだけです。これにより、必要な数のドキュメントがあることが簡単に保証され、配布が可能な限りランダムになることも保証されます。これは、ドキュメントを分割する任意の数のリストに対しても機能します。
使用例:
documents = [random.choice("abcdefghijklmn") for _ in xrange(100)]
partitions = [10, 50, 40]
print partition_docs(documents, partitions)
print partition_docs(documents, partitions)
print partition_docs(documents, partitions)
print partition_docs(documents, partitions)
出力:
[['b', 'n', 'm', 'l', 'c', 'j', 'l', 'e', 'i', 'f'], ['k', 'n', 'i', 'b', 'k', 'f', 'h', 'j', 'i', 'g', 'n', 'c', 'd', 'h', 'd', 'd', 'm', 'g', 'i', 'd', 'i', 'e', 'e', 'a', 'k', 'k', 'f', 'e', 'h', 'm', 'k', 'c', 'h', 'k', 'j', 'k', 'g', 'f', 'j', 'l', 'b', 'e', 'm', 'c', 'd', 'n', 'b', 'h', 'm', 'm'], ['a', 'g', 'f', 'f', 'm', 'k', 'n', 'a', 'n', 'f', 'd', 'j', 'h', 'h', 'k', 'g', 'h', 'k', 'i', 'l', 'm', 'h', 'm', 'i', 'c', 'i', 'c', 'g', 'm', 'l', 'a', 'j', 'g', 'd', 'd', 'n', 'b', 'b', 'n', 'k']]
[['m', 'n', 'e', 'a', 'k', 'b', 'm', 'd', 'k', 'f'], ['d', 'h', 'c', 'g', 'h', 'i', 'l', 'k', 'b', 'g', 'n', 'd', 'n', 'm', 'a', 'm', 'd', 'i', 'n', 'g', 'f', 'g', 'm', 'b', 'j', 'l', 'k', 'f', 'c', 'j', 'i', 'n', 'j', 'h', 'j', 'k', 'k', 'd', 'i', 'm', 'e', 'h', 'c', 'h', 'm', 'i', 'k', 'e', 'f', 'l'], ['m', 'i', 'h', 'j', 'l', 'b', 'e', 'k', 'k', 'h', 'd', 'h', 'm', 'n', 'k', 'f', 'c', 'l', 'g', 'm', 'f', 'n', 'c', 'i', 'd', 'a', 'e', 'f', 'b', 'a', 'd', 'g', 'k', 'n', 'j', 'b', 'i', 'c', 'h', 'g']]
[['f', 'f', 'l', 'g', 'c', 'k', 'i', 'k', 'm', 'h'], ['i', 'k', 'm', 'b', 'c', 'h', 'k', 'i', 'd', 'k', 'n', 'n', 'f', 'c', 'm', 'm', 'e', 'd', 'f', 'm', 'e', 'j', 'k', 'n', 'd', 'g', 'a', 'g', 'j', 'a', 'l', 'l', 'b', 'e', 'c', 'd', 'e', 'a', 'i', 'f', 'j', 'm', 'i', 'd', 'm', 'i', 'n', 'g', 'h', 'c'], ['g', 'h', 'h', 'k', 'k', 'i', 'n', 'n', 'i', 'd', 'l', 'b', 'l', 'f', 'a', 'j', 'b', 'g', 'm', 'n', 'k', 'm', 'g', 'j', 'j', 'm', 'h', 'h', 'c', 'k', 'b', 'd', 'e', 'b', 'n', 'k', 'f', 'h', 'h', 'd']]
[['h', 'k', 'b', 'l', 'h', 'g', 'g', 'n', 'm', 'c'], ['g', 'a', 'i', 'c', 'f', 'i', 'd', 'i', 'k', 'h', 'j', 'b', 'f', 'k', 'm', 'd', 'g', 'm', 'b', 'h', 'f', 'c', 'h', 'd', 'f', 'j', 'n', 'l', 'k', 'n', 'k', 'b', 'h', 'g', 'h', 'b', 'm', 'm', 'i', 'l', 'b', 'k', 'j', 'f', 'm', 'd', 'e', 'm', 'g', 'k'], ['d', 'e', 'm', 'f', 'a', 'l', 'n', 'd', 'j', 'i', 'd', 'n', 'g', 'n', 'k', 'c', 'a', 'c', 'j', 'e', 'f', 'h', 'c', 'i', 'j', 'k', 'm', 'd', 'h', 'e', 'm', 'e', 'a', 'i', 'i', 'l', 'k', 'k', 'n', 'n']]
異なるパーティションの場合:
partitions = [30, 30, 40]
print partition_docs(documents, partitions)
print partition_docs(documents, partitions)
print partition_docs(documents, partitions)
print partition_docs(documents, partitions)
出力:
[['g', 'h', 'n', 'i', 'j', 'l', 'a', 'm', 'g', 'h', 'd', 'l', 'g', 'e', 'b', 'i', 'e', 'l', 'i', 'f', 'j', 'a', 'l', 'j', 'e', 'h', 'h', 'j', 'm', 'n'], ['c', 'm', 'g', 'm', 'c', 'e', 'i', 'e', 'm', 'k', 'f', 'e', 'h', 'c', 'k', 'i', 'f', 'd', 'm', 'b', 'm', 'i', 'k', 'd', 'l', 'j', 'f', 'n', 'd', 'l'], ['k', 'j', 'n', 'h', 'b', 'h', 'm', 'j', 'i', 'f', 'e', 'n', 'k', 'n', 'b', 'h', 'm', 'b', 'n', 'j', 'l', 'a', 'e', 'i', 'a', 'h', 'k', 'k', 'h', 'a', 'i', 'k', 'c', 'b', 'c', 'a', 'l', 'm', 'c', 'e']]
[['l', 'l', 'i', 'd', 'm', 'n', 'c', 'm', 'a', 'h', 'g', 'l', 'k', 'f', 'b', 'n', 'i', 'a', 'e', 'j', 'm', 'h', 'h', 'g', 'e', 'm', 'g', 'i', 'f', 'b'], ['n', 'c', 'i', 'e', 'h', 'j', 'i', 'h', 'k', 'c', 'h', 'g', 'j', 'a', 'k', 'n', 'b', 'a', 'm', 'j', 'b', 'c', 'l', 'k', 'j', 'd', 'k', 'c', 'm', 'e'], ['e', 'm', 'b', 'j', 'e', 'c', 'j', 'f', 'h', 'd', 'e', 'j', 'i', 'h', 'h', 'i', 'n', 'k', 'n', 'd', 'i', 'l', 'a', 'a', 'k', 'f', 'l', 'i', 'l', 'b', 'm', 'h', 'e', 'm', 'l', 'm', 'f', 'n', 'e', 'k']]
[['h', 'e', 'c', 'n', 'm', 'l', 'i', 'k', 'a', 'm', 'k', 'i', 'k', 'g', 'f', 'b', 'j', 'l', 'j', 'm', 'i', 'h', 'g', 'h', 'h', 'k', 'l', 'n', 'f', 'j'], ['m', 'j', 'e', 'm', 'a', 'd', 'm', 'l', 'g', 'i', 'e', 'm', 'i', 'j', 'f', 'e', 'f', 'j', 'd', 'b', 'c', 'l', 'n', 'j', 'j', 'e', 'c', 'k', 'm', 'e'], ['i', 'n', 'b', 'i', 'a', 'c', 'a', 'd', 'k', 'h', 'h', 'n', 'h', 'e', 'e', 'h', 'l', 'm', 'h', 'b', 'c', 'f', 'd', 'e', 'm', 'n', 'g', 'i', 'a', 'b', 'c', 'b', 'a', 'i', 'n', 'k', 'h', 'l', 'l', 'k']]
[['l', 'e', 'k', 'k', 'm', 'l', 'g', 'c', 'j', 'd', 'a', 'l', 'k', 'k', 'f', 'l', 'h', 'e', 'g', 'm', 'k', 'm', 'j', 'i', 'b', 'l', 'c', 'b', 'h', 'g'], ['e', 'j', 'd', 'm', 'f', 'f', 'e', 'e', 'n', 'm', 'b', 'h', 'j', 'h', 'm', 'j', 'j', 'i', 'i', 'f', 'e', 'l', 'n', 'i', 'd', 'h', 'a', 'a', 'h', 'i'], ['i', 'n', 'n', 'n', 'b', 'c', 'm', 'j', 'e', 'a', 'h', 'h', 'b', 'h', 'l', 'a', 'i', 'm', 'i', 'a', 'e', 'c', 'i', 'm', 'k', 'n', 'g', 'l', 'd', 'e', 'm', 'j', 'h', 'n', 'k', 'k', 'c', 'c', 'f', 'b']]
于 2012-08-17T14:23:51.953 に答える
0
これが機械学習の目的である場合、スライスを使用しない方が賢明だと思います。偏りが生じる可能性があるためです (ドキュメントは特定のロジックに従ってソートされている可能性があります)。だから私はクラウディウスシャッフルソリューションを使います。
正確なセットのサイズではなく、いくつかの比率を満たしたいだけの場合は、各ドキュメントが各セットに属する確率を計算できます。
import random
def split(documents, prob_training=0.8, prob_test=0.1, prob_dev=0.1):
#you may check some rules on input probabilities e.g. that they indeed *are* probabilities
training = []
test = []
dev = []
for document in documents:
prob = random.random()
if prob < prob_training:
training.append(document)
elif prob < prob_training + prob_test:
test.append(document)
elif prob < prob_training + prob_test + prob_dev:
dev.append(document)
return training, test, dev
于 2012-08-17T15:19:05.130 に答える