Issue
I have a dataset with a column that contains numbers as strings such as "one", "three", "five", "five" and etc. I want to use an ordinal encoder: one will be 0, two will be 1 three will be 3, and so on. How to do it? Also in HotEncoder, I have a sparse option and in ordinal encoder, I don't have this option. Should I need to do sparse here?
my code:
#independent variables-Matrix
X = df.iloc[:, :-1].values
#dependent variables vectors
Y = df.iloc[:, -1].values
from sklearn.preprocessing import LabelEncoder, OneHotEncoder, OrdinalEncoder
Encoder = OrdinalEncoder()
Z2= Encoder.fit_transform(X[:, [17]])
#X = np.hstack(( [![Z][1]][1]2, X[:,:17] , X[:,18:])).astype('float')
#handling the dummy variable trap
#X = X[:, 1:]
Solution
In your case I will use a function instead of using Sklearn.
def label_encoder(column):
values = ['one', 'two', 'three', 'four', 'five']
new_row = []
for row in column:
for i, ii in enumerate(values):
if row == ii:
new_row.append(i)
else:
continue
return new_row
or you can use list comprehensions
def label_encoder(column):
values = ['one', 'two', 'three', 'four', 'five']
new_row = [i for row in column for (i, ii) in enumerate(values) if row==ii]
return new_row
This functions will transform an array of ['one', 'one', 'two', ...]
to [1, 1, 2, ...]
Answered By - Stack
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.