Using Python to generate a connection/network graph - python

I have a text file with about 8.5 million data points in the form:
Company 87178481
Company 893489
Company 2345788
[...]
I want to use Python to create a connection graph to see what the network between companies looks like. From the above sample, two companies would share an edge if the value in the second column is the same (clarification from/for Hooked).
I've been using the NetworkX package and have been able to generate a network for a few thousand points, but it's not making it through the full 8.5 million-node text file. I ran it and left for about 15 hours, and when I came back, the cursor in the shell was still blinking, but there was no output graph.
Is it safe to assume that it was still running? Is there a better/faster/easier approach to graph millions of points?

If you have 1000K points of data, you'll need some way of looking at the broad picture. Depending on what you are looking for exactly, if you can assign a "distance" between companies (say number of connections apart) you can visualize relationships (or clustering) via a Dendrogram.
Scipy does clustering:
http://docs.scipy.org/doc/scipy/reference/cluster.hierarchy.html#module-scipy.cluster.hierarchy
and has a function to turn them into dendrograms for visualization:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.hierarchy.dendrogram.html#scipy.cluster.hierarchy.dendrogram
An example for a shortest path distance function via networkx:
http://networkx.lanl.gov/reference/generated/networkx.algorithms.shortest_paths.generic.shortest_path.html#networkx.algorithms.shortest_paths.generic.shortest_path
Ultimately you'll have to decide how you want to weight the distance between two companies (vertices) in your graph.

