私は大量の文書を含むnltk
ライブラリのコーパスを使用しています。私の仕事は、データの前処理ありと前処理なしのこれらのレビューの予測パフォーマンスを取得することです。しかし、リストに同じ文書があり、両方のリストで同じ順序を維持するためにそれらをシャッフルする必要があるという問題があります。リストをシャッフルするたびに異なる結果が得られるため、個別にシャッフルすることはできません。そのため、最終的にそれらを比較する必要があるため(順序によって異なります)、同じ順序で一度にシャッフルする必要があります。私はPython 2.7を使用しています。movie_reviews
documents
documents2
例 (実際には文字列はトークン化されていますが、相対的ではありません):
documents = [(['plot : two teen couples go to a church party , '], 'neg'),
(['drink and then drive . '], 'pos'),
(['they get into an accident . '], 'neg'),
(['one of the guys dies'], 'neg')]
documents2 = [(['plot two teen couples church party'], 'neg'),
(['drink then drive . '], 'pos'),
(['they get accident . '], 'neg'),
(['one guys dies'], 'neg')]
そして、両方のリストをシャッフルした後、次の結果を取得する必要があります。
documents = [(['one of the guys dies'], 'neg'),
(['they get into an accident . '], 'neg'),
(['drink and then drive . '], 'pos'),
(['plot : two teen couples go to a church party , '], 'neg')]
documents2 = [(['one guys dies'], 'neg'),
(['they get accident . '], 'neg'),
(['drink then drive . '], 'pos'),
(['plot two teen couples church party'], 'neg')]
次のようなコードがあります:
def cleanDoc(doc):
stopset = set(stopwords.words('english'))
stemmer = nltk.PorterStemmer()
clean = [token.lower() for token in doc if token.lower() not in stopset and len(token) > 2]
final = [stemmer.stem(word) for word in clean]
return final
documents = [(list(movie_reviews.words(fileid)), category)
for category in movie_reviews.categories()
for fileid in movie_reviews.fileids(category)]
documents2 = [(list(cleanDoc(movie_reviews.words(fileid))), category)
for category in movie_reviews.categories()
for fileid in movie_reviews.fileids(category)]
random.shuffle( and here shuffle documents and documents2 with same order) # or somehow
ベストアンサー1
次のように実行できます:
import random
a = ['a', 'b', 'c']
b = [1, 2, 3]
c = list(zip(a, b))
random.shuffle(c)
a, b = zip(*c)
print a
print b
[OUTPUT]
['a', 'c', 'b']
[1, 3, 2]
もちろん、これはより単純なリストの例ですが、あなたのケースでも適応は同じになります。