Nach dem besten zweiten Spiel Produkt in einem Katalog

Intelligenz:

Ich arbeite das Produkt mit dem besten zweiten Spiel auf der Suche. In einem Katalog, könnten wir die genauen Treffer 100%, aber das Ziel ist es, das beste zweite Spiel zu bekommen.

Unten ist der Code, aber es wird für die beste Übereinstimmung läuft, nicht das beste zweite Spiel.

Ich bin immer an dieser Stelle stecken. Bitte lassen Sie mich wissen, wenn Sie einige Lösungen um es zu finden.

import pandas as pd
import numpy as np

seller_product =([['mai', 'dubai', '200ml']])

catalog_product=([['mai', 'dubai', '200ml'],
                  ['mai', 'dubai', '300ml']])

# SEARCH ENGINE
def available(product, catalog):
    items = [_ for _ in product if _ != "NaN"]
    if isinstance(catalog[0], str):
        catalog = [catalog]

    max_match = (0, [])
    for catalog in catalog:
        matched_count = 0
        for item in items:
            if item in catalog:
                matched_count += 1
        max_match = max(max_match, (matched_count, catalog)) # tuple score + catalog_item

    # return "_".join(items), max_match[1], max_match[0] / len(items)
    return "_".join(items), max_match[1], max_match[0] / len(items) if len(items) != 0 else 0

def availables(products, catalog):
    return [available(product, catalog) for product in products]

output=[]
for res in availables(seller_product, catalog_product):
#     res=print(" -> ".join(map(str, res)))
    output.append(res)

import pandas as pd
df_output=pd.DataFrame(output)
df_output

aktueller Output:

[mai_dubai_200ml]   [mai, dubai, 200ml]   1.0

Gewünschte Ausgabe:

[mai_dubai_200ml]   [mai, dubai, 300ml]   0.6667

Vielen Dank

Beste Phong

FBruzzesi:

Nicht sicher, dieser Ansatz ist zu 100%, was Sie suchen, aber es sollte in der Nähe sein:

# added some sample datapoints
seller_product =([['mai', 'dubai', '200ml'],
                  ['may', 'dubai', '200ml'],
                  ['mai', 'milan', '200ml']
                  ])

catalog_product=([['mai', 'dubai', '200ml'],
                  ['mai', 'dubai', '300ml'],
                  ['may', 'dubai', '300ml']])


items = ['_'.join(p) for p in seller_product]
catalogs = ['_'.join(p) for p in catalog_product]

# Create a dataframe with catalog items as columns and seller items as indexes
df = pd.DataFrame(
    np.zeros((len(items), len(catalogs))),
    index=items, columns = catalogs)

# Compute row name intersection with column names
def comp_intersection(r, col_names):

    item = set(r.name.split('_'))
    for c in col_names:
        cat = set(c.split('_'))

        v = len(item.intersection(cat))/len(cat)

        r[c] = v
    return r

# Apply the function row-wise
result_df = df.apply(lambda r: comp_intersection(r, list(df)), axis=1)

# Store second largest value for each row
second_largest_value = pd.DataFrame(
                            np.squeeze(np.sort(result_df.values)[:,-2:-1]), 
                            columns=['2nd-largest'],
                            index = result_df.index.to_numpy())

# Finally compare the dataframe
final_df = result_df[result_df == second_largest_value.values]
final_df = final_df.reset_index()\
                   .melt('index').dropna()\
                   .rename(columns={'index':'product',
                                    'variable':'second_match',
                                    'value':'score'})
for c in ['product', 'second_match']:
    final_df[c] = final_df[c].str.split('_').str.join(' ')
    final_df[c] = final_df[c].str.replace('NaN', '').str.strip()

final_df
           product     second_match     score
1  may dubai 200ml  mai dubai 200ml  0.666667
3  mai dubai 200ml  mai dubai 300ml  0.666667
5  mai milan 200ml  mai dubai 300ml  0.333333
7  may dubai 200ml  may dubai 300ml  0.666667

Beachte das:

  • Auf diese Weise erhalten Sie auch Elemente mit dem gleichen „zweitgrößte score“
  • Wenn Sie zwei oder mehr Einzelteile mit einem perfekten 1 Punktzahl, wird die Funktion nicht die zweitgrößte abzurufen.

Ich denke du magst

Origin http://43.154.161.224:23101/article/api/json?id=373196&siteId=1
Empfohlen
Rangfolge