perform derivative inside a loop - python

Hi I wrote the following code,
Z=1 #particle position
dz=0.1
time=1
dt=0.1
v=0
Fz=0
for time in np.arange(1, 10, dt):
#####simulation loop#######
theta=np.arccos(-Z/R) #contact angle
theta_e=((math.pi*110)/180) #equilibrium contact angle
Z_e=-R*np.cos(theta_e)#equilibrium position of particle
C=3.14*gamma*(R-Z_e) #additive constant
Fsz= (gamma*math.pi*(Z-Z_e)**2)+(tau*2*math.pi*math.sqrt(R**2-Z**2))+C
Fz=Fsz+(0.5*deltaF*np.sin((2*math.pi/lamda)*(Z-Z_e)-phi))#surface force
w_a=gamma*lamda_m**2*(1-np.cos(theta_e)) #work of adhesion
epsilon_z=2*math.pi*R*np.sin(theta)*mu*(nu/(lamda_m**3))*np.exp(w_a/KbT)#transitional drag
epsilon_s=khi*mu*((4*math.pi**2*R**2)/math.sqrt(Ad))*(1-(Z/R)**2)
epsilon=epsilon_z+epsilon_s
Ft=math.sqrt(2*KbT*epsilon)*series #thermal force
v=(-np.diff(Fz,Z)+Ft)/epsilon ##new velocity
Z=v*dt #new position
I was trying to calculate dFz/dzbut it is gave me following error,
File "C:/Users/mohammad.hossain1/Desktop/particle.py", line 62, in <module>
v=(-np.diff(Fz,Z)+Ft)/epsilon ##new velocity
File "C:\Users\mohammad.hossain1\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\lib\function_base.py", line 1924, in diff
slice1[axis] = slice(1, None)
IndexError: list assignment index out of range
As my initial condition is Fz=0 Z=0and it changes with time I suppose to get dFz/dz. I have imported all necessary module and all variables are defined properly at the beginning of the code.But I got error while I have introduced the derivative. So most likely my approach is not going with argument. Is it possible to show me the mistake that I have done during my coding.

It's hard too say for sure even with the stack trace. Obviously numpy is trying to access a list called slice1 with an axis index that the list does not have. Not sure why this is the case, but it must be something with this line:
v=(-np.diff(Fz,Z)+Ft)/epsilon
I suspect specifically the part of this line that is causing this issue is the np.diff(), since it is np code throwing this error. My best guess is that Fz and Z are equal in this case, or otherwise unacceptable values for the .diff method. Try adding the following if like so:
if Fz != Z:
v=(-np.diff(Fz,Z)+Ft)/epsilon
If that doesn't stop the crash try printing the values of Fz and Z right before this line and seeing if they look weird/suspicious.

Related

