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

Answer by Arunesh Singh for How to perform OneHotEncoding in Sklearn, getting value error

$
0
0

Below implementation should work well. Note that the input of onehotencoder fit_transform must not be 1-rank array and also output is sparse and we have used to_array() to expand it.

import pandas as pdimport numpy as npfrom sklearn.preprocessing import LabelEncoderfrom sklearn.preprocessing import OneHotEncoderdata= [["AUS", "Sri"],["USA","Vignesh"],["IND", "Pechi"],["USA","Raj"]]df = pd.DataFrame(data, columns=['Country', 'Name'])X = df.valuesle = LabelEncoder()X_num = le.fit_transform(X[:,0]).reshape(-1,1)ohe = OneHotEncoder()X_num = ohe.fit_transform(X_num)print (X_num.toarray())X[:,0] = X_numprint (X)

Viewing all articles
Browse latest Browse all 5

Trending Articles



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