Trouble creating adjacency matrix using networkx - python

In the answer to this question there is code that creates all trees with a certain number of nodes.
The problem is that I tried to create the corresponding adjacency matrix using a built-in function in networkx nx.to_numpy_array but for some reason it's not working, the code is next:
#Function created by warped
import itertools
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
def make_all_trees(nodes):
# generate all pairwise combinations of nodes
edges = [a for a in itertools.product(range(nodes), range(nodes))]
# use sets to lose..
# ..symmetric edges: (0,1), (1,0) => keep only (0,1)
edges = list(set([tuple(set(e)) for e in edges]))
# ..and self-loops: (0,0)
edges = [e for e in edges if len(e)>1]
trees = []
# generate all graphs that have nodes-1 edges
for o in itertools.combinations(edges, nodes-1):
#make sure that all nodes are in the edgelist:
flattened = [item for sublist in o for item in sublist]
if len(set(flattened)) == nodes:
G = nx.Graph()
G.add_edges_from(o)
# make sure all nodes are connected
if len(list(nx.connected_components(G)))==1:
trees.append(G)
return trees
#This is what I added it to create the corresponding adjacency matrix
trees = make_all_trees(3) #This create all the graph trees with 3 nodes, so it creates 3 trees
adjaux = []
for i in trees:
adjaux.append(nx.to_numpy_array(i))
print(np.array(adjaux))
#Draws the graph
for p, tree in enumerate(trees):
plt.subplot(4,4, p+1)
nx.draw_networkx(tree)
plt.show()
The output is the following
#Adjacency matrix created
adjaux = [[[0. 1. 0.] [[0. 1. 1.] [[0. 1. 0.]
[1. 0. 1.] [1. 0. 0.] [1. 0. 1.]
[0. 1. 0.]] [1. 0. 0.]] [0. 1. 0.]]]
As you can see, although all the trees graph are correct and the first two adjacency matrix are correct, the last one is incorrect, the output should be:
adjaux = [[[0. 1. 0.] [[0. 1. 1.] [[0. 0. 1.]
[1. 0. 1.] [1. 0. 0.] [0. 0. 1.]
[0. 1. 0.]] [1. 0. 0.]] [1. 1. 0.]]]
I tried re-creating the code step by step, but I can't see what and why it's not working, all seems to be fine, so any help will be appreciated, thank you!

documentation of nx.to_numpy_array:
[...]
nodelist (list, optional) – The rows and columns are ordered according
to the nodes in nodelist. If nodelist is None, then the ordering is
produced by G.nodes().
[...]
Checking the order for your graphs:
trees = make_all_trees(3)
for tree in trees:
print(tree.nodes())
#output:
[0, 1, 2] # first tree
[0, 1, 2] # second tree
[1, 2, 0] # third tree, node order is changed
So, the adjacency matrix is correct in all cases (the graphs are displayed correctly, so edges must be recorded correctly), but the order is messed up.
You need to explicitly specify the order of nodes in the nodelist argument:
adjaux=[]
for tree in trees:
adjaux.append(nx.to_numpy_array(tree, nodelist=sorted(tree.nodes())))
for a in adjaux:
print('-'*10)
print(a)
----------
[[0. 1. 0.]
[1. 0. 1.]
[0. 1. 0.]]
----------
[[0. 1. 1.]
[1. 0. 0.]
[1. 0. 0.]]
----------
[[0. 0. 1.]
[0. 0. 1.]
[1. 1. 0.]]

Related

scipy.sparse.csr.csr_matrix.toarray given arrays with all 0's

I am looking to use TfidfVectorizer and then convert csr matrix to array, but the array returned only contain 0's. Need to understand what's going on.
vector = TfidfVectorizer() # convert data to Matrix
x_feature_train = vector.fit_transform(X_train) # Fit our Train Data
x_test_feature_test = vector.transform(X_test) # Fit our Test Data
arr= x_feature_train.toarray()
print(arr[0][0])
Output
0.0
<class 'scipy.sparse.csr.csr_matrix'> [[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
...
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]]
The output is a sparse matrix meaning that it is a matrix that contains a lot of zeros.
There is a chance that all the values are at the center area of this large matrix thus when the printing statement only print out the head and tail of the matrix, it seemed like they are all zeros.
Another case might be that there is something wrong with the X_train data.
Without additional information, we won't be able to tell, thus, as #desertnaut recommended, posting a minimal reproducible example would be helpful.

Listing each iteration of rolling a six-sided-die in Python

