0

I read about positional arguments and keyword arguments, but I still don't understand why I get SyntexError: non-keyword arg after keyworkd arg from running the following code. I didn't specify any keyword arguments in my objective function, right?

def obj_func(center, P, ACCUM, sentiment):
    d = 0
    for row in range(ACCUM[sentiment-1],ACCUM[sentiment]):
        v = P[row,:]
        d += v.dot(center) / (norm(v) * norm(center))
    return(-d)

iter_init = np.array([random() for ele in range(k)])
CENTERS = dict()
for sentiment in range(3):
    CENTERS[sentiment] = minimize(obj_func, x0=iter_init, args(P,ACCUM,sentiment),)

Also, I'm not so sure the last comma in minimize(). The guy in the tutorial wrote like this. Is it correct?

Paw in Data
  • 1,262
  • 2
  • 14
  • 32

1 Answers1

1

The error message is caused because in line CENTERS[sentiment] = minimize(obj_func, x0=iter_init, args(P,ACCUM,sentiment),) you have keyword agrument x0=iter_init and after that you have non-keyword argument args(P,ACCUM,sentiment)

Here args is a function you are calling and adding values it returns as an argument. I bet you want to give keyword argument named args which is a tuple. In that case you are missing = and right syntax would be CENTERS[sentiment] = minimize(obj_func, x0=iter_init, args=(P,ACCUM,sentiment))

If you are unsure, you can check arguments by saying help(minimize) in Python console once you've imported library providing that function.

ex4
  • 2,289
  • 1
  • 14
  • 21