在numpy multiply()函数中出现了意外的尖括号“<”。

huangapple go评论78阅读模式
英文:

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 &gt; adj) work?

The code runs fine but I don't expect an angle bracket &gt; 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 &gt; 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 &gt; adj) - adj.multiply(adj.T &gt; 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 &gt; asdf) - asdf.multiply(asdf.T &gt; asdf)
print(&quot;asdf&quot;)
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

  1. adj.T > adj

    它构建了一个与矩阵 adj 相同形状的布尔类型矩阵。

  2. multiply()

    该函数实际上执行的是哈达玛积(Hadamard product),而不是点积(Dot product)。官方文档描述为:"对另一个矩阵进行逐点乘法"。

  3. 构建对称邻接矩阵的方法

    我们最好保留 (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 &gt; A).toarray())
print(A.T.multiply(A.T &gt; A).toarray())

A = A + A.T.multiply(A.T &gt; A) - A.multiply(A.T &gt; 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 &gt; adj) - adj.multiply(adj.T &gt; 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 &gt; A).toarray())
print(A.T.multiply(A.T &gt; A).toarray())

A = A + A.T.multiply(A.T &gt; A) - A.multiply(A.T &gt; A)
print(&quot;The symmetric adjacency matrix:&quot;, A.toarray(), sep=&#39;\n&#39;)

答案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&gt;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 &lt; 3) - asdf.multiply(7 != 8)
print(&quot;asdf&quot;)
print(asdf.toarray())

huangapple
  • 本文由 发表于 2020年1月3日 22:33:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定