I'm a bit confused - creating an ML model here.
I'm at the step where I'm trying to take categorical features from a "large" dataframe (180 columns) and one-hot them so that I can find the correlation between the features and select the "best" features.
Here is my code:
# import labelencoder
from sklearn.preprocessing import LabelEncoder
# instantiate labelencoder object
le = LabelEncoder()
# apply le on categorical feature columns
df = df.apply(lambda col: le.fit_transform(col))
df.head(10)
When running this I get the following error:
TypeError: ('argument must be a string or number', 'occurred at index LockTenor')
So I head over to the LockTenor field and look at all the distinct values:
df.LockTenor.unique()
this results in the following:
array([60.0, 45.0, 'z', 90.0, 75.0, 30.0], dtype=object)
looks like all strings and numbers to me. Is the error caused because it's a float and not necessarily an INT?
You get this error because indeed you have a combination of floats and strings. Take a look at this example:
If you run this code, you will see that
df1is encoded with no problem, since all its values are floats. However, you will get the error that you are reporting fordf2.An easy fix, is to cast the column to a string. You can do this in the corresponding lambda function:
As an additional suggestion, I would recommend you take a look at your data and see if they are correct. For me, it is a bit weird having a mix of floats and strings in the same column.
Finally, I would just like to point out that sci-kit's
LabelEncoderperforms a simple encoding of variables, it does not performe one-hot encoding. If you wish to do so, I recommend you take a look atOneHotEncoder