Quantcast
Channel: How to perform OneHotEncoding in Sklearn, getting value error - Stack Overflow
Viewing all articles
Browse latest Browse all 5

Answer by Thom Ives for How to perform OneHotEncoding in Sklearn, getting value error

$
0
0

You can go directly to OneHotEncoding now without using the LabelEncoder, and as we move toward version 0.22 many might want to do things this way to avoid warnings and potential errors (see DOCS and EXAMPLES).


Example code 1 where ALL columns are encoded and where the categories are explicitly specified:

import pandas as pdimport numpy as npfrom sklearn.preprocessing import OneHotEncoderdata= [["AUS", "Sri"],["USA","Vignesh"],["IND", "Pechi"],["USA","Raj"]]df = pd.DataFrame(data, columns=['Country', 'Name'])X = df.valuescountries = np.unique(X[:,0])names = np.unique(X[:,1])ohe = OneHotEncoder(categories=[countries, names])X = ohe.fit_transform(X).toarray()print (X)

Output for code example 1:

[[1. 0. 0. 0. 0. 1. 0.] [0. 0. 1. 0. 0. 0. 1.] [0. 1. 0. 1. 0. 0. 0.] [0. 0. 1. 0. 1. 0. 0.]]

Example code 2 showing the 'auto' option for specification of categories:

The first 3 columns encode the country names, the last four the personal names.

import pandas as pdimport numpy as npfrom sklearn.preprocessing import OneHotEncoderdata= [["AUS", "Sri"],["USA","Vignesh"],["IND", "Pechi"],["USA","Raj"]]df = pd.DataFrame(data, columns=['Country', 'Name'])X = df.valuesohe = OneHotEncoder(categories='auto')X = ohe.fit_transform(X).toarray()print (X)

Output for code example 2 (same as for 1):

[[1. 0. 0. 0. 0. 1. 0.] [0. 0. 1. 0. 0. 0. 1.] [0. 1. 0. 1. 0. 0. 0.] [0. 0. 1. 0. 1. 0. 0.]]

Example code 3 where only the first column is one hot encoded:

Now, here's the unique part. What if you only need to One Hot Encode a specific column for your data?

(Note: I've left the last column as strings for easier illustration. In reality it makes more sense to do this WHEN the last column was already numerical).

import pandas as pdimport numpy as npfrom sklearn.preprocessing import OneHotEncoderdata= [["AUS", "Sri"],["USA","Vignesh"],["IND", "Pechi"],["USA","Raj"]]df = pd.DataFrame(data, columns=['Country', 'Name'])X = df.valuescountries = np.unique(X[:,0])names = np.unique(X[:,1])ohe = OneHotEncoder(categories=[countries]) # specify ONLY unique country namestmp = ohe.fit_transform(X[:,0].reshape(-1, 1)).toarray()X = np.append(tmp, names.reshape(-1,1), axis=1)print (X)

Output for code example 3:

[[1.0 0.0 0.0 'Pechi'] [0.0 0.0 1.0 'Raj'] [0.0 1.0 0.0 'Sri'] [0.0 0.0 1.0 'Vignesh']]

Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>