Issue
I was trying to use OneHot to encode a column of different categories and save the encoding in a dictionary, since I want to use these exact encodings on a second column with the same categories but in different order. In the dictionary, the valuepairs are saved as
{'name1': array([0., 0., 1., 0., 0.]), 'name2': array([0., 1., 0., 0., 0.]), ...}
and this form can't be used for e.g. applying
StandardScalar.fit.transform()
to them. Is there a way to change these values to suit the "classical" form of OneHotEncoding below?
{'name1': 0 0 1 0 0 , 'name2': 0 1 0 0 0 , ...}
Thanks for any help!
Solution
conver them do DataFrame df
which could be also converted as classified one hot array y
:
import numpy as np
import pandas as pd
a={'name1': np.array([0., 0., 1., 0., 0.]), 'name2': np.array([0., 1., 0., 0., 0.])}
df=pd.DataFrame(a)
y=np.array(df)
df
content is:
name1 name2
0 0.0 0.0
1 0.0 1.0
2 1.0 0.0
3 0.0 0.0
4 0.0 0.0
y
content is:
array([[0., 0.],
[0., 1.],
[1., 0.],
[0., 0.],
[0., 0.]])
so, you can use either of them
Answered By - Nour-Allah Hussein
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.