Home > Enterprise >  Create matrix from indices and value points
Create matrix from indices and value points

Time:10-29

I want to read a text file with values of matrix. Let's say you have got a .txt file looking like this:

0 0 4.0
0 1 5.2
0 2 2.1
1 0 2.1
1 1 2.9
1 2 3.1

Here, the first column gives the indices of the matrix on the x-axis and the second column fives the indices of the y-axis. The third column is a value at this position in the matrix. When values are missing the value is just zero.

I am well aware of the fact, that data formats like the .mtx format exist, but I would like to create a scipy sparse matrix or numpy array from this txt file alone instead of adjusting it to the .mtx file format. Is there a Python function out there, which does this for me, which I am missing?

CodePudding user response:

Starting from the array (a) sorted on the first column (major) and second (minor) as in your example, you can reshape:

# a = np.loadtxt('filename')
x = len(np.unique(a[:,0]))
y = len(np.unique(a[:,1]))
a[:,2].reshape(x,y).T

Output:

array([[4. , 2.1],
       [5.2, 2.9],
       [2.1, 3.1]])

CodePudding user response:

import numpy

with open('filename.txt','r') as f:
    lines = f.readlines()
f.close()

data = [i.split(' ') for i in lines]
z = list(zip(*data))

row_indices = list(map(int,z[0]))
column_indices = list(map(int,z[1]))
values = list(map(float,z[2]))

m = max(row_indices) 1
n = max(column_indices) 1

p = max([m,n])

A = numpy.zeros((p,p))
A[row_indices,column_indices]=values

print(A)

If you want a square matrix with maximum of column 1 as the number of rows and and the maximum of column 2 to be the size, then you can remove p = max([m,n]) and replace A = numpy.zeros((p,p)) with A = numpy.zeros((m,n)).

  • Related