How to Resolve: TypeError: argument must be a string or number

I’m trying to implement below link in the Google Cloud.

Error while running this code from above link:

from sklearn.preprocessing import LabelEncoder

X = np.array(temp.feature.tolist())
y = np.array(temp.label.tolist())

lb = LabelEncoder()

y = np_utils.to_categorical(lb.fit_transform(y))

ERROR:

TypeError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/sklearn/preprocessing/_label.py in _encode(values, uniques, encode, check_unknown)
111 try:
–> 112 res = _encode_python(values, uniques, encode)
113 except TypeError:

3 frames
/usr/local/lib/python3.6/dist-packages/sklearn/preprocessing/_label.py in _encode_python(values, uniques, encode)
59 if uniques is None:
—> 60 uniques = sorted(set(values))
61 uniques = np.array(uniques, dtype=values.dtype)

TypeError: ‘<’ not supported between instances of ‘NoneType’ and ‘str’

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)
in ()
6 lb = LabelEncoder()
7
----> 8 y = lb.fit_transform(y)

/usr/local/lib/python3.6/dist-packages/sklearn/preprocessing/label.py in fit_transform(self, y)
250 “”"
251 y = column_or_1d(y, warn=True)
–> 252 self.classes
, y = _encode(y, encode=True)
253 return y
254

/usr/local/lib/python3.6/dist-packages/sklearn/preprocessing/_label.py in _encode(values, uniques, encode, check_unknown)
112 res = _encode_python(values, uniques, encode)
113 except TypeError:
–> 114 raise TypeError(“argument must be a string or number”)
115 return res
116 else:

TypeError: argument must be a string or number

That is odd. Could you print out the first five values for variables “X” and “y” and also the data types of these variables?

1 Like

I don’t know what just happened but it’s working fine now.
But now I’m having problem at this line:
model.fit(X, y, batch_size=32, epochs=5, validation_data=(val_x, val_y))

ERROR:
NameError Traceback (most recent call last)
in ()
----> 1 model.fit(X, y, batch_size=32, epochs=5, validation_data=(val_x, val_y))

NameError: name ‘val_x’ is not defined

© Copyright 2013-2019 Analytics Vidhya