0
$\begingroup$

I have a data set which contains categorical data, for example:

+-----+-----+------+
| age | sex | grade|
+-----+-----+------+
| 18  |  M  |  59  |
+-----+-----+------+
| 19  |  F  |  16  |
+-----+-----+------+  

How can i map the sex column to numerical data? generally how can i do map categorical data to numerical data in KNN? Is this a good idea to do this mapping?

$\endgroup$
0
$\begingroup$

You can map M to 0 and F to 1. In general, if you have N categories, you can map each category to a number in the range 1,2,..,N. If N=2, this is fine for a k-nearest neighbor classifier.

If N>2, this is not a good idea for a k-nearest neighbor classifier, as it makes some assumptions (e.g., that the first category is more similar to the second category than the third). If you want to use a k-nearest neighbor classifier and you have no knowledge about the meanings of the categories, the best you can do is to use a one-hot encoding. If you do have some knowledge about the meanings of the categories, the best you can do is to define a distance function or similarity function between categories, and use that directly (instead of mapping the categories to numbers).

| cite | 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.