unsupported operand type(s) for -: 'str' and 'str' - Implementation LimeTabularExplainer

1.9k Views Asked by At

my issue is that I get the following error

enter image description here

when I try to follow the implementation given in the notebook https://github.com/klemag/PyconUS_2019-model-interpretability-tutorial/blob/master/02-interpretability_LIME-solution.ipynb

by Kevin Lemagnen. I have used his suggested way of preprocessing the data and converting them to the format needed by the LIME XAI technique.

I have used the following helper function:

`def convert_to_lime_format(X, categorical_names, col_names=None, invert=False): """Converts data with categorical values as string into the right format for LIME, with categorical values as integers labels. It takes categorical_names, the same dictionary that has to be passed to LIME to ensure consistency. col_names and invert allow to rebuild the original dataFrame from a numpy array in LIME format to be passed to a Pipeline or sklearn OneHotEncoder """

# If the data isn't a dataframe, we need to be able to build it
if not isinstance(X, pd.DataFrame):
    X_lime = pd.DataFrame(X, columns=col_names)
else:
    X_lime = X.copy()

for k, v in categorical_names.items():
    if not invert:
        label_map = {
            str_label: int_label for int_label, str_label in enumerate(v)
        }

    else:
        label_map = {
            int_label: str_label for int_label, str_label in enumerate(v)
        }

    X_lime.iloc[:, k] = X_lime.iloc[:, k].map(label_map)

return X_lime`

How can I fix this issue? Any help would be greatly appreciated.

I have already looked around on Stackoverflow and I have googled the TypeError and I found the following explanation:

The python error TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’ occurs when you try to subtract a string from another that contains numbers in both strings. The TypeError is due to the operand type minus (‘-‘) is unsupported between str (string). Auto casting is not supported by python. You can subtract a number from a different number. If you try to subtract a string from another string that may contain a number, the error TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’ will be thrown.

In python, an arithmetic operation can be used between valid numbers. For example, you can subtract a number from a different number. The integer can be subtracted from a float number. If you try to subtract a string from a string that contains a number, the error TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’ will be thrown.

Objects other than numbers can not be used in python substraction. The arithmetic subtract can be used only for numbers. If a number is stored as a string, it should be converted to an integer before subtracting it from each string. If you try to subtract a string to a string containing a number, the error TypeError: unsupported operand type(s) for +: ‘str’ and ‘str’ will be shown.

However, I was not able to resolve the problem.

0

There are 0 best solutions below