How can I create an array based on another array?

Viewed 1009

I am new to python, I would like to try to create a array based on another array.

If I have a array like:

array = [[1, 1], [1, 2], [2, 2], [3, 2], [3, 3], [4, 2], [5, 1], [5, 3]]

Then if the matrix would like to be created based on array if there's a value for corresponding position, like this: In array, the first number represents row in the matrix, and the second number represents column in the matrix, such as [1, 1] means row1 and column1 have value, then = 1; there's no [1,3] in the array means row1 and column 3 equal to 0. So I want the result to be like this:

      col1 col2 col3
row1 [ 1    1    0 ]
row2 [ 0    1    0 ]
row3 [ 0    1    1 ]
row4 [ 0    1    0 ]
row5 [ 1    0    1 ]

result = [[1, 1, 0], [0, 1, 0], [0, 1, 1], [0, 1, 0], [1, 0, 1]]

Note that the value in the array is just an example not the exact position of the matrix.

I've tried the insert the value in the empty array but the corresponding position is hard to be identified in the matrix.

Another example is:

array =[[4, 3], [4, 23], [5, 308], [5, 432], [8, 432], [8, 429]]

and the matrix would be like:

      col1 col2 col3 col4 col5
row1 [ 1    1    0    0    0  ]
row2 [ 0    0    1    1    0  ]
row3 [ 0    0    0    1    1  ]

Not sure this is clear for the problem description.

3 Answers

The first array (named array) is called a sparse binary matrix representation. The second array (named result) is called a dense binary matrix representation. If you want to convert the values to ranks (while respecting duplicates) beforehand, you can use the numpy.unique function. So, the full procedure would be:

from scipy.sparse import csr_matrix
from numpy import unique

array = [[1, 1], [1, 2], [2, 2], [3, 2], [3, 3], [4, 2], [5, 1], [5, 3]]

r_unique, rows = unique([v[0] for v in array], return_inverse=True)
c_unique, cols = unique([v[1] for v in array], return_inverse=True)
values = [1] * len(array)
n = len(r_unique)
m = len(c_unique)

result = csr_matrix((values, (rows, cols)), shape=(n, m)).toarray()

This works because r_unique is the set of all unique numbers of one dimension in array, which is the size of the result in the corresponding dimension. rows then contains mapping from the number to its rank within this dimension. The same applies correspondingly to cols and c_unique.

you can do this pretty easily with numpy

rows,columns = numpy.array(list(zip(*array)))
matrix_size = (6,6)
result = numpy.zeros(matrix_size)
result[(rows,columns)] = 1
print(result)

if your array values are one based you will need to make them zero based ... just change

result[(rows-1,columns-1)] = 1 # now we are 0 based instead of 1 based

Solution without library:

data_list = [[1, 1], [1, 2], [2, 2], [3, 2], [3, 3], [4, 2], [5, 1], [5, 3]]

rows_ = (max(data_list, key = lambda x : x[0] ))[0]
cols_ = (max(data_list, key = lambda x : x[1] ))[1]

matrix =  [ [0]*cols_ for r in range(rows_) ]


for data in data_list:
    row_index = data[0] - 1
    col_index = data[1] - 1
    matrix[row_index][col_index] = 1

for matrix_row in matrix:
    print(matrix_row)

Reuslt:

enter image description here

Related