Reverse-OneHotEncoding an array

Asked by At

I was using LabelEncoder and OneHotEncoder to alter my categorical data to numerical data but as I am pretty new to Machine Learning I've done some things that I shouldn't have done with OneHotEncoder. So I want to reverse the thing I've done with OneHotEncoder.

I've made some searching on Internet and tried pd.get_dummies() but as OneHotEncoder changed my data to be 9 dimensional I get

Exception: Data must be 1-dimensional

error, so it doesn't work.

I also tried apply to decode the columns according to my search with a code looks like this.

X_level = pd.DataFrame({'1':[1,0,0,0,0,0,0,0,0],'2':[0,1,0,0,0,0,0,0,0],'3':[0,0,1,0,0,0,0,0,0],'4':[0,0,0,1,0,0,0,0,0],'5':[0,0,0,0,1,0,0,0,0],'6':[0,0,0,0,0,1,0,0,0],'7':[0,0,0,0,0,0,1,0,0],'8':[0,0,0,0,0,0,0,1,0],'9':[0,0,0,0,0,0,0,0,1]})
def get_X_level(row):
    for c in X_level.columns:
        if row[c]==1:
            return c
X_level.apply(get_X_level, axis=1)

Even though I got

0    1
1    2
2    3
3    4
4    5
5    6
6    7
7    8
8    9
dtype: object

as out I want this to be on my array not on my out. So it doesn't help me with my problem also.

I want this result to be on my X_level array not on my Out

1 Answers

0
Community On Best Solutions
X_level = pd.DataFrame({'1':[1,2,3,4,5,6,7,8,9]})
def get_X_level(row):
    for c in X_level.columns:
        if row [c]==1:
            return c

That code helped me with my problem.