Answers.Body: <p>What are you trying to achieve?</p>
  
  <p>If you need to encode it to some integer use hash table. If you are using something like linear regression or neural network it would be better to use dummy features (one-hot encoding). So for your dictionary of 5 words ("America", "Brazil", "Chile", "Denmark", "Estonia") you get 5 features (x1, x2, x3, x4, x5) which indicate if some word is equal to one in dictionary. So "Brazil" is represented by (0,1,0,0,0), "Germany" is (0,0,0,0,0). Number of features grows with number of words in dictionary making some features practically useless.</p>
  
  <p>If you are using decision trees you don't need to convert string to integer unless specific algorithm asks you to do so. Again, use hash table to do it. In R you can use factor() function.</p>
  
  <p>If you convert your string to integers and use it as single feature ("America" - 123, "Brazil" - 245), algorithm will try to find patterns in it by comparing numbers but may fail to recognize specific countries.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-22T09:55:46.263Z
Answers.LastActivityDate: 2016-11-22T09:55:46.263Z
Answers.UserDisplayName: Kronos
Answers.Body: <p>This depends a lot on what you want to achieve, but if you aim to generalise beyond the words encountered in your training data, you should consider using something like <a href="https://en.wikipedia.org/wiki/Word2vec" rel="nofollow noreferrer">word2vec</a>. </p>
  
  <p>In word2vec semantically similar words are represented by similar vectors and what's more, semantic differences translate into geometrical differences. To overuse a standard example: vec(Paris)-vec(France)+vec(Italy)=vec(Rome).</p>
  
  <p>These relationships allow the network to generalise to completely new content.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-22T10:43:50.240Z
Answers.LastActivityDate: 2016-11-22T10:43:50.240Z
Answers.UserDisplayName: BlindKungFuMaster
Answers.Body: <p>You shouldn't use a single number for the word, perhaps a number for each letter. Since B isn't the midpoint of A and C, the numbers really shouldn't be 1, 2, 3, etc. One large but effective way of converting is the letter a is 10000000000000000000000000 such that there are 26 digits, and each digit  is a letter, so 0000100000... would be E.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-27T16:41:09.420Z
Answers.LastActivityDate: 2016-11-27T16:41:09.420Z
Answers.UserDisplayName: I. Golsby
Body: <p>Let's say I have a string "America" and I want to convert it into a number to feed into a machine learning algorithm. If I use two digits for each letter, e.g. A = 01, B = 02 and so on, then the word "America" will be converted to <code>01XXXXXXXXXX01</code> (10<sup>11</sup>). This is a very high number for a <code>long int</code>, and many words longer than "America" are expected. </p>
  
  <p>How can I deal with this problem?</p>
  
  <p>Suggest an algorithm for efficient and meaningful conversions.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-11-22T07:58:53.690Z
Comments.Text: If you look at it as a number you treat it like an ordinal scale value. I think you need a hash or maybe a semantic hash.
Comments.UserDisplayName: Erhard Dinhobl
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-11-22T08:16:46.500Z
Comments.Text: Use vectors (list of numbers) to represent the words, not a single number.
Comments.UserDisplayName: Ankur
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-11-22T14:03:54.967Z
Comments.Text: Could you please clarify how these numbers will be used in the machine learning part? That might affect the answers.
Comments.UserDisplayName: Ben N
Tags: <machine-learning>
Title: How to convert string to number and number to string efficiently?
UserDisplayName: Patel Sunil
ThreadText: # How to convert string to number and number to string efficiently?
  
  (Asked by: username_0 on 2016-11-22)
  
  Let's say I have a string "America" and I want to convert it into a number to feed into a machine learning algorithm. If I use two digits for each letter, e.g. A = 01, B = 02 and so on, then the word "America" will be converted to `01XXXXXXXXXX01` (1011). This is a very high number for a `long int`, and many words longer than "America" are expected.
  
  How can I deal with this problem?
  
  Suggest an algorithm for efficient and meaningful conversions.
  
  **Tags:** machine-learning
  
  > **Comments:**
  > **username_1**: If you look at it as a number you treat it like an ordinal scale value. I think you need a hash or maybe a semantic hash.  
  > **username_2**: Use vectors (list of numbers) to represent the words, not a single number.  
  > **username_3**: Could you please clarify how these numbers will be used in the machine learning part? That might affect the answers.  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_4 on 2016-11-22. Score: 1
  
  What are you trying to achieve?
  
  If you need to encode it to some integer use hash table. If you are using something like linear regression or neural network it would be better to use dummy features (one-hot encoding). So for your dictionary of 5 words ("America", "Brazil", "Chile", "Denmark", "Estonia") you get 5 features (x1, x2, x3, x4, x5) which indicate if some word is equal to one in dictionary. So "Brazil" is represented by (0,1,0,0,0), "Germany" is (0,0,0,0,0). Number of features grows with number of words in dictionary making some features practically useless.
  
  If you are using decision trees you don't need to convert string to integer unless specific algorithm asks you to do so. Again, use hash table to do it. In R you can use factor() function.
  
  If you convert your string to integers and use it as single feature ("America" - 123, "Brazil" - 245), algorithm will try to find patterns in it by comparing numbers but may fail to recognize specific countries.
  
  
  ---
  
  ### Answer by username_5 on 2016-11-22. Score: 1
  
  This depends a lot on what you want to achieve, but if you aim to generalise beyond the words encountered in your training data, you should consider using something like [word2vec](https://en.wikipedia.org/wiki/Word2vec).
  
  In word2vec semantically similar words are represented by similar vectors and what's more, semantic differences translate into geometrical differences. To overuse a standard example: vec(Paris)-vec(France)+vec(Italy)=vec(Rome).
  
  These relationships allow the network to generalise to completely new content.
  
  
  ---
  
  ### Answer by username_6 on 2016-11-27. Score: 0
  
  You shouldn't use a single number for the word, perhaps a number for each letter. Since B isn't the midpoint of A and C, the numbers really shouldn't be 1, 2, 3, etc. One large but effective way of converting is the letter a is 10000000000000000000000000 such that there are 26 digits, and each digit is a letter, so 0000100000... would be E.
  
  
  ---
  
  