I have an adjacency matrix adj and a cellarray nodeManes that contains names that will be given to the graph G that will be constructed from adj.
So I use G = digraph(adj,nodeNames); and I get the following graph :
Now, I want to find the strongly connected components in G and do a graph condensation so I use the following:
C = condensation(G);
p2 = plot(C);
and get this results :
So I have 6 strongly connected components, but my problem is that I lost the node names, I want to get something like:
Is that any way to get the nodes names in the result of the condentation?
I think the official documentation can take you to the right point:
Output Arguments
C - Condensation Graph
Condensation graph, returned as a digraph object. C is a directed
acyclic graph (DAG), and is topologically sorted. The node numbers in
C correspond to the bin numbers returned by conncomp.
Let's take a loot at conncomp:
conncomp(G) returns the connected components of graph G as bins. The
bin numbers indicate which component each node in the graph belongs to
Look at the examples... I think that if you use conncomp on your graph before using the condensation function, you will be able to rebuild your node names on your new graph with a little effort.
Related
When I call G = nx.convert_matrix.from_numpy_array(A, create_using=nx.DiGraph), where A is a 0-1 adjacency matrix, the resulting graph automatically contains edge weights of 1.0 for each edge. How can I prevent this attribute from being added?
I realize I can write
for _,_,d in G.edges(data=True):
d.clear()
but I would prefer if the attributes were not added in the first place.
There is no way to do that with native networkx functions. This is how you can do it:
G = nx.empty_graph(0, nx.DiGraph)
G.add_nodes_from(range(A.shape[0]))
G.add_edges_from(((int(e[0]), int(e[1])) for e in zip(*A.nonzero())))
This is exactly how the nx.convert_matrix.from_numpy_array function is implemented internally. I got however rid of all controls, so be careful with this. Additional details can be found here
I would like to create a map of isolines for a given country.
I fetched road map using OSMNX:
cf = '["highway"~"motorway|motorway_link|trunk|primary|secondary"]'
G = ox.graph_from_place('Venezuela', network_type='drive', custom_filter=cf)
# project the graph to UTM
G_projected = ox.project_graph(G)
Then I created a subgraph of all nodes within given radius from starting point (lat, long):
node = ox.get_nearest_node(G, (8.284904228184445, -62.72239713960212))
subgraph = nx.ego_graph(G_projected, node, radius=10000, distance="length")
Resulting subgraph contains only 1 node (no matter what radius is used)
list(subgraph.nodes())
[5098062996]
List of neighbours is empty:
list(G_projected.neighbors(5098062996))
But for graph G (before projection was conducted):
list(G.neighbors(5098062996))
[5098062995]
The CRS of the starting point is EPSG:4326, WGS 84, project_graph projects it to UTM zone in which the graph’s centroid lies. But what is the resulting CRS, I guess that in this case, it is also WGS 84 because there is no difference in terms of geometry between results for G and G_projected. Is it necessary to project G to UTM each time, and what about the disconnected nodes, is it the result of projection operation?
Following methods turned out to be not very helpful in identifying the problem:
list(nx.isolates(G_projected))
list(nx.isolates(G))
Both return empty list. Which mean that every node has neighbours (is connected?)
Given node is also in the list of connected components of a graph:
5098062996 in list(nx.connected_components(G_projected.to_undirected()))[0]
True
We have contradicted information, the node is connected but has no neighbours and resulting subgraph contains only the starting point.
I have to emphasise that for different nodes everything work fine, I encountered issues only with that particular node. For G without projection it works. I only used the projection because I saw it in notebook examples at OSMNX github page.
I should simply do not use the projection or I should use it and there is some other issue with the graph connectivity?
Any advice would be appreciated.
I cannot reproduce your exact query because getting the graph for all of Venezuela requires over 700 downloads. But I can reproduce your area of interest around that point, and everything appears to be working correctly:
import networkx as nx
import osmnx as ox
ox.config(use_cache=True, log_console=True)
pt = 8.28490, -62.72240
cf = '["highway"~"motorway|motorway_link|trunk|primary|secondary"]'
G = ox.graph_from_point(pt, dist=5000, network_type='drive', custom_filter=cf)
G_projected = ox.project_graph(G)
node = ox.get_nearest_node(G, pt)
print(list(G.neighbors(node))) #[]
print(list(G_projected.neighbors(node))) #[]
print(list(G.predecessors(node))) #[5098062995]
print(list(G_projected.predecessors(node))) #[5098062995]
x = G.nodes[node]['x']
y = G.nodes[node]['y']
bbox = ox.utils_geo.bbox_from_point((y, x), dist=200, project_utm=True)
nc = ['r' if n==node else 'w' for n in G.nodes()]
fig, ax = ox.plot_graph(G_projected, node_color=nc, node_size=50, bbox=bbox)
The node in question is this one, which given the network types in your custom filter, has no successor nodes in your graph. For a MultiDiGraph, neighbors is equivalent to successors and thus in both the unprojected and projected graphs, you get an empty list (and an empty subgraph when induced on this node). This node does however have one predecessor in your graph, which you can see printed in the code snippet using the predecessors method.
I have small query. I have two data sets. In one data sets for example I did binning and calculated the mean value and std value along with group binning. Now in I have second data sets of same parameters say X. I would like identify this X data sets belong to which bin groups of my previous data sets using matlab.
Could you give some example how to identify the incoming data points belongs to which bin group...??
I used following binning which is available in matlab :
binEdges = linspace(botEdge, topEdge, numBins+1);
[h,whichBin] = histc(x, binEdges);
Well... you already have your bin edges. Anything inside specific edges is in that bin.
If you know that the data is inside the ranges you defined then, for each new data
newdatabin=find(newdata>binedges,1,'last'); %this is the bin number where the new data goes in
h(newdatabin)=h(newdatabin)+1; %add one!
Also consider using histcounts if your MATLAB version is new enough.
I am running kmeans in Mahout and as an output I get folders clusters-x, clusters-x-final and clusteredPoints.
If I understood well, clusters-x are centroid locations in each of iterations, clusters-x-final are final centroid locations, and clusteredPoints should be the points being clustered with cluster id and weight which represents probability of belonging to cluster (depending on the distance between point and its centroid). On the other hand, clusters-x and clusters-x-final contain clusters centroids, number of elements, features values of centroid and the radius of the cluster (distance between centroid and its farthest point.
How do I examine this outputs?
I used cluster dumper successfully for clusters-x and clusters-x-final from terminal, but when I used it clusteredPoints, I got an empty file? What seems to be the problem?
And how can I get this values from code? I mean, the centroid values and points belonging to clusters?
FOr clusteredPoint I used IntWritable as key, and WeightedPropertyVectorWritable for value, in a while loop, but it passes the loop like there are no elements in clusteredPoints?
This is even more strange because the file that I get with clusterDumper is empty?
What could be the problem?
Any help would be greatly appreciated!
I believe your interpretation of the data is correct (I've only been working with Mahout for ~3 weeks, so someone more seasoned should probably weigh in on this).
As far as linking points back to the input that created them I've used NamedVector, where the name is the key for the vector. When you read one of the generated points files (clusteredPoints) you can convert each row (point vector) back into a NamedVector and retrieve the name using .getName().
Update in response to comment
When you initially read your data into Mahout, you convert it into a collection of vectors with which you then write to a file (points) for use in the clustering algorithms later. Mahout gives you several Vector types which you can use, but they also give you access to a Vector wrapper class called NamedVector which will allow you to identify each vector.
For example, you could create each NamedVector as follows:
NamedVector nVec = new NamedVector(
new SequentialAccessSparseVector(vectorDimensions),
vectorName
);
Then you write your collection of NamedVectors to file with something like:
SequenceFile.Writer writer = new SequenceFile.Writer(...);
VectorWritable writable = new VectorWritable();
// the next two lines will be in a loop, but I'm omitting it for clarity
writable.set(nVec);
writer.append(new Text(nVec.getName()), nVec);
You can now use this file as input to one of the clustering algorithms.
After having run one of the clustering algorithms with your points file, it will have generated yet another points file, but it will be in a directory named clusteredPoints.
You can then read in this points file and extract the name you associated to each vector. It'll look something like this:
IntWritable clusterId = new IntWritable();
WeightedPropertyVectorWritable vector = new WeightedPropertyVectorWritable();
while (reader.next(clusterId, vector))
{
NamedVector nVec = (NamedVector)vector.getVector();
// you now have access to the original name using nVec.getName()
}
check the parameter named "clusterClassificationThreshold".
clusterClassificationThreshold should be 0.
You can check this http://mail-archives.apache.org/mod_mbox/mahout-user/201211.mbox/%3C50B62629.5020700#windwardsolutions.com%3E
let us suppose that we have following graph of singular value distribution
which was given by following command
stem(SV)
SV_singular values,from visually of course we can find approximate values of singular values,but is there any possibility to get values from graph itself?of course someone may say that if we have SV,we can directly access,but i want just graphicl tool to get it from picture itself,for example like this
b=stem(SV);
but when i type b,i am getting following number
b
b =
174.0051
it is matlab self learning,so please help me to learn how to find values from graphics in matlab
The value stored in your variable b is a handle to the current axes. You can access the properties of this axes using get. To access the values in the plot, you can use
b=stem(SV);
values = get(b, 'ydata');