csv转为矩阵 python_利用python将二值csv格式转换为矩阵

该博客介绍如何使用Python将二值CSV文件转换为邻接矩阵。通过读取CSV文件,处理数据,创建并填充邻接矩阵,然后将结果写入新的文件中,实现了药物和靶向蛋白的相互作用矩阵转换。
摘要由CSDN通过智能技术生成

#!/usr/bin/env python#coding:utf-8#import pandas as pd, numpy as np;

‘‘‘将csv文件转换为对应的邻接矩阵mat‘‘‘

from numpy import *;defprotein_complexes_trans():

file= open(‘protein_complexes.csv‘);

filePro= open(‘complexes‘, ‘a‘);

fileTarget= open(‘targets(complexes)‘,‘a‘);

fileInter= open(‘protein_complexes_interaction_matrix‘, ‘a‘);

proteins=[];

targets=[];while 1:

line=file.readline();if notline:break;

token= line.split(‘;‘);

targ= token[1].split(‘,‘);for i inrange(0,len(targ)):

targets.append(targ[i]);

proteins.append(token[0]);

file.seek(0);#将指针重置回第一行

proArr= unique(array(proteins)).tolist(); #去重,并删去最后一项(标题)

tarArr = unique(array(targets)).tolist(); #删去最后

#mat为邻接矩阵

mat = zeros((len(proArr),len(tarArr)), dtype =int16);while 1:

line=f

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值