我有一個這樣的 df:
d = { 'user':[1,1,1,2,2,2,3,3],
'id': ['C1t','S4g','OT2','3Ba','d6a,'d9o','tot','p5t'],
'label': ['dog','cat','bird','table','tab','mop','mom','dad']}
df1 = pd.DataFrame(d)
print(df1)
user id label
0 1 C1t dog
1 1 Syg cat
2 1 OT2 bird
3 2 3Ba table
4 2 d6a tab
5 2 d9o mop
6 3 tot mom
7 3 p5t dad
然后我創建一個字典,每個用戶作為一個鍵,如下所示:
from collections import defaultdict
df_to_dict = defaultdict(list)
for index,row in df1.iterrows():
df_to_dict[row["user"]].append(
{"label": row["label"],
'id':row['id']})
print(df_to_dict)
defaultdict(list,
{1: [{'label': 'dog', 'id': 'C1t'},
{'label': 'cat', 'id': 'S4g'},
{'label': 'door', 'id': 'OT2'}],
2: [{'label': 'table', 'id': '3Ba'},
{'label': 'tab', 'id': 'd6a'},
{'label': 'mop', 'id': 'd9o'}],
3: [{'label': 'mom', 'id': 'tot'},
{'label': 'dad', 'id': 'p5t'}]})
現在我的目標是檢查“標簽”列的值對行的字串相似性,但僅限每個用戶(例如,對于用戶 1 dog-cat、cat-bird 和 dog-bird,并生成匯出的字串相似性索引從這個功能:
def split(word):
return [char for char in word]
# function for calculating the jaccard distance
def DistJaccard(str1, str2):
l1 = set(split(str1))
l2 = set(split(str2))
res = float(len(l1 & l2)) / len(l1 | l2)
return res
我想要的結果是創建一個資料框,其中包含與字串相似度函式進行比較的每條記錄的“用戶”、“id”列、來自它們的標簽和字串相似度分數。下面是這樣的:
user id1 id2 label1 label2 similarity score
0 1 C1t S4g dog cat 0.0
1 1 OT2 C1t door dog 0.5
2 1 OT2 S4g door cat 0.0
3 2 3Ba d6a table tab 0.6
4 2 d9o 3Ba mop table 0.0
5 2 d9o d6a mop tab 0.0
# and so on for the user 3
所以我的問題是如何為每個用戶的每對“標簽”應用字串相似度函式,然后將它們全部匯出到資料幀。關于如何解決這個問題的任何想法?我可以只用熊貓操作來做,但我必須先在字典中做,因為它更快。謝謝!
uj5u.com熱心網友回復:
def create_similarity_df(df_to_dict):
df_similarity = pd.DataFrame()
for user in df_to_dict:
for i in range(len(df_to_dict[user])):
for j in range(i 1,len(df_to_dict[user])):
df_similarity = df_similarity.append(
{'user':user,
'id1':df_to_dict[user][i]['id'],
'id2':df_to_dict[user][j]['id'],
'label1':df_to_dict[user][i]['label'],
'label2':df_to_dict[user][j]['label'],
'similarity':DistJaccard(df_to_dict[user][i]['label'],df_to_dict[user][j]['label'])},
ignore_index=True)
return df_similarity
產生
user id1 id2 label1 label2 similarity
0 1.0 C1t Syg dog cat 0.000000
1 1.0 C1t OT2 dog bird 0.166667
2 1.0 Syg OT2 cat bird 0.000000
3 2.0 eBa dFa table door 0.000000
4 2.0 eBa dzo table mop 0.000000
5 2.0 dFa dzo door mop 0.200000
6 3.0 tot p5t mom dad 0.000000
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/369427.html
