下面是对卢万的描述。
我想传递一个特定的adj矩阵,但是,我尝试了下面的最小示例,得到了“值的长度(4)不匹配索引(6)的长度”的结果。这个错误是由于滥用稀疏矩阵造成的吗?
代码:
import scanpy as sc
import torch
import numpy as np
import networkx as nx
nodes = [[0, 0, 0, 1], [0, 0, 0, 2], [0, 10, 0, 0], [0, 11, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0]]
features = torch.tensor(nodes)
print(features.shape)
edgelist = [(0,1), (1,2), (2,3)]
G = nx.Graph(edgelist)
G_adj = nx.convert_matrix.to_scipy_sparse_matrix(G) # transform to scipy sparse matrix
adata = sc.AnnData(features.numpy())
sc.pp.neighbors(adata, n_neighbors=2, use_rep='X')
sc.tl.louvain(adata, resolution=0.01, adjacency=G_adj) # pass the adj here
y_pred = adata.obs['louvain'].astype(int).to_numpy()
n_clusters = len(np.unique(y_pred))请指出问题所在,并举例说明如何在使用scanpy.tl.louvain时显式传递邻接矩阵。谢谢!
发布于 2022-06-19 16:55:11
G是一个由四个节点创建的图,因此G_adj是一个(4,4)稀疏矩阵。adata是一个具有6个观测值和4个变量的稀疏对象。该算法对观测值进行聚类,从而得到形状(6,6)的邻接矩阵。
不知道你想做什么:
如果您真的有6个节点,那么您应该修改图的代码:
print(features.shape)
edgelist = [(0,1), (1,2), (2,3)]
G = nx.Graph()
G.add_nodes_from(range(6))
G.add_edges_from(edgelist)
G_adj = nx.convert_matrix.to_scipy_sparse_matrix(G) # transform to scipy sparse matrix
adata = sc.AnnData(features.numpy())如果有4个节点,请修改adata创建行:
adata = sc.AnnData(features.numpy().T)https://stackoverflow.com/questions/70705764
复制相似问题