Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

python - Transform OneHot-encoded data from array to sequence of numbers

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...