gabrielvie
7/27/2019 - 3:10 AM

Handling Categorical Values

# Scikit Learn Encoders
from sklearn.preprocessing import LabelEncoder

import pandas as pd

data_frame = pd.read_csv('data_file')

target_feature = 'Feature Label'

# use encoder to tranform
encoder = LabelEncoder()
encoded_values = encoder.fit_transform(data_frame[target_feature].values)
data_frame[target_feature] = pd.Series(encoded_values, index=data_frame.index)

# inverse
decoded_values = encoder.inverse_transform(data_frame[target_feature].values)
data_frame[target_feature] = pd.Series(decoded_values, index=data_frame.index)