gpt4 book ai didi

记录链接 : how to pair only best matches and export a merged table?

转载 作者:行者123 更新时间:2023-12-02 01:16:09 25 4
gpt4 key购买 nike

我正在尝试使用 R 包 RecordLinkage 将采购订单列表中的项目与主目录中的条目进行匹配。下面是 R 代码和使用两个虚拟数据集(DOrders 和 DCatalogue)的可重现示例:

DOrders <- structure(list(Product = structure(c(1L, 2L, 7L, 3L, 4L, 5L, 
6L), .Label = c("31471 - SOFTSILK 2.0 SCREW 7mm x 20mm", "Copier paper white A4 80gsm",
"High resilience memory foam standard mattress", "Liston forceps bone cutting 152mm",
"Micro reciprocating blade 25.4mm x 8.0mm x 0.38mm", "Micro reciprocating blade 39.5 x 7.0 x 0.38",
"microaire dual tooth 18 x 90 x 0.89"), class = "factor"), Supplier = structure(c(5L,
6L, 2L, 1L, 4L, 3L, 3L), .Label = c("KAROMED LTD", "Morgan Steer Ortho Limited",
"ORTHOPAEDIC SOLUTIONS", "SURGICAL HOLDINGS", "T J SMITH NEPHEW LTD",
"XEROX SOLUTIONS"), class = "factor"), UOI = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 2L), .Label = c("Each", "Pack"), class = "factor"),
Price = c(5.99, 6.99, 40, 230, 35, 80, 79)), .Names = c("Product",
"Supplier", "UOI", "Price"), class = "data.frame", row.names = c(NA,
-7L))

DCatalogue <- structure(list(Product = structure(c(7L, 3L, 4L, 5L, 6L, 2L,
8L, 1L), .Label = c("7.0mm cann canc scr 32x80mm non sterile single use",
"A4 80gsm white copier paper", "High resilience memory foam standard hospital mattress with stitched seams has a fully enclosing cover",
"Liston bone cutting forceps with fluted handle straight 152mm",
"Micro reciprocating blade 25.4mm x 8.0mm x 0.38mm", "Micro reciprocating blade 39.5mm x 7.0mm x 0.38mm",
"microaire large osc dual tooth 18mm x 90mm x 0.89mm", "Softsilk 2.0 pkg 7x20 ster"
), class = "factor"), Supplier = structure(c(3L, 2L, 6L, 4L,
4L, 7L, 5L, 1L), .Label = c("BIOMET MERCK LTD", "KAROMED LIMITED",
"MORGAN STEER ORTHOPAEDICS LTD", "ORTHO SOLUTIONS", "SMITH & NEPHEW ADVANCED SURGICAL DEVICES",
"SURGICAL HOLDINGS", "XEROX"), class = "factor"), UOI = structure(c(1L,
1L, 1L, 2L, 2L, 1L, 1L, 1L), .Label = c("Each", "Pack"), class = "factor"),
RefPrice = c(38.7, 274.18, 34.96, 79.48, 81.29, 6.99, 5.99,
5)), .Names = c("Product", "Supplier", "UOI", "RefPrice"), class = "data.frame", row.names = c(NA,
-8L))

出于实验目的,DOrders 有 7 个条目,每个条目与引用集 DCatalogue 中的九行之一匹配。在真实数据中,并非所有订单都会匹配。

head(DOrders)
Product Supplier UOI Price
1 31471 - SOFTSILK 2.0 SCREW 7mm x 20mm T J SMITH NEPHEW LTD Each 5.99
2 Copier paper white A4 80gsm XEROX SOLUTIONS Each 6.99
3 microaire dual tooth 18 x 90 x 0.89 Morgan Steer Ortho Limited Each 40.00
4 High resilience memory foam standard mattress KAROMED LTD Each 230.00
5 Liston forceps bone cutting 152mm SURGICAL HOLDINGS Each 35.00
6 Micro reciprocating blade 25.4mm x 8.0mm x 0.38mm ORTHOPAEDIC SOLUTIONS Each 80.00

