Darren J. Fitzpatrick
Darren J. Fitzpatrick

Reputation: 7409

Populate arrays in python (numpy)?

Given a file in the format below:

a a 0
a b 1
a c 1
b b 0
b a 1
b c 1
c c 0
c a 1
c b 1

The third column is the distance between the items in the first and second columns. If I read such a file into pyton as a nested list, how do I convert it to a symmetrical matrix, i.e.,

  a b c
a 0 1 1
b 1 0 1
b 1 1 0

? I also wish to include the column and row names.

I would preferably like to use numpy to complete this task.

Any suggestions?

Thanks, D.

Upvotes: 3

Views: 2360

Answers (2)

Mike T
Mike T

Reputation: 43622

A slightly different approach:

import numpy as np
# Load "Row Col Value" text file
ar = np.loadtxt('file.txt', [('R','|S1'), ('C','|S1'), ('V','i')])
names = np.unique(np.row_stack((ar['R'], ar['C']))).tolist()
vf = np.vectorize(lambda x: names.index(x), otypes='i')
# load them in an output array
out = np.empty((len(names), len(names)), 'i')
out[vf(ar['R']), vf(ar['C'])] = ar['V']

Upvotes: 2

Lauritz V. Thaulow
Lauritz V. Thaulow

Reputation: 50985

import numpy as np
from itertools import count

data = [line.split() for line in inputfile.readlines()]
rows = dict(zip(sorted(set(line[0] for line in data)), count()))
cols = dict(zip(sorted(set(line[1] for line in data)), count()))
array = np.zeros((len(rows), len(cols)))

for row, col, val in data:
    index = (rows[row], cols[col])
    array[index] = val

I don't know how to label rows and columns in numpy, so I just made a dict mapping the row label to the row index and another doing the same for the columns. If you need it you can make a reverse map, as below, or you can make rows and cols a bidict.

rows_reverse = dict((v, k) for k, v in rows)
cols_reverse = dict((v, k) for k, v in cols)

Upvotes: 4

Related Questions