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"


2 Answers 2


If the order of words doesn't matter in the description of the stone, you could use a bag of words model. You don't need the hashing trick 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')) 
    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.

  • $\begingroup$ 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? $\endgroup$ Apr 24 at 14:20
  • $\begingroup$ What do you mean by running time in this context? $\endgroup$ Apr 24 at 14:32
  • $\begingroup$ 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) = nk1 + nk2 + 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. $\endgroup$ Apr 24 at 16:58
  • 1
    $\begingroup$ 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). $\endgroup$ Apr 24 at 20:05
  • $\begingroup$ @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? $\endgroup$ Apr 30 at 7:34

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.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .