Python Forum
How to implement OneHotEncoder for Multiple Categorical Columns?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to implement OneHotEncoder for Multiple Categorical Columns?
#1
Hi, I am trying to convert the car evaluation dataset from the UCI repository to implement a KNN algorithm on it and I need to first convert the categorical data into numerical values. I know how to convert one column but I am facing difficulty in converting multiple columns. My code snippet is as below (I am very new to Python so this may look very messy and the results I got from below is not what is expected as not all the columns were encoded correctly and I am not sure if I am doing it the right way)

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#importing the dataset
attributes = ['buying', 'maint', 'doors', 'persons', 'lug_boot', 'safety']
target = ['acceptability']
dataset = pd.read_csv('car.data',names = attributes+target)
X = dataset.iloc[:,:-1].values
y = dataset.iloc[:,6].values

#handling categorical data
labelencoder_X = LabelEncoder()
X[:,0]=labelencoder_X.fit_transform(X[:,0])
X[:,1]=labelencoder_X.fit_transform(X[:,1])
X[:,2]=labelencoder_X.fit_transform(X[:,2])
X[:,3]=labelencoder_X.fit_transform(X[:,3])
X[:,4]=labelencoder_X.fit_transform(X[:,4])
X[:,5]=labelencoder_X.fit_transform(X[:,5])

#perform dummy encoding to feature scale the data into a standardize format
onehotencoder = OneHotEncoder(categorical_features=[0])
X = onehotencoder.fit_transform(X).toarray()
onehotencoder = OneHotEncoder(categorical_features=[1])
X = onehotencoder.fit_transform(X).toarray()
onehotencoder = OneHotEncoder(categorical_features=[2])
X = onehotencoder.fit_transform(X).toarray()
onehotencoder = OneHotEncoder(categorical_features=[3])
X = onehotencoder.fit_transform(X).toarray()
onehotencoder = OneHotEncoder(categorical_features=[4])
X = onehotencoder.fit_transform(X).toarray()
onehotencoder = OneHotEncoder(categorical_features=[5])
X = onehotencoder.fit_transform(X).toarray()
Any help on this will be much appreciated.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Dividing a single column of dataframe into multiple columns based on char length darpInd 2 2,459 Mar-14-2020, 09:19 AM
Last Post: scidam
  How to add data to the categorical index of dataframe as data arrives? AlekseyPython 1 2,343 Oct-16-2019, 06:26 AM
Last Post: AlekseyPython
  [pandas] Convert categorical data to numbers pradeep_as400 1 2,666 Jun-15-2019, 08:27 AM
Last Post: ThomasL
  Grab columns from multiple files, combine into one jon0852 0 2,019 Feb-12-2019, 02:53 AM
Last Post: jon0852
  Dropping all rows of multiple columns after the max of one cell Thunberd 2 2,954 Jun-01-2018, 10:18 PM
Last Post: Thunberd

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020