10.2 Construct A Simple Network

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 9

Let's start by looking at how we can construct a simple network

and what we can do with that.

So this is very basic stuff.

So first thing you want to do always, of course,

with Python is we want to figure out

what are the imports that we need to use for this.

So we want to import NetworkX, which you should probably

get easily, given that you've installed Anaconda's Python.

We're going to draw the network.

So you want to make sure that you

are using Matplotlib Inline, so that we can see

the network on the notebook.

We want to import NumPy.

Because we might need it.

And we want to import Pyplot.

Because we might need it, OK?

These are the basic imports.

And this should be pretty much it for the networks.

But What we will see down the road.

So we want to make a simple network.

A network is a set of nodes and a set of edges.

So the first question when you want to create a network

is we want to ask ourselves what are the nodes

and what are the edges.

So let's say we start by creating


a bunch of nodes that are these in this list, 1, 2, 3, 4, 5, 6,

7, 8.

So notice that these are all immutable integers.

1 is a node.

2 is a node.

3 is a node.

We can give anything we want, as long as it is immutable.

So we set up a network by using this function nx.graph.

graph.

Nx, if you recall, is our NetworkX module, the alias

for our NetworkX module.

So we set up our network by saying nx.graph.

So what this does is it creates a network object,

a graph object for our network.

And what we want to do now is we have the network.

It's empty.

We haven't added any nodes or edges to it.

So what we want to do is we want to add these nodes

and add the edges.

So our nodes are these things here.

And we can just add them using the function add_nodes_from

and give it a list or some collection of nodes.

So that's going to add the nodes 1, 2, 3, 4, 5, 6, 7,

8 into the graph.

Then, we want to add the edges.


Edges are the linkage between two pairs of nodes,

in between a pair of nodes.

So this tells us that 1 and 2 is linked, 2 and 3 is linked,

1 and 3 are linked, 4 and 5 is linked.

So clearly, 1 and 4 don't have an edge between them.

1 and 2 have an edge.

2 and 3 have an edge.

1 and 3 have an edge.

But 1 and 4 doesn't have.

Because we don't have a 1 and 4 here anywhere.

So we add that using the function add_edges_from.

And then, we can take a look at our network by just drawing it.

So let's draw that.

And we don't get this because I have not imported it.

OK, we can ignore these warnings.

Anything that comes in pink is a warning.

And this is telling us that some of these functions that

actually NetworkX is using, I believe,

are deprecated and may not be valid in later versions

of Matplotlib.

This is one thing you have to watch out for in Python.

When you have something that's working,

you probably don't want to change the libraries

or upgrade the libraries for that application.

So one of the things that you can do in Python


is run virtual machines.

And we don't have time in this class to look at that.

But you might want to look up that stuff here--

virtual environments, I'm sorry, and look that up online,

and see how you can set up a virtual environment.

What the virtual environment will do

is it'll take all your inputs and seal them

in that environment.

And then, if you upgrade a library

in some other environment, it's now

going to upgrade it in this environment,

so that you know your package will keep running.

So it's not a bad idea.

But anyway, this is our network now.

So we got this network here.

And it shows us our nodes and our edges.

Of course, we don't know what the nodes are.

So what we can do is we can add a few labels to the nodes.

So I'm just going to walk through this.

Because we will be using this a lot down the road.

Or you will be using it a lot.

So let's say we start with our simple network.

And we're going to give it a layout.

There are many possibilities for layouts in Python and NetworkX.

So for example, I could see here, nx.randomlayout.


I think that's right, random layout.

That would be another layout.

And there are quite a few of them.

You can look up all the various layouts on NetworkX.

But spring layout is just a very simple way

of looking at a network.

So we'll use that.

So we have nx.springlayout.

And we tell it we want to take a simple network, the one we

defined earlier with the edges and nodes,

and keep that in this network--

keep that as the layout of our network.

Note that the layer of the network

has nothing to do with the graph itself.

It's more for the visualization than anything else.

Then, we draw the nodes.

So we draw the nodes.

And we give it, using the function, draw_networkx_nodes.

It's very straightforward.

We tell it the network we want to draw it for.

We want to give it the layout that we want to use, right?

That's our [? boss ?] here, which is the layout

that we are using for this.

We give it the color of the nodes.

And R is for red.


B is for blue.

Y is for yellow.

G is for green, that kind of stuff.

We give it a node size.

And this, again, you can play around with a little bit

to see.

So I'm giving it 500.

Obviously, if you give a higher number,

you're going to get a larger visual of the node.

And a smaller number will give a smaller node.

And the alpha is the Matplotlib thing that we're looking for,

which what it does is it tells you

how dense the color of the node is going to be.

A lower alpha will make it more transparent.

And a higher alpha will make it more opaque.

So that draws our nodes.

Once we do this, our nodes will get drawn.

Then, we want to draw the edges.

So for edges, again, NetworkX has simple function,

add_networkx_edges.

And we tell it the network, the layout, the list of edges--

and the list of edges is what we gave up ahead on the top--

the width of the edge-- so this tells you

how thick the line is.

Edges are represented as lines.


So this tells you how thick the line is.

Then of course, alpha is the color density,

how opaque or transparent it is.

And we give a color to the edge, B, which is blue.

So we're going to get a network with red nodes and blue edges,

which is different from here, where

we had, well, some odd looking color,

orange, reddish, pinkish thing as nodes

and black as the edges.

Now, we want to give the nodes names.

So what I'm going to do here is I'm

going to create here a list of names of nodes,

or rather a dictionary of node names.

And then, for each node in my list of nodes, the 1, 2, 3, 4,

5, 6, the list of nodes we had, I'm

going to add to that the node number.

So it's an integer and a string version of that node.

Because it'll look nicer, more better, with strings anyway.

So then, what we want to do is we need to pass it as string.

So I think we need to pass it as a string.

So that's why we have a string node here.

And then, we draw the labels.

So the labels are what's going to show up as 1,

2, 3, 4, 5, 6, 7 in the thing.

We give it our network.


We give it the layout.

We give it the dictionary of node names.

And we give it the font size.

So this is really a mapping from our node number to a node name.

So that's what we have over there.

We don't know the axis.

Because we don't need an axis for a network.

There's no x and y.

Then, we show it.

So let's see how that looks like.

And we get a nice looking graph like this.

All right, note that this is going to be different

every time you run it.

Because it's really just laying it out as you're like here.

So this tells us now we have our nodes numbered and edges

nicely colored in blue.

And we can play around with this a little bit--

we will do that down the road--

to add more information to our graph.

But the basic graph is very straightforward.

What you want to do is you want to decide

what the format of your node is, the color, the transparency,

et cetera.

You want to decide with the format of the edges

are, the color, the transparency, et cetera.


You want to decide what data you want to show up in the node.

Because remember, the node can actually contain lots of data.

For example, if there are people,

you might have the social security number, the name,

the address, phone number, all that kind of stuff.

And you don't want to pack all that into this.

You might want to just back in the first name of the person

or the last name of the person.

So you can extract that and use that over here,

instead of using the entire contents.

That's the idea there.

And then, you use the Network X labels just to actually render

the labels on the graph.

So that becomes our simple graph over here.

End of transcript. Skip to the start.

Previous

You might also like