I am trying to implement a random walk in python. This is the error I get. I feel my implementation is wrong or at least not the best. Can someone have a look at it. Keep in mind I am a beginner in python and this is how I think someone would code something, so I can be totally off.
in randomWalk(stepSize, stepNumber)
37 for _ in range(stepNumber):
38 r = randint(1,4)
---> 39 x,y = movement[r]
40 xList.append(x)
41 yList.append(y)
TypeError: 'function' object is not iterable
This is my code
from pylab import *
import numpy as np
import matplotlib.pyplot as plt
import random as rnd
matplotlib.rcParams.update({'font.size': 20})
x = 0.
y = 0.
xList = []
yList = []
def goRight(stepSize, y):
direction = np.cos(0)
x = stepSize*direction
return [x,y]
def goUp(stepSize, x):
direction = np.cos(90)
y = stepSize*direction
return [x,y]
def goLeft(stepSize, y):
direction = np.cos(180)
x = stepSize*direction
return [x,y]
def goDown(stepSize, x):
direction = np.cos(270)
y = stepSize*direction
return [x,y]
def randomWalk(stepSize, stepNumber):
movement = {1: goRight,
2: goUp,
3: goLeft,
4: goDown}
for _ in range(stepNumber):
r = randint(1,4)
x,y = movement[r]
xList.append(x)
yList.append(y)
plt.ioff()
plot(x, y)
plt.show()
randomWalk(1.,4)
You are putting functions in your dict movement. movement[r] is not calling the function, only accessing them. What you line is basically doing, is:
x, y = goDown
If you want to call the function in that line, you have to add parentheses and arguments, something like:
x, y = movement[r](stepSize, x)
Which shows that you have a problem in your design, since some functions expect x and some expect y. You could maybe fix that by having all the functions take both coordinates, x and y, and then your line should go like
x, y = movement[r](stepSize, x, y)
The problem is with the line
x,y = movement[r]
The dictionary movement is a list of ints to functions when you call movement[r] only a single function is returned, but here you are trying to unpack it. Instead I think you want:
x,y = movement[r](stepSize)
This will call your function and so return the coords you want.
Also you need to change all your step methods to only take the one parameter...
You can not call a function in a dictionary of fucntions like this , use this instead :
functionToCall = movement[r] # look up for function in dictionary
x,y = functionToCall(stepsize , x, y) # then call function with desired parameters
Related
I'm trying to do a simple animation of a surface with Mayavi, but due to the lack of online examples (the few are extremely unclear or not useful) and official documentation, I'm struggling a lot. I wrote a very simple code to show where I've gone so far
import numpy as np
from mayavi import mlab
import time
#Meshgrid + u + Surface
x = np.arange(0,1,0.1)
y = np.arange(0,1,0.1)
X,Y = np.meshgrid(x,y)
u = np.ones((10,10))
surf = mlab.mesh(X,Y,u)
#Surface animation
#mlab.animate(delay=1000)
def anim():
for n in range(1,10):
global u
print(n)
u = u+1
surf.mlab_source.scalars = u
yield
anim()
mlab.show()
So very simple, every iteration I pass from a 10x10 matrix of ones to a 10x10 matrix of twos and so on.
The problems I've encountered are 3
I really can't understand what mlab_source.scalars does: fundamentally I don't know how to tell it that the z axis is changing and in doing so it follows the variation of u
Mayavi opens up, but the grid is all plane and black, like there is some issues with u
I don't understand what yield does: in the actual code I'm writing it stops the computation in a lot of advance (like in 300 steps it stops it at the step 29), but I can't remove it
I really can't get my head around, it's weeks I'm trying, hope somebody helps. Thanks in advance.
Edit:
I tried to extend #E.Klahn code in a case resembling more mine
import numpy as np
from mayavi import mlab
s = 0.01
x = np.arange(0,1,0.1)
y = np.arange(0,1,0.1)
X,Y = np.meshgrid(x,y)
Z = np.ones((10,10))
m = mlab.mesh(X, Y, Z)
#mlab.animate(delay=20)
def anim():
for i in range(1,100):
m.mlab_source.z = np.ones((10,10))*s*i
yield
anim()
mlab.show()
But I obtain only a flat surface going up, not a tridimensional object evolving, such as in his code.
Edit2:
Here the working code just copying the example of #E.Klahn, which I thank very very much
import numpy as np
from mayavi import mlab
x = np.arange(0,1,0.1)
y = np.arange(0,1,0.1)
X,Y = np.meshgrid(x,y)
X,Y=X.T,Y.T #seems an important command
u = np.ones((10,10))
surf = mlab.surf(X,Y,u)
#mlab.animate(delay=500)
def anim():
for n in range(1,10):
print(n)
surf.mlab_source.scalars = 1+np.sin(X)*np.sin(Y)*np.sin(n)
yield
anim()
mlab.show()
I just print it to give continuity to code above and to spot the differences.
Here I've shown the code to plot a cube and animate it so that it grows out from a height of 0.01 to a height of 1 using mlab.mesh. What yield does is that it returns control to the decorator so that the scene can be updated.
import numpy as np
from mayavi import mlab
s = 0.01
X = np.array([[0,0,1,1],[0,0,1,1],[0,0,0,0],[1,1,1,1]])
Y = np.array([[0,1,1,0],[0,1,1,0],[1,0,0,1],[1,0,0,1]])
Z = np.array([[0,0,0,0],[s,s,s,s],[0,0,s,s],[0,0,s,s]])
m = mlab.mesh(X, Y, Z)
#mlab.animate(delay=20)
def anim():
for i in range(1,101):
print(i, end='\r')
m.mlab_source.z = np.array([[0,0,0,0],[s*i,s*i,s*i,s*i],[0,0,s*i,s*i],[0,0,s*i,s*i]])
yield
anim()
mlab.show()
scalars is one way to access the underlying data structure if that data structure has the scalars attribute. However, for mesh that attribute is not used, rather you want to access z and set those values. I would not know what your specific issue with u if unless there is a traceback to work from. The same with yield - I see no reason why that keyword would stop the animation at any particular step.
The code below shows, instead of a box evolving over time, a surface evolving with time.
import numpy as np
from mayavi import mlab
def evolving_function(X, Y, t, Lx=1, Ly=1):
return np.sin(X+Lx*t)*np.sin(Y+Ly*t)
Lx, Ly = 1,2
x = np.linspace(-10,10,100)
y = np.linspace(-10,10,100)
X,Y = np.meshgrid(x,y)
X, Y = X.T, Y.T
s = evolving_function(X, Y, 0, Lx=Lx, Ly=Ly)
m = mlab.surf(X, Y, s)
dt = 50
t = 10
steps = int(10*1000/dt)
#mlab.animate(delay=dt)
def anim():
for i in range(1,steps):
m.mlab_source.scalars = evolving_function(X, Y, dt*i, Lx=Lx, Ly=Ly)
yield
anim()
mlab.show()
I'm currently trying to plot a graph of iterations of a certain function in python. I have defined the function as stated below but I am unsure on how to plot the graph such that the y value is on the y axis and the iteration number is on the x axis.
So, I have tried using the plt.plot function with different values in as my x values but using logistic(4, 0.7) as the y value for the y axis.
def logistic(A, x):
y = A * x * (1 - x)
return y
But each return an error. Can anyone shed any light on this, I want to do a total of 1000 iterations.
I dont understand much what you are saying concerning x being number ofiteration while you are showing us function logistic(4, 0.7). As far as I know, iterations is integer, whole number. You cant iterate just halfly or partially
def logistic(A, x):
y = A * x * (1 - x)
return y
A = 1
x_vals = []
y_vals = []
for x in range(1,1000):
x_vals.append(x)
y_vals.append(logistic(A,x))
#plt.plot(x_vals,y_vals) # See every iteration
#plt.show()
plt.plot(x_vals,y_vals) # See all iterations at once
plt.show()
Ah, the logistic map. Are you trying to make a cobweb plot? If so, your error may be elsewhere. As others have mentioned, you should post the error message and your code, so we can better help you. However, based on what you've given us, you can use numpy.arrays to achieve your desired result.
import numpy as np
import matplotlib.pyplot as plt
start = 0
end = 1
num = 1000
# Create array of 'num' evenly spaced values between 'start' and 'end'
x = np.linspace(start, end, num)
# Initialize y array
y = np.zeros(len(x))
# Logistic function
def logistic(A, x):
y = A * x * (1 - x)
return y
# Add values to y array
for i in range(len(x)):
y[i] = logistic(4, x[i])
plt.plot(x,y)
plt.show()
However, with numpy.arrays, you can omit the for loop and just do
x = np.linspace(start, end, num)
y = logistic(4, x)
and you'll get the same result, but faster.
Could anyone please take a look at the following function code in Python?
import numpy as np
import scipy.stats as stats
from scipy.stats import poisson, norm
cs = 100
co = 300
mu = 4.7
G = poisson(mu)
p = G.pmf(np.arange(3*mu))
# Define Z(Q) function
def Z(Q):
for i in range(len(p)):
return sum(p[i]*cs*max((Q-i), 0) + p[i]*co*max((i-Q), 0))
# Plot Q and (Q)
import pylab as pl
x = []
y = []
for Q in range(0, 12):
x.append(Q)
y.append(Z(Q))
pl.plot(x, y, '-o')
pl.show()
Error Message shows up in the last 'Plot' procedure:
In Z(Q), 'numpy.float64' object is not iterable.
I would like Z(Q) returns value that sum each i in the range (0, len(p)), which is a function with variable Q. And finally the plot is Q(X Axis) and Z(Q)(Y Axis) for each Q it can be plotted.
How can I modify Z(Q)? Thank you!
And if I would like to output [Q, Z(Q)] how could I make it? My Code:
with open('opt.csv', 'wb') as fd:
a = csv.writer(fd, delimiter=',')
data1 = [['Order_Number', 'Cost'],
[Q, Z(Q)]]
a.writerows(data1)
If you're expecting Z(Q) to return multiple values, you may want to use the "yield" statement:
def Z(Q):
for i in range(len(p)):
yield sum(p[i]*cs*max((Q-i), 0) + p[i]*co*max((i-Q), 0))
This will allow it to be iterated over and will return the sum for each run of the for loop.
If you need the ouput as a list rather than a generic iterable, you'll either need to construct the list in that call, or wrap your call to Z(Q) in a list: list(Z(Q))
Edit:
After taking a closer look (and getting the right libraries installed...), my original answer seems to be incorrect. The stack trace (which would ideally be included in your question :) ) points to the return line in Z itself. This is because your original code is trying to run a call to sum every time it iterates over range(len(p)) rather than running it once on all the results.
You can adjust your code to do correctly return a single summed value by moving the sum outside the function, ie:
import numpy as np
import scipy.stats as stats
from scipy.stats import poisson, norm
cs = 100
co = 300
mu = 4.7
G = poisson(mu)
p = G.pmf(np.arange(3*mu))
# Define Z(Q) function
def Z(Q):
for i in range(len(p)):
yield p[i]*cs*max((Q-i), 0) + p[i]*co*max((i-Q), 0)
# Plot Q and (Q)
import pylab as pl
x = []
y = []
for Q in range(0, 12):
x.append(Q)
y.append(sum(Z(Q)))
pl.plot(x, y, '-o')
pl.show()
To summarise the changes:
Z(Q) now yields each iteration of the function as described
The sum operation is now run on the result of Z(Q)
I hope that helps!
import numpy
import matplotlib.pyplot as plt
from scipy import integrate
def f(x,y):
return x*y + x**2
def integral(x,y):
I = integrate.quad(f, 0, x, args=(y,))[0]
return I
def gau(x,y):
return (1+x)*integral(x,y)
xlist = numpy.linspace(-3.0, 3.0, 100)
ylist = numpy.linspace(-3.0, 3.0, 100)
X, Y = numpy.meshgrid(xlist, ylist)
Z = gau(2, Y)
print(Z)
I keep on getting the error message "Supplied function does not return a valid float." , I think the problem is that I try to pass an array to the quad function. I thought about evaluating the integral for every entry of the array with something like that:
yi=numpy.linspace(-3.0,3.0,100)
for i, item in enumerate(yi):
return integral[i]=integrate.quad(f,0,x,args=(yi,))[0]
It doesn't work but is it the right way? Any other/better suggestions?
You could use a universal function (see https://docs.scipy.org/doc/numpy/reference/ufuncs.html) which operates on arrays element-by-element. You can create these universal functions from any function using the frompyfunc function (https://docs.scipy.org/doc/numpy/reference/generated/numpy.frompyfunc.html):
ugau = numpy.frompyfunc(gau,2,1)
Z=ugau(X,Y)
It if your f() that does not provide a valid float when passed an array, not the scipy.integral itself;
why do you pass an array to your f() ?
You can use quadpy (one of my projects). quadpy is fully vectorized with respect to the dimensionality of the function range and the domains, so you can plug in a function that returns a vector and integrate that function over many intervals at once. You just have to make sure that the input function deals with vectorized input correctly. In your case, that would be
import numpy
import quadpy
def f(x, y):
return numpy.multiply.outer(y, x) + numpy.multiply.outer(numpy.ones_like(y), x ** 2)
def integral(x, y):
scheme = quadpy.line_segment.gauss_legendre(5)
intervals = numpy.array([numpy.zeros_like(x), x])
out = scheme.integrate(lambda t: f(t, y), intervals)
return out
def gau(x, y):
return (1 + x) * integral(x, y)
xlist = numpy.linspace(-3.0, 3.0, 100)
ylist = numpy.linspace(-3.0, 3.0, 100)
Z = gau(2, ylist)
print(Z)
You also insert xlist instead of 2 here to compute it all at once.
I woul like to solve an n-dimensional optimisation problem using iminuit.
So my approach is the following.
I am trying to figure out how to extend this:
def f(x,y,z):
return (x-1.)**2 + (y-2*x)**2 + (z-3.*x)**2 -1.
to a variable "x" that is a numpy.array.
I would like to do something like this:
x = [1,2,3,4,5]
y = [2,4,6,8,10]# y=2x
class StraightLineChi2:
def __init__(self,x,y):
self.x = x
self.y = y
def __call__(self,m,c): #lets try to find slope and intercept
chi2 = sum((y - m*x+c)**2 for x,y in zip(self.x,self.y))
return chi2
but in my case x is my unknown, and it is an array. Like in many optimization/minimization problems, the function is a f=f(x1,...,xn) where n can be big. x1,...,xn are the unknowns of the problem.
(These examples are taken from here)
Something similar is achieved "hacking" pyminuit2, like described here
For your example I recommend you using iminuit and probfit. Having an argument as a list of parameter is not exactly what you want to do since you will get confused which parameter is what very soon.
Here is an example taken straight from probfit tutorial. Also see the documentation
import iminuit
import probfit
x = np.linspace(0, 10, 20)
y = 3 * x + 15 + np.random.randn(len(x))
err = np.ones(len(x))
def line(x, m, c): # define it to be parabolic or whatever you like
return m * x + c
chi2 = probfit.Chi2Regression(line, x, y, err)
minuit = iminuit.Minuit(chi2)
minuit.migrad();
print(minuit.values) #{'c': 16.137947520534624, 'm': 2.8862774144823855}