PDL 成对行比较
我创建了一个 PDL 矩阵。我需要在每一行之间进行成对比较。目前我正在使用“where”和“cov”命令返回两个切片的成对比较(在 perl 循环中生成)。
我的问题:如何使用“范围”和“切片”以成对的方式循环行?如何返回我的索引位置?我已经使用 perl 循环了矩阵。我读过,使用 perl 循环确实削弱了 PDL 的功能。
所需的输出:
indexA indexB Value
pos1 pos5 1
pos1 pos6 5
pos1 pos0 7
需要明确的是,我只想使用 PDL 功能。
这是一些伪代码(希望)可以更好地说明我的观点。
p $b
[
[1 0 3 0]
[0 1 0 1]
[1 3 1 3] <- example piddle y
[0 1 0 1] <- example piddle z
]
my concept function{
slice $b (grab row z) - works fine
slice $b (grab row y) - works fine
($a, $b) = where($a,$b, $a < 3 && $b < 3 ) - works fine
p $a [1 1]
p $b [0 0]
cov($a $b) - works just fine.
}
我只需要一种在所有行中成对执行的方法。我需要进行阶乘(n 行)比较。
I have created a PDL matrix. I need to do a pairwise comparison between each row. Currently I am using the 'where' and 'cov' command to return the pairwise comparison for two slices (generated in a perl loop).
My question: How can I use 'range' and 'slice' to loop over the rows in a pairwise fashion? How can I return my index position? I have looped over the matrix using perl. I have read that looping with perl really cripples the power of PDL.
Desired output:
indexA indexB Value
pos1 pos5 1
pos1 pos6 5
pos1 pos0 7
To be clear I only want to use PDL functionality.
Here is some pseudo code that will (hopeful) illustrate my point better.
p $b
[
[1 0 3 0]
[0 1 0 1]
[1 3 1 3] <- example piddle y
[0 1 0 1] <- example piddle z
]
my concept function{
slice $b (grab row z) - works fine
slice $b (grab row y) - works fine
($a, $b) = where($a,$b, $a < 3 && $b < 3 ) - works fine
p $a [1 1]
p $b [0 0]
cov($a $b) - works just fine.
}
I just need a way to execute pairwise across all rows. I will need to do factorial(n rows) comparisons.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PDL 线程是您在这里寻找的概念。沿维度循环的一般技术是在适当的位置添加虚拟维度,以便计算生成所需的隐式线程循环。对于多维问题,可以有多种不同的方法来添加暗淡,从而创建线程循环。
对于成对行计算,您可以在切片索引上选择两个嵌套循环,其中对两个索引计数进行 Perl 循环,并将沿行生成 PDL 线程。您可以仅对索引使用一个 Perl 循环,但利用隐式线程循环一次性计算所有行。
完全 PDL 线程循环计算是为每个参数的行循环添加一个虚拟维度,以便您可以一次计算整个 N**2 行计算。下面是形状 [4,3] 数组的示例,其计算为 == 运算符:
结果是形状 [4,3,3] piddle,其第 0 维对应于成对计算产生的行,并且第一个和第二个暗淡对应于 == 操作中涉及的行索引。
如果您需要这些线程循环计算之一的索引值,请使用 xvals、yvals、zvals 或 axisvals< /em> 生成一个 piddle,其索引值对应于该数组轴。
有很多与 PDL 线程的实现相关的细节(与 perl 线程或 posix 线程不同)。我推荐 perldl 邮件列表供其他 PDL 用户和开发人员参考和讨论。另请参阅 PDL Book 的第一个在线草稿,其中包含更全面地覆盖 PDL 计算和线程。
PDL threading is the concept you are looking for here. The general technique for looping along dimensions is to add dummy dimensions in the appropriate places so that the calculation generates implicit threadloops needed. For a multi-dimensional problem, there can be a number of different ways to add dims and hence to create the threadloops.
For your pairwise row calculation, you can choose two nested loops over slice indexes which has perl loops over the two index counts and will generate PDL threading along the rows. You could use just one perl loop over indexes but take advantage of implicit threadlooping to calculate for all rows at once.
A fully PDL-threadloop computation would be to add a dummy dimension for the loop over rows for each of the arguments so that you would calculate the entire N**2 row calculations at once. Here is an example for a shape [4,3] array with the calculation being the == operator:
The result is a shape [4,3,3] piddle with the 0th dimension corresponding to the rows resulting from the pairwise calculation and the 1st and 2nd dims correspond to the row indexes involved in the == operation.
If you need an index value from or for one of these threadloop calculations, use the xvals, yvals, zvals, or axisvals to generate a piddle with the index values corresponding to that array axis.
There are a lot of details relating to the implementation of the PDL threading (not the same as perl threading or posix threads). I recommend the perldl mailing list for reference and discussion with other PDL users and developers. Also, see the first on-line draft of the PDL Book which has more comprehensive coverage of PDL computation and threading.
我认为您正在寻找一种方法来查找数组中所有不同的行对,然后使用 cov 处理每对行?如果这是正确的,那么我还没有听说过 cov,并且快速搜索文档也没有帮助。不过我可以说一些可能有帮助的事情。
我认为您对于从
PDL
中删除到 Perl 代码中过于谨慎,如果您所做的只是循环遍历所有行对的索引并使用切片
。下面的一些示例代码显示了这一点。另外,您不能像
$a
$a
这样调用
等本身就是 piddle,布尔运算符不会对它们执行您想要的操作。请改用where
。 3&
运算符,并添加一些括号以确保表达式以正确的顺序执行。除此之外,除非您纠正我对问题的理解或指导我查看 cov 子例程的一些文档,否则我无法提供帮助。
I think what you're looking for is a method to find all different pairs of rows in the array and then process each pair using
cov
? If that's correct then I haven't heard ofcov
and a quick search through the documentation doesn't help. However I can say a few things that may help.I think you're being overly cautious about dropping out of
PDL
into Perl code, which will be fine if all you are doing is looping over the indices of all row pairs and pulling those rows out usingslice
. This is shown in the some sample code below.Also you can't call
where
like that as$a < 3
etc. are piddles themselves and the boolean operator won't do what you want on them. Use the&
operator instead, and add some parentheses to make sure the expression gets executed in the right order.Beyond that I can't help unless you correct my understanding of your question or direct me to some documentation of the
cov
subroutine.