使用带有 sklearn 亲和力传播的稀疏矩阵
Using a Sparse Matrix with sklearn Affinity Propagation
我在使用 scipy COO 稀疏矩阵作为亲和传播的输入时遇到问题,但它与 numpy 数组一起工作得很好。
举个例子,假设我的相似度矩阵是:
[[1.0, 0.9, 0.2]
[0.9, 1.0, 0.0]
[0.2, 0.0, 1.0]]
Numpy 矩阵版本
import numpy as np
import sklearn.cluster
simnp = np.array([[1,0.9,0.2],[0.9,1,0],[0.2,0,1]])
affprop = sklearn.cluster.AffinityPropagation(affinity="precomputed")
affprop.fit(simnp)
按预期工作。
稀疏矩阵版本
import scipy.sparse as sps
import sklearn.cluster
simsps = sps.coo_matrix(([1,1,1,0.9,0.9,0.2,0.2],([0,1,2,0,1,0,2],[0,1,2,1,0,2,0])),(3,3))
affprop = sklearn.cluster.AffinityPropagation(affinity="precomputed")
affprop.fit(simsps)
returns下面的错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python\Python27\lib\site-packages\sklearn\cluster\affinity_propagation_.py", line 301, in fit
copy=self.copy, verbose=self.verbose, return_n_iter=True)
File "C:\Python\Python27\lib\site-packages\sklearn\cluster\affinity_propagation_.py", line 90, in affinity_propagation
preference = np.median(S)
File "C:\Python\Python27\lib\site-packages\numpy\lib\function_base.py", line 3084, in median
overwrite_input=overwrite_input)
File "C:\Python\Python27\lib\site-packages\numpy\lib\function_base.py", line 2997, in _ureduce
r = func(a, **kwargs)
File "C:\Python\Python27\lib\site-packages\numpy\lib\function_base.py", line 3158, in _median
return mean(part[indexer], axis=axis, out=out)
File "C:\Python\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 2878, in mean
out=out, keepdims=keepdims)
File "C:\Python\Python27\lib\site-packages\numpy\core\_methods.py", line 70, in _mean
ret = ret.dtype.type(ret / rcount)
ValueError: setting an array element with a sequence.
我的笔记本电脑没有足够的 RAM 来处理密集矩阵,因此想使用稀疏矩阵。
我做错了什么?
谢谢。
http://scikit-learn.org/stable/modules/generated/sklearn.cluster.AffinityPropagation.html
fit(X, y=None)
Parameters:
X: array-like, shape (n_samples, n_features) or (n_samples, n_samples)
predict(X)
Parameters:
X : {array-like, sparse matrix}, shape (n_samples, n_features)
http://scikit-learn.org/stable/modules/generated/sklearn.cluster.SpectralClustering.html
fit(X, y=None)
Parameters:
X : array-like or sparse matrix, shape (n_samples, n_features)
所以有些方法确实接受稀疏矩阵。但是 AffinityPropagation.fit
并没有做出这样的声明。这是文档遗漏,还是表明它不适用于稀疏矩阵?您的错误表明是后者 - 由于某种原因,它尚未适应稀疏。
我不是 scikit-learn
的用户,但已经在该包中回答了一些关于稀疏矩阵的问题。我的印象是稀疏处理相对较新,在某些情况下,他们必须使用 todense()
将稀疏矩阵转回密集矩阵。
正如我在评论中所写,numpy
代码本身并不能正确处理稀疏矩阵。它仅在将操作委托给稀疏方法时才有效。 np.median
和 np.mean
似乎没有正确委托给 sparse.coo_matrix.mean
。
尝试:
np.median(simnp)
np.mean(simnp)
simnp.mean()
关于 sklearn 当前状态的更新(2019 年 6 月)可能会有用。
在最初提出问题时,已经有一个 fix of an issue reporting that AffinityPropagation was not working with sparse matrices. Recently (May 2019) it was reported again AffinityPropagation 不适用于稀疏矩阵。
其实总结是:
- 只有当亲和力不是预先计算的而是欧几里得(因为它调用 sklearn.metrics.euclidean_distances,它适用于稀疏矩阵)时,拟合才适用于稀疏矩阵。这实际上在内存消耗方面没有任何优势。
- 如果亲和力是预先计算的,则拟合不适用于稀疏矩阵。当前阻塞的代码行似乎是中位数的计算。
我在使用 scipy COO 稀疏矩阵作为亲和传播的输入时遇到问题,但它与 numpy 数组一起工作得很好。
举个例子,假设我的相似度矩阵是:
[[1.0, 0.9, 0.2]
[0.9, 1.0, 0.0]
[0.2, 0.0, 1.0]]
Numpy 矩阵版本
import numpy as np
import sklearn.cluster
simnp = np.array([[1,0.9,0.2],[0.9,1,0],[0.2,0,1]])
affprop = sklearn.cluster.AffinityPropagation(affinity="precomputed")
affprop.fit(simnp)
按预期工作。
稀疏矩阵版本
import scipy.sparse as sps
import sklearn.cluster
simsps = sps.coo_matrix(([1,1,1,0.9,0.9,0.2,0.2],([0,1,2,0,1,0,2],[0,1,2,1,0,2,0])),(3,3))
affprop = sklearn.cluster.AffinityPropagation(affinity="precomputed")
affprop.fit(simsps)
returns下面的错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python\Python27\lib\site-packages\sklearn\cluster\affinity_propagation_.py", line 301, in fit
copy=self.copy, verbose=self.verbose, return_n_iter=True)
File "C:\Python\Python27\lib\site-packages\sklearn\cluster\affinity_propagation_.py", line 90, in affinity_propagation
preference = np.median(S)
File "C:\Python\Python27\lib\site-packages\numpy\lib\function_base.py", line 3084, in median
overwrite_input=overwrite_input)
File "C:\Python\Python27\lib\site-packages\numpy\lib\function_base.py", line 2997, in _ureduce
r = func(a, **kwargs)
File "C:\Python\Python27\lib\site-packages\numpy\lib\function_base.py", line 3158, in _median
return mean(part[indexer], axis=axis, out=out)
File "C:\Python\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 2878, in mean
out=out, keepdims=keepdims)
File "C:\Python\Python27\lib\site-packages\numpy\core\_methods.py", line 70, in _mean
ret = ret.dtype.type(ret / rcount)
ValueError: setting an array element with a sequence.
我的笔记本电脑没有足够的 RAM 来处理密集矩阵,因此想使用稀疏矩阵。
我做错了什么?
谢谢。
http://scikit-learn.org/stable/modules/generated/sklearn.cluster.AffinityPropagation.html
fit(X, y=None) Parameters:
X: array-like, shape (n_samples, n_features) or (n_samples, n_samples)predict(X) Parameters:
X : {array-like, sparse matrix}, shape (n_samples, n_features)
http://scikit-learn.org/stable/modules/generated/sklearn.cluster.SpectralClustering.html
fit(X, y=None) Parameters:
X : array-like or sparse matrix, shape (n_samples, n_features)
所以有些方法确实接受稀疏矩阵。但是 AffinityPropagation.fit
并没有做出这样的声明。这是文档遗漏,还是表明它不适用于稀疏矩阵?您的错误表明是后者 - 由于某种原因,它尚未适应稀疏。
我不是 scikit-learn
的用户,但已经在该包中回答了一些关于稀疏矩阵的问题。我的印象是稀疏处理相对较新,在某些情况下,他们必须使用 todense()
将稀疏矩阵转回密集矩阵。
正如我在评论中所写,numpy
代码本身并不能正确处理稀疏矩阵。它仅在将操作委托给稀疏方法时才有效。 np.median
和 np.mean
似乎没有正确委托给 sparse.coo_matrix.mean
。
尝试:
np.median(simnp)
np.mean(simnp)
simnp.mean()
关于 sklearn 当前状态的更新(2019 年 6 月)可能会有用。
在最初提出问题时,已经有一个 fix of an issue reporting that AffinityPropagation was not working with sparse matrices. Recently (May 2019) it was reported again AffinityPropagation 不适用于稀疏矩阵。
其实总结是:
- 只有当亲和力不是预先计算的而是欧几里得(因为它调用 sklearn.metrics.euclidean_distances,它适用于稀疏矩阵)时,拟合才适用于稀疏矩阵。这实际上在内存消耗方面没有任何优势。
- 如果亲和力是预先计算的,则拟合不适用于稀疏矩阵。当前阻塞的代码行似乎是中位数的计算。