programing tip

동일한 순서로 한 번에 두 목록을 섞습니다.

itbloger 2020. 11. 7. 09:05
반응형

동일한 순서로 한 번에 두 목록을 섞습니다.


많은 문서가있는 nltk corpus movie_reviews를 사용하고 있습니다. 내 임무는 데이터를 사전 처리하고 사전 처리하지 않고 이러한 리뷰의 예측 성능을 얻는 것입니다. 그러나 목록에, 문제가 documentsdocuments2나는 같은 문서가 나는 두 목록에 같은 순서를 유지하기 위해 그들을 셔플 필요가있다. 목록을 섞을 때마다 다른 결과가 나오기 때문에 따로 섞을 수 없습니다. 그래서 결국 비교가 필요하기 때문에 (순서에 따라 다름) 같은 순서로 한 번에 섞을 필요가 있습니다. 파이썬 2.7을 사용하고 있습니다.

예 (실제로는 토큰 화 된 문자열이지만 상대적이지 않음) :

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

다음과 같이 할 수 있습니다.

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]

물론 이것은 더 간단한 목록의 예 였지만 적용은 귀하의 경우에 동일합니다.

도움이 되었기를 바랍니다. 행운을 빕니다.


나는 이것을하는 쉬운 방법을 얻는다

import numpy as np
a = np.array([0,1,2,3,4])
b = np.array([5,6,7,8,9])

indices = np.arange(a.shape[0])
np.random.shuffle(indices)

a = a[indices]
b = b[indices]
# a, array([3, 4, 1, 2, 0])
# b, array([8, 9, 6, 7, 5])

임의의 수의 목록을 동시에 섞습니다.

from random import shuffle

def shuffle_list(*ls):
  l =list(zip(*ls))

  shuffle(l)
  return zip(*l)

a = [0,1,2,3,4]
b = [5,6,7,8,9]

a1,b1 = shuffle_list(a,b)
print(a1,b1)

a = [0,1,2,3,4]
b = [5,6,7,8,9]
c = [10,11,12,13,14]
a1,b1,c1 = shuffle_list(a,b,c)
print(a1,b1,c1)

산출:

$ (0, 2, 4, 3, 1) (5, 7, 9, 8, 6)
$ (4, 3, 0, 2, 1) (9, 8, 5, 7, 6) (14, 13, 10, 12, 11)

참고 :에서
반환 된 객체 shuffle_list()tuples.

PS shuffle_list()는 또한 적용 할 수 있습니다numpy.array()

a = np.array([1,2,3])
b = np.array([4,5,6])

a1,b1 = shuffle_list(a,b)
print(a1,b1)

산출:

$ (3, 1, 2) (6, 4, 5)

from sklearn.utils import shuffle

a = ['a', 'b', 'c','d','e']
b = [1, 2, 3, 4, 5]

a_shuffled, b_shuffled = shuffle(np.array(a), np.array(b))
print(a_shuffled, b_shuffled)

#['e' 'c' 'b' 'd' 'a'] [5 3 2 4 1]

이를 수행하는 쉽고 빠른 방법은 random.shuffle ()과 함께 random.seed ()를 사용하는 것입니다. 원하는 여러 번 동일한 임의 순서를 생성 할 수 있습니다. 다음과 같이 표시됩니다.

a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
seed = random.random()
random.seed(seed)
a.shuffle()
random.seed(seed)
b.shuffle()
print(a)
print(b)

>>[3, 1, 4, 2, 5]
>>[8, 6, 9, 7, 10]

이것은 또한 메모리 문제로 인해 두 목록을 동시에 작업 할 수없는 경우에도 작동합니다.


셔플 함수의 두 번째 인수를 사용하여 셔플 링 순서를 수정할 수 있습니다.

Specifically, you can pass the second argument of shuffle function a zero argument function which returns a value in [0, 1). The return value of this function fixes the order of shuffling. (By default i.e. if you do not pass any function as the second argument, it uses the function random.random(). You can see it at line 277 here.)

This example illustrates what I described:

import random

a = ['a', 'b', 'c', 'd', 'e']
b = [1, 2, 3, 4, 5]

r = random.random()            # randomly generating a real in [0,1)
random.shuffle(a, lambda : r)  # lambda : r is an unary function which returns r
random.shuffle(b, lambda : r)  # using the same function as used in prev line so that shuffling order is same

print a
print b

Output:

['e', 'c', 'd', 'a', 'b']
[5, 3, 4, 1, 2]

참고URL : https://stackoverflow.com/questions/23289547/shuffle-two-list-at-once-with-same-order

반응형