Answers.Body: <p>If the order of words doesn't matter in the description of the stone, you could use a <a href="https://en.wikipedia.org/wiki/Bag-of-words_model" rel="nofollow noreferrer">bag of words model</a>. You don't need the <a href="https://en.wikipedia.org/wiki/Feature_hashing" rel="nofollow noreferrer">hashing trick</a> because there's likely only a small fixed set of words used to describe stones. Let's call this set of words the &quot;vocabulary&quot;, and denote <span class="math-container">$N$</span> its size.</p>
  <p>You assign each word an index beforehand, and then, for each stone description, you populate a vector <span class="math-container">$V \in \mathbb{R}^N$</span>, with <span class="math-container">$V_i =$</span> number of times the ith word in the vocabulary appears in the stone description. Then, <span class="math-container">$V$</span> is the input to your neural network.</p>
  <p>For example, if <span class="math-container">$N = 10$</span> and &quot;natural&quot; is the word with index 3, &quot;fancy&quot; with index 0, &quot;deep&quot; with index 8, &quot;yellowish&quot; with index 2 and &quot;brown&quot; with index 6, the description &quot;Natural Fancy Deep Yellowish Brown&quot; becomes <span class="math-container">$V=[1, 0, 1, 1, 0, 0, 1, 0, 1, 0]$</span>.</p>
  <p>If the description of the stone contains an arbitrary number of words and the order matters, I would do the following. With <span class="math-container">$N$</span> the size of your vocabulary, for each word in the description do <span class="math-container">$\text{input} = \text{hash(word)} \mod N$</span>. For example, in Python</p>
  <pre><code>import hashlib
  input = []
  for word in description:
    hashed_word = hashlib.sha1(word.encode('utf-8')) 
    input.append(
      int(hashed_word.hexdigest(), 16)
  </code></pre>
  <p>Then, I would use <span class="math-container">$\text{input}$</span> as the input to an RNN. This way, you can handle descriptions with arbitrary lengths.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-24T14:20:25.187Z
Answers.Text: Ah, I know of BoW and word embeddings. But I guess I would have to use a 2-Gram, as Fancy Blue Natural Green != Fancy Green Natural Blue.
  
  But I think your answer may be correct; But what about the running time as opposed to a hashing function?
Answers.UserDisplayName: GeorgeWTrump
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-24T14:32:54.587Z
Answers.Text: What do you mean by running time in this context?
Answers.UserDisplayName: Raphael Lopez Kaufman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-24T16:58:26.963Z
Answers.Text: Hmm... Never mind, I think I misunderstood your implementation. Essentially it is O(n), as we iterate over the description once, then over the BoW with our hashfunction which (I presume) is O(1): So basically T(n) = n*k1 + n*k2 + n*k3, right? This seems managable.
  
  All I am worried about is getting a fast as possible solution, as I will be dealing with literal millions of data points, each with multiple (10+) features.
Answers.UserDisplayName: GeorgeWTrump
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-24T20:05:41.183Z
Answers.Text: What do k1, k2, etc. refer to? If you embed a description with N words in it, then you spend O(N) if we assume that hashing is O(1).
Answers.UserDisplayName: Raphael Lopez Kaufman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-30T07:34:23.683Z
Answers.Text: @Rpahael I have an issue with your BoW approach: Wouldn't this mean, I would have to keep a constant dictionary available to train the model? If I keep changing what each row of the vector means, the ML wouldn't be able to learn?
  
  Also, this doesn't seem like a very compact way of representing the colours; For a 2-gram representation (Natural blue is not the same as natural green,) I would need V^2. Where V (AFAIK now) is at least 16 to represent the colour alone. 
  
  The approximate size of the input vector minus the colours is 8. This does not seem like a good solution?
Answers.UserDisplayName: GeorgeWTrump
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-04T19:15:52.720Z
Answers.Text: Yes, you'd need to keep the dictionary around, but that's not so much data. If that's a constraint, then you should use the hashing trick I described. What prevents you from using the RNN approach? That's more general than the 2-gram representation
Answers.UserDisplayName: Raphael Lopez Kaufman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-24T10:33:03.503Z
Answers.LastActivityDate: 2022-04-24T10:39:36.590Z
Answers.LastEditDate: 2022-04-24T10:39:36.590Z
Answers.UserDisplayName: Raphael Lopez Kaufman
Answers.Body: <p>Based on my own experience, where I have tried to predict something that varies and is not exact (e.g. water, hot, cold, tepid, not quite so hot etc...) perhaps a dynamic of fuzzy logic could be applied. So you have something that is slightly more green or slightly less green, for example, based on the RGB values</p>
  <p>Trying to research this myself via Google leads me mainly to papers on stock exchange prediction, but I mean you can combine the aspects of fuzzy logic with a neural network here, which might make the colours varying a lot easier to handle rather than using fuzzy logic for the entire project, just the colours.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-27T13:19:25.090Z
Answers.LastActivityDate: 2022-04-27T13:19:25.090Z
Answers.UserDisplayName: jamiecropley
Body: <p>Say I want to predict the price of a gemstone based on its colour.</p>
  <p>I have two options:</p>
  <ul>
  <li>averaging over its colour on an RGB scale, or</li>
  <li>using its textual description.</li>
  </ul>
  <p>If I was to choose the latter, how would I go about feeding this to my neural network?</p>
  <p>Priori knowledge: Usually a gemstone is defined by its colour and the &quot;degree&quot; of this colour: Example fancy bright green.</p>
  <p>Here I could obviously let every combination of colour and degree be its own value in the one-hot vector. To implement this I could use some sort of hash function, if this makes sense, how specifically would I make a hash function that could do this?</p>
  <p>If this solution doesn't make sense, what would you suggest?</p>
  <p>Tough example of data:
  &quot;Natural Fancy Deep Yellowish Brown&quot;</p>
  
Tags: <neural-networks><machine-learning><natural-language-processing><data-preprocessing><bag-of-words>
Title: How to represent multi-label colours in one-hot encoding?
UserDisplayName: GeorgeWTrump
ThreadText: # How to represent multi-label colours in one-hot encoding?
  
  (Asked by: username_0 on 2022-04-23)
  
  Say I want to predict the price of a gemstone based on its colour.
  
  I have two options:
  
  * averaging over its colour on an RGB scale, or
  * using its textual description.
  
  If I was to choose the latter, how would I go about feeding this to my neural network?
  
  Priori knowledge: Usually a gemstone is defined by its colour and the "degree" of this colour: Example fancy bright green.
  
  Here I could obviously let every combination of colour and degree be its own value in the one-hot vector. To implement this I could use some sort of hash function, if this makes sense, how specifically would I make a hash function that could do this?
  
  If this solution doesn't make sense, what would you suggest?
  
  Tough example of data:
  "Natural Fancy Deep Yellowish Brown"
  
  **Tags:** neural-networks, machine-learning, natural-language-processing, data-preprocessing, bag-of-words
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2022-04-24. Score: 2
  
  If the order of words doesn't matter in the description of the stone, you could use a [bag of words model](https://en.wikipedia.org/wiki/Bag-of-words_model). You don't need the [hashing trick](https://en.wikipedia.org/wiki/Feature_hashing) because there's likely only a small fixed set of words used to describe stones. Let's call this set of words the "vocabulary", and denote $N$ its size.
  
  You assign each word an index beforehand, and then, for each stone description, you populate a vector $V \in \mathbb{R}^N$, with $V\_i =$ number of times the ith word in the vocabulary appears in the stone description. Then, $V$ is the input to your neural network.
  
  For example, if $N = 10$ and "natural" is the word with index 3, "fancy" with index 0, "deep" with index 8, "yellowish" with index 2 and "brown" with index 6, the description "Natural Fancy Deep Yellowish Brown" becomes $V=[1, 0, 1, 1, 0, 0, 1, 0, 1, 0]$.
  
  If the description of the stone contains an arbitrary number of words and the order matters, I would do the following. With $N$ the size of your vocabulary, for each word in the description do $\text{input} = \text{hash(word)} \mod N$. For example, in Python
  
  ```
  import hashlib
  input = []
  for word in description:
    hashed_word = hashlib.sha1(word.encode('utf-8')) 
    input.append(
      int(hashed_word.hexdigest(), 16)
  
  ```
  
  Then, I would use $\text{input}$ as the input to an RNN. This way, you can handle descriptions with arbitrary lengths.
  
  > **Comments:**
  > **username_0**: Ah, I know of BoW and word embeddings. But I guess I would have to use a 2-Gram, as Fancy Blue Natural Green != Fancy Green Natural Blue.  But I think your answer may be correct; But what about the running time as opposed to a hashing function?  
  > **username_1**: What do you mean by running time in this context?  
  > **username_0**: Hmm... Never mind, I think I misunderstood your implementation. Essentially it is O(n), as we iterate over the description once, then over the BoW with our hashfunction which (I presume) is O(1): So basically T(n) = n*k1 + n*k2 + n*k3, right? This seems managable.  All I am worried about is getting a fast as possible solution, as I will be dealing with literal millions of data points, each with multiple (10+) features.  
  > **username_1**: What do k1, k2, etc. refer to? If you embed a description with N words in it, then you spend O(N) if we assume that hashing is O(1).  
  > **username_0**: @Rpahael I have an issue with your BoW approach: Wouldn't this mean, I would have to keep a constant dictionary available to train the model? If I keep changing what each row of the vector means, the ML wouldn't be able to learn?  Also, this doesn't seem like a very compact way of representing the colours; For a 2-gram representation (Natural blue is not the same as natural green,) I would need V^2. Where V (AFAIK now) is at least 16 to represent the colour alone.   The approximate size of the input vector minus the colours is 8. This does not seem like a good solution?  
  > **username_1**: Yes, you'd need to keep the dictionary around, but that's not so much data. If that's a constraint, then you should use the hashing trick I described. What prevents you from using the RNN approach? That's more general than the 2-gram representation  
  
  ---
  
  ### Answer by username_2 on 2022-04-27. Score: 0
  
  Based on my own experience, where I have tried to predict something that varies and is not exact (e.g. water, hot, cold, tepid, not quite so hot etc...) perhaps a dynamic of fuzzy logic could be applied. So you have something that is slightly more green or slightly less green, for example, based on the RGB values
  
  Trying to research this myself via Google leads me mainly to papers on stock exchange prediction, but I mean you can combine the aspects of fuzzy logic with a neural network here, which might make the colours varying a lot easier to handle rather than using fuzzy logic for the entire project, just the colours.
  
  
  ---
  
  