user3117891
user3117891

Reputation: 145

How to create a matlab data set from python?

I have several graph instances and I have measured their features, like density, order, size, nodes degree, etc. in Python, using networkx. Now, I want to create a file where the feature vector of each instance is saved, so I can load it into matlab, in a similar way to:

load hald

in order to process it.

This is a python dictionary of the feature vector of an instance:

{'orden': 100, 'name': 'random_P1_N100_I1', 'density': 0.1006060606060606, 'diameter': 4, 'radius': 3, 'size': 498}

I have several of those feature vectors, now I want to put them into a .mat file so it is easy to analize the data in Matlab.

I tried scipy.io.savemat but was not succesful. So maybe a more "manual" way to do it exists?

Upvotes: 0

Views: 189

Answers (1)

user3117891
user3117891

Reputation: 145

Well, I have managed to do it, here it is what I came with.

First, I created a python dictionary with the names of the features as keys and empty lists as values.

WholeDict = {'name':list(), 'order':list(), 'size':list(), 'density':list(), 'diameter':list(), 'radius':list(), 'nodesEccentricity':list()}

Then I measure the features of an instance and append each feature value to the respective feature key in the dictionary. I do that for each instance. When this process ends, I have got a dictionary in wich each item is a list representing the values of the measured feature across instances.

WholeDict['name'].append(instanceName)
WholeDict['order'].append(order)
WholeDict['size'].append(size)
WholeDict['density'].append(density)
WholeDict['diameter'].append(diameter)
WholeDict['radius'].append(radius)
WholeDict['nodesEccentricity'].append(nodesEccentricity.items())

Having measured three different instances, the python dictionary has this content:

{'diameter': [2, 3, 3], 'name': ['c3c3', 'c3c4', 'c3c5'], 'density': [0.5, 0.36363636363636365, 0.2857142857142857], 'nodesEccentricity': [[(1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2), (7, 2), (8, 2), (9, 2)], [(1, 3), (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3), (8, 3), (9, 3), (10, 3), (11, 3), (12, 3)], [(1, 3), (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3), (8, 3), (9, 3), (10, 3), (11, 3), (12, 3), (13, 3), (14, 3), (15, 3)]], 'radius': [2, 3, 3], 'order': [9, 12, 15], 'size': [18, 24, 30]}

Happily, it works even with the features wich values are lists, like node eccentricities. So I can save it using this:

sio.savemat('aMatFile', {'featureSet':WholeDict})

And open it in Matlab with:

load aMatFile

In Matlab, the dictionary becomes an struct called featureSet, looking like this:

>> featureSet

featureSet = 

         diameter: [2 3 3]
             name: [3x4 char]
          density: [0.5 0.363636363636364 0.285714285714286]
nodesEccentricity: {[9x2 int64]  [12x2 int64]  [15x2 int64]}
           radius: [2 3 3]
            order: [9 12 15]
             size: [18 24 30]

And the feature values are accesible as:

>> featureSet.name

ans =

c3c3
c3c4
c3c5

If I want to see the nodes eccentricity of instance c3c3:

featureSet.nodesEccentricity{1}

ans =

                1                    2
                2                    2
                3                    2
                4                    2
                5                    2
                6                    2
                7                    2
                8                    2
                9                    2

I think this will work for me, and I hope it is also useful for someone else. Thanks everybody.

Upvotes: 0

Related Questions