> head(DCatalogue)
Product Supplier UOI RefPrice
1 microaire large osc dual tooth 18mm x 90mm x 0.89mm MORGAN STEER ORTHOPAEDICS LTD Each 38.70
2 High resilience memory foam standard hospital mattress with stitched seams has a fully enclosing cover KAROMED LIMITED Each 274.18
3 Liston bone cutting forceps with fluted handle straight 152mm SURGICAL HOLDINGS Each 34.96
4 Micro reciprocating blade 25.4mm x 8.0mm x 0.38mm ORTHO SOLUTIONS Pack 79.48
5 Micro reciprocating blade 39.5mm x 7.0mm x 0.38mm ORTHO SOLUTIONS Pack 81.29
6 A4 80gsm white copier paper XEROX Each 6.99

链接的第一步是确保项目按发行单位 (UOI) 匹配。这是因为一包元素显然与一个单位不同,即使元素完全相同。例如:

Micro reciprocating blade 25.4mm x 8.0mm x 0.38mm      ORTHOPAEDIC SOLUTIONS Each  80.00

是相同的项目,但应该不匹配:

Micro reciprocating blade 25.4mm x 8.0mm x 0.38mm               ORTHO SOLUTIONS Pack    79.48

因此,我使用阻塞参数 blockfld = 3 来尝试仅匹配第三列中具有相同值的条目。另外,使用 exclude = 4 来从匹配中排除价格。这在订单和目录之间是不同的,并且本身就是匹配的主要兴趣。匹配是使用产品和供应商名称上的 jarowinkler 字符串比较器(如 here 中所述)完成的:

library(RecordLinkage)

rpairs <- compare.linkage(DOrders, DCatalogue,
blockfld = 3,
exclude = 4,
strcmp = 1:2,
strcmpfun = jarowinkler)

接下来,我使用 Contiero 等人计算每对的权重。 (2005)方法:

rpairs <- epiWeights(rpairs)
> summary(rpairs)
Weight distribution:

[0.3,0.4] (0.4,0.5] (0.5,0.6] (0.6,0.7] (0.7,0.8] (0.8,0.9] (0.9,1]
1 1 19 10 3 0 4

根据此分布,我想仅将那些权重 > 0.7 的对分类为匹配

result <- epiClassify(rpairs, 0.7)
> summary(result)
7 links detected
0 possible links detected
31 non-links detected

这是我所了解的,但是存在一些问题。

首先,getPairs(result) 显示 DOrders 中的一个条目可以与 DCatalogue 中的多个条目进行高权重匹配。例如

这对匹配正确,权重为0.948

Micro reciprocating blade 39.5 x 7.0 x 0.38 ORTHOPAEDIC SOLUTIONS   Pack    79  
Micro reciprocating blade 39.5mm x 7.0mm x 0.38mm ORTHO SOLUTIONS Pack 81.29 0.9480503

但也与权重 0.928 匹配不正确:

Micro reciprocating blade 39.5 x 7.0 x 0.38 ORTHOPAEDIC SOLUTIONS   Pack    79  
Micro reciprocating blade 25.4mm x 8.0mm x 0.38mm ORTHO SOLUTIONS Pack 79.48 0.9283522

显然,我需要将配对限制为只有一个权重最高的最佳匹配,但该怎么做呢?

最后,我正在寻找的最终结果是一个合并的数据集,其中在一行中包含来自订单和目录的匹配条目,并且两个原始集合中的所有列并排进行比较。 getPairs 以一种尴尬的格式生成输出:

> getPairs(result)
id Product Supplier UOI Price Weight
1 7 Micro reciprocating blade 39.5 x 7.0 x 0.38 ORTHOPAEDIC SOLUTIONS Pack 79
2 5 Micro reciprocating blade 39.5mm x 7.0mm x 0.38mm ORTHO SOLUTIONS Pack 81.29 0.9480503
3
4 5 Liston forceps bone cutting 152mm SURGICAL HOLDINGS Each 35
5 3 Liston bone cutting forceps with fluted handle straight 152mm SURGICAL HOLDINGS Each 34.96 0.9329244
...

