var response = this.ElasticClient.Search<ElasticListing>(s => s
.AllTypes()
.Query(query => query
.Bool(b => b
.Must(must => must
.QueryString(qs => qs.Query(keyword))
)
)
)
.Size(pageSize)
.Explain()
);
我正在检查稀疏矩阵是否对称但我得到以下错误 - AttributeError:all not found
答案 0 :(得分:1)
对于该主题的一些随机探索:
In [77]: from scipy import sparse
制作稀疏矩阵
In [78]: M = sparse.random(100,100,.2, 'csr')
In [79]: M
Out[79]:
<100x100 sparse matrix of type '<class 'numpy.float64'>'
with 2000 stored elements in Compressed Sparse Row format>
它不喜欢相等测试 - 它确实会发出警告,原来的2000非零值增加了3倍
In [80]: M==M.T
/usr/local/lib/python3.5/dist-packages/scipy/sparse/compressed.py:226: SparseEfficiencyWarning: Comparing sparse matrices using == is inefficient, try using != instead.
" != instead.", SparseEfficiencyWarning)
Out[80]:
<100x100 sparse matrix of type '<class 'numpy.bool_'>'
with 6436 stored elements in Compressed Sparse Row format>
差异仍会增加非零术语的数量,但不会增加
In [81]: (M-M.T)
Out[81]:
<100x100 sparse matrix of type '<class 'numpy.float64'>'
with 3564 stored elements in Compressed Sparse Row format>
Python abs
有效,因为它委托给稀疏方法:M.__abs__
In [85]: abs(M-M.T)
Out[85]:
<100x100 sparse matrix of type '<class 'numpy.float64'>'
with 3564 stored elements in Compressed Sparse Row format>
如果我们询问有多少是小的另一个警告 - 0的差异都是0:
In [86]: abs(M-M.T)<1e-10
/usr/local/lib/python3.5/dist-packages/scipy/sparse/compressed.py:274: SparseEfficiencyWarning: Comparing a sparse matrix with a scalar greater than zero using < is inefficient, try using >= instead.
warn(bad_scalar_msg, SparseEfficiencyWarning)
Out[86]:
<100x100 sparse matrix of type '<class 'numpy.bool_'>'
with 6436 stored elements in Compressed Sparse Row format>
创建对称matix:
In [87]: Ms = (M+M.T)/2
现在所有条款都很小
In [88]: abs(Ms-Ms.T)<1e-10
/usr/local/lib/python3.5/dist-packages/scipy/sparse/compressed.py:274: SparseEfficiencyWarning: Comparing a sparse matrix with a scalar greater than zero using < is inefficient, try using >= instead.
warn(bad_scalar_msg, SparseEfficiencyWarning)
Out[88]:
<100x100 sparse matrix of type '<class 'numpy.bool_'>'
with 10000 stored elements in Compressed Sparse Row format>
相反,我们可以检查有多少差异过大:
In [89]: abs(Ms-Ms.T)>1e-10
Out[89]:
<100x100 sparse matrix of type '<class 'numpy.bool_'>'
with 0 stored elements in Compressed Sparse Row format>
In [90]: abs(M-M.T)>1e-10
Out[90]:
<100x100 sparse matrix of type '<class 'numpy.bool_'>'
with 3564 stored elements in Compressed Sparse Row format>
因此矩阵是对称的,如果:
In [94]: (abs(Ms-Ms.T)>1e-10).nnz == 0
Out[94]: True
答案 1 :(得分:0)
即使eq-operator在这里工作,它也很密集。与:比较:
import scipy.sparse as sp
import numpy as np
np.random.seed(1)
a = sp.random(5, 5, density=0.5)
blub = a == a.T
print(blub.shape)
(5, 5)
所以不要走这条路。
假设data.attribute可用(至少对于csc),当然还有对称性,我会这样做:
sym_err = a - a.T
sym_check_res = np.all(np.abs(sym_err.data) < 1e-10) # tune this value
效果:
sym_err.data
的形状为(nnz,)
,此处:(12,)
(当然sym_err
在温和条件下也很稀疏:见下一条注释)
在涉及fp-math的许多用例中,引入的阈值处理是必要的。只有当您知道自己在做什么时才跳过它或降低阈值。