scipy稀疏:获取两个矩阵中存在的值
scipy sparse: obtain values present in both matrices
我有两个 sparse
矩阵,我想按元素进行比较:
from scipy import sparse as sp
t1 = sp.random(10, 10, 0.5)
t2 = sp.random(10, 10, 0.5)
特别是我想为两个矩阵中存在的那些元素(即非零)制作散点图,但到目前为止我能想到的唯一方法是将它们转换为密集格式:
import matplotlib.pyplot as plt
plt.plot(t1.todense().flatten(),
t2.todense().flatten(),
'ko',
alpha=0.1)
当矩阵非常大时,这会非常有效。有没有更有效的方法来做到这一点?
In [256]: t1
Out[256]:
<10x10 sparse matrix of type '<class 'numpy.float64'>'
with 50 stored elements in COOrdinate format>
In [257]: t2
Out[257]:
<10x10 sparse matrix of type '<class 'numpy.float64'>'
with 50 stored elements in COOrdinate format>
绘制 t1.todense().flatten()
时,您绘制 t1
的所有元素的数据点,无论是否为零。在这种情况下为 100 分。
'weed' 出零元素的一种方法是:
In [258]: t3 = t1.multiply(t2)
In [259]: t3
Out[259]:
<10x10 sparse matrix of type '<class 'numpy.float64'>'
with 28 stored elements in Compressed Sparse Row format>
In [260]: t11 = t3.astype(bool).multiply(t1)
In [261]: t21 = t3.astype(bool).multiply(t2)
In [262]: t11
Out[262]:
<10x10 sparse matrix of type '<class 'numpy.float64'>'
with 28 stored elements in Compressed Sparse Row format>
t3
具有非零值,其中 t1
和 t2
都非零。 t11
具有 t1
的对应元素(t3
浮点数变为布尔值 True 并在乘法中隐式为 1。)稀疏 multiply
相对有效(可能不如相应的密集乘法甚至稀疏矩阵乘法)。
我们可以绘制 t11.todense.ravel()
等。这将是相同的,除了值集中为 (0.0, 0.0)。但是 data
属性具有非零值,并且 t11
和 t21
的稀疏性是相同的,所以我们可以只绘制这些 - 在这种情况下只有 28 个值:
plt.plot(t11.data,
t21.data,
'ko',
alpha=0.1);
可能还有其他方法可以获得 t11
和 t21
矩阵,但基本思想仍然适用 - 获得两个具有相同稀疏度的矩阵,并仅绘制它们的 data
值.
我有两个 sparse
矩阵,我想按元素进行比较:
from scipy import sparse as sp
t1 = sp.random(10, 10, 0.5)
t2 = sp.random(10, 10, 0.5)
特别是我想为两个矩阵中存在的那些元素(即非零)制作散点图,但到目前为止我能想到的唯一方法是将它们转换为密集格式:
import matplotlib.pyplot as plt
plt.plot(t1.todense().flatten(),
t2.todense().flatten(),
'ko',
alpha=0.1)
当矩阵非常大时,这会非常有效。有没有更有效的方法来做到这一点?
In [256]: t1
Out[256]:
<10x10 sparse matrix of type '<class 'numpy.float64'>'
with 50 stored elements in COOrdinate format>
In [257]: t2
Out[257]:
<10x10 sparse matrix of type '<class 'numpy.float64'>'
with 50 stored elements in COOrdinate format>
绘制 t1.todense().flatten()
时,您绘制 t1
的所有元素的数据点,无论是否为零。在这种情况下为 100 分。
'weed' 出零元素的一种方法是:
In [258]: t3 = t1.multiply(t2)
In [259]: t3
Out[259]:
<10x10 sparse matrix of type '<class 'numpy.float64'>'
with 28 stored elements in Compressed Sparse Row format>
In [260]: t11 = t3.astype(bool).multiply(t1)
In [261]: t21 = t3.astype(bool).multiply(t2)
In [262]: t11
Out[262]:
<10x10 sparse matrix of type '<class 'numpy.float64'>'
with 28 stored elements in Compressed Sparse Row format>
t3
具有非零值,其中 t1
和 t2
都非零。 t11
具有 t1
的对应元素(t3
浮点数变为布尔值 True 并在乘法中隐式为 1。)稀疏 multiply
相对有效(可能不如相应的密集乘法甚至稀疏矩阵乘法)。
我们可以绘制 t11.todense.ravel()
等。这将是相同的,除了值集中为 (0.0, 0.0)。但是 data
属性具有非零值,并且 t11
和 t21
的稀疏性是相同的,所以我们可以只绘制这些 - 在这种情况下只有 28 个值:
plt.plot(t11.data,
t21.data,
'ko',
alpha=0.1);
可能还有其他方法可以获得 t11
和 t21
矩阵,但基本思想仍然适用 - 获得两个具有相同稀疏度的矩阵,并仅绘制它们的 data
值.