KeyError in a dictionary when in the previous iteration of a while loop the key:value pair should have been created [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am using a while loop to integrate various quantities from the surface of a star inwards using appropriate boundary conditions and stellar structure equations.
I am using dictionaries to represent physical variables such as pressure and density, where the plan is for the radii to be keys, and the value to be the pressure or density.
I have a key:value pair for the surface, and then I step inwards iteratively using a while loop updating the dictionaries as below:
import constants
import math
import matplotlib.pyplot as plt
mass=3*constants.solar_mass
radius=1.5*constants.solar_radius
#Variables to guess
core_temperature=1.4109*10**7
core_pressure= 2.6851*10**14
luminosity=pow(3,3.5)*constants.solar_luminosity
#Functions we are searching for
temperature={}
#guess
temperature[0]=core_temperature
#From the steffan boltzmann law
temperature[radius]=pow(luminosity/(4*math.pi*pow(radius,2)*constants.stefan_boltzmann_constant),0.25)
pressure={}
#guess
pressure[0]=core_pressure
#Pressure surface boundary condition
pressure[radius]=(2*constants.gravitation_constant*mass)/(3*constants.opacity*pow(radius,2))
mass_enclosed={}
#boundary conditions
mass_enclosed[0]=0
mass_enclosed[radius]=mass
density={}
#density surface boundary condition
density[radius]=(constants.mean_molecular_weight*pressure[radius])/(constants.gas_constant*temperature[radius])
delta_radius=int(radius/100)
#Polytropic constant
K=(pressure[radius]*constants.mean_molecular_weight)/(constants.gas_constant*pow(density[radius],constants.adiabatic_constant))
def integrate_from_surface():
i=0
while radius-i*delta_radius>(0.5*radius):
#temporary radius just for each loop through
r=radius-i*delta_radius
#updating pressure
pressure[r-delta_radius]=pressure[r]+(density[r]*constants.gravitation_constant*mass_enclosed[r]*delta_radius)/pow(r,2)
#updating density
density[r-delta_radius]=pow((pressure[r-delta_radius]*constants.mean_molecular_weight)/(constants.gas_constant*K),1.0/constants.adiabatic_constant)
#updating mass enclosed
mass_enclosed[r-delta_radius]=mass_enclosed[r]-4*math.pi*pow(r,2)*delta_radius*density[r]
i=i+1
integrate_from_surface()
While Loop: Radius and dictionaries are defined above
I am getting a KeyError, as shown below:
Traceback (most recent call last):
File "main.py", line 63, in <module>
integrate_from_surface()
File "main.py", line 51, in integrate_from_surface
pressure[r-delta_radius]=pressure[r]+(density[r]*constants.gravitation_constant*mass_enclosed[r]*delta_radius)/pow(r,2)
KeyError: 1043966868.09
KeyError message
If I print out the variable r in the while group, the process works perfectly until r is 1043966868.09. I do not understand, surely on the previous iteration I made this a key, so there should be no KeyError.
Constants file below:
solar_mass=1.9891*10**30
solar_radius=6.9598*10**8
solar_luminosity=3.8515*10**26
gas_constant=8.3145*10**3
gravitation_constant=6.6726*10**-11
radiation_constant=7.5646*10**-16
speed_of_light=2.9979*10**8
stefan_boltzmann_constant= radiation_constant*speed_of_light * 0.25
opacity=0.034
adiabatic_constant = 5.0/3
mean_molecular_weight = 8.0/13
Thanks in advance for any help.
As I stated in the comments, this behaviour is probably linked to the difficulty of hashing floats. In the end, using floats as dictionary keys is only as precise as your float precision. There are already extensive articles about the consequences and mechanisms at work here, for instance this one.
As an example (credits to the aforementioned article), you can see that hash(10-9.8) == hash(.2) returns False : using such keys in a dictionnary would create two entries.
A workaround (if you want to stick to float keys) would be to first evaluate all possible keys, then reuse those as many time as needed.
There is another good reason for this approch, as you will have to rewrite your "while loop" and replace it with a "for loop" : while loops are slower than for loops (more hints in here).
In your case, you could evaluate your steps this way :
radiuses = [radius-i*delta_radius for i in range(1,50)]
Afterwards, switching the loop iteration is very straightforward (for r in radiuses:). And more to the point, your code won't raise any exception.
Note that you could also store all your datas in a pandas.DataFrame, combining the way of accessing you steps by order (using .iloc) or by radius (using .loc).
That could be used this way (note that I'm storing all intermediate results in lists rather than dictionaries, this is in relation with the pandas.DataFrame constructor ) :
import math
import pandas as pd
SOLAR_MASS = 1.9891*10**30
SOLAR_RADIUS = 6.9598*10**8
SOLAR_LUMINOSITY = 3.8515*10**26
RADIATION_CONSTANT = 7.5646*10**-16
SPEED_OF_LIGHT = 2.9979*10**8
STEFAN_BOLTZMANN = RADIATION_CONSTANT * SPEED_OF_LIGHT * 0.25
OPACITY = 0.034
GRAVITATION_CONSTANT = 6.6726*10**-11
MEAN_MOLECULAR_WEIGHT = 8.0/13
GAS_CONSTANT = 8.3145*10**3
ADIABATIC_CONSTANT = 5.0/3
mass=3*SOLAR_MASS
radius=1.5*SOLAR_RADIUS
luminosity=(3**3.5)*SOLAR_LUMINOSITY
TEMPERATURE_R = (luminosity/(4*math.pi*(radius**2)*STEFAN_BOLTZMANN))**0.25
PRESSURE_R = (2*GRAVITATION_CONSTANT*mass)/(3*OPACITY*(radius**2))
MASS_R = mass
DENSITY_R = (MEAN_MOLECULAR_WEIGHT*PRESSURE_R)/(GAS_CONSTANT*TEMPERATURE_R)
K=(PRESSURE_R*MEAN_MOLECULAR_WEIGHT)/(GAS_CONSTANT*DENSITY_R**ADIABATIC_CONSTANT)
def integrate_from_surface_df():
delta_radius=int(radius/100)
pressure=[PRESSURE_R]
mass_enclosed=[MASS_R]
density=[DENSITY_R]
radiuses = [radius-i*delta_radius for i in range(1,50)]
for r in radiuses:
pressure.append(pressure[-1]+(density[-1]*GRAVITATION_CONSTANT*mass_enclosed[-1]*delta_radius)/r**2)
density.append(((pressure[-2]*MEAN_MOLECULAR_WEIGHT)/(GAS_CONSTANT*K))**(1.0/ADIABATIC_CONSTANT))
mass_enclosed.append(mass_enclosed[-1]-4*math.pi*r**2*delta_radius*density[-2])
df = pd.DataFrame({"pressure":pressure, "density":density, "mass_enclosed":mass_enclosed}, index=[radius]+radiuses)
return df
print(integrate_from_surface_df())
Which would return :
pressure density mass_enclosed
1.043970e+09 7.163524e+03 0.000043 5.967300e+30
1.033530e+09 1.743478e+05 0.000043 5.967300e+30
1.023091e+09 3.449615e+05 0.000292 5.967300e+30
1.012651e+09 1.527176e+06 0.000439 5.967300e+30
1.002211e+09 3.344824e+06 0.001072 5.967300e+30
9.917715e+08 7.876679e+06 0.001716 5.967300e+30
9.813318e+08 1.528565e+07 0.002870 5.967300e+30
9.708921e+08 2.793971e+07 0.004271 5.967299e+30
9.604524e+08 4.718766e+07 0.006134 5.967299e+30
9.500127e+08 7.543907e+07 0.008400 5.967298e+30
9.395730e+08 1.149941e+08 0.011132 5.967297e+30
9.291333e+08 1.685944e+08 0.014335 5.967296e+30
9.186936e+08 2.391984e+08 0.018035 5.967294e+30
9.082539e+08 3.300760e+08 0.022246 5.967292e+30
8.978142e+08 4.447982e+08 0.026988 5.967290e+30
8.873745e+08 5.872670e+08 0.032278 5.967287e+30
8.769348e+08 7.617399e+08 0.038133 5.967284e+30
8.664951e+08 9.728621e+08 0.044575 5.967280e+30
8.560554e+08 1.225701e+09 0.051622 5.967276e+30
8.456157e+08 1.525790e+09 0.059297 5.967271e+30
8.351760e+08 1.879167e+09 0.067624 5.967265e+30
8.247363e+08 2.292433e+09 0.076627 5.967259e+30
8.142966e+08 2.772804e+09 0.086334 5.967253e+30
8.038569e+08 3.328175e+09 0.096773 5.967245e+30
7.934172e+08 3.967188e+09 0.107976 5.967237e+30
7.829775e+08 4.699315e+09 0.119976 5.967229e+30
7.725378e+08 5.534939e+09 0.132808 5.967219e+30
7.620981e+08 6.485454e+09 0.146512 5.967209e+30
7.516584e+08 7.563373e+09 0.161127 5.967198e+30
7.412187e+08 8.782447e+09 0.176699 5.967187e+30
7.307790e+08 1.015780e+10 0.193274 5.967174e+30
7.203393e+08 1.170609e+10 0.210904 5.967161e+30
7.098996e+08 1.344566e+10 0.229642 5.967147e+30
6.994599e+08 1.539674e+10 0.249548 5.967133e+30
6.890202e+08 1.758168e+10 0.270684 5.967117e+30
6.785805e+08 2.002515e+10 0.293117 5.967101e+30
6.681408e+08 2.275445e+10 0.316921 5.967083e+30
6.577011e+08 2.579981e+10 0.342172 5.967066e+30
6.472614e+08 2.919473e+10 0.368956 5.967047e+30
6.368217e+08 3.297638e+10 0.397363 5.967027e+30
6.263820e+08 3.718607e+10 0.427491 5.967007e+30
6.159423e+08 4.186974e+10 0.459445 5.966985e+30
6.055026e+08 4.707856e+10 0.493339 5.966963e+30
5.950629e+08 5.286959e+10 0.529296 5.966940e+30
5.846232e+08 5.930656e+10 0.567451 5.966917e+30
5.741835e+08 6.646075e+10 0.607948 5.966892e+30
5.637438e+08 7.441197e+10 0.650945 5.966867e+30
5.533041e+08 8.324981e+10 0.696611 5.966841e+30
5.428644e+08 9.307487e+10 0.745135 5.966814e+30
5.324247e+08 1.040004e+11 0.796717 5.966786e+30
There is also an other workaround, which you already guessed by yourself : that would be to use integers as keys in your dictionary. I don't think this to be awkward in any way, but I'd rather have keys with a real, direct meaning (that is, your radius) rather than the iteration step... But this is really up to you.

SymPy: parse string as function on manifold

I'm trying to define an equation on a manifold using SymPy, and SymPy's diffgeom package. Since this equation is input by a user, it's sent into the program as a string and is therefore defined prior to the manifold definition in the code. To perform meaningful calculations, I'm trying to replace the "sympified" symbols with symbols defined on the manifold.
Here's a function, supplied by the user.
H = sympify('m*a - f')
Coordinates for the manifold are also supplied by the user as strings.
# Variables defined as symbols (non-diffgeom)
a = Symbol('a')
f = Symbol('f')
And everything afterwards is automated.
from sympy.diffgeom import Manifold, Patch, CoordSystem
from sympy import sympify, Symbol
# Standard manifold definitions
M = Manifold(name='Temp', dim=2)
P = Patch('P', M)
R = CoordSystem('R', P, ['a','f'])
coords = R.coord_functions()
Dx = R.base_vectors()
print(H.diff(a)) # Returns 'm' as expected
print(Differential(H)(Dx[0])) # Returns '0' as expected
The first substitution works great. I can take derivatives as expected using Differential().
H = H.subs(a,coords[0])
print(H.diff(a)) # Returns '0' as expected
print(Differential(H)(Dx[0])) # Returns 'm' as expected
print(Differential(H)(Dx[1])) # Returns '0' as expected
The second substitution is where things get strange. The ().diff() command works fine and returns 0, since the new coordinates are defined on the manifold and not as standard symbols, but I can no longer take derivatives using Differential().
H = H.subs(f,coords[1])
print(H.diff(f)) # Returns '0' as expected
print(Differential(H)(Dx[0])) # Crashes code
print(Differential(H)(Dx[1])) # Also crashes code
It appears as if diffgeom is trying to perform a transformation to calculate the derivatives, but there shouldn't really be any transforming going on since this is all in the same coordinate system. Am I fundamentally missing something here? Or is there an easier method to parse strings as expressions on manifolds?
Here's the full error thrown. I really can't make much out of it other than SymPy is attempting to transform coordinates when I wouldn't expect it.
Traceback (most recent call last):
File "/Users/msparapa/Documents/Python/gprops/examples/test.py", line 37, in <module>
print(Differential(H)(Dx[0])) # Crashes code
File "/Users/msparapa/anaconda/lib/python3.5/site-packages/sympy/diffgeom/diffgeom.py", line 765, in __call__
return vector_fields[0].rcall(self._form_field)
File "/Users/msparapa/anaconda/lib/python3.5/site-packages/sympy/core/basic.py", line 538, in rcall
return Basic._recursive_call(self, args)
File "/Users/msparapa/anaconda/lib/python3.5/site-packages/sympy/core/basic.py", line 552, in _recursive_call
return expr_to_call(*on_args)
File "/Users/msparapa/anaconda/lib/python3.5/site-packages/sympy/diffgeom/diffgeom.py", line 592, in __call__
jac = self._coord_sys.jacobian(b._coord_sys, coords)
File "/Users/msparapa/anaconda/lib/python3.5/site-packages/sympy/diffgeom/diffgeom.py", line 277, in jacobian
to_sys, self._dummies).jacobian(self._dummies)
File "/Users/msparapa/anaconda/lib/python3.5/site-packages/sympy/diffgeom/diffgeom.py", line 270, in coord_tuple_transform_to
transf = self.transforms[to_sys]
KeyError: CoordSystem(R, Patch(P, Manifold(Temp, 2)), (a, f))
After further investigation, the cause was having Symbols of the same name. The fix is to essentially substitute using methods described here, or to use different naming conventions altogether.
To perform the substitution, I needed to swap all of the variables at once. This was done with the following block of code.
set = dict(zip([a,f],coords))
H = H.subs(set, simultaneous=True)
Where "a" and "f" are basic symbols, and "coords" is the list of symbols on the manifold. Substitutions not performed simultaneously, but rather sequentially, such as
set = dict(zip([a,f],coords))
H = H.subs(set)
throw the same error. I believe this is due to the fact that both "a" and "f" are buried within each manifold coordinate. To see where this exactly comes up, we can look at the Repr output of coords[0].
BaseScalarField(CoordSystem(Symbol('R'), Patch(Symbol('P'), Manifold(Symbol('M'), Integer(2))), Tuple(Symbol('f'), Symbol('a'))), Integer(0))
Both Symbol('f') and Symbol('a') show up under the coordinate system. What happens here is that when I substituted the second variable, f, in my expression, SymPy was looking at my manifold variable, a, and seeing a variable of the same name. Thus it attempted to not only substitute the basic symbol, f, but also the symbol f buried within my CoordSystem definition, probably then prompting a coordinate system transformation.

List index out of range in 2D list

I understand that there are similar questions to this here, here, and here. The first one addresses 1D lists, the second is great except it doesn't seem to work, and the third is close, but I still don't quite understand my issue.
Here is what I am trying to do. I need to create an 2D list (a 2D array in java and C++, which I am much more familiar with) filled with 0's. It needs to be size 20 across and 15 down.
Here is what I have tried:
self.grid = [[0 for x in range(GRID_COLUMN_SIZE)] for y in range(GRID_ROW_SIZE)] # where GRID_ROW_SIZE = 15, GRID_COLUMN_SIZE = 20
Note, I tried with the two constants switched (COLUMN first, then ROW), and it broke slightly later. In addition, I print the 2D list out and it's the wrong dimensions (15 across and 20 down).
Here is my later use of self.grid. Without getting too deep, I am iterating through all the values of the list (grid) and getting the surrounding points.
def populatePaths(self):
for row in range(len(self.grid)):
for column in range(len(self.grid[row])):
if self.isPointAccessible(column, row):
self.addPaths(column, row)
def addPaths(self, x, y):
key = Point(x, y)
print "Each: %s" % (key.toString())
points = key.getSurroundingPoints()
self.removeBarriersFromPath(points)
self.paths[key] = points # a map from Points to lists of surrounding Points
Basically, I remove points along the path that can't be reached:
def removeBarriersFromPath(self, path):
for point in list(path):
print "Surrounding %s" % (point.toString())
if not self.isPointAccessible(point.x, point.y):
path.remove(point)
return path
self.isPointAccessible() is trivial, but this is where it breaks. It checks to see if the value at the (x,y) location is 0: return self.grid[x][y] == 0
I added these print statements (point.toString() returns (x,y)) to show me the points as they happen, and I am able to iterate until x==14, but it breaks at x==15.
I suspect that I am getting the column/row order in the looping incorrect, but I'm not sure when/how.
Let me know if I didn't explain something clearly enough.
Edit Here is the traceback:
Traceback (most recent call last):
File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/sphero_intrude_gui.py", line 70, in start
self.populatePaths()
File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/sphero_intrude_gui.py", line 156, in populatePaths
self.addPaths(column, row)
File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/sphero_intrude_gui.py", line 162, in addPaths
self.removeBarriersFromPath(points)
File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/sphero_intrude_gui.py", line 168, in removeBarriersFromPath
if not self.isPointAccessible(point.x, point.y):
File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/sphero_intrude_gui.py", line 173, in isPointAccessible
return self.grid[x][y] == 0
IndexError: list index out of range
You did not post the whole source for isPointAccessible but from the error message it looks like your return line must be:
return self.grid[y][x] == 0
since y denotes the row number and x is the column.

Python plots - I fixed this code, but need someone to explain how it worked in the first place

I have the following python code:
def graph(seconds,now, dayold, threedayold,weekold):
x = np.arange(len(seconds))
ynow = np.array(now)
yday = np.array(dayold)
y3day = np.array(threedayold)
yweek = np.array(weekold)
# y2 should go on top, so shift them up
plt.plot(x,ynow)
plt.plot(x,yday)
plt.plot(x,y3day,'purple')
plt.plot(x,yweek)
plt.fill_between(x,ynow,yday,color='lightblue')
plt.fill_between(x,yday,y3day,color='green')
plt.fill_between(x,y3day,yweek,color='purple')
plt.fill_between(x,yweek,0,color='red')
plt.show()
which produces this graph:
However, 'seconds' is a list of non-continues results and what I really want is:
def graph(seconds,now, dayold, threedayold,weekold):
x = np.array(seconds)
ynow = np.array(now)
yday = np.array(dayold)
y3day = np.array(threedayold)
yweek = np.array(weekold)
# y2 should go on top, so shift them up
plt.plot(x,ynow)
plt.plot(x,yday)
plt.plot(x,y3day,'purple')
plt.plot(x,yweek)
plt.fill_between(x,ynow,yday,color='lightblue')
plt.fill_between(x,yday,y3day,color='green')
plt.fill_between(x,y3day,yweek,color='purple')
plt.fill_between(x,yweek,0,color='red')
plt.show()
So that I could get a proper X-Y plot. However when I try the second bit of code I get this error:
josephs-mbp-3$Traceback (most recent call last):
File "./temp.py", line 47, in <module>
graph(a[0],a[1],a[2],a[3],a[4])
File "./temp.py", line 41, in graph
plt.fill_between(x,yweek,0,color='red')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/pyplot.py", line 2287, in fill_between
ret = ax.fill_between(x, y1, y2, where, interpolate, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 6489, in fill_between
y2 = np.ones_like(x)*y2
NotImplementedError: Not implemented for this type
Can anyone tell me what is causing this and how I fix it?
EDIT: Ah-ha! I fixed it, but would like someone to tell me how...
Changing
plt.fill_between(x,yweek,0,color='red')
to
plt.fill_between(x,yweek,[0]* len(seconds),color='red')
produced what I wanted - and I can understand why this would be a problem, but I'm confused as to why the first version worked at all... any ideas?
I think that seconds is probably a list of number strings, perhaps loaded from a file, rather than a list of numeric types.
The fill_between function of matplotlib expects an array of numeric type as an argument or else it crashes. This is a bit confusing since the plot function seems to do a cast from string to float when you call it.
So I think doing something like:
x = [float(i) for i in seconds]
rather than:
x = np.array(seconds)
should solve your problem!

Sampling from degree distribution of graph

I have a simple, stupid Python problem. Given a graph, I'm trying to sample from a random variable whose distribution is the same as that of the degree distribution of the graph.
This seems like it should pretty straightforward. Yet somehow I am still managing to mess this up. My code looks like this:
import numpy as np
import scipy as sp
import graph_tool.all as gt
G = gt.random_graph(500, deg_sampler=lambda: np.random.poisson(1), directed=False)
deg = gt.vertex_hist(G,"total",float_count=False)
# Extract counts and values
count = list(deg[0])
value = list(deg[1])
# Generate vector of probabilities for each node
p = [float(x)/sum(count) for x in count]
# Load into a random variable for sampling
x = sp.stats.rv_discrete(values=(value,p))
print x.rvs(1)
However, upon running this it returns an error:
Traceback (most recent call last):
File "temp.py", line 16, in <module>
x = sp.stats.rv_discrete(values=(value,p))
File "/usr/lib/python2.7/dist-packages/scipy/stats/distributions.py", line 5637, in __init__
self.pk = take(ravel(self.pk),indx, 0)
File "/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 103, in take
return take(indices, axis, out, mode)
IndexError: index out of range for array
I'm not sure why this is. If in the code above I write instead:
x = sp.stats.rv_discrete(values=(range(len(count)),p))
Then the code runs fine, but it gives a weird result--clearly the way I've specified this distribution, a value of "0" ought to be most common. But this code gives "1" with high probability and never returns a "0," so something is getting shifted over somehow.
Can anyone clarify what is going on here? Any help would be greatly appreciated!
I believe the first argument for x.rvs() would be the loc arg. If you make loc=1 by calling x.rvs(1), you're adding 1 to all values.
Instead, you want
x.rvs(size=1)
As an aside, I'd recommend that you replace this:
# Extract counts and values
count = list(deg[0])
value = list(deg[1])
# Generate vector of probabilities for each node
p = [float(x)/sum(count) for x in count]
With:
count, value = deg # automatically unpacks along first axis
p = count.astype(float) / count.sum() # count is an array, so you can divide all elements at once

Categories

Resources