カタログで最高の第二試合の製品を検索

インテリジェンス:

私は最高の第二試合で製品を検索するに取り組んでいます。カタログでは、我々は100%一致する製品を持っているかもしれませんが、目的は、最高の第二試合を取得することです。

以下のコードですが、それはベストマッチではなく、最良の第二試合のために実行されています。

私はこの時点で立ち往生しています。あなたはその周りにいくつかの解決策を見つけた場合、私に知らせてください。

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

電流出力:

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

所望の出力:

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

ありがとうございました

ベストフォン

FBruzzesi:

わからないこのアプローチは、あなたが探しているものが100%であるが、それは近いはず。

# 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

お知らせその:

  • このように、あなたはまた、同じ「二番目に大きいスコア」との項目を取得します
  • あなたは完璧な1点の得点を持つ2つの以上のアイテムを持っている場合、この関数は第二位を取得するために失敗します。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=373205&siteId=1