最佳答案

首先,感谢您提供了一个可重现的示例,这大大方便了回答您的问题。我将从你的第二个问题开始:

And finally, the end result that I am looking for is a merged dataset that contains matched entries from both Orders and Catalogue in one row, with all columns from both original sets side by side for comparison.

使用single.rows=TRUE,getPairs 在一行中列出两个条目。此外,show="links" 将输出限制为属于一起的对(有关详细信息,请参阅 ?getPairs):

> matchedPairs <- getPairs(result, single.rows=TRUE, show="links")

但是,这不会将匹配的列彼此相邻,而是记录一的所有列后面跟着记录二的所有列(最后将匹配权重作为最后一列)。我在这里只显示列名称,因为整个表非常宽:

> names(matchedPairs)
[1] "id1" "Product.1" "Supplier.1" "UOI.1" "Price.1" "id2" "Product.2" "Supplier.2" "UOI.2" "RefPrice.2" "Weight"

因此,如果您想以这种格式直接进行列与列的比较,则必须重新排列列以满足您的需求。

Obviously, I need to restrict pairing to only one best match with the highest weight, but how to do it?

该功能不是由包提供的,我相信从记录链接结果中选择一对一分配的过程本身需要一些概念上的关注。我从未深入研究过这一步,因此以下内容可能只是一个开始的想法。您可以使用 data.table 库从具有相同左侧 id 的每组对中选择具有最大权重的对(比较 How to select the row with the maximum value in each group ):

> library(data.table)
> matchedPairs <- data.table(matchedPairs)
> matchedPairs[matchedPairs[,.I[which.max(Weight)],by=id1]$V1, list(id1,id2)]
id1 id2
1: 7 5
2: 5 3
3: 4 2
4: 2 6
5: 6 1
6: 3 1

此处,list(id1,id2) 将输出限制为记录 ID。

为了消除右侧 id 的双重映射(在本例中,1 对于 id2 出现两次),您必须对 id2 重复该过程。但请注意,在某些情况下,在步骤 1 中选择权重最高的对(减少到 id1 的唯一值)可能会删除给定 id2 值时权重最大的对>。因此,为了选择最佳的整体映射(例如,最大化所有选定映射的权重总和),需要一种非贪婪优化策略。

更新:使用大数据集的类和方法

对于大型数据集,可以使用所谓的“大数据”类和方法(请参阅 https://cran.r-project.org/web/packages/RecordLinkage/vignettes/BigData.pdf )。它们使用文件支持的数据结构,因此大小限制是可用磁盘空间。语法大部分相同,但并不完全相同。对于此示例,实现与上述相同结果所需的调用如下:

rpairs <- RLBigDataLinkage(DOrders, DCatalogue, 
blockfld = 3,
exclude = 4,
strcmp = 1:2,
strcmpfun = "jarowinkler")

rpairs <- epiWeights(rpairs)
result <- epiClassify(rpairs, 0.7)
matchedPairs <- getPairs(result, single.rows=TRUE, filter.link="link")
matchedPairs <- data.table(matchedPairs)
matchedPairs[matchedPairs[,.I[which.max(Weight)],by=id.1]$V1, list(id.1,id.2)]

但是,对于您估计的 2 TB 大小,这仍然不可行。我认为你必须通过额外的阻塞来进一步减少对的数量。

这种情况下的问题是该包仅支持“硬”阻止标准(即两条记录必须在阻止字段中完全匹配)。当链接个人数据时(这是我们开发包时的用例),通常可以将出生日期的日、月和年部分组合起来进行阻止,这样可以显着减少对的数量,而不会丢失匹配的候选者。据我从示例中可以判断,您的数据不可能进一步“硬”阻止,因为匹配对仅具有相似但不相等的属性值(除了您已经使用的“发行单位”)阻塞)。像“仅考虑产品名称的字符串相似度大于[某个阈值]的对”之类的标准对我来说似乎最合适。要实现此目的,您必须扩展 compare.linkage()RLBigDataLinkage()

关于记录链接 : how to pair only best matches and export a merged table?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40380872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com