Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inscrutable error
#1
The following Python code gives an error when it is interpreted.

GridsearchCV(
    cv=TimeSeriesSplit(gap=0, max_train_size=None, n_splits=5, test_size=None),
    estimator=XGBClassifier(
        base_score=None,
        booster=None,
        colsample_bylevel=None,
        colsample_bynode=None,
        colsaample_bytree=None,
        gamma=None,
        gpu_id=None,
        importnce_type="gain",
        interaction_constraints=None,
        learning_rate=None,
        max_delta_step=None,
        max_depth=None,
        min_child_weight=None,
        missing=nan,
        monotone_constraints=None,
        n_estimators=100,
        n_jobs=None,
        num_parallel_tree=None,
        random_state=None,
        reg_alpha == None,
        reg_lambda=None,
        scale_pos_weight=None,
        subsample=None,
        tree_method=None,
        validate_paramters=None,
        verbosity=None,
    ),
    n_jobs=-1,
    param_grid={
        "max_depth": [1, 3, 5, 7, 10, 20],
        "n_estimators": [5, 10, 20, 30, 40, 50],
    },
    return_train_score=True,
    scoring="f1_macro",
)
The error is now posted.

Input In [35]
    ),
    ^
SyntaxError: positional argument follows keyword argument
It is referring to line numbers, so I am attaching a screenshot showing the above code with line numbers.

What is this error. How do I get rid of it?

Respectfully,

LZ

Attached Files

Thumbnail(s)
   
Reply
#2
In line 34, do you mean to have a comma after the list?
Reply
#3
I would write it like this:
cv=TimeSeriesSplit(gap=0, max_train_size=None, n_splits=5, test_size=None)
estimator=XGBClassifier(
        base_score=None,
        booster=None,
        colsample_bylevel=None,
        colsample_bynode=None,
        colsaample_bytree=None,
        gamma=None,
        gpu_id=None,
        importnce_type="gain",
        interaction_constraints=None,
        learning_rate=None,
        max_delta_step=None,
        max_depth=None,
        min_child_weight=None,
        missing=nan,
        monotone_constraints=None,
        n_estimators=100,
        n_jobs=None,
        num_parallel_tree=None,
        random_state=None,
        reg_alpha == None,
        reg_lambda=None,
        scale_pos_weight=None,
        subsample=None,
        tree_method=None,
        validate_paramters=None,
        verbosity=None,
    )

param_grid={
        "max_depth": [1, 3, 5, 7, 10, 20],
        "n_estimators": [5, 10, 20, 30, 40, 50],
}

GridsearchCV(
    cv=cv,
    estimator=estimator,
    n_jobs=-1,
    param_grid=param_grid,
    return_train_score=True,
    scoring="f1_macro"
)
This will show you if the error is in the TimeSeriesSplit, XGBClassifier, param_grid or GridsearchCV call. You might find the error is in a prior line.

Why are you providing None values to optional arguments? Makes it hard to see what you think is important.
Reply
#4
To answer the first question, I am somewhat confused and intrigued by commas at the end of list in Python 3. Clearly each member of a list is separated by commas.

But in Python it seems that the last member of a list is also followed by a comma. The list itself is usually enclosed in parenthesis or brackets. Thus, while programming, I routinely left the last comma following the last member of the list off and get error, I put the comma there and got no error.

While programming, I usually got am error at some point. So, after trial an error, I made the error go way by putting a comma there. It works. Why I do not know. But it works. I can produce an example to show you what I mean if that is needed.

Respectfully,

LZ
Reply
#5
You are confused about lists and tuples. You never have to put a trailing comma on a list. It doesn't hurt, but it also doesn't matter.
x = [1, 2, 3, 4,]
y = [1, 2, 3, 4]
print(x, y,)
Output:
[1, 2, 3, 4] [1, 2, 3, 4]
On a tuple you need a trailing comma if there is only one item in the tuple.
w = (1, 2, 3, 4,)
x = (1, 2, 3, 4)
y = (1,)
z = (1) # <- Not a tuple.  Is parenthesis around an expression, like 2 * (3 + 4)
print(w, x, y, z)
(1, 2, 3, 4) (1, 2, 3, 4) (1,) 1
Making a problem go away without understanding why is not fixing the problem.

Your error is not inscrutable. The error message is both accurate and very descriptive. The error is just hard to see.

This is the problem with your code. You need to replace == with =.
        random_state=None,
        reg_alpha == None,    <- This is an expression.  Will evaluate to True or False.  reg_alpha=None would be a named argument.
        reg_lambda=None,
Using == instead of = changes this from a named argument to an expression. You can pass an expression as an argument to a function (it is evaluated and the value passed), but there is no name associated with this expression, making it a positional argument. Positional arguments cannot appear after named arguments.
ibreeden likes this post
Reply
#6
This is the code where it happens. I posted the error down below and it gives a hint as to where the error is.


cv=TimeSeriesSplit(gap=0, max_train_size=None, n_splits=5, test_size=None)
estimator=XGBClassifier(
        base_score=None,
        booster=None,
        colsample_bylevel=None,
        colsample_bynode=None,
        colsaample_bytree=None,
        gamma=None,
        gpu_id=None,
        importnce_type="gain",
        interaction_constraints=None,
        learning_rate=None,
        max_delta_step=None,
        max_depth=None,
        min_child_weight=None,
        missing=nan,
        monotone_constraints=None,
        n_estimators=100,
        n_jobs=None,
        num_parallel_tree=None,
        random_state=None,
        reg_alpha == None,
        reg_lambda=None,
        scale_pos_weight=None,
        subsample=None,
        tree_method=None,
        validate_paramters=None,
        verbosity=None,
    )
Input In [1]
    )
    ^
SyntaxError: positional argument follows keyword argument
I am guessing it tells us that the error is in the first few python lines I do not think that the number 1 surrounded by brackets means a lot. If I were to run the code again that one would probably change to two.

What exactly am I looking for?

As I said it is in the first couple of lines. I just do not know what I am looking for.

I use black for code formatting, and it does not flag anything like this error when I run it.

Any help appreciated.

Respectfully,

LZ
Reply
#7
Here is some additional python code that also shows this error.

# Training with the best alpha value
svm_clf = SGDClassifier(
    loss="hinge", alpha=param_alpha, penalty="I2", n_job == 1, random_state=21
)
svm_clf.fit(X_train, y_train)
get_result(svm_clf, X_test, y_test)
It is very basic python. What am I looking for?

Respectfully,
Reply
#8
Where an error is, and where an error is reported are not always the same. Looks like conda reports the expression where the syntax error occurred, not the exact line. As mentioned earlier, this is the line that results in the error in your first example and in the second example calling XGBClassifier() by itself.:
reg_alpha == None
You have the same thing happening in your latest example. Do you see it here?
svm_clf = SGDClassifier(
    loss="hinge", alpha=param_alpha, penalty="I2", n_job == 1, random_state=21
)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020