I'm working on an animated bar plot to show how the number frequencies of rolling a six-sided die converge the more you roll the die. I'd like to show the number frequencies after each iteration, and for that I have to get a list of the number frequencies for that iteration in another list. Here's the code so far:
import numpy as np
import numpy.random as rd
rd.seed(23)
n_samples = 3
freqs = np.zeros(6)
frequencies = []
for roll in range(n_samples):
x = rd.randint(0, 6)
freqs[x] += 1
print(freqs)
frequencies.append(freqs)
print()
for x in frequencies:
print(x)
Output:
[0. 0. 0. 1. 0. 0.]
[1. 0. 0. 1. 0. 0.]
[1. 1. 0. 1. 0. 0.]
[1. 1. 0. 1. 0. 0.]
[1. 1. 0. 1. 0. 0.]
[1. 1. 0. 1. 0. 0.]
Desired output:
[0. 0. 0. 1. 0. 0.]
[1. 0. 0. 1. 0. 0.]
[1. 1. 0. 1. 0. 0.]
[0. 0. 0. 1. 0. 0.]
[1. 0. 0. 1. 0. 0.]
[1. 1. 0. 1. 0. 0.]
The upper three lists indeed show the number frequencies after each iteration. However, when I try to append the list to the 'frequencies' list, in the end it just shows the final number frequencies each time as can be seen in the lower three lists. This one's got me stumped, and I am rather new to Python. How would one get each list like in the first three lists of the output, in another? Thanks in advance!
You can do it like that by changing only frequencies.append(freqs) with frequencies.append(freqs.copy()). Like that, you can make a copy of freqs that would be independent of original freqs. A change in freqs won't change freqs.copy().
import numpy as np
import numpy.random as rd
rd.seed(23)
n_samples = 3
freqs = np.zeros(6)
frequencies = []
for roll in range(n_samples):
x = rd.randint(0, 6)
freqs[x] += 1
print(freqs)
frequencies.append(freqs.copy())
print(frequencies)
print()
for x in frequencies:
print(x)
Python is keeping track of freqs as single identity, and its value gets changed even after it gets appended. There is a good explanation for this beyond my comprehension =P
However, here is quick and dirty work around:
import numpy as np
import numpy.random as rd
rd.seed(23)
n_samples = 3
freqs = np.zeros(6)
frequencies = []
for roll in range(n_samples):
x = rd.randint(0, 6)
freqs_copy = []
for item in freqs:
freqs_copy.append(item)
freqs_copy[x] += 1
print(freqs_copy)
frequencies.append(freqs_copy)
print()
for x in frequencies:
print(x)
The idea is to make a copy of "freqs" that would be independent of original "freqs". In the code above "freqs_copy" would be unique to each iteration.

how to add attributes to ndarrary adjacency matrix in networkx?

