0
$\begingroup$

I am building a neural net and am at the point of working with categorical variables. Below you can see that I transformed the categorical variables into numeric variables.

#Encode "UserName"
labelencoder_X_1 = LabelEncoder()
X[:, 0] = labelencoder_X_1.fit_transform(X[:, 0])
#Encode "Token"
labelencoder_X_2 = LabelEncoder()
X[:, 1] = labelencoder_X_2.fit_transform(X[:, 1])
#Encode "ChildEXE"
labelencoder_X_3 = LabelEncoder()
X[:, 3] = labelencoder_X_3.fit_transform(X[:, 3])
#Encode "ParentEXE"
labelencoder_X_4 = LabelEncoder()
X[:, 4] = labelencoder_X_4.fit_transform(X[:, 4])
#Encode "ChildFilePath"
labelencoder_X_5 = LabelEncoder()
X[:, 5] = labelencoder_X_5.fit_transform(X[:, 5])
#Encode "ParentFilePath"
labelencoder_X_6 = LabelEncoder()
X[:, 6] = labelencoder_X_6.fit_transform(X[:, 6])

Now I am supposed to transform any that have more than 2. For instance "UserName" has "Machine Name", "User", and "Local Service". "Token" has about 10 options, "ChildEXE" has thousands etc.

X
array([[2, 0, 20788, ..., 46, 31, 24],        
[2, 0, 19088, ..., 46, 31, 24],        
[2, 0, 2840, ..., 27, 42, 15],       
 ...,        
[2, 0, 20148, ..., 17, 40, 32],        
[2, 0, 20148, ..., 47, 23, 0],        
[2, 0, 3176, ..., 48, 42, 32]], dtype=object) 

Do I use the code below several times for each variable that has more that 2?

onehotencoder = OneHotEncoder(categorical_features = [0])
X = onehotencoder.fit_transform(X).toarray()
$\endgroup$
1
$\begingroup$

I found that this works:

onehotencoder = OneHotEncoder(categorical_features = [0,1,3,4,5,6])
X = onehotencoder.fit_transform(X).toarray()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.