Issue
I'm confused because it's going to be a problem if you first do OneHotEncoder
and then StandardScaler
because the scaler will also scale the columns previously transformed by OneHotEncoder
. Is there a way to perform encoding and scaling at the same time and then concatenate the results together?
Solution
Sure thing. Just separately scale and one-hot-encode the separate columns as needed:
# Import libraries and download example data
from sklearn.preprocessing import StandardScaler, OneHotEncoder
dataset = pd.read_csv("https://stats.idre.ucla.edu/stat/data/binary.csv")
print(dataset.head(5))
# Define which columns should be encoded vs scaled
columns_to_encode = ['rank']
columns_to_scale = ['gre', 'gpa']
# Instantiate encoder/scaler
scaler = StandardScaler()
ohe = OneHotEncoder(sparse=False)
# Scale and Encode Separate Columns
scaled_columns = scaler.fit_transform(dataset[columns_to_scale])
encoded_columns = ohe.fit_transform(dataset[columns_to_encode])
# Concatenate (Column-Bind) Processed Columns Back Together
processed_data = np.concatenate([scaled_columns, encoded_columns], axis=1)
Answered By - Max Power
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.