f() missing 1 required positional argument: 't' - python-3.x

I just tried to execute this code. but it shows always this error:'f() missing 1 required positional argument:'t'
please can you tell me what should I change?
import numpy as np
def f(y,z,t):
return np.array([2*y+z-t,z+y])
import matplotlib.pyplot as plt
from scipy.integrate import odeint
t=np.linspace(0,2,1000)
sol=odeint(f,[0,1],t)
y,z=sol[:,0],sol[:,1]
plt.plot(t,y,label='y')
plt.plot(t,z,label='z')
plt.show()

Depending on what you are trying to do, you can get around it two ways. To pass in additional arguments other than y and t you need to include them as a constant in the function parameter.
import numpy as np
def f(t,y,z):
return np.array([2*y+z-t,z+y])
import matplotlib.pyplot as plt
from scipy.integrate import odeint
t=np.linspace(0,2,1000)
z = 10.0
sol=odeint(f,[0,1],t, tfirst=True, args=(z, ))
y,z=sol[:,0],sol[:,1]
plt.plot(t,y,label='y')
plt.plot(t,z,label='z')
plt.show()
This will still cause an error. However, if you are also trying to obtain z as result then you should be able to run:
import numpy as np
def f(t,inp):
y, z = inp
return np.array([2*y+z-t,z+y])
import matplotlib.pyplot as plt
from scipy.integrate import odeint
t=np.linspace(0,2,1000)
sol=odeint(f,[0,1],t, tfirst=True)
y,z=sol[:,0],sol[:,1]
plt.plot(t,y,label='y')
plt.plot(t,z,label='z')
plt.show()
This should run without any errors but you may need to double check that this is the result you are expecting.
(The tfirst argument is just for clarity to make ensure the order of arguments provided is correct, you can remove and re-order if you want as well.)
Documentation for odeint function here: https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.odeint.html

Related

NameError: name 'pairwise_distances_argmin' is not defined

Upon running this code:
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
from sklearn.datasets import make_blobs
X, y_true = make_blobs(n_samples = 300, centers= 4, cluster_std =0.60,random_state = 0)
plt.scatter(X[:,0], X[:,1], s=50)
labels = pairwise_distances_argmin(X, centres)
I am getting a NameError
"name 'pairwise_distances_argmin' is not defined"
What may be the possible reasons for it.
Please note that I am relatively new to this field, elaborated answers are most welcome.
Thanks in advance.
Have searched Stack overflow and unsuccessfully try solutions suggested on these pages:
Weird results of sklearn.metrics.pairwise_distances_argmin_min when computing euclidean distance
NameError: name 'sklearn' is not defined
You wish to use the
pairwise_distances_argmin
function.
You're going to have to import it:
from sklearn.metrics import pairwise_distances_argmin

Maintenance for matplotlib.pyplot quiver? Loss of ":" in specifying vectors

Long story short, the highest upvoted answer to this question no longer works in Google Colab, although it used to work perfectly. What exactly has changed, and how does one fix it?
Made more explicit:
import numpy as np
import matplotlib.pyplot as plt
V = np.array([[1,1],[-2,2],[4,-7]])
origin = [0], [0] # origin point
plt.quiver(*origin, V[:,0], V[:,1], color=['r','b','g'], scale=21)
plt.show()
no longer works, since it seems the capability of using ":" has been taken out of plt.quiver. The following works, however:
import numpy as np
import matplotlib.pyplot as plt
V = np.array([[1,1],[-2,2],[4,-7]])
origin = [0], [0] # origin point
plt.quiver(*origin, V[0,0], V[0,1], color=['r'], scale=21)
plt.quiver(*origin, V[1,0], V[1,1], color=['g'], scale=21)
plt.quiver(*origin, V[2,0], V[2,1], color=['b'], scale=21)
plt.show()
will do what the old code did. How can I recover the use of ":"?
Evidently, the *origin part no longer works.
import numpy as np
import matplotlib.pyplot as plt
V = np.array([[1,1],[-2,2],[4,-7]])
O=np.array([[0],[0],[0]])
ax=plt.quiver(O[:],O[:],V[:,0],V[:,1], color=['r','g','b'], scale=21)
plt.show()

How do I make my plot look like this with matplotlib?

So right now I'm trying to simulate a Poisson process for an assignment, here's the code so far:
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
y = np.arange(0,21,1)
x = np.cumsum(np.random.exponential(2,21))
print(y)
print(x)
sns.set()
plt.plot(x,y)
plt.show()
The problem arises when I try plotting it. The code above, as expected, produces a normal matplotlib plot that looks like this:
However I need it to look like this:
Is there an easy way of doing it? I tried messing with bar plots but was unable to produce something that looks good.
The graph that you are wanting to plot is called as step plot in matplotlib. In order to plot it replace plt.plot(x,y) with plt.step(x,y)
So, your code becomes:
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
y = np.arange(0,21,1)
x = np.cumsum(np.random.exponential(2,21))
print(y)
print(x)
sns.set()
plt.step(x,y)
plt.show()

Python exponential plot is wrong

I am new using python and try to do some plots. I realized, that a plot of a bump function is incorrect. I have no idea how python came to this result.
This is my 'code'
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
class MainBody():
x = np.linspace(0.0001,99.9999,1000)
result = np.exp((-1.0)/(x*(100.0-x)))
plt.plot(x,result)
plt.show()
I got this result
but I should get this
I know that Python is powerful but I think such simple things should work without occuring such errors, where is my mistake?
Thank you
Matthias
Use plt.ylim to set the y-limits. Otherwise, by default, matplotlib will try to show the entire dataset, whose y-limits go roughly from 0 to 1:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.0001,99.9999,1000)
result = np.exp((-1.0)/(x*(100.0-x)))
plt.plot(x,result)
plt.ylim(0.9975, 0.9999)
plt.show()

Reason of ValueError: negative dimensions are not allowed?

I have been trying to plot the output of the function defined below,but not able to get the output. I tried several things but getting different errors every time. If somebody can help me with what it is that I am doing wrong, I shall be grateful.
import matplotlib.pyplot as plt
%matplotlib inline
import math
import sympy as sym
x = sym.symbols('x',positive = True)
lambd = 4
a= 3
def f(x):
return lambd**a * x**(a-1) * sym.exp(-lambd*x) / math.factorial(a-1)
x1 = np.linspace(0,1,10)
plt.plot(x1,f(x1))
In case I change the x1 as np.linspace(0,1,100) then the error is
"ValueError: sequence too large; cannot be greater than 32"
What can be the reason for that? Some guidance in this will be highly appreciated.
You're passing a numpy array x1 to a function f. The problem that inside this function you have sympy.exp() which does not understand what to do with an array, since it only works on symbols and numbers.
The easiest would be to use numpy.exp instead.
import matplotlib.pyplot as plt
import numpy as np
import math
lambd = 4
a= 3
def f(x):
return lambd**a * x**(a-1) * np.exp(-lambd*x) / math.factorial(a-1)
x1 = np.linspace(0,1,10)
plt.plot(x1,f(x1))
plt.show()
If, for whatever reason, you need to use some function that only works on single numbers and not arrays, you can use numpy.vectorize to convert the function to one that evaluates the input array elementwise.
import matplotlib.pyplot as plt
import sympy as sym
import numpy as np
import math
lambd = 4
a= 3
def f(x):
return lambd**a * x**(a-1) * sym.exp(-lambd*x) / math.factorial(a-1)
fv = np.vectorize(f)
x1 = np.linspace(0,1,10)
plt.plot(x1,fv(x1))
plt.show()

Resources