Scipy:来自数组的稀疏指示矩阵
Scipy: Sparse indicator matrix from array(s)
从一个或两个数组 a,b
计算稀疏布尔矩阵 I
的最有效方法是什么,其中 I[i,j]==True
其中 a[i]==b[j]
?以下速度很快但内存效率低下:
I = a[:,None]==b
以下在创建过程中很慢并且内存效率仍然很低:
I = csr((a[:,None]==b),shape=(len(a),len(b)))
下面至少给出了 rows,cols 更好的 csr_matrix
初始化,但它仍然创建了完整的密集矩阵并且同样慢:
z = np.argwhere((a[:,None]==b))
有什么想法吗?
您可以使用 numpy.isclose
,公差较小:
np.isclose(a,b)
a.eq(b)
注意这个 returns True
False
的数组。
一种方法是首先使用 set
识别 a
和 b
共有的所有不同元素。如果 a
和 b
中的值没有太多不同的可能性,这应该会很好地工作。然后只需要遍历不同的值(在下面的变量 values
中)并使用 np.argwhere
来识别 a
和 b
中出现这些值的索引。然后可以使用 np.repeat
和 np.tile
:
构造稀疏矩阵的二维索引
import numpy as np
from scipy import sparse
a = np.random.randint(0, 10, size=(400,))
b = np.random.randint(0, 10, size=(300,))
## matrix generation after OP
I1 = sparse.csr_matrix((a[:,None]==b),shape=(len(a),len(b)))
##identifying all values that occur both in a and b:
values = set(np.unique(a)) & set(np.unique(b))
##here we collect the indices in a and b where the respective values are the same:
rows, cols = [], []
##looping over the common values, finding their indices in a and b, and
##generating the 2D indices of the sparse matrix with np.repeat and np.tile
for value in values:
x = np.argwhere(a==value).ravel()
y = np.argwhere(b==value).ravel()
rows.append(np.repeat(x, len(x)))
cols.append(np.tile(y, len(y)))
##concatenating the indices for different values and generating a 1D vector
##of True values for final matrix generation
rows = np.hstack(rows)
cols = np.hstack(cols)
data = np.ones(len(rows),dtype=bool)
##generating sparse matrix
I3 = sparse.csr_matrix( (data,(rows,cols)), shape=(len(a),len(b)) )
##checking that the matrix was generated correctly:
print((I1 != I3).nnz==0)
生成 csr 矩阵的语法取自 documentation. The test for sparse matrix equality is taken from 。
旧答案:
我不知道性能如何,但至少您可以通过使用简单的生成器表达式来避免构建完整的密集矩阵。这里有一些代码使用两个 1d 随机整数数组首先按照 OP 发布的方式生成稀疏矩阵,然后使用生成器表达式测试所有元素是否相等:
import numpy as np
from scipy import sparse
a = np.random.randint(0, 10, size=(400,))
b = np.random.randint(0, 10, size=(300,))
## matrix generation after OP
I1 = sparse.csr_matrix((a[:,None]==b),shape=(len(a),len(b)))
## matrix generation using generator
data, rows, cols = zip(
*((True, i, j) for i,A in enumerate(a) for j,B in enumerate(b) if A==B)
)
I2 = sparse.csr_matrix((data, (rows, cols)), shape=(len(a), len(b)))
##testing that matrices are equal
## from
print((I1 != I2).nnz==0) ## --> True
我认为没有办法绕过双循环,理想情况下将其推入 numpy
,但至少对于生成器,循环在某种程度上得到了优化 ...
从一个或两个数组 a,b
计算稀疏布尔矩阵 I
的最有效方法是什么,其中 I[i,j]==True
其中 a[i]==b[j]
?以下速度很快但内存效率低下:
I = a[:,None]==b
以下在创建过程中很慢并且内存效率仍然很低:
I = csr((a[:,None]==b),shape=(len(a),len(b)))
下面至少给出了 rows,cols 更好的 csr_matrix
初始化,但它仍然创建了完整的密集矩阵并且同样慢:
z = np.argwhere((a[:,None]==b))
有什么想法吗?
您可以使用 numpy.isclose
,公差较小:
np.isclose(a,b)
a.eq(b)
注意这个 returns True
False
的数组。
一种方法是首先使用 set
识别 a
和 b
共有的所有不同元素。如果 a
和 b
中的值没有太多不同的可能性,这应该会很好地工作。然后只需要遍历不同的值(在下面的变量 values
中)并使用 np.argwhere
来识别 a
和 b
中出现这些值的索引。然后可以使用 np.repeat
和 np.tile
:
import numpy as np
from scipy import sparse
a = np.random.randint(0, 10, size=(400,))
b = np.random.randint(0, 10, size=(300,))
## matrix generation after OP
I1 = sparse.csr_matrix((a[:,None]==b),shape=(len(a),len(b)))
##identifying all values that occur both in a and b:
values = set(np.unique(a)) & set(np.unique(b))
##here we collect the indices in a and b where the respective values are the same:
rows, cols = [], []
##looping over the common values, finding their indices in a and b, and
##generating the 2D indices of the sparse matrix with np.repeat and np.tile
for value in values:
x = np.argwhere(a==value).ravel()
y = np.argwhere(b==value).ravel()
rows.append(np.repeat(x, len(x)))
cols.append(np.tile(y, len(y)))
##concatenating the indices for different values and generating a 1D vector
##of True values for final matrix generation
rows = np.hstack(rows)
cols = np.hstack(cols)
data = np.ones(len(rows),dtype=bool)
##generating sparse matrix
I3 = sparse.csr_matrix( (data,(rows,cols)), shape=(len(a),len(b)) )
##checking that the matrix was generated correctly:
print((I1 != I3).nnz==0)
生成 csr 矩阵的语法取自 documentation. The test for sparse matrix equality is taken from
旧答案:
我不知道性能如何,但至少您可以通过使用简单的生成器表达式来避免构建完整的密集矩阵。这里有一些代码使用两个 1d 随机整数数组首先按照 OP 发布的方式生成稀疏矩阵,然后使用生成器表达式测试所有元素是否相等:
import numpy as np
from scipy import sparse
a = np.random.randint(0, 10, size=(400,))
b = np.random.randint(0, 10, size=(300,))
## matrix generation after OP
I1 = sparse.csr_matrix((a[:,None]==b),shape=(len(a),len(b)))
## matrix generation using generator
data, rows, cols = zip(
*((True, i, j) for i,A in enumerate(a) for j,B in enumerate(b) if A==B)
)
I2 = sparse.csr_matrix((data, (rows, cols)), shape=(len(a), len(b)))
##testing that matrices are equal
## from
print((I1 != I2).nnz==0) ## --> True
我认为没有办法绕过双循环,理想情况下将其推入 numpy
,但至少对于生成器,循环在某种程度上得到了优化 ...