0

Suppose I have the following simple function and inputs:

dates = pd.date_range('20170101',periods=20)
a1 = np.ones(3)
b1 = pd.DataFrame(np.random.randint(10,size=(20,3)),index=dates,columns=['foo','bar','see'])
def test_func(a,b):
        c = (a*b).sum(axis=1)
        d = c.std()*np.sqrt(3)
        e = c.mean()/d
        return -np.array(e) 

I would like to solve this function for a that minimizes the output (maximizes e).

scipy.optimize.fmin(test_func,a1,args=(b1))

But this throws a type error

TypeError: test_func() takes 2 positional arguments but 4 were given

My quesiton is i) is this a good way to solve for the max of such a function and ii) what the devil is the problem?

1
  • the second part is clear. you are misusing the test_func. check here. you are providing extra inputs Commented Apr 28, 2017 at 18:48

1 Answer 1

1

You are missing a comma after the b1 in the extra argument:

scipy.optimize.fmin(test_func,a1,args=(b1,))

seems to work.

Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant! This is helpful. So to be clear, the 'args' parameter requires a trailing comma with 1, but not 2, variables? For other functions that require more inputs, I've used 'args=(b1,c1,d1)' and it's worked just fine.
That is correct: (b1,) is a tuple with one element, while (b1) is just b1 itself. From the the docs: "A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.