Repository navigation
Example: spectral clustering based graph - #4643
sharwinbobde wants to merge 15 commits into
Conversation
…tral clustered by scikit learn
|
@dschult we changed the colour schemes for plots of Step 1 and 2 to indicate that because clustering is an unsupervised method, the grouping does not necessarily indicate class membership. |
|
This is much clearer--thank you. But it still is not clear how you are assigning the edges in a network. It looks like you start with a dataset with three variables for each observation ==> two are continuous variables and the third is a binary classification. Then you use an AI method (which one?) to classify the observations into two groups. The clustering output shows these groups and they are close but not the same as the original groups. Is this correct? Then you show the network -- but there is no explanation for what the nodes represent, how the edges are chosen, etc. |
|
I'll write a detailed flow here first thing in the morning :) |
Demonstartion ExplanationTask overview
Usage
Step 1: Generating sample dataset
Step 2: Clustering
Step 3: constructing the network
|
|
Oops! posted the messages on the issue thread instead of this PR before. @dschult hope this is clearer :) |
|
Much more complete information here... Thank you! The original description had the sentence: "Then using the data from the spectral clustering to generate a graph for the data points." If I understand correctly, the "graph" is constructed using the affinity matrix by computing k-nearest-neighbors. But maybe here you mean "graph" (as in draw) a scatter plot of the feature space showing the clusters. In general, stay away from the word "graph" to mean a scatter plot when you've got graph theory in the mix. Graph Theory graphs can be called "networks", while graphs of datasets can be called scatter plots (or whatever other plot they are). What is the right visualization of the network that will make this algorithm more clear? I think you have formed the network using distance in the feature space between data points. When you plot it you use a spring_layout for the nodes (which are the data points). Maybe it would be better to use the feature values as the coordinates of the nodes. Then you will show the essentially the scatter plot with edges between neighbors. Another approach would be to plot the nodes using laplacian eigenvector values for two main eigenvalues. That gives a spectral layout which may (or may not depending on the meaning of "spectral clustering") show how the clustering is being determined. Perhaps both pictures of the network would be helpful in getting the reader to understand 1) where the matrix/network is coming from, and 2) where the clustering is coming from once the network is formed. What do you think? Or maybe you can explain why you chose to show the axes and positions you chose -- what is your pedagogical goal and how are you working to attain it? |
rossbar
left a comment
There was a problem hiding this comment.
representing items in a network and creating avenues to using graph algorithms on feature-space information.
This example could be made much more compelling by actually applying a graph algorithm on feature-space information. The one high-level comment I have is that it's not clear why networkx is even necessary, since the data generation and analysis (clustering) are both done w/ scikit-learn. It's nice to be able to explicitly see the edges after converting to a networkx graph, and the demonstration of the layout is also nice, but in its current form this seems more like a pure scikit-learn example than a scikit-learn + networkx example.
That's not to say it isn't valuable though! I'm not an expert so it's quite possible I'm missing interesting details - in that case adding some narrative text in the form of rst comments might help.
|
I see that some choices we made can be a bit confusing and deliver the wrong message. We will try to address these with a modified example. Here you can clearly see that.
Modification considerations:
This example can be used to observe cluster quality for high-dimensional data using a 2d network plot. Clarifications for comments:
If the use case is still doubtful then this is the output if we select the number of clusters incorrectly (here 5). So now you see we can exactly interpret what the affinity matrix is and why we need to plot the graph to show high-dimensional information. |
|
I'll update the above "Demonstration Explanation" |
|
One more Iteration! |
|
One thing to keep in mind is that the example should highlight something about graph/network analysis. There is a fine line to walk between making an example compelling and making it understandable. For me anyways, making the example more complex is heading in the wrong direction :). That's not to say that it isn't valuable - in fact, as the scope of the example here continues to increase, it may be a better fit for the networkx-notebooks repo, which is a more natural fit for larger examples and examples that would benefit from some narrative/explanatory text. Also - there's no need to continue attaching images to the GH comments - all examples are run by the CI and can be previewed by clicking on the "ci/circleci: build artifact" tab in the CI check summary. This is actually better for review because it also guarantees that the image is being generated as expected (which is not currently the case). |
|
The PR appears abandoned, and the code no longer runs on the current release. The code can be fixed using slightly edited lines 24-27: |
| def spectral_clustering_to_graph(X, n_clusters: int, sc: SpectralClustering): | ||
| # get affinity matrix from spectral clustering | ||
| clusters = sc.fit(X) | ||
| cluster_affinity_matrix = clusters.affinity_matrix_ |
There was a problem hiding this comment.
cluster_affinity_matrix = clusters.affinity_matrix_.H
| cluster_affinity_matrix = clusters.affinity_matrix_ | ||
|
|
||
| pred_labels = clusters.labels_.astype(int) | ||
| G = nx.from_scipy_sparse_matrix(cluster_affinity_matrix) |
There was a problem hiding this comment.
G = nx.from_scipy_sparse_array(cluster_affinity_matrix)
|
Thanks for the ping @lobpcg - I still have the sense that this is a better fit for a scikit-learn gallery rather than networkx. Since it's been 18 months since last activity and is still in draft, I'm inclined to close. I'll do so soon if there are no objections! |
|
@rossbar Yes, I agree that this PR could be closed as stalled. I like the idea, but the proposed implementation is still far from being acceptable unfortunately. |



[Updated]
This example is meant to address Issue #4261
More specifically, using scikit-learn to cluster data using Spectral Clustering. Then using the adjacency matrix from the same to generate a graph for the data points to understand the clustering.
Follow the below thread and the issue to view the iterations of the demo.