2017-06-11 2 views
0

Ich möchte alle möglichen Kombinationen innerhalb eines Iterable Objekts finden.Alle Kombinationen nach Gruppen suchen PySpark

Meine Eingabe ist

Object1|DrDre|1.0 
Object1|Plane and a Disaster|2.0 
Object1|Tikk Takk Tikk|3.5 
Object1|Tennis Dope|5.0 
Object2|DrDre|11.0 
Object2|Plane and a Disaster|14.0 
Object2|Just My Luck|2.0 
Object2|Tennis Dope|45.0 

Die erwartete Ausgabe in etwa so sein würde:

[(('DrDre', 'Plane and a Disaster'), (11.0, 14.0, 1.0, 2.0)), 
(('DrDre', 'Tikk Takk Tikk'), (1.0, 3.5)), 
(('DrDre', 'Tennis Dope'), (11.0, 45.0, 1.0, 5.0)), 
(('Plane and a Disaster', 'Tikk Takk Tikk'), (2.0, 3.5)), 
(('Plane and a Disaster', 'Tennis Dope'), (14.0, 45.0, 2.0, 5.0)), 
(('Tikk Takk Tikk', 'Tennis Dope'), (3.5, 45.0)), 
(('DrDre', 'Just My Luck'), (11.0, 2.0)), 
(('Plane and a Disaster', 'Just My Luck'), (14.0, 2.0)), 
(('Just My Luck', 'Tennis Dope'), (2.0, 45.0))] 

Dies ist mein aktueller Code, der mir nicht die richtigen Kombinationen am Ende nicht geben.

def iterate(iterable): 
    r = [] 
    for v1_iterable in iterable: 
     for v2 in v1_iterable: 
      r.append(v2) 

    return tuple(r) 

def parseVector(line): 
    ''' 
    Parse each line of the specified data file, assuming a "|" delimiter. 
    Converts each rating to a float 
    ''' 
    line = line.split("|") 
    return line[0],(line[1],float(line[2])) 

def FindPairs(object_id,items_with_usage): 
    ''' 
    For each objects, find all item-item pairs combos. (i.e. items with the same user) 
    ''' 
    for item1,item2 in combinations(items_with_usage,2): 
     return (item1[0],item2[0]),(item1[1],item2[1]) 


''' 
Obtain the sparse object-item matrix: 
    user_id -> [(object_id_1, rating_1), 
       [(object_id_2, rating_2), 
       ...] 
''' 
object_item_pairs = lines.map(parseVector).groupByKey().map(
    lambda p: sampleInteractions(p[0],p[1],500)).cache() 


''' 
Get all item-item pair combos: 
    (item1,item2) -> [(item1_rating,item2_rating), 
         (item1_rating,item2_rating), 
         ...] 
''' 

pairwise_objects = object_item_pairs.filter(
    lambda p: len(p[1]) > 1).map(
    lambda p: findItemPairs(p[0],p[1])).groupByKey() 



x = pairwise_objects.mapValues(iterate) 
x.collect() 

Dies gibt mir nur das erste Paar, und sonst nichts.

[(('DrDre', 'Ebene und eine Disaster'), (11.0, 14.0, 1.0, 2.0))]

Habe ich die Funktionalität der Kombinationen verkennen() Funktion?

Vielen Dank für Ihre Eingaben

+0

Sie den 'return' Befehl innerhalb der _for_ Schleife, das bedeutet, dass die Schleife im ersten Zyklus endet. Deshalb haben Sie nur das erste Paar, weil Sie nicht alle Elemente von 'combinations (items_with_usage, 2) speichern', Sie geben nur das erste Paar von Artikeln zurück – titiro89

+0

Ah, großer Dank titiro89 !! :) – ponthu

Antwort

1

Ich glaube, Sie Ihre FindPairs auf diese Weise Jetzt

def FindPairs(object_id,items_with_usage): 
''' 
For each objects, find all item-item pairs combos. (i.e. items with the same user) 
''' 
t = [] 
for item1,item2 in combinations(items_with_usage,2): 
    t.append(((item1[0],item2[0]),(item1[1],item2[1]))) 
return t 

verwandeln kann, wird Ihre Funktion eine Liste mit allen Paaren der Kombination zurück.

Dann

pairwise_objects= pairwise_objects.filter(lambda p: len(p[1]) > 1) 
pairwise_objects= pairwise_objects.map(lambda p: FindPairs(p[0],p[1])) 

[[(('DrDre', 'Plane and a Disaster'), (1.0, 2.0)), 
(('DrDre', 'Tikk Takk Tikk'), (1.0, 3.5)), 
(('DrDre', 'Tennis Dope'), (1.0, 5.0)), 
(('Plane and a Disaster', 'Tikk Takk Tikk'), (2.0, 3.5)), 
(('Plane and a Disaster', 'Tennis Dope'), (2.0, 5.0)), 
(('Tikk Takk Tikk', 'Tennis Dope'), (3.5, 5.0))], # end of the first line of the RDD 
[(('DrDre', 'Plane and a Disaster'),(11.0, 14.0)), 
(('DrDre', 'Just My Luck'), (11.0, 2.0)), 
(('DrDre', 'Tennis Dope'), (11.0, 45.0)), 
(('Plane and a Disaster', 'Just My Luck'), (14.0, 2.0)), 
(('Plane and a Disaster', 'Tennis Dope'), (14.0, 45.0)), 
(('Just My Luck', 'Tennis Dope'), (2.0, 45.0))]] 

Verwenden flatMap (so dass Sie nur eine einzige Zeile mit allen Ihren Paare haben) vor Ihrer RDD Gruppierung und Anwendung Ihrer Funktion

pairwise_objects=pairwise_objects.flatMap(lambda p: p).groupByKey().mapValues(iterate) 

fertige Ausgabe:

[(('DrDre', 'Tennis Dope'), (1.0, 5.0, 11.0, 45.0)), 
(('DrDre', 'Plane and a Disaster'), (1.0, 2.0, 11.0, 14.0)), 
(('Plane and a Disaster', 'Tennis Dope'), (2.0, 5.0, 14.0, 45.0)), 
(('Plane and a Disaster', 'Just My Luck'), (14.0, 2.0)), 
(('Plane and a Disaster', 'Tikk Takk Tikk'), (2.0, 3.5)), 
(('DrDre', 'Tikk Takk Tikk'), (1.0, 3.5)), 
(('Tikk Takk Tikk', 'Tennis Dope'), (3.5, 5.0)), 
(('DrDre', 'Just My Luck'), (11.0, 2.0)), 
(('Just My Luck', 'Tennis Dope'), (2.0, 45.0))] 
Verwandte Themen