scipy/cluster/hierarchy.py
中的 linkage
函数将参数 y
处理为:
y = _convert_to_double(np.asarray(y, order='c'))
if y.ndim == 1:
distance.is_valid_y(y, throw=True, name='y')
[y] = _copy_arrays_if_base_present([y])
elif y.ndim == 2:
if method in _EUCLIDEAN_METHODS and metric != 'euclidean':
raise ValueError("Method '{0}' requires the distance metric "
"to be Euclidean".format(method))
y = distance.pdist(y, metric)
else:
raise ValueError("`y` must be 1 or 2 dimensional.")
当我将`asarray`应用于`dok`时,我会得到一个0d对象数组。 它只是将字典包装在数组中。
In [905]: M=sparse.dok_matrix([[1,0,0,2,3],[0,0,0,0,1]])
In [906]: M
Out[906]:
<2x5 sparse matrix of type '<class 'numpy.int32'>'
with 4 stored elements in Dictionary Of Keys format>
In [908]: m = np.asarray(M)
In [909]: m
Out[909]:
array(<2x5 sparse matrix of type '<class 'numpy.int32'>'
with 4 stored elements in Dictionary Of Keys format>, dtype=object)
In [910]: m.shape
Out[910]: ()
linkage
接受一个一维压缩风格的距离矩阵或等效的二维矩阵。
深入了解 linkage
后,我推断出 ward
使用了 nn_chain
,它在编译后的 scipy/cluster/_hierarchy.cpython-35m-i386-linux-gnu.so
文件中。这使得该方法的工作部分更加难以接近普通的 Python 程序员。
scikit-learn
的函数可以处理稀疏矩阵,但并非全部。 - hpaulj