How do I modify a single attribute of a single vertex in the SGraph?

User 5297 | 6/21/2016, 6:36:46 PM

I have a SGraph which contains vertices with some attribute x (attribute x is a list). I want to modify/change the attribute x of a vertex whose id is known beforehand (lets say id = 1001), but I could not find an efficient way to do that other than recreating whole graph with that vertex modified.

I would appreciate any help.


User 940 | 6/22/2016, 6:39:09 PM

Hi @yigitcankaya94 ,

You can actually pull out thevertices attibute of the graph and modify a column of that SFrame (though remember SArrays are immutable).

Here's an example:

python from graphlab import SGraph, Vertex, Edge g = SGraph() verts = [Vertex(0, attr={'breed': 'labrador'}), Vertex(1, attr={'breed': 'labrador'}), Vertex(2, attr={'breed': 'vizsla'})] g = g.add_vertices(verts) g = g.add_edges(Edge(1, 2)) vs = g.vertices vs['breed'] = vs.apply(lambda x: 'new_breed' if x['__id'] == 2 else x['breed'] ) g.vertices # should be changed now

I hope this helps! Let me know if you have any more questions.

Cheers! -Piotr

User 5338 | 6/29/2016, 5:23:08 PM

I think I'm trying to do something similar so I'm adding a question to this thread rather than starting a new one.

I have an SFrame where one column is a id uniquely associated with something like a dog breed and each row corresponds to someone bringing a dog into the animal shelter. I have more rows than dog breeds ids. I want to build an SGraph where each vertex is a dog breed and each vertex has an attribute indicating how many times that breed has visited the shelter.

Is there a straightforward way to do this?

I've tried creating a list of all unique breeds in my SFrame. I can then loop through that list and search the SFrame for all rows matching that breed and create a vertex using the number of rows in the SFrame search result. It works but it's slow. I've tried other similar strategies like converting what I can to numpy arrays, but that doesn't help much. I can imagine other strategies similar to what was proposed above but they all seem to require loops and multiple searches/applies to either my original SFrame or my vertices SFrame.


User 16 | 6/29/2016, 7:54:21 PM

@"John Kucewicz" - First step would be to aggregate the visits then create the graph.

Below is what I would do. The second line is just creating some fake data. You'll want to adjust the last to line to your needs (i.e. change SFrame name and column names).

` import graphlab as gl

dog_sframe = gl.SFrame({'id': ['dog1', 'dog1', 'dog2', 'dog3'], 'vist' : [1, 2,3,4]})

dogvisits = dogsframe.groupby('id', gl.aggregate.COUNT) dogvisitgraph = gl.SGraph(vertices=dogvisits, vidfield='id') `

Hope this helps.

User 5338 | 6/29/2016, 9:10:28 PM

Thank you. I should have known about groupby. My solution was taking minutes on a ~60000 row SFrame. Your solution takes about a second.