0
$\begingroup$

I have a location feature in a dataset. Some examples are: London, Uk; Sheefield Town, Ohio; UK ; North Carolina. etc. How to encode them into features? Is there any word embeddings suitable for such location information?

$\endgroup$
2
  • $\begingroup$ Hi @Alex_ban, welcome to the site. Can you give us a little more information on the problem you're trying to solve? There are several possibilities for encoding location depending on what you're trying to do. For example, a discrete number of cities can be encoded as 1..n, geographical information can be encoded as latitude, longitude pairs, word embeddings can be used if you're solving an NLP related task and so on. $\endgroup$ Oct 13, 2023 at 6:18
  • 1
    $\begingroup$ @brewmaster321 It is a NLP problem and location is a feature along with a text. Location feature contains city, state or country name. I cannot use a one hot encoding since if a feature for a data point is London,UK then a different datapoint can have London. Though both is same ,will have different one hot. $\endgroup$
    – Alex_ban
    Oct 15, 2023 at 11:03

1 Answer 1

1
$\begingroup$

So embeddings for cities can be either loaded from pre-existing models, or created. Some of the most popular embeddings I'm familiar with include Glove and Word2Vec. The easiest way is probably to use gensim to load existing embeddings:

from gensim.models.word2vec import Word2Vec
import gensim.downloader as api
import pandas as pd

model_w2vec_news = api.load("word2vec-google-news-300")
vec_london = model_w2vec_news["London"]

You can test if specific locations exist in the vocabulary of the model:

for city in cities_df["city"]:
    try:
        vec_city = model_w2vec_news[city]
        print(f"embedding for city {city} was found in model")
    except KeyError:
        print(f"The city {city} does not appear in this model")

It's also pretty straightforward to train your own embeddings. The gensim tutorials online live here: https://radimrehurek.com/gensim/auto_examples/index.html#core-tutorials-new-users-start-here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.