I have features like state, city and location. Currently i am inserting this in respective tables in DB and tranform it using its primary key.

eg. country,state,city = IN,Maharastra,Pune = 2,5,10 (primary key in DB)

transforming it as 002005010

Is this approach correct? If not suggest me correct one


1 Answer 1


In order to accurately input location data into a machine learning model it really depends on what your goal is and what type of algorithm you are working with. If you are working with a strictly numerical algorithm and your data seems to be spread far apart, it might be easier to convert your country-state-city location to a longitude, latitude feature where the exact value is the centroid location of the given city. This kaggle post has a good writeup of how to run build features for geo-spatial data.

However, if you have only a small number of different locations per identifier (country, city, state) you could just represent them as seperate location classes and subclasses like you would for machine learning detection model. You could think "vehicle" -> "bike" -> "road-bike" similarly to "country" -> "state" -> "city". In this case, you would want to look at hierachical methods for machine learning and see some ways they represent their data. Although I would say this method is more frowned upon for larger datasets, for smaller datasets it might be a better option.

  • $\begingroup$ The location data i have is spread far apart. So converting it to lat and lon will be better right? $\endgroup$
    – Abishek
    Commented Mar 15, 2019 at 4:38
  • $\begingroup$ If it's widely spread apart and you have a lot of different data points then it might be a good idea to start there if you're trying to learn some sort of a regression on your data. $\endgroup$
    – juicedatom
    Commented Mar 15, 2019 at 4:44
  • $\begingroup$ I am trying to create a anomaly detection model (ai.stackexchange.com/q/11218/23174). $\endgroup$
    – Abishek
    Commented Mar 15, 2019 at 4:49

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