# Ways to convert textual data to numerical data

I've been looking for ways to wrangle my data which contains both text and numerical attributes.

There are of course several algorithms for numerical data, but I am looking for suggestions regarding how to deal with textual data, for instance: for sorting based on K-means clustering and predicting missing data using kNN. I would really appreciate any thoughts regarding that. I am using scikit-learn.

Do you really mean textual attribute or just Categorical attribute? e.g. if an attribute has three values $a$,$b$ and $c$ it does not mean that you need to work with text but just categories. Here I assume you have really textual attribute e.g.

| attr1: Age    | attr2: msg                            |
| ------------- |---------------------------------------|
| 45            | I do NLP                              |
| 21            | I do math                             |
| 34            | I am a mathematician who does NLP     |


In this case you have 2 options; either going for classic ways like Bag of Words representation of text data and counting frequency of terms (words/bi-grams/tri-grams/etc.) and see how it works, or try to shortcut the way if you have a specific corpus with specific info to be extracted e.g. in example above a "Keyword Extraction" after a Stemming step will give you a vocabulary of fields in each of which a person can get a value ($0,1$).

For K-means, one should have in mind that it works with numerical data. So if you want to use it, first you need to embedd your text into a n-dimensional space using text embedding algorithms (word2vec, doc2vec, etc) or even using frequency terms (as they are numbers) but the problem with text embedding is that despite you want to train your own Neural Network (which needs descent amount of data) you have to use pre-trained NNs which might not work well if your corpus is about a very specific kind of text.

The other point is that KNN algorithm is supervised and k-means is unsupervised. So be careful that you understood the underlying concepts properly otherwise you will end up with improper results.

• Thanks @Kasra. That helped me understand what I was wondering about. Commented Jan 17, 2018 at 11:52
• I am glad my friend :) Good Luck! Commented Jan 17, 2018 at 12:52

A renown method is using two layers Neural Network which transform each word to a vector. you can use word2vec library also to do this. To know more about this you can find this online article useful.

In basic term you actualy studying in "Natural Language Processing"(NLP) I advice to you "nltk" library for python. And that library have a good count vectorization tool for describe a word as number.

Also you can easily delete "stopwords" and can easily manage "stemmer" function.

It's not enough to just somehow encode everything.

That is easy: just encode everything as 0.

What you need to consider are properties of this encoding.

For example, KMeans is only meaningful when least squared errors on the encoding imply a better result in your original data. I don't know any encoding of text that has this property.

Don't just do something because you don't know anything else to do. First understand your date, then the problem, then the solution that solves the right problem. Spell out the mathematical objective for your problem, and why it is important to solve.