TransWikia.com

How to determine population from a list of arbitrary jurisdictions (cities, states, counties, et al.)?

Mathematica Asked by uselesslemma on February 6, 2021

I am an aspiring data scientist who’s working on a passion project, namely to assign danger-level scores to police departments in the United States.

One specific metric I’m developing results from analyzing data on fatal encounter incidents with police since 2000, including the name of the specific agency involved in the death(s), which I’ve imported as strings, stripped of qualifiers like "Police Department" (among others), and arranged into a list called dept.

For example, take the first element of the list, "Mendocino County Sheriff’s Office", which then becomes "Mendocino County", the geographic area I want to know the population of. This is simple enough with semantic interpretation, i.e.,

jurisdiction = SemanticInterpretation[dept[[1]]]

(* output: Entity["AdministrativeDivision", {"MendocinoCounty", "California", 
  "UnitedStates"}] *)

To get the population:

population = AdministrativeDivisionData[jurisdiction, "Population"]

(* output: 87649 people *)

From here it’s trivial to calculate my "danger" index for that department and expand to do the calculation for all incidents.

deathCounts = Values[Counts[dept]];
dangerCalc[i_] := Module[{jur, pop, dVal},
   jur = SemanticInterpretation[dept[[i]]];
   pop = QuantityMagnitude[AdministrativeDivisionData[jur, "Population"]];
   dVal = N[deathCounts[[i]]/pop*100];
];

However, I run into problems trying to apply this same process to non-county jurisdictions (testing the first 100 entries). As an example, the second incident is Highland Park (a city). With dept[[2]] being "Highland Park", I get:

danger = {};
For[i = 1, i < 101, ++i, AppendTo[danger, dangerCalc[i]]];

(* output: 

...

AdministrativeDivisionData::notent: Highland Park is not a known entity, class, or tag for AdministrativeDivisionData. Use AdministrativeDivisionData[] for a list of entities.

... 

*)

This seems to be related to different classifications like "City", etc. I’m a total novice in this area of Mathematica, so I have little to no clue how to handle varying geographic regions uniformly. I had success with Highland Park by testing:

Interpreter["City"][depts[[2]]]

(* output: Entity["City", {"HighlandPark", "Illinois", "UnitedStates"}] *)

Obviously this doesn’t fit neatly into my current code, and given the multiplicity of different inputs, I’m unsure how to create a general-handling function accounting for all possibilities.

Mathematica’s built-in functions are so numerous, and the semantic interpretation is so sophisticated that I have no idea how to proceed. The "Entity" and other classifiers confuse me conceptually; in plain English, I want to get the population of cities, counties, states, townships, districts, etc. without knowing ahead of time which category each list element belongs to.

Can anyone more knowledgeable than myself suggest a path forward?

One Answer

Try appending " Population" after your strings and let SemanticInterpretation do the work for you?
This seems to work for the two inputs in OP:

SemanticInterpretation["Mendocino County Population"]
SemanticInterpretation["Highland Park Population"]

Out[7]= Quantity[87649, "People"]

Out[8]= Quantity[29767, "People"]

Answered by George Varnavides on February 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP