error with random.sample: raise NotImplementedError(“ >= and <= don't work with 0.”) NotImplementedError: >= and <= don't work with 0

138
June 26, 2018, at 7:30 PM

I have a problem with this code, I really dont understand why it cannot reach the second cycle of the most internal for:

def remove_random_links(A,E):
    rs = random.sample(A.edges, E)
    A.remove_edges_from(rs)
N = 30
L = [0,60,30,40,30,30,20,40]
rep = 15
d = []
g = []
for j in range(rep):
    R = nx.grid_graph(dim=[N,N])  
    _deg = []
    _gap = [] 
    for i in L:
        remove_random_links(R,i)
        A = nx.adj_matrix(R)
        L = nx.laplacian_matrix(R) 
        eigwd, eigv = LA.eigh(L.todense())
        eigw = np.sort(eigwd)
        eigw0 = eigw[0]
        eigw1 = eigw[1]
        eigw2 = eigw[2]
        eigw3 = eigw[3]
        gap = eigw2 - eigw1
        deg = eigw3 - eigw2
        _deg.append(deg)
        _gap.append(gap)
        print("meow")    
    d.append(_deg)
    g.append(_gap)
    print("M")
mean_deg = [np.mean(d[i]) for i in range(rep)]    
mean_gap = [np.mean(g[i]) for i in range(rep)]
std_deg = [np.std(d[i]) for i in range(rep)]
std_gap = [np.std(g[i]) for i in range(rep)]

It goes on its first for cycle (the one in j) and then into its first for cycle in i, it completes it and then it is not able to do the second one because of this error with the function remove_random_links(), which gives me the following error:

File "/Users/Sofia/Desktop/delta.py", line 23, in remove_random_links(R,i)

File "/Users/Sofia/Desktop/delta.py", line 9, in remove_random_links rs = random.sample(A.edges, E)

File "/anaconda3/lib/python3.6/random.py", line 317, in sample if not 0 <= k <= n:

File "/anaconda3/lib/python3.6/site-packages/scipy/sparse/compressed.py", line 322, in ge "Comparing a sparse matrix with a scalar "

File "/anaconda3/lib/python3.6/site-packages/scipy/sparse/compressed.py", line 272, in _inequality raise NotImplementedError(" >= and <= don't work with 0.")

NotImplementedError: >= and <= don't work with 0.

Answer 1

It's because you are using the same variable L twice, once here

L = [0,60,30,40,30,30,20,40]

Then reassigning it while iterating over it here

for i in L:
        remove_random_links(R,i)
        A = nx.adj_matrix(R)
        L = nx.laplacian_matrix(R)   #<--------change variable name

Use different variable names and that should do the trick, try this for instance

import random
from numpy import linalg as LA
def remove_random_links(A,E):
    print A.edges, E
    rs = random.sample(A.edges, E)
    A.remove_edges_from(rs)
N = 30
L_edges = [0,60,30,40,30,30,20,40]    #<--- changed it to L_edges
rep = 15
d = []
g = []
for j in range(rep):
    R = nx.grid_graph(dim=[N,N])  
    _deg = []
    _gap = [] 
    for i in L_edges:     #<--- changed it to L_edges
        remove_random_links(R,i)
        A = nx.adj_matrix(R)
        L = nx.laplacian_matrix(R) 
        eigwd, eigv = LA.eigh(L.todense())
        eigw = np.sort(eigwd)
        eigw0 = eigw[0]
        eigw1 = eigw[1]
        eigw2 = eigw[2]
        eigw3 = eigw[3]
        gap = eigw2 - eigw1
        deg = eigw3 - eigw2
        _deg.append(deg)
        _gap.append(gap)
        print("meow")    
    d.append(_deg)
    g.append(_gap)
    print("M")
mean_deg = [np.mean(d[i]) for i in range(rep)]    
mean_gap = [np.mean(g[i]) for i in range(rep)]
std_deg = [np.std(d[i]) for i in range(rep)]
std_gap = [np.std(g[i]) for i in range(rep)] 
Rent Charter Buses Company
READ ALSO
An error occur trying to post detail in datatable in php

An error occur trying to post detail in datatable in php

This is server side ajax datatable script which error to get data in sqlrecord, records are emptyI am creating a datatable in php

152
ListView show element only after press button

ListView show element only after press button

I'm new to android and don't quite get it's concepts yetI have 1 activity and 2 Fragments

193
Android Studio not opening JDK works fine

Android Studio not opening JDK works fine

After installing JDK and Setting Environment Variables I installed Android StudioHowever, after install when I open it it only shows the image below:

179