英文:
Unexpected angle bracket "<" in numpy multiply() of matrices
问题
How does adj.multiply(adj.T > adj)
work?
The code runs fine but I don't expect an angle bracket >
in a multiply statement. I believe the docs stated to provide two values to multiply()
, but it's still working and producing an output matrix by replacing (adj.T > adj)
with (True)
, (False)
, (adj.T != adj)
, but not (adj.T = adj)
. Also, that multiply()
method is not attached on the end of a variable, whereas it is used as adj.multiply()
here. The source of the method multiply seemed to just convert it to a csr_matrix and run numpy's multiply()
, then IIRC converts it back to coo_matrix. The .T
of course means "transpose".
build symmetric adjacency matrix
adj = adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj)
For some context, "adj" is a scipy coo_matrix from graph convolutional network on GitHub, which I'm trying to understand how the input is prepared for.
adj = sp.coo_matrix((np.ones(edges.shape[0]), (edges[:, 0], edges[:, 1])),
shape=(labels.shape[0], labels.shape[0]),
dtype=np.float32)
Attempting to reproduce the code requires running that whole page.
The following is easier to recreate and test:
import numpy as np
import scipy.sparse as sp
asdf = sp.coo_matrix((np.ones(5), (np.ones(5), np.ones(5))), shape=(5, 5),
dtype=np.float32)
print(asdf)
print(asdf.toarray())
asdf = asdf + asdf.T.multiply(asdf.T > asdf) - asdf.multiply(asdf.T > asdf)
print("asdf")
print(asdf.toarray())
at row=1, col=1, value was 5, where a asdf.T.multiply(True)
statement doubled its value 5, to 10. Passing two variables separated by space or comma doesn't work.
Update:
I placed a number (not a whole matrix) before the ">" angle bracket and it produced this error:
/usr/local/lib/python3.6/dist-packages/scipy/sparse/compressed.py:287: SparseEfficiencyWarning: Comparing a sparse matrix with a scalar greater than zero using < is inefficient, try using >= instead.
warn(bad_scalar_msg, SparseEfficiencyWarning)
Seeing that, I realized that there was a different sparse multiply() method that didn't show in Google without explicitly typing "sparse". Its documentation is here, but I don't see how it handles an angle bracket or condition.
英文:
How does adj.multiply(adj.T > adj)
work?
The code runs fine but I don't expect an angle bracket >
in a multiply statement. I believe the docs stated to provide two values to multiply()
, but it's still working and producing an output matrix by replacing (adj.T > adj)
with (True)
, (False)
, (adj.T != adj)
, but not (adj.T = adj)
. Also, that multiply()
method is not attached on the end of a variable, whereas it is used as adj.multiply()
here. The source of the method multiply seemed to just convert it to a csr_matrix and run numpy's multiply()
, then IIRC converts it back to coo_matrix. The .T
of course means "transpose".
# build symmetric adjacency matrix
adj = adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj)`
For some context, "adj" is a scipy coo_matrix from graph convolutional network on github, which I'm trying to understand how the input is prepared for.
adj = sp.coo_matrix((np.ones(edges.shape[0]), (edges[:, 0], edges[:, 1])),
shape=(labels.shape[0], labels.shape[0]),
dtype=np.float32)
Attempting to reproduce the code requires running that whole page.
The following is easier to recreate and test:
import numpy as np
import scipy.sparse as sp
asdf = sp.coo_matrix((np.ones(5), (np.ones(5), np.ones(5))), shape=(5,5),
dtype=np.float32)
print(asdf)
print(asdf.toarray())
asdf = asdf + asdf.T.multiply(asdf.T > asdf) - asdf.multiply(asdf.T > asdf)
print("asdf")
print(asdf.toarray())
at row=1,col=1, value was 5, where a asdf.T.multiply(True)
statement doubled its value 5, to 10. Passing two variables separated by space or comma doesn't work.
Update:
I placed a number (not a whole matrix) before the ">" angle bracket and it produced this error:
> /usr/local/lib/python3.6/dist-packages/scipy/sparse/compressed.py:287: SparseEfficiencyWarning: Comparing a sparse matrix with a scalar greater than zero using < is inefficient, try using >= instead.
warn(bad_scalar_msg, SparseEfficiencyWarning)
Seeing that, I realized that there was a different sparse multiply() method that didn't show in google without explicitly typing "sparse". Its documentation is here, but I don't see how it handles an angle bracket or condition.
答案1
得分: 2
-
adj.T > adj
它构建了一个与矩阵 adj 相同形状的布尔类型矩阵。
-
multiply()
该函数实际上执行的是哈达玛积(Hadamard product),而不是点积(Dot product)。官方文档描述为:"对另一个矩阵进行逐点乘法"。
-
构建对称邻接矩阵的方法
我们最好保留 (i, j) 和 (j, i) 之间较大的权重项,而不是简单地将它们相加,因为后者会将边的权重相加在一起。
以下是解释它们的代码:
import numpy as np
import scipy.sparse as sp
row = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])
col = np.array([0, 1, 2, 0, 1, 2, 0, 1, 2])
A = sp.coo_matrix(([0, 3, 2, 0, 0, 0, 7, 4, 0], (row, col)),
shape=(3, 3), dtype=np.float32)
print(A.toarray())
print((A.T > A).toarray())
print(A.T.multiply(A.T > A).toarray())
A = A + A.T.multiply(A.T > A) - A.multiply(A.T > A)
print("对称邻接矩阵:", A.toarray(), sep='\n')
(注意:上述代码中的">"是HTML编码,实际上表示大于符号 ">"。)
英文:
Make a record for this problem.
When I try to handle confusion about this line of code:
adj = adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj)
I need to understand three parts:
1. adj.T > adj
It builds a bool type matrix with the same shape of matrix adj.
2. multiply()
The function achieves Hadamard product actually, rather than Dot product. The official document gives the description: "Point-wise multiplication by another matrix".
3. the way to build symmetric adjacency matrix
We'd better reserve the larger weight item between (i, j) and (j, i) instead of adding them together simply, as the latter will add the weight of the edge.
There is the code to explain them:
import numpy as np
import scipy.sparse as sp
row = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])
col = np.array([0, 1, 2, 0, 1, 2, 0, 1, 2])
A = sp.coo_matrix(([0, 3, 2, 0, 0, 0, 7, 4, 0], (row, col)),
shape=(3, 3), dtype=np.float32)
print(A.toarray())
print((A.T > A).toarray())
print(A.T.multiply(A.T > A).toarray())
A = A + A.T.multiply(A.T > A) - A.multiply(A.T > A)
print("The symmetric adjacency matrix:", A.toarray(), sep='\n')
答案2
得分: 0
I'll provide translations for the code parts as requested:
以下是要翻译的内容:
With the following code, I consistently find the multiply()
will multiply by a scalar or matrix value, but with a condition, it performs the operator previous to it if the condition is true, without performing a multiplication.
通过以下代码,我一直发现 multiply()
会乘以标量或矩阵值,但在条件下,如果条件为真,则执行它之前的操作符,而不进行乘法运算。
For example, the following:
例如,以下代码:
asdf = asdf + asdf.T.multiply(True)
will only perform the +
operation because of the True
boolean being passed. In this case there is no multiplication. Same results, if instead of True
we have 3>2
, which is true, (it doesn't multiply by the 3 or 2).
将仅执行 +
操作,因为传递了 True
布尔值。在这种情况下,没有进行乘法运算。相同的结果,如果我们有 3>2
而不是 True
,这是真的,(它不会乘以3或2)。
row = np.array([0,1,2,3,4])
col = np.array([0,1,2,3,4])
asdf = sp.coo_matrix(([1,2,3,4,5], (row, col)), shape=(5,5), dtype=np.float32)
print(asdf)
print(asdf.toarray())
asdf = asdf + asdf.T.multiply(2 < 3) - asdf.multiply(7 != 8)
print("asdf")
print(asdf.toarray())
I hope this helps! If you have any more code to translate or need further assistance, please let me know.
英文:
With the following code, I consistently find the multiply()
will multiply by a scalar or matrix value, but with a condition, it performs the operator previous to it if the condition is true, without performing a multiplication.
For example, the following:
asdf = asdf + asdf.T.multiply(True)
will only perform the +
operation because of the True
boolean being passed. In this case there is no multiplication. Same results, if instead of True
we have 3>2
, which is true, (it doesn't multiply by the 3 or 2).
row = np.array([0,1,2,3,4])
col = np.array([0,1,2,3,4])
asdf = sp.coo_matrix(([1,2,3,4,5], (row, col)), shape=(5,5), dtype=np.float32)
print(asdf)
print(asdf.toarray())
asdf = asdf + asdf.T.multiply(2 < 3) - asdf.multiply(7 != 8)
print("asdf")
print(asdf.toarray())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论