-1
$\begingroup$

I have a database in which the output feature Y is categorical, for example (oversimplification)

A          B        C        Y
1.0        0.2      5.1      Car
3.0        1.1      0.1      Car
7.6        6.9      2.7      Bike
2.5        3.8      0.3      Train
6.1        9.5      8.4      Car
8.4        0.7      5.6      Train

and so on.......

I would like to run a classification algorithm like kNN, Logistic Regression or Random Forest using as the output feature the column Y, i.e., to predict which transport was used.

How could I implement that in python since the output is not numerical?

$\endgroup$
1
$\begingroup$

Assuming that yours data are in pandas dataframe, you could use Label Encoding

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df['Y'] = le.fit_transform(df['Y'])
#check labels
# le.inverse_transform(df['Y])
| improve this answer | |
$\endgroup$
1
$\begingroup$

So what you are looking for is scikit-learn's LabelEncoder which will transform categorical values (e.g. car, bike, motorbike) to numerical values (1, 2, 3).

In case you have also categorical variables as features (X-Values) in your data set, I recommend using OneHotEncoder or pandas implementation of get_dummies() when dealing with categories. LabelEncoder would imply an order of the features (e.g. responds to a survey "Would you buy Product A" - (1) Never, (2) Not likely, (3) Don't know, (4) Likely, (5) Yes)

| improve this answer | |
$\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.