我尝试使用稀疏矩阵计算silhouette_score或silhouette_samples,但得到以下错误:
ValueError: diag需要至少二维的数组
示例代码如下:
edges = [
(1, 2, 0.9),
(1, 3, 0.7),
(1, 4, 0.1),
(1, 5, 0),
(1, 6, 0),
(2, 3, 0.8),
(2, 4, 0.2),
(2, 5, 0),
(2, 6, 0.3),
(3, 4, 0.3),
(3, 5, 0.2),
(3, 6, 0.25),
(4, 5, 0.8),
(4, 6, 0.6),
(5, 6, 0.9),
(7, 8, 1.0)]
gg = nx.Graph()
for u,v, w in edges:
gg.add_edge(u, v, weight=w)
adj = nx.adjacency_matrix(gg)
adj.setdiag(0)
from sklearn.metrics import silhouette_score, silhouette_samples
print(silhouette_score(adj, metric='precomputed', labels=labels))
silhouette_samples(adj, metric='precomputed', labels=labels)发布于 2020-11-13 02:54:29
这是一个bug。你应该上报。Relevant code.
X, labels = check_X_y(X, labels, accept_sparse=['csc', 'csr'])
# Check for non-zero diagonal entries in precomputed distance matrix
if metric == 'precomputed':
atol = np.finfo(X.dtype).eps * 100
if np.any(np.abs(np.diagonal(X)) > atol):
raise ValueError(
'The precomputed distance matrix contains non-zero '
'elements on the diagonal. Use np.fill_diagonal(X, 0).'
)尽管输入检查显式接受CSC/CSR矩阵,但如果度量为'precomputed',它会将X放入不适用于稀疏矩阵的numpy函数中。
https://stackoverflow.com/questions/64798306
复制相似问题