2014-04-25 10 views
29

Sto usando nltk corpus movie_reviews dove ci sono molti documenti. Il mio compito è ottenere prestazioni predittive di queste revisioni con pre-elaborazione dei dati e senza pre-elaborazione. Ma c'è un problema, negli elenchi documents e documents2 ho gli stessi documenti e ho bisogno di mescolarli per mantenere lo stesso ordine in entrambe le liste. Non posso mescolarli separatamente perché ogni volta che mischio la lista ottengo altri risultati. Questo è il motivo per cui ho bisogno di mescolare il tutto nello stesso ordine perché ho bisogno di confrontarli alla fine (dipende dall'ordine). Sto usando Python 2.7Mescola due liste contemporaneamente con lo stesso ordine

Esempio (in tempo reale sono stringhe token, ma non è parente):

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')] 

E ho bisogno di ottenere questo risultato dopo riordino entrambe le liste:

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')] 

ho questo codice:

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 

risposta

57

si può fare come:

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] 

Naturalmente, questo era un esempio con elenchi più semplici, ma l'adattamento sarà lo stesso per il tuo caso.

Spero che aiuti. In bocca al lupo.

+0

@thefourtheye, Grazie mille! Ho aggiornato la mia risposta. – sshashank124

+0

Grazie, è esattamente ciò di cui ho bisogno. –

+0

(domanda noob) - cosa significa *? –

-4

È possibile utilizzare il secondo argomento della funzione shuffle per correggere l'ordine di mischiare.

In particolare, è possibile passare al secondo argomento della funzione shuffle una funzione di argomento zero che restituisce un valore in [0, 1). Il valore di ritorno di questa funzione corregge l'ordine di mischiare. (Per impostazione predefinita, vale a dire se non si passa alcuna funzione come secondo argomento, si utilizza la funzione random.random() Lo si può vedere in linea di 277 here..)

Questo esempio illustra quello che ho descritto:

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 

Uscita:

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

La funzione 'random.shuffle' chiama la funzione' random' più di una volta, quindi l'uso di un 'lambda' che restituisce sempre lo stesso valore potrebbe avere effetti indesiderati sull'ordine di output. – Blckknght

+0

Hai ragione. Questo sarà un mischiatore parziale, a seconda del valore di r. Può essere praticamente buono per molti casi ma non sempre. –

3

Mescolare un numero di elenchi arbitray simultaneamente.

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) 

uscita:

$ (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) 

Nota:
oggetti restituiti da shuffle_list() sono tuples.

P.S. shuffle_list() può essere applicato anche ad numpy.array()

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

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

uscita:

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