Skip to content
Advertisement

create a matrix from combinations with values

I have some combinations like

JavaScript

Any idea on how I efficiently can create a four by four matrix with these 0,1 values from all these combinations? So the result will be something like:

JavaScript

Advertisement

Answer

Imagine if the “combinations” are stored in a file in the following format (or similar):

JavaScript

Then you can do:

JavaScript

Example (using your sample data):

JavaScript

Now df contains:

JavaScript

From that point, a little bit of massaging will get you what you want:

JavaScript

We get:

JavaScript

Note: if you prefer to stay in int-only (and set the diagonal to 0), then:

JavaScript

and now:

JavaScript

For speed

Now, if you know for sure that you are dealing with 4×4 matrices and that the order is exactly as you indicated (ordered by the upper triangle), you can do the following for a faster set up:

JavaScript

The result is a simple numpy array (no labels):

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement