意外的尖括号“<"在矩阵的numpy乘法()中 [英] Unexpected angle bracket &quot;&lt;&quot; in numpy multiply() of matrices

查看:118
本文介绍了意外的尖括号“<"在矩阵的numpy乘法()中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

adj.multiply(adj.T > adj) 是如何工作的?

代码运行良好,但我不希望在乘法语句中出现尖括号 >.我相信文档说明为 multiply() 提供两个值,但它仍然可以工作并通过将 (adj.T > adj) 替换为 来生成输出矩阵>(True), (False), (adj.T != adj),但不是 (adj.T = adj)代码>.此外,该 multiply() 方法没有附加在变量的末尾,而在此处用作 adj.multiply() .方法multiply 的来源似乎只是将其转换为csr_matrix 并运行numpy 的multiply(),然后IIRC 将其转换回coo_matrix..T 当然是转置"的意思.

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)`

对于某些上下文,adj"是来自 graph 的 scipy coo_matrixgithub 上的卷积网络,我试图了解输入是如何准备的.

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())

在 row=1,col=1 处,值为 5,其中 asdf.T.multiply(True) 语句将其值 5 翻倍至 10.传递两个变量,以空格或逗号分隔不起作用.

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.

更新:

我在 ">" 尖括号之前放置了一个数字(不是整个矩阵),它产生了这个错误:

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: 使用 < 将稀疏矩阵与大于零的标量进行比较效率低下,请尝试使用 >= 代替.警告(bad_scalar_msg,稀疏效率警告)

/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)

看到这一点,我意识到有一个不同的 sparse multiply() 方法没有在没有明确输入sparse"的情况下显示在谷歌中.它的文档在这里,但是我看不出它是如何处理尖括号或条件的.

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.

推荐答案

使用以下代码,我始终发现 multiply() 将乘以标量或矩阵值,但有条件,如果条件为真,则执行前面的运算符,而不执行乘法.

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.

例如以下内容:

asdf = asdf + asdf.T.multiply(True)

将只执行 + 操作,因为 True 布尔值被传递.在这种情况下,没有乘法.相同的结果,如果我们有 3>2 而不是 True,这是真的,(它不会乘以 3 或 2).

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())

这篇关于意外的尖括号“<"在矩阵的numpy乘法()中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