首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在sklearn中使用稀疏矩阵计算silhouette_score

无法在sklearn中使用稀疏矩阵计算silhouette_score
EN

Stack Overflow用户
提问于 2020-11-12 13:15:38
回答 1查看 249关注 0票数 1

我尝试使用稀疏矩阵计算silhouette_score或silhouette_samples,但得到以下错误:

ValueError: diag需要至少二维的数组

示例代码如下:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2020-11-13 02:54:29

这是一个bug。你应该上报。Relevant code.

代码语言:javascript
复制
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函数中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64798306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档