Search code examples
pythonscikit-learnlabel-encoding

Why I get 'can't assign to operator' error?


When I try to assign LabelEncoder() to label_encoder, I get an error:

SyntaxError: can't assign to operator

However, I assigned LabelEncoder() to label_encoder in the past.
Here is my code:

label_encoder = LabelEncoder()
play-label = label_encoder.fit_transform(play)

Solution

  • This is not an issue related to assigning value to a variable. This is a Syntax Error. You haven't followed the proper naming convention to name the variable play-label.

    If you try doing something like this :

    >>> a-b = 3
      File "<stdin>", line 1
    SyntaxError: can't assign to operator
    

    You'll get the same error.
    Replace dash - with Underscore _ and it will work fine.
    The correct variable name should be play_label. Refer to this link, for an indepth description . Python Naming Convention