在r中抽样
给定一个矩阵,例如MAT
> set.seed(1)
> mat <- matrix(rbinom(100,1,0.5),10,10)
> rownames(mat) <- paste0(sample(LETTERS[1:2],10,replace=T),c(1:nrow(mat)))
> colnames(mat) <- paste0(sample(LETTERS[1:2],10,replace=T),c(1:ncol(mat)))
> mat
A1 A2 A3 B4 B5 B6 B7 A8 B9 B10
B1 0 0 1 0 1 0 1 0 0 0
B2 0 0 0 1 1 1 0 1 1 0
B3 1 1 1 0 1 0 0 0 0 1
A4 1 0 0 0 1 0 0 0 0 1
A5 0 1 0 1 1 0 1 0 1 1
A6 1 0 0 1 1 0 0 1 0 1
A7 1 1 0 1 0 0 0 1 1 0
B8 1 1 0 0 0 1 1 0 0 0
A9 1 0 1 1 1 1 0 1 0 1
A10 0 1 0 0 1 0 1 1 0 1
我想采样表单的子播放:
A B
A 0 1
B 1 0
编辑:具体来说,submatrix应在a-row/b column中包含一个1,b row/a中的1个 - 列和其他两个单元格。
我可以通过随机选择一个A行和一个B行,然后随机选择一个A柱和一个B柱,然后检查其是否具有所需模式来做到这一点。但是,我试图找到一种更有效的方法,即使在大/稀疏的矩阵中也可以工作。谢谢!
Given a matrix like mat
> set.seed(1)
> mat <- matrix(rbinom(100,1,0.5),10,10)
> rownames(mat) <- paste0(sample(LETTERS[1:2],10,replace=T),c(1:nrow(mat)))
> colnames(mat) <- paste0(sample(LETTERS[1:2],10,replace=T),c(1:ncol(mat)))
> mat
A1 A2 A3 B4 B5 B6 B7 A8 B9 B10
B1 0 0 1 0 1 0 1 0 0 0
B2 0 0 0 1 1 1 0 1 1 0
B3 1 1 1 0 1 0 0 0 0 1
A4 1 0 0 0 1 0 0 0 0 1
A5 0 1 0 1 1 0 1 0 1 1
A6 1 0 0 1 1 0 0 1 0 1
A7 1 1 0 1 0 0 0 1 1 0
B8 1 1 0 0 0 1 1 0 0 0
A9 1 0 1 1 1 1 0 1 0 1
A10 0 1 0 0 1 0 1 1 0 1
I want to sample submatrices of the form:
A B
A 0 1
B 1 0
EDIT: Specifically, the submatrix should contain a 1 in the A-row/B-column, a 1 in the B-row/A-column, and 0s in the other two cells.
I can do this by randomly selecting one A-row and one B-row, then randomly selecting one A-column and one B-column, then checking whether it has the desired pattern. But, I'm trying to find a more efficient method that will work even in large/sparse matrices. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在维度名称上进行
示例
:You can
sample
on the dimension names:一个人可以列举包含值
1
的元素的所有可能的成对组合,然后消除共享一个行或列和对的对,该对不会导致0
submatrix main的元素对角线。来自每个剩余对的结果行和列将定义满足所需模式的所有可能的子膜,这些都将是微不足道的样品。对于1
元素(例如,在可用的内存上挖掘)的矩阵,这是可行的。对于具有大量
1
元素的稀疏矩阵,获得高效,矢量化解决方案的一种直接方法也是拒绝的:1
的示例对元素> 元素的元素元素如果相应的主角元素不是0
,则拒绝。下面的解决方案假定<代码> 0 比1
更多的元素。 (如果相反的话,则应修改为主要对角线的两个0
元素进行采样,如果抗异构元素不是1
。)拒绝率将取决于主要在稀疏基质的密度上。示例矩阵相当致密,因此其排斥率相对较高。在这里,它作为一个函数实现,该函数返回有或没有替换的指定数量的样本。
One could enumerate all possible pairwise combinations of elements containing the value
1
, then eliminate pairs that share a row or column and pairs that would not result in0
elements for the submatrix main diagonal. The resulting rows and columns from each remaining pair would define all possible submatrices that meet the desired pattern and these would be trivial to sample. This is feasible for matrices with a relatively small number of1
elements (e.g., <100K--depending on available memory).For sparse matrices with a large number of
1
elements, a straightforward way to get an efficient, vectorized solution is also with rejection: sample pairs of1
elements for each submatrix's antidiagonal and reject if the corresponding main diagonal elements are not0
. The below solution assumes more elements are0
than1
. (If the opposite is true, it should be modified to sample two0
elements for the main diagonal and reject if the antidiagonal elements are not1
.) The rejection rate will depend mainly on the density of the sparse matrix. The example matrix is rather dense, so it has a relatively high rejection rate.Here it is implemented as a function that returns a specified number of samples either with or without replacement.