I am having problems with using a scipy COO sparse matrix as an input for Affinity propagation, but it works perfectly fine with a numpy array.
Just an example, say my similarity matrix is:
[[1.0, 0.9, 0.2]
[0.9, 1.0, 0.0]
[0.2, 0.0, 1.0]]
Numpy matrix version
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)
works as expected.
Sparse Matrix version
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 the following error
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.
My laptop does not have enough RAM to take a dense matrix thus wanting to use a sparse matrix.
What am I doing wrong?
Thanks.
numpyfunctions (such asmedian) are not sparse matrix aware. Somethings work because they delegate the task to a sparse method. Doessklearnsay you can use a sparse matrix in this way? - hpauljsys.getsizeof()function. For the algorithm, I was planning on making the value 0 if it was less than 0.3 since that would have been a bad match anyway. I guess I'll just have to convert it back to a dense matrix before passing it to the sklearn methods. Thanks! - Lance