Orginally I have a hashtag co-occ network stored in dataframe like this:
0 ['#A', '#B', '#C', '#D]
1 ['#A', '#E']
2 ['#b', '#c', '#D']
3 ['#C', '#D']
Then I converted it into an adjacency matrix like this:
,#A,#B,#C,#D,#E,#F,#G,#H,#I,#J,#K
#A,0,1,1,0,1,1,1,1,0,1,0
#B,1,0,0,0,1,1,1,1,0,1,0
#C,1,0,0,0,1,1,1,1,0,1,0
...
I want to load the net into networkx in order to do the math and draw the graph. So I use the np.genfromtext method to load the data into ndarrary. I have loaded the data successfully but I don't know how to label them.
mydata = genfromtxt(src5+fname[0], delimiter=',',encoding='utf-8',comments='**')
adjacency = mydata[1:,1:]
print(adjacency)
[[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
...
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]]
By the way, can I just input the data from the original dataframe instead of using the adjacency matrix?
You can display both edge and node labels. Suppose you have adjacency matrix and hashtag list:
# matrix from question
A = np.array([[0,1,1,0,1,1,1,1,0,1,0],
[1,0,0,0,1,1,1,1,0,1,0],
[1,0,0,0,1,1,1,1,0,1,0],
[0,0,0,0,0,0,0,0,0,0,0],
[1,1,1,0,0,0,0,0,0,0,0],
[1,1,1,0,0,0,0,0,0,0,0],
[1,1,1,0,0,0,0,0,0,0,0],
[1,1,1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0],
[1,1,1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0]])
labels = ['#A','#B','#C','#D','#E','#F','#G','#H','#I','#J','#K']
Here is some visulisation example:
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
# labels to dict
labels = {k: v for k, v in enumerate(labels)}
# create graph and compute layout coords
G = nx.from_numpy_matrix(A, parallel_edges=True)
# k controls node closeness, 0 <= k <= 1
coord = nx.spring_layout(G, k=0.55, iterations=20)
# optional: set label coords a bit upper the nodes
node_label_coords = {}
for node, coords in coord.items():
node_label_coords[node] = (coords[0], coords[1] + 0.04)
# draw the network, node and edge labels
plt.figure(figsize=(20, 14))
nx.draw_networkx_nodes(G, pos=coord)
nx.draw_networkx_edges(G, pos=coord)
nx.draw_networkx_edge_labels(G, pos=coord)
nx.draw_networkx_labels(G, pos=node_label_coords, labels=labels)
You can find more info on the adjacency matrix graph creation at the NetworkX documentation
Update:
Refer to set_node_attributes function to add attributes to your network nodes
degree_centr = nx.degree_centrality(G)
nx.set_node_attributes(G, degree_centr, "degree")
nx.write_gexf(G, "test.gexf")
After saving graph to file with write_gexf, you'll have a file with attributes suitable for Gephi.

Numpy historgramm 2d vector

I have a list of x y like the picture above
in code it works like this:
np.array([[1.3,2.1],[1.5,2.2],[3.1,4.8]])
now I would like to set a grid of which I can set the start, the number of columns and rows as well as the row and columns size, and then count the number of points in each cell.
in this example [0,0] has 1 point in it, [1,0] has 1, [2,0] has 3, [0,1] has 4 and so on.
I know it would probably be trivial to do by hand, even without numpy, but I need to create it as fast as possible, since I will have to process a ton of data this way.
whats a good way to do this? Basicly create a 2D Histogramm of points? And more importantly, how can I do it as fast as possible?
I think numpy.histogram2d is the best option.
a = np.array([[1.3,2.1],[1.5,2.2],[3.1,4.8]])
H, _, _ = np.histogram2d(a[:, 0], a[:, 1], bins=(range(6), range(6)))
print(H)
# [[0. 0. 0. 0. 0.]
# [0. 0. 2. 0. 0.]
# [0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 1.]
# [0. 0. 0. 0. 0.]]

How to plot a PCM wave using the binary inputs from a .txt file

In my case I will have a PCM.txt file which contains the binary representation of a PCM data like below.
[1. 1. 0. 1. 0. 1. 1. 1. 1. 1. 0. 1. 1. 1. 1. 1. 1. 1. 0. 1. 1. 1. 0.
1.
0. 1. 0. 1. 0. 0. 1. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 1.
0. 1. 0. 1. 0. 1. 0. 1. 1. 1. 1. 1. 0. 1. 1. 1. 1. 1. 1. 1. 0. 1. 1. 1.
0. 1. 0. 1. 0. 1. 0. 0. 1. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 1. 0.
0. 1. 0. 1.]
1's meaning binary 1
0's meaning binary 0
This is nothing but 100 samples of data.
Is it possible to implement a python code which will read this PCM.txt as the input and plot this PCM data using matplotlib. ? Could you please give me some tips to implement this scenario ?
Will this plotted figure look like a square wave ?
I think you want this:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(100)
y = [1.,1.,0.,1.,0.,1.,1.,1.,1.,1.,0.,1.,1.,1.,1.,1.,1.,1.,0.,1.,1.,1.,0.,1.,0.,1.,0.,1.,0.,0.,1.,0.,0.,0.,0.,0.,1.,0.,0.,0.,0.,0,0.,0.,1.,0.,0.,1.,0.,1.,0.,1.,0.,1.,0.,1.,1.,1.,1.,1.,0.,1.,1.,1.,1.,1.,1.,1.,0.,1.,1.,1.,0.,1.,0.,1.,0.,1.,0.,0.,1.,0.,0.,0.,0.,0.,1.,0.,0.,0.,0.,0.,0.,0.,1.,0.,0.,1.,0.,1.]
plt.step(x, y)
plt.show()
If you are having trouble reading the file, you can just use a regex to find things that look like numbers:
import matplotlib.pyplot as plt
import numpy as np
import re
# Slurp entire file
with open('data') as f:
s = f.read()
# Set y to anything that looks like a number
y = re.findall(r'[0-9.]+', s)
# Set x according to number of samples found
x = np.arange(len(y))
# Plot that thing
plt.step(x, y)
plt.show()

Categories

Resources