【问题标题】:Scipy sparse matrix from edge list来自边缘列表的 Scipy 稀疏矩阵
【发布时间】:2017-03-16 21:51:33
【问题描述】:

如何将 边列表 (data) 转换为 python scipy 稀疏矩阵 得到这个结果:

数据集(其中“agn”是节点类别一,“fct”是节点类别二):

data['agn'].tolist()
['p1', 'p1', 'p1', 'p1', 'p1', 'p2', 'p2', 'p2', 'p2', 'p3', 'p3', 'p3', 'p4', 'p4', 'p5']

data['fct'].tolist()
['f1', 'f2', 'f3', 'f4', 'f5', 'f3', 'f4', 'f5', 'f6', 'f5', 'f6', 'f7', 'f7', 'f8', 'f9']

(不工作)python代码:

from scipy.sparse import csr_matrix, coo_matrix

csr_matrix((data_sub['agn'].values, data['fct'].values), 
                    shape=(len(set(data['agn'].values)), len(set(data_sub['fct'].values))))

-> 错误:“TypeError:输入格式无效” 我真的需要三个数组来构造矩阵吗,就像 scipy csr 文档中的示例所建议的那样(只能使用两个链接,抱歉!)?

(工作)R 代码用于构造只有两个向量的矩阵:

library(Matrix)

grph_tim <- sparseMatrix(i = as.numeric(data$agn), 
                     j = as.numeric(data$fct),  
                     dims = c(length(levels(data$agn)),
                              length(levels(data$fct))),
                     dimnames = list(levels(data$agn),
                                     levels(data$fct)))

编辑: 在我修改了here 的代码并添加了所需的数组后,它终于起作用了:

import numpy as np
import pandas as pd
import scipy.sparse as ss

def read_data_file_as_coo_matrix(filename='edges.txt'):
    "Read data file and return sparse matrix in coordinate format."

    # if the nodes are integers, use 'dtype = np.uint32'
    data = pd.read_csv(filename, sep = '\t', encoding = 'utf-8')

    # where 'rows' is node category one and 'cols' node category 2
    rows = data['agn']  # Not a copy, just a reference.
    cols = data['fct']

    # crucial third array in python, which can be left out in r
    ones = np.ones(len(rows), np.uint32)
    matrix = ss.coo_matrix((ones, (rows, cols)))
    return matrix

此外,我将节点的字符串名称转换为整数。因此data['agn'] 变为[0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4]data['fct'] 变为[0, 1, 2, 3, 4, 2, 3, 4, 5, 4, 5, 6, 6, 7, 8]

我得到了这个稀疏矩阵:

(0, 0) 1 (0, 1) 1 (0, 2) 1 (0, 3) 1 (0, 4) 1 (1, 2) 1 (1, 3) 1 (1, 4) 1 (1, 5) 1 (2, 4) 1 (2, 5) 1 (2, 6) 1 (3, 6) 1 (3, 7) 1 (4, 8) 1

【问题讨论】:

  • 有点不清楚你想要什么:系数是什么,指数是什么?从数据图片(顺便说一句,您不应该放图片,只需将数据复制粘贴为文本以便我们可以使用它)我们看到索引是 p1,f1 等这些不是整数(据我所知) 所以它们不能直接用作索引。
  • 即使我改用数值,我仍然得到同样的错误! data_a = [0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4] data_b = [0, 1, 2, 3, 4, 2, 3, 4、5、4、5、6、6、7、8]。 mtx = csr_matrix((data_a, data_b), shape=(len(set(data_a)), len(set(data_b)))))
  • 是的,scipy 稀疏需要 data 以及 rowscols 数组。它不假定data 值都是1。原始稀疏矩阵代码用于线性代数问题,其中data 是浮点数。

标签: python r matrix scipy sna


【解决方案1】:

在我修改here的代码并添加所需的数组后,它终于起作用了:

import numpy as np
import pandas as pd
import scipy.sparse as ss

def read_data_file_as_coo_matrix(filename='edges.txt'):
    "Read data file and return sparse matrix in coordinate format."

    # if the nodes are integers, use 'dtype = np.uint32'
    data = pd.read_csv(filename, sep = '\t', encoding = 'utf-8')

    # where 'rows' is node category one and 'cols' node category 2
    rows = data['agn']  # Not a copy, just a reference.
    cols = data['fct']

    # crucial third array in python, which can be left out in r
    ones = np.ones(len(rows), np.uint32)
    matrix = ss.coo_matrix((ones, (rows, cols)))
    return matrix

此外,我将节点的字符串名称转换为整数。因此data['agn'] 变为[0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4]data['fct'] 变为[0, 1, 2, 3, 4, 2, 3, 4, 5, 4, 5, 6, 6, 7, 8]

我得到了这个稀疏矩阵:

(0, 0) 1 (0, 1) 1 (0, 2) 1 (0, 3) 1 (0, 4) 1 (1, 2) 1 (1, 3) 1 (1, 4) 1 (1, 5) 1 (2, 4) 1 (2, 5) 1 (2, 6) 1 (3, 6) 1 (3, 7) 1 (4, 8) 1

【讨论】:

    猜你喜欢
    • 2016-12-04
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 2017-01-19
    • 2012-05-15
    • 2017-03-26
    • 2017-03-31
    • 2023-04-10
    相关资源
    最近更新 更多