Answers.Body: <p>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 <a href="https://www.kaggle.com/camnugent/geospatial-feature-engineering-and-visualization" rel="nofollow noreferrer">kaggle post</a> has a good writeup of how to run build features for geo-spatial data.</p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-15T04:38:56.430Z
Answers.Text: The location data i have is spread far apart. So converting it to lat and lon will be better right?
Answers.UserDisplayName: Abishek
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-15T04:44:22.667Z
Answers.Text: 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.
Answers.UserDisplayName: juicedatom
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-15T04:49:22.113Z
Answers.Text: I am trying to create a anomaly detection model (https://ai.stackexchange.com/q/11218/23174).
Answers.UserDisplayName: Abishek
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-14T18:39:07.727Z
Answers.LastActivityDate: 2019-03-14T18:39:07.727Z
Answers.UserDisplayName: juicedatom
Body: <p>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.</p>
  
  <p>eg. country,state,city = IN,Maharastra,Pune = 2,5,10 (primary key in DB)</p>
  
  <p>transforming it as 002005010</p>
  
  <p>Is this approach correct? If not suggest me correct one</p>
  
Tags: <machine-learning><datasets>
Title: transform Location data into int which will be used as input to ML model
UserDisplayName: Abishek
ThreadText: # transform Location data into int which will be used as input to ML model
  
  (Asked by: username_0 on 2019-03-14)
  
  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
  
  **Tags:** machine-learning, datasets
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-03-14. Score: 1 | Accepted answer
  
  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](https://www.kaggle.com/camnugent/geospatial-feature-engineering-and-visualization) 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.
  
  > **Comments:**
  > **username_0**: The location data i have is spread far apart. So converting it to lat and lon will be better right?  
  > **username_1**: 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.  
  > **username_0**: I am trying to create a anomaly detection model (https://ai.stackexchange.com/q/11218/23174).  
  
  ---
  
  