I have a small module written as:
Contents of psychometrics.py
def prob3pl(theta, D = 1.7, a, b, c):
result = c + (1 - c) / (1 + np.exp(-D * a * (theta - b)))
return(result)
def gpcm(theta, d, score, a, D=1.7):
Da = D * a
result = np.exp(np.sum(Da * (theta - d[0:score]))) / np.sum(np.exp(np.cumsum(Da * (theta - d))))
return(result)
if __name__ == '__main__':
gpcm(theta, d, score, a, D=1.7)
prob3pl(theta, D = 1.7, a, b, c)
Now using the python interpret I do the following:
import psychometrics as py
import numpy as np
py.prob3pl(0, a = 1, b= 0, c=0)
However, when running this yields
>>> py.prob3pl(0,a=1,b=0,c=0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: prob3pl() missing 1 required positional argument: 'D'
When I copy and paste the function into the interpreter it runs using the default value of D = 1 but when importing that isn't happening.
What error am I making such that the default value for D is not recognized when I import the module?
Thank you
Your code has syntax error -
SyntaxError: non-default argument follows default argument
Therefore, kindly change function prob3pl() as
def prob3pl(theta, a, b, c, D = 1.7):
result = c + (1 - c) / (1 + np.exp(-D * a * (theta - b)))
return(result)
Reason - In python function declaration, there should not be any non-default argument after any default argument. as D=1.7 here
Related
Simple question on how to link (or string together) multiple functions that depend on each other. I have the following example function (in Jupyter):
### First function
def function_one():
## process one
a = "one" + "two"
print(a)
## process two
b = "red" + "blue"
print(b)
## process three
c = "this" + "sucks"
print(c)
return a, b, c
### Second function
def function_two(a, b, c):
## process four
d = a + b
print(d)
## process five
e = b + c
print(e)
## process six
f = a + c
print(f)
return d, e, f
### Third function
def function_three():
g = a + b + c + d + e + f
print(g)
return g
### Calling functions
initial = function_one()
second = function_two(initial)
third = ... #I can't get past the following error to even link this function in
The first function works when called, but when I try to send that data downstream to the second function, I get this error:
onetwo
redblue
thissucks
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-7c5562b97c86> in <module>
1 initial = function_one()
----> 2 second = function_two(initial)
TypeError: function_two() missing 2 required positional arguments: 'b' and 'c'
How do I remedy this?
When returning multiple objects, you are actually returning a tuple. Ex:
>>> def foo():
>>> return 1, 2, 3
>>> type(foo())
<class 'tuple'>
So, right now the whole tuple is treated as argument a and hence b and c are missing. In order to pass that on as 3 separate arguments, you need to unpack that tuple:
initial = function_one()
second = function_two(*initial)
third = function_three(*initial, *second)
Assign the return values to variables then pass it to the second function:
a, b, c = function_one()
function_two(a, b, c)
For your third function you need it to accept parameters as well
def function_three(a, b, c, d, e, f):
g = a + b + c + d + e + f
print(g)
return g
Then putting it all together:
a, b, c = function_one()
d, e, f = function_two(a, b, c)
g = function_three(a, b, c, d, e, f)
I am making a script where you can input a, b and c into the quadratic formula and it will give you the answer.
It says that b is not defined when I run it.
from cmath import sqrt
qf = lambda a, b, c: (-b-cmath.sqrt((b**2) - (4*a*c)))/(2*a), (-b+cmath.sqrt((b**2) - (4*a*c)))/(2*a)
a, b, c = input('Enter a, b and c with spaces in between the values \n').split()
a = float(a) ; b = float(b) ; c = float(c)
Print(qf(a, b,c)
Traceback (most recent call last):
File "/storage/emulated/0/Download/.last_tmp.py", line 2, in
qf = lambda a, b, c: (-b-cmath.sqrt((b2) - (4*a*c)))/(2*a), (-b+cmath.sqrt((b2) - (4*a*c)))/(2*a)
NameError: name 'b' is not defined
Check this out:
from math import sqrt
def get_roots(a, b, c):
if a == 0:
raise ZeroDivisionError('Not a quadratic equation')
val = b ** 2 - 4 * a * c
if val < 0:
raise Exception('Imaginary Roots')
val = sqrt(val)
root1 = (-b - val) / (2 * a)
root2 = (-b + val) / (2 * a)
return root1, root2
a, b, c = input('Enter a, b and c with spaces in between the values \n').split()
a, b, c = float(a), float(b), float(c)
print(get_roots(a,b,c))
Lambda functions can only return one thing so you need to group your outputs into either a tuple or a list by adding an enclosing () or []. The python parser is getting to:
qf = lambda a, b, c: (-b-sqrt((b**2) - (4*a*c)))/(2*a)
and assuming the lambda function is over. It then begins reading:
, (-b+sqrt((b**2) - (4*a*c)))/(2*a)
and tries to interpret -b in the global scope (where it doesn't exist) this gives you your name error. If you were to get rid of all the variables and for a moment pretend the second result of the quadratic formula was always 0, you'd get a tuple with the first element being a lambda function and the second being the integer 0
>>>qf = lambda a, b, c: [(-b-sqrt((b**2) - (4*a*c)))/(2*a),0
>>>qf
(<function <lambda> at 0x000002B44EF72F28>, 0)
This doesn't quite get what you're after, because you want a single function rather than a tuple of two separate functions to compute each root separately.
Here's what it'd look like if you want the lambda to return a list:
qf = lambda a, b, c: [(-b-sqrt((b**2) - (4*a*c)))/(2*a), (-b+sqrt((b**2) - (4*a*c)))/(2*a)]
Here's the error message:
RuntimeError: dictionary changed size during iteration
Here's the segment of my code (<= marks the error line):
# Probability Distribution from a sequence of tuple tokens
def probdist_from_tokens (tokens, N, V = 0, addone = False):
cfd = ConditionalFreqDist (tokens)
pdist = {}
for a in cfd: # <= line with the error
pdist[a] = {}
S = 1 + sum (1 for b in cfd[a] if cfd[a][b] == 1)
A = sum (cfd[a][b] for b in cfd[a])
# Add the log probs.
for b in cfd[a]:
B = sum (cfd[b][c] for c in cfd[b])
boff = ((B + 1) / (N + V)) if addone else (B / N)
pdist[a][b] = math.log ((cfd[a][b] + (S * boff)) / (A + S))
# Add OOV for tag if relevant
if addone:
boff = 1 / (N + V)
pdist[a]["<OOV>"] = math.log ((S * boff) / (A + S))
return pdist
I'm basically just using cfd as a reference to put the correct values in pdist. I'm not trying to change cfd, I just want to iterate over it's keys and the keys of it's sub dictionary.
I think the problem is caused by the lines where I set the variables A and B, I got the same error when I had different code on those lines but I don't get the error when I replace them with a constant value.
nltk.probability.ConditionalFreqDist inherits defaultdict, which means if you read a non-existing entry cfd[b], a new entry (b, FreqDist()) will be inserted to the dictionary, and thus changing its size. Demonstration of the problem:
import collections
d = collections.defaultdict(int, {'a': 1})
for k in d:
print(d['b'])
Output:
0
Traceback (most recent call last):
File "1.py", line 4, in <module>
for k in d:
RuntimeError: dictionary changed size during iteration
So you should check this line:
for b in cfd[a]:
B = sum (cfd[b][c] for c in cfd[b])
are you sure the b key really exists in cfd? You may want to change it to
B = sum(cfd[b].values()) if b in cfd else 0
# ^~~~~~~~~~~
essentially I have a function which I need to pass to a bunch of values in two different moments (here skipped for practical reasons), hence, I thought need to split all the parameters in two tuple sets after which I'd pass the first set and then the second thinking - mistakenly - that python would allocate all the available n parameters of the function to the values passed with the tuple and then the rest with the second tuple. That is not the case:
def example_F_2(x, y, z, g):
return x * y + z * g
e = (1,2)
r = (3,4)
print(example_F_2(e, r))
in fact:
Traceback (most recent call last):
File "C:/Users/francesco/PycharmProjects/fund-analysis/test_sheet.py", line 7, in <module>
print(example_F_2(e, r))
TypeError: example_F_2() missing 2 required positional arguments: 'z' and 'g'
what can I do? I am guessing something from this page should work (https://docs.python.org/3.5/library/functools.html) but I have been unable to do it myself
A very simple way is to concatenate the tuples and then unpack them as you pass them to your function:
print(example_F_2(*(e+r)))
Perhaps you can use something like this:
def example_F_2(e=None, r=None):
first_term = 0
second_term = 0
if e is not None:
first_term = e[0] * e[1]
if r is not None:
second_term = r[0] * r[1]
return first_term + second_term
e = (1, 2)
r = (3, 4)
print example_F_2(e, r)
> 14
print example_F_2(e=e)
> 2
print example_F_2(r=r)
> 12
from FuncDesigner import *
a, b, c = oovars('a', 'b', 'c')
f = [2*a+3*b-2*c+5, 2*a+13*b+15, a+4*b+2*c-45]
linSys = sle(f)
r = linSys.solve()
A, B, C = r(a, b, c)
maxRes = r.ff
when I execute this code in terminal it is showing the error
"TypeError: formDictOfFixedFuncs() takes exactly 4 arguments (3 given)"
I don't know what to do
Please help me
works for me (I have latest svn snapshot v 0.51+)
A, B, C
(24.999999999999996, -4.9999999999999991, 19.999999999999996)
maxRes
7.1054273576010019e-15