I'd like to know how I can do matrix addition in Python
, and I'm running into quite a number of roadblocks trying to figure out the best way.
Here's the problem, written as best as I can formulate it right now.
I have a data set, which is an adjacency matrix for a directed graph, in which isolates of an biological virus is connected to another influenza virus via a directed edge, going from Isolate 1
to Isolate 2
. The current representation of this adjacency matrix is as follows:
Adjacency Matrix for Part 1
===========================
Isolate 1 Isolate 2 Connected?
--------- --------- ---------
ID1 ID2 1
ID1 ID3 1
ID2 ID4 1
As is seen above, not every isolate is connected to another isolate, for a given part. I have another sparse matrix, illustrating the same type of connections but for a different part. Here's what it's like:
Adjacency Matrix for Part 2
===========================
Isolate 1 Isolate 2 Connected?
--------- --------- ----------
ID1 ID2 1
ID1 ID3 1
ID1 ID4 1
The difference here is that ID1 is connected to ID4, rather than ID2 being connected to ID4.
So what I'd like to do is to add these two adjacency matrices. What I would expect is the following:
Summed Adjacency Matrix
=======================
Isolate 1 Isolate 2 Connected?
--------- --------- ---------
ID1 ID2 2
ID1 ID3 2
ID1 ID4 1
ID2 ID4 1
Does anybody know how I can do this efficiently using Python
packages? Most of my work has been done in iPython
's HTML notebook, and I've been relying heavily on Pandas 0.11
to do this analysis. If there's an answer in which I could avoid transforming the data into a huge matrix (500x500), that would be the best!
Thanks everybody!