成对距离在稀疏矩阵上失败,并出现无信息的错误消息
pairwise distance fails on a sparse matrix with an uninformative error message
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from scipy.spatial import distance
X = CountVectorizer().fit_transform(docs)
X = TfidfTransformer(use_idf=False).fit_transform(X)
print (X.shape) #prints (100, 1760)
然而,当我尝试计算对距离时,出现此错误:
distance.pdist(X, metric='cosine')
ValueError: A 2-dimensional array must be passed.
形状表明 X 是一个二维数组,可能是什么问题?
=====2017 年 7 月 6 日更新======
这是 scipy 中的错误,sklearn 具有稀疏矩阵的正确实现。
我已提议对此处的 scipy 存储库进行代码更改:
https://github.com/scipy/scipy/pull/7566
=====2018 年 2 月 23 日更新======
如果你来到这里,你可能也遇到了这个问题。
自从我提出的在线修复被推送到 scipy 存储库以来已经超过 8 个月了。
pdist
开头为:
def pdist(X, metric='euclidean', p=2, w=None, V=None, VI=None):
....
X = np.asarray(X, order='c')
# The C code doesn't do striding.
X = _copy_array_if_base_present(X)
s = X.shape
if len(s) != 2:
raise ValueError('A 2-dimensional array must be passed.')
但是,如果我制作一个 scipy.sparse
矩阵,并应用 asarray
,我不会得到一个二维数组:
In [258]: from scipy import sparse
In [259]: M = sparse.random(100,100, format='csr')
In [260]: M
Out[260]:
<100x100 sparse matrix of type '<class 'numpy.float64'>'
with 100 stored elements in Compressed Sparse Row format>
In [263]: np.asarray(M)
Out[263]:
array(<100x100 sparse matrix of type '<class 'numpy.float64'>'
with 100 stored elements in Compressed Sparse Row format>, dtype=object)
In [264]: _.shape
Out[264]: ()
pdist
不是为接受稀疏矩阵而设计的。稀疏矩阵不是 ndarray
的子类。你必须先把它弄得密实。
In [266]: np.asarray(M.toarray()).shape
Out[266]: (100, 100)
http://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.pairwise_distances.html
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from scipy.spatial import distance
X = CountVectorizer().fit_transform(docs)
X = TfidfTransformer(use_idf=False).fit_transform(X)
print (X.shape) #prints (100, 1760)
然而,当我尝试计算对距离时,出现此错误:
distance.pdist(X, metric='cosine')
ValueError: A 2-dimensional array must be passed.
形状表明 X 是一个二维数组,可能是什么问题?
=====2017 年 7 月 6 日更新======
这是 scipy 中的错误,sklearn 具有稀疏矩阵的正确实现。
我已提议对此处的 scipy 存储库进行代码更改:
https://github.com/scipy/scipy/pull/7566
=====2018 年 2 月 23 日更新======
如果你来到这里,你可能也遇到了这个问题。
自从我提出的在线修复被推送到 scipy 存储库以来已经超过 8 个月了。
pdist
开头为:
def pdist(X, metric='euclidean', p=2, w=None, V=None, VI=None):
....
X = np.asarray(X, order='c')
# The C code doesn't do striding.
X = _copy_array_if_base_present(X)
s = X.shape
if len(s) != 2:
raise ValueError('A 2-dimensional array must be passed.')
但是,如果我制作一个 scipy.sparse
矩阵,并应用 asarray
,我不会得到一个二维数组:
In [258]: from scipy import sparse
In [259]: M = sparse.random(100,100, format='csr')
In [260]: M
Out[260]:
<100x100 sparse matrix of type '<class 'numpy.float64'>'
with 100 stored elements in Compressed Sparse Row format>
In [263]: np.asarray(M)
Out[263]:
array(<100x100 sparse matrix of type '<class 'numpy.float64'>'
with 100 stored elements in Compressed Sparse Row format>, dtype=object)
In [264]: _.shape
Out[264]: ()
pdist
不是为接受稀疏矩阵而设计的。稀疏矩阵不是 ndarray
的子类。你必须先把它弄得密实。
In [266]: np.asarray(M.toarray()).shape
Out[266]: (100, 100)
http://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.pairwise_distances.html