|
|
|
@ -1,4 +1,5 @@
|
|
|
|
|
from ConfigSpace import Categorical, Configuration, ConfigurationSpace, Integer, Float
|
|
|
|
|
import os
|
|
|
|
|
from ConfigSpace import Categorical, Configuration, ConfigurationSpace, Integer
|
|
|
|
|
from ConfigSpace.conditions import InCondition
|
|
|
|
|
import py_entitymatching as em
|
|
|
|
|
import py_entitymatching.catalog.catalog_manager as cm
|
|
|
|
@ -6,15 +7,13 @@ import pandas as pd
|
|
|
|
|
|
|
|
|
|
from smac import HyperparameterOptimizationFacade, Scenario
|
|
|
|
|
from md_discovery.functions.multi_process_infer_by_pairs import my_Levenshtein_ratio
|
|
|
|
|
from entrance import *
|
|
|
|
|
from settings import *
|
|
|
|
|
|
|
|
|
|
# 数据在外部加载
|
|
|
|
|
########################################################################################################################
|
|
|
|
|
ltable = pd.read_csv(ltable_path, encoding='ISO-8859-1')
|
|
|
|
|
cm.set_key(ltable, ltable_id)
|
|
|
|
|
ltable.fillna("", inplace=True)
|
|
|
|
|
rtable = pd.read_csv(rtable_path, encoding='ISO-8859-1')
|
|
|
|
|
cm.set_key(rtable, rtable_id)
|
|
|
|
|
rtable.fillna("", inplace=True)
|
|
|
|
|
mappings = pd.read_csv(mapping_path)
|
|
|
|
|
|
|
|
|
@ -32,21 +31,12 @@ for index, row in mappings.iterrows():
|
|
|
|
|
# 仅保留两表中出现在映射表中的行,增大正样本比例
|
|
|
|
|
selected_ltable = ltable[ltable[ltable_id].isin(lid_mapping_list)]
|
|
|
|
|
selected_ltable = selected_ltable.rename(columns=lr_attrs_map) # 参照右表,修改左表中与右表对应但不同名的字段
|
|
|
|
|
tables_id = rtable_id # 不论左表右表ID字段名是否一致,经上一行调整,统一以右表为准
|
|
|
|
|
selected_rtable = rtable[rtable[rtable_id].isin(rid_mapping_list)]
|
|
|
|
|
selected_attrs = selected_ltable.columns.values.tolist() # 两张表中的字段名
|
|
|
|
|
attrs_with_l_prefix = ['ltable_'+i for i in selected_attrs]
|
|
|
|
|
attrs_with_r_prefix = ['rtable_'+i for i in selected_attrs]
|
|
|
|
|
cm.set_key(selected_ltable, ltable_id)
|
|
|
|
|
cm.set_key(selected_rtable, rtable_id)
|
|
|
|
|
########################################################################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_test():
|
|
|
|
|
block_attr_items = selected_attrs[:]
|
|
|
|
|
block_attr_items.remove(rtable_id)
|
|
|
|
|
print(block_attr_items)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def evaluate_prediction(df: pd.DataFrame, labeled_attr: str, predicted_attr: str, matching_number: int,
|
|
|
|
|
test_proportion: float) -> dict:
|
|
|
|
|
new_df = df.reset_index(drop=False, inplace=False)
|
|
|
|
@ -82,6 +72,8 @@ def load_mds(paths: list) -> list:
|
|
|
|
|
all_mds = []
|
|
|
|
|
# 传入md路径列表
|
|
|
|
|
for md_path in paths:
|
|
|
|
|
if not os.path.exists(md_path):
|
|
|
|
|
continue
|
|
|
|
|
mds = []
|
|
|
|
|
# 打开每一个md文件
|
|
|
|
|
with open(md_path, 'r') as f:
|
|
|
|
@ -102,7 +94,7 @@ def is_explicable(row, all_mds: list) -> bool:
|
|
|
|
|
explicable = True # 假设这条md能解释当前元组
|
|
|
|
|
for a in attrs:
|
|
|
|
|
threshold = md[a]
|
|
|
|
|
if my_Levenshtein_ratio(str(getattr(row, 'ltable_'+a)), str(getattr(row, 'rtable_'+a))) < threshold:
|
|
|
|
|
if my_Levenshtein_ratio(str(getattr(row, 'ltable_' + a)), str(getattr(row, 'rtable_' + a))) < threshold:
|
|
|
|
|
explicable = False # 任意一个字段的相似度达不到阈值,这条md就不能解释当前元组
|
|
|
|
|
break # 不再与当前md的其他相似度阈值比较,跳转到下一条md
|
|
|
|
|
if explicable:
|
|
|
|
@ -116,13 +108,12 @@ class Classifier:
|
|
|
|
|
# Build Configuration Space which defines all parameters and their ranges
|
|
|
|
|
cs = ConfigurationSpace(seed=0)
|
|
|
|
|
block_attr_items = selected_attrs[:]
|
|
|
|
|
block_attr_items.remove(rtable_id)
|
|
|
|
|
block_attr_items.remove(tables_id)
|
|
|
|
|
|
|
|
|
|
block_attr = Categorical("block_attr", block_attr_items)
|
|
|
|
|
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")
|
|
|
|
|
# todo 其他可调参数(如feature table删去某列)
|
|
|
|
|
|
|
|
|
|
use_overlap_size = InCondition(child=overlap_size, parent=ml_blocker, values=["over_lap"])
|
|
|
|
|
cs.add_hyperparameters([block_attr, overlap_size, ml_matcher, ml_blocker])
|
|
|
|
@ -131,6 +122,11 @@ class Classifier:
|
|
|
|
|
|
|
|
|
|
# train 就是整个函数 只需将返回结果由预测变成预测结果的评估
|
|
|
|
|
def train(self, config: Configuration, seed: int = 0) -> float:
|
|
|
|
|
|
|
|
|
|
attrs_with_l_prefix = ['ltable_' + i for i in selected_attrs] # 字段名加左前缀
|
|
|
|
|
attrs_with_r_prefix = ['rtable_' + i for i in selected_attrs] # 字段名加右前缀
|
|
|
|
|
cm.set_key(selected_ltable, tables_id)
|
|
|
|
|
cm.set_key(selected_rtable, tables_id)
|
|
|
|
|
if config["ml_blocker"] == "over_lap":
|
|
|
|
|
blocker = em.OverlapBlocker()
|
|
|
|
|
candidate = blocker.block_tables(selected_ltable, selected_rtable, config["block_attr"], config["block_attr"],
|
|
|
|
@ -145,13 +141,13 @@ class Classifier:
|
|
|
|
|
|
|
|
|
|
candidate_match_rows = []
|
|
|
|
|
for index, row in candidate.iterrows():
|
|
|
|
|
l_id = row['ltable_' + ltable_id]
|
|
|
|
|
l_id = row['ltable_' + tables_id]
|
|
|
|
|
map_row = mappings[mappings[mapping_lid] == l_id]
|
|
|
|
|
|
|
|
|
|
if map_row is not None:
|
|
|
|
|
r_id = map_row[mapping_rid]
|
|
|
|
|
for value in r_id:
|
|
|
|
|
if value == row['rtable_' + rtable_id]:
|
|
|
|
|
if value == row['rtable_' + tables_id]:
|
|
|
|
|
candidate_match_rows.append(row["_id"])
|
|
|
|
|
else:
|
|
|
|
|
continue
|
|
|
|
@ -165,9 +161,12 @@ class Classifier:
|
|
|
|
|
candidate_mismatch = candidate_mismatch.sample(n=len(candidate_match))
|
|
|
|
|
# 拼接正负样本
|
|
|
|
|
candidate_for_train_test = pd.concat([candidate_mismatch, candidate_match])
|
|
|
|
|
if len(candidate_for_train_test) == 0:
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
cm.set_key(candidate_for_train_test, '_id')
|
|
|
|
|
cm.set_fk_ltable(candidate_for_train_test, 'ltable_' + ltable_id)
|
|
|
|
|
cm.set_fk_rtable(candidate_for_train_test, 'rtable_' + rtable_id)
|
|
|
|
|
cm.set_fk_ltable(candidate_for_train_test, 'ltable_' + tables_id)
|
|
|
|
|
cm.set_fk_rtable(candidate_for_train_test, 'rtable_' + tables_id)
|
|
|
|
|
cm.set_ltable(candidate_for_train_test, selected_ltable)
|
|
|
|
|
cm.set_rtable(candidate_for_train_test, selected_rtable)
|
|
|
|
|
|
|
|
|
@ -178,7 +177,18 @@ class Classifier:
|
|
|
|
|
train_set = sets['train']
|
|
|
|
|
test_set = sets['test']
|
|
|
|
|
|
|
|
|
|
matcher = None
|
|
|
|
|
cm.set_key(train_set, '_id')
|
|
|
|
|
cm.set_fk_ltable(train_set, 'ltable_' + tables_id)
|
|
|
|
|
cm.set_fk_rtable(train_set, 'rtable_' + tables_id)
|
|
|
|
|
cm.set_ltable(train_set, selected_ltable)
|
|
|
|
|
cm.set_rtable(train_set, selected_rtable)
|
|
|
|
|
|
|
|
|
|
cm.set_key(test_set, '_id')
|
|
|
|
|
cm.set_fk_ltable(test_set, 'ltable_' + tables_id)
|
|
|
|
|
cm.set_fk_rtable(test_set, 'rtable_' + tables_id)
|
|
|
|
|
cm.set_ltable(test_set, selected_ltable)
|
|
|
|
|
cm.set_rtable(test_set, selected_rtable)
|
|
|
|
|
|
|
|
|
|
if config["ml_matcher"] == "dt":
|
|
|
|
|
matcher = em.DTMatcher(name='DecisionTree', random_state=0)
|
|
|
|
|
elif config["ml_matcher"] == "svm":
|
|
|
|
@ -198,24 +208,20 @@ class Classifier:
|
|
|
|
|
attrs_after=['gold'],
|
|
|
|
|
show_progress=False)
|
|
|
|
|
|
|
|
|
|
# todo 属性名解耦
|
|
|
|
|
test_feature_after = attrs_with_l_prefix[:]
|
|
|
|
|
test_feature_after.extend(attrs_with_r_prefix)
|
|
|
|
|
for _ in test_feature_after:
|
|
|
|
|
if _.endswith(tables_id):
|
|
|
|
|
test_feature_after.remove(_)
|
|
|
|
|
test_feature_after.append('gold')
|
|
|
|
|
test_feature_vecs = em.extract_feature_vecs(test_set, feature_table=feature_table,
|
|
|
|
|
attrs_after=['ltable_title', 'ltable_description', 'ltable_manufacturer',
|
|
|
|
|
'ltable_price', 'rtable_name', 'rtable_description',
|
|
|
|
|
'rtable_manufacturer', 'rtable_price', 'gold'], show_progress=False)
|
|
|
|
|
|
|
|
|
|
# todo 参数可调 用drop删除特征向量中的列?
|
|
|
|
|
# 1.exclude_attrs
|
|
|
|
|
# 去掉id相关的相似度
|
|
|
|
|
matcher.fit(table=train_feature_vecs,
|
|
|
|
|
exclude_attrs=['_id', 'ltable_id', 'rtable_id', 'gold'],
|
|
|
|
|
target_attr='gold')
|
|
|
|
|
|
|
|
|
|
# 1.exclude_attrs
|
|
|
|
|
predictions = matcher.predict(table=test_feature_vecs, exclude_attrs=['_id', 'ltable_id', 'rtable_id', 'ltable_title',
|
|
|
|
|
'ltable_description', 'ltable_manufacturer',
|
|
|
|
|
'ltable_price', 'rtable_name', 'rtable_description',
|
|
|
|
|
'rtable_manufacturer', 'rtable_price', 'gold'],
|
|
|
|
|
attrs_after=test_feature_after, show_progress=False)
|
|
|
|
|
|
|
|
|
|
fit_exclude = ['_id', 'ltable_' + tables_id, 'rtable_' + tables_id, 'gold']
|
|
|
|
|
matcher.fit(table=train_feature_vecs, exclude_attrs=fit_exclude, target_attr='gold')
|
|
|
|
|
|
|
|
|
|
test_feature_after.extend(['_id', 'ltable_' + tables_id, 'rtable_' + tables_id])
|
|
|
|
|
predictions = matcher.predict(table=test_feature_vecs, exclude_attrs=test_feature_after,
|
|
|
|
|
append=True, target_attr='predicted', inplace=False)
|
|
|
|
|
eval_result = em.eval_matches(predictions, 'gold', 'predicted')
|
|
|
|
|
em.print_eval_summary(eval_result)
|
|
|
|
@ -231,11 +237,12 @@ class Classifier:
|
|
|
|
|
|
|
|
|
|
# 默认路径为 "../md_discovery/output/xxx.txt"
|
|
|
|
|
# 真阳/假阴 mds/vio 共4个md文件
|
|
|
|
|
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']
|
|
|
|
|
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
|
|
|
|
|
nepl_mismatch = 0 # 不可解释,预测mismatch
|
|
|
|
|
md_list = load_mds(md_paths) # 从全局变量中读取所有的md
|
|
|
|
|
if len(md_list) > 0:
|
|
|
|
|
for row in predictions.itertuples():
|
|
|
|
|
if is_explicable(row, md_list):
|
|
|
|
|
if getattr(row, 'predicted') == 1:
|
|
|
|
@ -243,22 +250,29 @@ class Classifier:
|
|
|
|
|
else:
|
|
|
|
|
if getattr(row, 'predicted') == 0:
|
|
|
|
|
nepl_mismatch += 1
|
|
|
|
|
epl_ability = (epl_match + nepl_mismatch) / len(predictions) # 可解释性
|
|
|
|
|
f1 = indicators['F1']
|
|
|
|
|
performance = interpretability_weight * epl_ability + (1 - interpretability_weight) * f1
|
|
|
|
|
interpretability = (epl_match + nepl_mismatch) / len(predictions) # 可解释性
|
|
|
|
|
# if indicators["my_recall"] >= 0.8:
|
|
|
|
|
# f1 = indicators["F1"]
|
|
|
|
|
# else:
|
|
|
|
|
# f1 = (2.0 * indicators["precision"] * indicators["my_recall"]) / (indicators["precision"] + indicators["my_recall"])
|
|
|
|
|
if indicators["my_recall"] < 0.8:
|
|
|
|
|
return 1
|
|
|
|
|
f1 = indicators["F1"]
|
|
|
|
|
performance = interpre_weight * interpretability + (1 - interpre_weight) * f1
|
|
|
|
|
return 1 - performance
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
def ml_er_hpo():
|
|
|
|
|
classifier = Classifier()
|
|
|
|
|
|
|
|
|
|
# Next, we create an object, holding general information about the run
|
|
|
|
|
scenario = Scenario(
|
|
|
|
|
classifier.configspace,
|
|
|
|
|
n_trials=12, # We want to run max 50 trials (combination of config and seed)
|
|
|
|
|
deterministic=True,
|
|
|
|
|
n_trials=10, # We want to run max 50 trials (combination of config and seed)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
initial_design = HyperparameterOptimizationFacade.get_initial_design(scenario, n_configs=3)
|
|
|
|
|
initial_design = HyperparameterOptimizationFacade.get_initial_design(scenario, n_configs=5)
|
|
|
|
|
|
|
|
|
|
# Now we use SMAC to find the best hyperparameters
|
|
|
|
|
smac = HyperparameterOptimizationFacade(
|
|
|
|
@ -268,9 +282,6 @@ if __name__ == "__main__":
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
# Get cost of default configuration
|
|
|
|
@ -280,6 +291,6 @@ if __name__ == "__main__":
|
|
|
|
|
# Let's calculate the cost of the incumbent
|
|
|
|
|
incumbent_cost = smac.validate(incumbent)
|
|
|
|
|
print(f"Incumbent cost: {incumbent_cost}")
|
|
|
|
|
|
|
|
|
|
print(f"Configuration:{incumbent.values()}")
|
|
|
|
|
print(f"MAX_F1:{1-classifier.train(incumbent)}")
|
|
|
|
|
|
|
|
|
|
return incumbent
|