You have too many datapoints and if you did visualize the network it won't make any sense. You need to have ways to 1)reduce the number of companies by removing those that are less important/less connected 2)summarize the graph somehow and then visualize.
to reduce the size of data it might be better to create the network independently (using your own code to create an edgelist of companies). This way you can reduce the size of your graph (by removing singletons for example, which may be many).
For summarization I recommend running a clustering or a community detection algorithm. This can be done very fast even for very large networks. Use the "fastgreedy" method in the igraph package: http://igraph.sourceforge.net/doc/R/fastgreedy.community.html
(there is a faster algorithm available online as well, this is by Blondel et al: http://perso.uclouvain.be/vincent.blondel/publications/08BG.pdf I know their code is available online somewhere)

Related

Matrix clustering using Python

I'm working on a growing matrix of data. I found that probably the best way to make my computations faster, I need to clusterize it somewhat in a way of this: Clusterized matrix
My matrix shows connections between nodes on a graph with their weights on the intersections.
I made a graph using NetworkX and noticed it does something similar. Screenshot: NX's Graph
Maybe I could use NetworkX's code to cluster it instead of growing my code by another function?
If not, then any python way of doing it would be helpful. I read many tutorial on hierarchical clustering but it all seems to be about connecting points in a two-dimensional space, not in the graph-space with given 'distances'.

Community Detection Algorithms using NetworkX

I have a network that is a graph network and it is the Email-Eu network that is available in here.
This dataset has the actual dataset, which is a graph of around 1005 nodes with the edges that form this giant graph. It also has the ground truth labels for the nodes and its corresponding communities (department). Each one of these nodes belongs to one of each 42 departments.
I want to run a community detection algorithm on the graph to find to the corresponding department for each node. My main objective is to find the nodes in the largest community.
So, first I need to find the first 42 departments (Communities), then find the nodes in the biggest one of them.
I started with Girvan-Newman Algorithm to find the communities. The beauty of Girvan-Newman is that it is easy to implement since every time I need to find the edge with the highest betweenness and remove it till I find the 42 departments(Communities) I want.
I am struggling to find other Community Detection Algorithms that give me the option of specifying how many communities/partitions I need to break down my graph into.
Is there any Community Detection Function/Technique that I can use, which gives me the option of specifying how many communities do I need to uncover from my graph? Any ideas are very much appreciated.
I am using Python and NetworkX.
A (very) partial answer (and solution) to your question is to use Fluid Communities algorithm implemented by Networkx as asyn_fluidc.
Note that it works on connected, undirected, unweighted graphs, so if your graph has n connected components, you should run it n times. In fact this could be a significant issue as you should have some sort of preliminary knowledge of each component to choose the corresponding k.
Anyway, it is worth a try.
You may want to try pysbm. It is based on networkx and implements different variants of stochastic block models and inference methods.
If you consider to switch from networkxto a different python based graph package you may want to consider graph-tool, where you would be able to use the stochastic block model for the clustering task. Another noteworthy package is igraph, may want to look at How to cluster a graph using python igraph.
The approaches directly available in networkx are rather old fashioned. If you aim for state of the art clustering methods, you may consider spectral clustering or Infomap. The selection depends on your desired usage of the inferred communities. The task of inferring ground truth from a network, falls under (approximate) the No-Free-Lunch theorem, i.e. (roughly) no algorithm exists, such that it returns "better" communities than any other algorithm, if we average the results over all possibilities.
I am not entirely sure of my answer but maybe you can try this. Are you aware of label propagation ? The main idea is that you have some nodes in graph which are labelled i.e. they belong to a community and you want to give labels to other unlabelled nodes in your graph. LPA will spread these labels across the graph and give you a list of nodes and the communities they belong to. These communities will be the same as the ones that your labelled set of nodes belong to.
So I think you can control the number of communities you want to extract from the graph by controlling the number of communities you initialise in the beginning. But I think it is also possible that after LPA converges some of the communities you initialised vanish from the graph due the graph structure and also randomness of the algorithm. But there are many variants of LPA where you can control this randomness. I believe this page of sklearn talks about it.
You can read about LPA here and also here

Pattern Matching or comparing two graphs (line charts)

Given:
Ideal graph - Depicts the expected reading my machine should have.
Actual graph - Depicts the actual reading my machine had at that instance.
X-axis: Force(N) from the machine
Y-axis: Time(s)
Both graphs were created using pyplot library in python.
What I need to do:
I need to compare the graph in its three phases: initialization (machine starts applying force), constant phase (constant force), end phase (machine stops applying force) and give the analysis of how close the phases in the actual read were to the ideal case (in terms of percentage). The analysis would allow me to conclude how the machine performed in those three phases for the actual read taken. I would need to do this for each reading taken every 50s.
Hurdle:
Now both the graphs were not created using the same number of data points. Ideal graph was created with 100 set of points and Actual graph was created using 30,000+ points. So I would not be able to compare the graphs using data points.
Idea:
Would it be wise to save the graph of the actual read as a png and compare it with the image of the ideal case graph?
Please give me some idea or solution to tackle this problem.
It's a bit late but I'm going to answer anyway:
I don't think resorting to a comparison of images is wise in this case, no.
What you probably want is to interpolate additional points between the 100 points on the 'Ideal graph' to match the 30,000+ points in the 'Actual graph'.
The example on 1-D Interpolation in the scipy.interpolate docs seems to be exactly what you need.
If you need further assistance (such as working code), you will have to provide a Minimal, Complete, and Verifiable Example for us to work with.

What's the right algorithm for finding isolated subsets

Picture is worth a thousand words, so:
My input is the matrix on the left, and what I need to find is the sets of nodes that are maximum one step away from each other (not diagonally). Node that is more than one up/down/left/right step away would be in a separate set.
So, my plan was running a BFS from every node I find, then returning the set it traversed through, and removing it from the original set. Iterate this process until I'm done. But then I've had the wild idea of looking for a graph analysis tools - and I've found NetworkX. Is there an easy way (algorithm?) to achieve this without manually writing BFS, and traverse the whole matrix?
Thanks
What you are trying to do is searching for "connected components" and
NetworX has itself a method for doing exactly that as can be seen in the first example on this documentation page as others has already pointed out on the comments.
Reading your question it seems that your nodes are on a discrete grid and the concept of connected that you describe is the same used on the pixel of an image.
Connected components algorithms are available for graphs and for images also.
If performances are important in your case I would suggest you to go for the image version of connected components.
This comes by the fact that images (grids of pixels) are a specific class of graphs so the connected components algorithms dealing with grids of nodes
are built knowing the topology of the graph itself (i.e. graph is planar, the max vertex degree is four). A general algorithm for graphs has o be able to work on general graphs
(i.e they may be not planar, with multiple edges between some nodes) so it has to spend more work because it can't assume much about the properties of the input graph.
Since connected components can be found on graphs in linear time I am not telling the image version would be orders of magnitude faster. There will only be a constant factor between the two.
For this reason you should also take into account which is the data structure that holds your input data and how much time will be spent in creating the input structures which are required by each version of the algorithm.

Could I detect guitar chords/notes via comparing it to other notes?

My idea is that instead of using DFT/FFT to calculate guitar chords or notes, I can attempt a different approach. How I plan to do it is so (in Python)
Record myself playing a large range of notes and chords and store them as a large dataset.
After this, I would create another recording of me doing a single note or chord which needs to be recognised.
In a similar manner of which I compare two datasets using spearman's rank correlation coefficient, I could compare the recording to each file in the dataset and see which note or chord is most similar.
For my situation, I aim for this calculation to occur as I play, so no preprocessing would be involved. To do this I would need to calibrate the background noise volume, so I could distinct each note/chord from each other.
Diagram to explain the concept:
To help explain my idea, I have created a simple image which has a dataset of two notes.
Imgur link of the diagram
My question is how viable this would be, and if it is feasible, how would I conduct it in Python?
Thanks,
Aj.

Categories