|
|
@ -1,17 +1,20 @@
|
|
|
|
from ConfigSpace import Categorical, Configuration, ConfigurationSpace, Integer
|
|
|
|
from ConfigSpace import Categorical, Configuration, ConfigurationSpace, Integer, Float
|
|
|
|
import py_entitymatching as em
|
|
|
|
import py_entitymatching as em
|
|
|
|
import py_entitymatching.catalog.catalog_manager as cm
|
|
|
|
import py_entitymatching.catalog.catalog_manager as cm
|
|
|
|
import pandas as pd
|
|
|
|
import pandas as pd
|
|
|
|
|
|
|
|
from py_entitymatching.blocker.blocker import Blocker
|
|
|
|
|
|
|
|
from py_entitymatching.matcher.mlmatcher import MLMatcher
|
|
|
|
|
|
|
|
|
|
|
|
from smac import HyperparameterOptimizationFacade, Scenario
|
|
|
|
from smac import HyperparameterOptimizationFacade, Scenario
|
|
|
|
from md_discovery.functions.multi_process_infer_by_pairs import my_Levenshtein_ratio
|
|
|
|
from md_discovery.functions.multi_process_infer_by_pairs import my_Levenshtein_ratio
|
|
|
|
# todo 距离度量用户可调
|
|
|
|
from entrance import *
|
|
|
|
|
|
|
|
# todo 距离度量用户可设置
|
|
|
|
|
|
|
|
|
|
|
|
# 全局变量,每次迭代后清空列表,加入新的md路径
|
|
|
|
# 全局变量,每次迭代后清空列表,加入新的md路径
|
|
|
|
# todo:
|
|
|
|
# todo:
|
|
|
|
# 默认路径为 "../md_discovery/output/xxx.txt"
|
|
|
|
# 默认路径为 "../md_discovery/output/xxx.txt"
|
|
|
|
# 真阳/假阴 mds/vio 共4个md文件
|
|
|
|
# 真阳/假阴 mds/vio 共4个md文件
|
|
|
|
md_paths = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def evaluate_prediction(df: pd.DataFrame, labeled_attr: str, predicted_attr: str, matching_number: int,
|
|
|
|
def evaluate_prediction(df: pd.DataFrame, labeled_attr: str, predicted_attr: str, matching_number: int,
|
|
|
@ -56,7 +59,9 @@ def load_mds(paths: list) -> list:
|
|
|
|
for line in f.readlines():
|
|
|
|
for line in f.readlines():
|
|
|
|
md_metadata = line.strip().split('\t')
|
|
|
|
md_metadata = line.strip().split('\t')
|
|
|
|
md = eval(md_metadata[0].replace('md:', ''))
|
|
|
|
md = eval(md_metadata[0].replace('md:', ''))
|
|
|
|
mds.append(md)
|
|
|
|
confidence = eval(md_metadata[2].replace('confidence:', ''))
|
|
|
|
|
|
|
|
if confidence > 0:
|
|
|
|
|
|
|
|
mds.append(md)
|
|
|
|
all_mds.extend(mds)
|
|
|
|
all_mds.extend(mds)
|
|
|
|
return all_mds
|
|
|
|
return all_mds
|
|
|
|
|
|
|
|
|
|
|
@ -80,65 +85,70 @@ class SVM:
|
|
|
|
def configspace(self) -> ConfigurationSpace:
|
|
|
|
def configspace(self) -> ConfigurationSpace:
|
|
|
|
# Build Configuration Space which defines all parameters and their ranges
|
|
|
|
# Build Configuration Space which defines all parameters and their ranges
|
|
|
|
cs = ConfigurationSpace(seed=0)
|
|
|
|
cs = ConfigurationSpace(seed=0)
|
|
|
|
|
|
|
|
# todo
|
|
|
|
l_overlap_attr = Categorical("l_overlap_attr", ["title", "description", "manufacturer", "price"], default="title")
|
|
|
|
# block_attr 取消打桩
|
|
|
|
|
|
|
|
block_attr = Categorical("block_attr", ["name", "description", "manufacturer", "price"], default="title")
|
|
|
|
overlap_size = Integer("overlap_size", (1, 3), default=1)
|
|
|
|
overlap_size = Integer("overlap_size", (1, 3), default=1)
|
|
|
|
|
|
|
|
ml_matcher = Categorical("ml_matcher", ["dt", "svm", "rf", "lg", "ln", "nb"], default="rf")
|
|
|
|
|
|
|
|
ml_blocker = Categorical("ml_blocker", ["over_lap", "attr_equiv"], default="over_lap")
|
|
|
|
|
|
|
|
|
|
|
|
cs.add_hyperparameters([l_overlap_attr, overlap_size])
|
|
|
|
cs.add_hyperparameters([block_attr, overlap_size, ml_matcher, ml_blocker])
|
|
|
|
|
|
|
|
|
|
|
|
return cs
|
|
|
|
return cs
|
|
|
|
|
|
|
|
|
|
|
|
# train 就是整个函数 只需将返回结果由预测变成预测结果的评估
|
|
|
|
# train 就是整个函数 只需将返回结果由预测变成预测结果的评估
|
|
|
|
def train(self, config: Configuration, seed: int = 0) -> float:
|
|
|
|
def train(self, config: Configuration) -> float:
|
|
|
|
path_Amazon = '/home/w/PycharmProjects/py_entitymatching/py_entitymatching/datasets/end-to-end/Amazon-GoogleProducts/Amazon.csv'
|
|
|
|
ltable = pd.read_csv(ltable_path, encoding='ISO-8859-1')
|
|
|
|
path_Google = '/home/w/PycharmProjects/py_entitymatching/py_entitymatching/datasets/end-to-end/Amazon-GoogleProducts/GoogleProducts.csv'
|
|
|
|
cm.set_key(ltable, ltable_id)
|
|
|
|
path_Mappings = '/home/w/PycharmProjects/py_entitymatching/py_entitymatching/datasets/end-to-end/Amazon-GoogleProducts/Amzon_GoogleProducts_perfectMapping.csv'
|
|
|
|
ltable.fillna("", inplace=True)
|
|
|
|
Amazon = pd.read_csv(path_Amazon, encoding='ISO-8859-1')
|
|
|
|
rtable = pd.read_csv(rtable_path, encoding='ISO-8859-1')
|
|
|
|
cm.set_key(Amazon, 'id')
|
|
|
|
cm.set_key(rtable, rtable_id)
|
|
|
|
Amazon.fillna("", inplace=True)
|
|
|
|
rtable.fillna("", inplace=True)
|
|
|
|
Google = pd.read_csv(path_Google, encoding='ISO-8859-1')
|
|
|
|
mappings = pd.read_csv(mapping_path)
|
|
|
|
cm.set_key(Google, 'id')
|
|
|
|
|
|
|
|
Google.fillna("", inplace=True)
|
|
|
|
|
|
|
|
Mappings = pd.read_csv(path_Mappings)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 仅保留两表中出现在映射表中的行,增大正样本比例
|
|
|
|
# 仅保留两表中出现在映射表中的行,增大正样本比例
|
|
|
|
l_id_list = []
|
|
|
|
lid_mapping_list = []
|
|
|
|
r_id_list = []
|
|
|
|
rid_mapping_list = []
|
|
|
|
# 全部转为字符串
|
|
|
|
# 全部转为字符串
|
|
|
|
Amazon = Amazon.astype(str)
|
|
|
|
ltable = ltable.astype(str)
|
|
|
|
Google = Google.astype(str)
|
|
|
|
rtable = rtable.astype(str)
|
|
|
|
Mappings = Mappings.astype(str)
|
|
|
|
mappings = mappings.astype(str)
|
|
|
|
matching_number = len(Mappings) # 所有阳性样本数,商品数据集应为1300
|
|
|
|
matching_number = len(mappings) # 所有阳性样本数,商品数据集应为1300
|
|
|
|
|
|
|
|
|
|
|
|
for index, row in Mappings.iterrows():
|
|
|
|
for index, row in mappings.iterrows():
|
|
|
|
l_id_list.append(row["idAmazon"])
|
|
|
|
lid_mapping_list.append(row[mapping_lid])
|
|
|
|
r_id_list.append(row["idGoogleBase"])
|
|
|
|
rid_mapping_list.append(row[mapping_rid])
|
|
|
|
selected_Amazon = Amazon[Amazon['id'].isin(l_id_list)]
|
|
|
|
|
|
|
|
selected_Google = Google[Google['id'].isin(r_id_list)]
|
|
|
|
selected_ltable = ltable[ltable[ltable_id].isin(lid_mapping_list)]
|
|
|
|
cm.set_key(selected_Amazon, 'id')
|
|
|
|
selected_ltable = selected_ltable.rename(columns=lr_attrs_map) # 参照右表,修改左表中与右表对应但不同名的字段
|
|
|
|
cm.set_key(selected_Google, 'id')
|
|
|
|
selected_rtable = rtable[rtable[rtable_id].isin(rid_mapping_list)]
|
|
|
|
|
|
|
|
selected_attrs = selected_ltable.columns.values.tolist() # 两张表中的字段名
|
|
|
|
# todo blocker可调
|
|
|
|
attrs_with_l_prefix = ['ltable_'+i for i in selected_attrs]
|
|
|
|
# 1.blocker类型(商品数据集可能只适合overlap)
|
|
|
|
attrs_with_r_prefix = ['rtable_'+i for i in selected_attrs]
|
|
|
|
# 2.overlap字段(对应关系)
|
|
|
|
cm.set_key(selected_ltable, ltable_id)
|
|
|
|
# 3.overlap_size
|
|
|
|
cm.set_key(selected_rtable, rtable_id)
|
|
|
|
blocker = em.OverlapBlocker()
|
|
|
|
|
|
|
|
overlap_attr = 'name' if config["l_overlap_attr"] == 'title' else config["l_overlap_attr"]
|
|
|
|
blocker = None
|
|
|
|
candidate = blocker.block_tables(selected_Amazon, selected_Google, config["l_overlap_attr"], overlap_attr,
|
|
|
|
if config["ml_blocker"] == "over_lap":
|
|
|
|
l_output_attrs=['id', 'title', 'description', 'manufacturer', 'price'],
|
|
|
|
blocker = em.OverlapBlocker()
|
|
|
|
r_output_attrs=['id', 'name', 'description', 'manufacturer', 'price'],
|
|
|
|
candidate = blocker.block_tables(selected_ltable, selected_rtable, config["block_attr"], config["block_attr"],
|
|
|
|
overlap_size=config["overlap_size"], show_progress=False)
|
|
|
|
l_output_attrs=selected_attrs, r_output_attrs=selected_attrs,
|
|
|
|
|
|
|
|
overlap_size=config["overlap_size"], show_progress=False, n_jobs=-1)
|
|
|
|
|
|
|
|
elif config["ml_blocker"] == "attr_equiv":
|
|
|
|
|
|
|
|
blocker = em.AttrEquivalenceBlocker()
|
|
|
|
|
|
|
|
candidate = blocker.block_tables(selected_ltable, selected_rtable, config["block_attr"], config["block_attr"],
|
|
|
|
|
|
|
|
l_output_attrs=selected_attrs, r_output_attrs=selected_attrs, n_jobs=-1)
|
|
|
|
|
|
|
|
|
|
|
|
candidate['gold'] = 0
|
|
|
|
candidate['gold'] = 0
|
|
|
|
|
|
|
|
|
|
|
|
candidate_match_rows = []
|
|
|
|
candidate_match_rows = []
|
|
|
|
for index, row in candidate.iterrows():
|
|
|
|
for index, row in candidate.iterrows():
|
|
|
|
l_id = row["ltable_id"]
|
|
|
|
l_id = row['ltable_' + ltable_id]
|
|
|
|
map_row = Mappings[Mappings['idAmazon'] == l_id]
|
|
|
|
map_row = mappings[mappings[mapping_lid] == l_id]
|
|
|
|
|
|
|
|
|
|
|
|
if map_row is not None:
|
|
|
|
if map_row is not None:
|
|
|
|
r_id = map_row["idGoogleBase"]
|
|
|
|
r_id = map_row[mapping_rid]
|
|
|
|
for value in r_id:
|
|
|
|
for value in r_id:
|
|
|
|
if value == row["rtable_id"]:
|
|
|
|
if value == row['rtable_' + rtable_id]:
|
|
|
|
candidate_match_rows.append(row["_id"])
|
|
|
|
candidate_match_rows.append(row["_id"])
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
continue
|
|
|
@ -148,14 +158,15 @@ class SVM:
|
|
|
|
# 裁剪负样本,保持正负样本数量一致
|
|
|
|
# 裁剪负样本,保持正负样本数量一致
|
|
|
|
candidate_mismatch = candidate[candidate['gold'] == 0]
|
|
|
|
candidate_mismatch = candidate[candidate['gold'] == 0]
|
|
|
|
candidate_match = candidate[candidate['gold'] == 1]
|
|
|
|
candidate_match = candidate[candidate['gold'] == 1]
|
|
|
|
candidate_mismatch = candidate_mismatch.sample(n=len(candidate_match))
|
|
|
|
if len(candidate_mismatch) > len(candidate_match):
|
|
|
|
|
|
|
|
candidate_mismatch = candidate_mismatch.sample(n=len(candidate_match))
|
|
|
|
# 拼接正负样本
|
|
|
|
# 拼接正负样本
|
|
|
|
candidate_for_train_test = pd.concat([candidate_mismatch, candidate_match])
|
|
|
|
candidate_for_train_test = pd.concat([candidate_mismatch, candidate_match])
|
|
|
|
cm.set_key(candidate_for_train_test, '_id')
|
|
|
|
cm.set_key(candidate_for_train_test, '_id')
|
|
|
|
cm.set_fk_ltable(candidate_for_train_test, 'ltable_id')
|
|
|
|
cm.set_fk_ltable(candidate_for_train_test, 'ltable_' + ltable_id)
|
|
|
|
cm.set_fk_rtable(candidate_for_train_test, 'rtable_id')
|
|
|
|
cm.set_fk_rtable(candidate_for_train_test, 'rtable_' + rtable_id)
|
|
|
|
cm.set_ltable(candidate_for_train_test, selected_Amazon)
|
|
|
|
cm.set_ltable(candidate_for_train_test, selected_ltable)
|
|
|
|
cm.set_rtable(candidate_for_train_test, selected_Google)
|
|
|
|
cm.set_rtable(candidate_for_train_test, selected_rtable)
|
|
|
|
|
|
|
|
|
|
|
|
# 分为训练测试集
|
|
|
|
# 分为训练测试集
|
|
|
|
train_proportion = 0.7
|
|
|
|
train_proportion = 0.7
|
|
|
@ -164,8 +175,20 @@ class SVM:
|
|
|
|
train_set = sets['train']
|
|
|
|
train_set = sets['train']
|
|
|
|
test_set = sets['test']
|
|
|
|
test_set = sets['test']
|
|
|
|
|
|
|
|
|
|
|
|
rf = em.RFMatcher(name='RF', random_state=0)
|
|
|
|
matcher = None
|
|
|
|
feature_table = em.get_features_for_matching(selected_Amazon, selected_Google, validate_inferred_attr_types=False)
|
|
|
|
if config["ml_matcher"] == "dt":
|
|
|
|
|
|
|
|
matcher = em.DTMatcher(name='DecisionTree', random_state=0)
|
|
|
|
|
|
|
|
elif config["ml_matcher"] == "svm":
|
|
|
|
|
|
|
|
matcher = em.SVMMatcher(name='SVM', random_state=0)
|
|
|
|
|
|
|
|
elif config["ml_matcher"] == "rf":
|
|
|
|
|
|
|
|
matcher = em.RFMatcher(name='RF', random_state=0)
|
|
|
|
|
|
|
|
elif config["ml_matcher"] == "lg":
|
|
|
|
|
|
|
|
matcher = em.LogRegMatcher(name='LogReg', random_state=0)
|
|
|
|
|
|
|
|
elif config["ml_matcher"] == "ln":
|
|
|
|
|
|
|
|
matcher = em.LinRegMatcher(name='LinReg')
|
|
|
|
|
|
|
|
elif config["ml_matcher"] == "nb":
|
|
|
|
|
|
|
|
matcher = em.NBMatcher(name='NaiveBayes')
|
|
|
|
|
|
|
|
feature_table = em.get_features_for_matching(selected_ltable, selected_rtable, validate_inferred_attr_types=False)
|
|
|
|
|
|
|
|
|
|
|
|
train_feature_vecs = em.extract_feature_vecs(train_set,
|
|
|
|
train_feature_vecs = em.extract_feature_vecs(train_set,
|
|
|
|
feature_table=feature_table,
|
|
|
|
feature_table=feature_table,
|
|
|
@ -180,12 +203,12 @@ class SVM:
|
|
|
|
# todo 参数可调 用drop删除特征向量中的列?
|
|
|
|
# todo 参数可调 用drop删除特征向量中的列?
|
|
|
|
# 1.exclude_attrs
|
|
|
|
# 1.exclude_attrs
|
|
|
|
# 去掉id相关的相似度
|
|
|
|
# 去掉id相关的相似度
|
|
|
|
rf.fit(table=train_feature_vecs,
|
|
|
|
matcher.fit(table=train_feature_vecs,
|
|
|
|
exclude_attrs=['_id', 'ltable_id', 'rtable_id', 'gold'],
|
|
|
|
exclude_attrs=['_id', 'ltable_id', 'rtable_id', 'gold'],
|
|
|
|
target_attr='gold')
|
|
|
|
target_attr='gold')
|
|
|
|
|
|
|
|
|
|
|
|
# 1.exclude_attrs
|
|
|
|
# 1.exclude_attrs
|
|
|
|
predictions = rf.predict(table=test_feature_vecs, exclude_attrs=['_id', 'ltable_id', 'rtable_id', 'ltable_title',
|
|
|
|
predictions = matcher.predict(table=test_feature_vecs, exclude_attrs=['_id', 'ltable_id', 'rtable_id', 'ltable_title',
|
|
|
|
'ltable_description', 'ltable_manufacturer',
|
|
|
|
'ltable_description', 'ltable_manufacturer',
|
|
|
|
'ltable_price', 'rtable_name', 'rtable_description',
|
|
|
|
'ltable_price', 'rtable_name', 'rtable_description',
|
|
|
|
'rtable_manufacturer', 'rtable_price', 'gold'],
|
|
|
|
'rtable_manufacturer', 'rtable_price', 'gold'],
|
|
|
@ -196,9 +219,14 @@ class SVM:
|
|
|
|
print(indicators)
|
|
|
|
print(indicators)
|
|
|
|
|
|
|
|
|
|
|
|
# 计算可解释性
|
|
|
|
# 计算可解释性
|
|
|
|
predictions = predictions[
|
|
|
|
predictions_attrs = []
|
|
|
|
['ltable_id', 'rtable_id', 'ltable_name', 'ltable_description', 'ltable_manufacturer', 'ltable_price',
|
|
|
|
predictions_attrs.extend(attrs_with_l_prefix)
|
|
|
|
'rtable_name', 'rtable_description', 'rtable_manufacturer', 'rtable_price', 'gold', 'predicted']]
|
|
|
|
predictions_attrs.extend(attrs_with_r_prefix)
|
|
|
|
|
|
|
|
predictions_attrs.extend(['gold', 'predicted'])
|
|
|
|
|
|
|
|
predictions = predictions[predictions_attrs]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
md_paths = ['../md_discovery/output/tp_mds.txt', '../md_discovery/output/tp_vio.txt',
|
|
|
|
|
|
|
|
'../md_discovery/output/fn_mds.txt', '../md_discovery/output/fn_vio.txt']
|
|
|
|
epl_match = 0 # 可解释,预测match
|
|
|
|
epl_match = 0 # 可解释,预测match
|
|
|
|
nepl_mismatch = 0 # 不可解释,预测mismatch
|
|
|
|
nepl_mismatch = 0 # 不可解释,预测mismatch
|
|
|
|
md_list = load_mds(md_paths) # 从全局变量中读取所有的md
|
|
|
|
md_list = load_mds(md_paths) # 从全局变量中读取所有的md
|
|
|
@ -211,8 +239,7 @@ class SVM:
|
|
|
|
nepl_mismatch += 1
|
|
|
|
nepl_mismatch += 1
|
|
|
|
epl_ability = (epl_match + nepl_mismatch) / len(predictions) # 可解释性
|
|
|
|
epl_ability = (epl_match + nepl_mismatch) / len(predictions) # 可解释性
|
|
|
|
f1 = indicators['F1']
|
|
|
|
f1 = indicators['F1']
|
|
|
|
performance = 0.5 * epl_ability + 0.5 * f1 # 可解释性与F1的权重暂时定为0.5
|
|
|
|
performance = interpretability_weight * epl_ability + (1 - interpretability_weight) * f1
|
|
|
|
# todo 权重用户可调
|
|
|
|
|
|
|
|
return 1 - performance
|
|
|
|
return 1 - performance
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -235,6 +262,9 @@ if __name__ == "__main__":
|
|
|
|
overwrite=True, # If the run exists, we overwrite it; alternatively, we can continue from last state
|
|
|
|
overwrite=True, # If the run exists, we overwrite it; alternatively, we can continue from last state
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# todo
|
|
|
|
|
|
|
|
# 如果new_recall过低则避免其成为最优解
|
|
|
|
|
|
|
|
# 将损失函数置为1/用new_recall降低F1从而提高损失函数
|
|
|
|
incumbent = smac.optimize()
|
|
|
|
incumbent = smac.optimize()
|
|
|
|
|
|
|
|
|
|
|
|
# Get cost of default configuration
|
|
|
|
# Get cost of default configuration
|
|
|
|