user3431083
user3431083

Reputation: 414

Python list into numpy array

I have seen lots of example of lists into arrays, but no examples of lists in this format into arrays, which is strange because the list format I present is the standard go-to way of defining a graph, point-to-point mapping that you would find in any table, csv, database, etc. I tried everything here with no luck. Thanks for any ideas.

Input and desired output

input=  [[A, A, 0],
        [A, B, 5],
        [A, C, 3],
        [B, A, 5],
        [B, B, 0],
        [B, C, 6],
        [C, A, 3],
        [C, B, 6],
        [C, C, 0]]

desiredOutput=  [[0, 5, 3],
                [5, 0, 6],
                [3, 6, 0]]

Upvotes: 0

Views: 117

Answers (2)

PM 2Ring
PM 2Ring

Reputation: 55469

Here's one way to produce your adjacency matrix as a 2D Numpy array. It assumes that the input graph data is correct, in particular, that its length is a perfect square.

import numpy as np

graph_data =  [
    ['A', 'A', 0], ['A', 'B', 5], ['A', 'C', 3],
    ['B', 'A', 5], ['B', 'B', 0], ['B', 'C', 6],
    ['C', 'A', 3], ['C', 'B', 6], ['C', 'C', 0],
]

size = np.sqrt(len(graph_data)).astype(np.int)
adjacency_matrix = np.array(graph_data)[:,-1].astype(np.int).reshape(size, size)
print(adjacency_matrix)

output

[[0 5 3]
 [5 0 6]
 [3 6 0]]

The above code also assumes that the graph data is in the proper order, since it ignores the letters. Of course, that's easily handled by sorting the graph data before attempting to convert it to an array. Eg,

graph_data.sort()

Here's a pure Python version that outputs a list of tuples:

graph_data =  [
    ['A', 'A', 0], ['A', 'B', 5], ['A', 'C', 3],
    ['B', 'A', 5], ['B', 'B', 0], ['B', 'C', 6],
    ['C', 'A', 3], ['C', 'B', 6], ['C', 'C', 0],
]

graph_data.sort()
size = int(len(graph_data) ** 0.5)
it = iter(row[-1] for row in graph_data)
print(list(zip(*[it]*size)))

output

[(0, 5, 3), (5, 0, 6), (3, 6, 0)]

Upvotes: 2

Julien Spronck
Julien Spronck

Reputation: 15423

You can just slice your array and reshape it:

input= [['A', 'A', 0], ['A', 'B', 5], ['A', 'C', 3], ['B', 'A', 5], ['B', 'B', 0], ['B', 'C', 6], ['C', 'A', 3], ['C', 'B', 6], ['C', 'C', 0]]
arr = np.array(input)
desiredOutput=arr[:, 2].reshape(3, 3).astype(np.float)
# array([[ 0.,  5.,  3.],
#        [ 5.,  0.,  6.],
#        [ 3.,  6.,  0.]])

Upvotes: 2

Related Questions