Wednesday, February 4, 2026

Modeling City Strolling Danger Utilizing Spatial-Temporal Machine Studying


After dinner in downtown San Francisco, I mentioned goodbye to buddies and pulled out my telephone to determine how you can get house. It was near 11:30 pm, and Uber estimates had been unusually lengthy. I opened Google Maps and checked out strolling instructions as a substitute. The routes had been related in distance, however I hesitated — not due to how lengthy the stroll would take, however as a result of I wasn’t positive how completely different components of the route would really feel at the moment of night time. Google Maps may inform me the quickest approach house, however it couldn’t assist reply the query I used to be truly asking: how can I filter for a route that takes me by way of safer blocks relatively than the quickest route?

Picture by Juliana Chyzhova on Unsplash

Defining the Drawback Assertion

Given a beginning location, ending location, day of the week, and time how can we predict the anticipated threat on the given strolling route? For instance, if I need to stroll from the Ferry Constructing to Decrease Nob Hill, Google Maps reveals me the next route(s):

Google Maps — strolling route from Chinatown to Market & South Van Ness. Screenshot by writer from Google Maps.

At a excessive stage, the issue I wished to unravel was this: given a beginning location, ending location, time of day, and day of week, how can we estimate the anticipated threat alongside a strolling route?

For instance, if I need to stroll from Chinatown to Market & Van Ness, Google Maps presents a pair route choices, all taking roughly 40 minutes. Whereas it’s helpful to match distance and length, it doesn’t assist reply a extra contextual query: which components of those routes are likely to look completely different relying on the time I’m making the stroll? How does the identical route examine at 9 am on a Tuesday versus 11 pm on a Saturday?

As walks get longer — or go by way of areas with very completely different historic exercise patterns — these questions turn out to be tougher to reply intuitively. Whereas San Francisco will not be uniquely unsafe in comparison with different main cities, public security remains to be a significant consideration, particularly when strolling by way of unfamiliar areas or at unfamiliar occasions. My purpose was to construct a software for locals and guests alike that provides context to those choices — utilizing historic information and machine studying to floor how threat varies throughout area and time, with out lowering town to simplistic labels.

Getting the Information + Pre-Processing

Fetching the Uncooked Dataset

The San Francisco Metropolis and County Departments publish police incident studies day by day by way of the San Francisco Open Information portal. The dataset spans from January 1, 2018 to the current and contains structured data comparable to incident class, subcategory, description, time, and site (latitude and longitude).

Provides a snapshot on how to query the San Francisco Open Data Database.
Filtered incident data from the San Francisco Open Information Portal. Screenshot by writer utilizing information from information.sfgov.org.

Categorizing Incidents Reported

One quick problem with this information is that not all incidents symbolize the identical stage or sort of threat. Treating all studies equally would blur significant variations — for instance, a minor vandalism report shouldn’t be weighted the identical as a violent incident. To deal with this, I first extracted all distinctive combos of incident class, subcategory, and outline, which resulted in somewhat over 800 distinct incident triplets.

Moderately than scoring particular person incidents immediately, I used an LLM to assign severity scores to every distinctive incident sort. This allowed me to normalize semantic variations within the information whereas conserving the scoring constant and interpretable. Every incident sort was scored on three separate dimensions, every on a 0–10 scale:

  • Hurt rating: the potential threat to human security and passersby
  • Property rating: the potential for harm or lack of property
  • Public disruption rating: the extent to which an incident disrupts regular public exercise

These three scores had been later mixed to kind an general severity sign for every incident, which may then be aggregated spatially and temporally. This method made it attainable to mannequin threat in a approach that displays each the frequency and the nature of reported incidents, relatively than counting on uncooked counts alone.

Geospatial Illustration

Offering uncooked latitude and longitude numbers is not going to add a lot worth to the ML mannequin as a result of I must group mixture incident context at a block and neighborhood-level. I wanted a technique to map a block or neighborhood to a set index to simplify characteristic engineering and construct a constant spatial mapping. Reduce to the seminal engineering weblog revealed by Uber — H3.

Uber’s H3 weblog describes how projecting an icosahedron (20-faced polyhedron) to the floor of the earth and hierarchically breaking them down into hexagonal shapes (and 12 strategically positioned pentagons) may also help tessellate the complete map. Hexagons are particular as a result of there are one of many few common polygons that kind common tessellations and its centerpoint is equidistant to it’s neighbors’, which simplifies smoothing over gradients.

Demonstrates how centerpoints on a hexagonal grid is equidistant whereas not equidistant on a square grid.
Neighbor distance comparability exhibiting unequal distances in sq. grids and uniform distances in hexagonal grids. Picture by writer.

The web site https://clupasq.github.io/h3-viewer/ is a enjoyable experiment to see what your location’s H3 Index is!

Snapshot to provide context to reader how H3 hexagonal grids are built on the SF map.
Snapshot from H3 Index Viewer at Decision 8. Screenshot by writer, utilizing the H3 library (Apache 2.0) and OpenStreetMap base map © OpenStreetMap contributors (ODbL).

Temporal Illustration

Time is simply as essential as location when modeling strolling threat. Nevertheless, naïvely encoding hour and day as integers introduces discontinuities — 23:59 and 00:00 are numerically far aside, regardless that they’re solely a minute aside in actuality.

To deal with this, I encoded time of day and day of week utilizing sine and cosine transformations, which symbolize cyclical values on a unit circle. This enables the mannequin to study that late-night and early-morning hours are temporally adjoining, and that days of the week wrap naturally from Saturday again to Sunday.

As well as, I aggregated incidents into 3-hour time home windows. Shorter home windows had been too sparse to provide dependable indicators, whereas bigger home windows obscured significant variations (for instance, early night versus late night time). Three-hour buckets struck a stability between granularity and stability, leading to intuitive durations comparable to early morning, afternoon, and late night.

Acquiring x, y-coordinates to symbolize time on a unit circle. Picture by writer

Remaining Characteristic Illustration

After preprocessing, every information level consisted of:

  • An H3 index representing location
  • Cyclically encoded hour and day options
  • An aggregated severity sign derived from historic incidents

The mannequin was then educated to foretell the anticipated threat for a given H3 cell, at a given time of day and day of week. In apply, which means that when a person opens the app and supplies a location and time, the system has sufficient context to estimate how strolling threat varies throughout close by blocks.

Coaching the Mannequin Utilizing XGBoost

Why XGBoost?

With the geospatial and temporal options prepared, I knew I wanted to leverage a mannequin which may seize non-linear patterns within the dataset whereas offering low latency to carry out inference on a number of segments in a route. XGBoost was a pure match for a pair causes:

  • Tree-based fashions are naturally strong at modeling heterogenous information — categorical spatial indices, cyclical time options, and sparse inputs can coexist with out heavy characteristic scaling or normalization
  • Characteristic results are extra interpretable than in deep neural networks, which are likely to introduce pointless opacity for tabular information.
  • Flexibility in aims and regularization made it attainable to mannequin threat in a approach that aligns with the construction of the issue.

Whereas I did think about alternate options comparable to linear fashions, random forests, and neural networks, they had been unsatisfactory as a consequence of lack of ability to seize nuance in information, excessive latency at inference time, or over-complication for tabular information. XGBoost strikes the perfect stability between efficiency and practicality.

Modeling Anticipated Danger

It’s essential to make clear earlier than we transfer on that modeling anticipated threat will not be a Gaussian drawback. When modeling incident charges within the metropolis, I seen per [H3, time] cell that:

  • a number of cells have incident depend = 0 and/or complete threat = 0
  • a number of cells have simply 1–2 incidents
  • handful cells have incidents many incidents (> 1000)
  • excessive occasions happen, however not often

These are indicators that my mannequin is neither symmetrical nor will the information factors cluster round a set imply. These properties instantly rule out widespread assumptions like usually distributed errors.

That is the place Tweedie regression turns into helpful.

What’s Tweedie Regression?

This graph shows that modeling risk, in general, is a right-skewed distribution when plotted on a log plot.
Zero-inflated information illustration in my dataset, i.e. threat fashions result in a right-skewed distribution as a consequence of uncommon and/or excessive occasions. Picture by writer

Put merely, Tweedie regression says: “Your worth is the sum of random occasions the place the variety of occasions is random and every occasion has a constructive random measurement.” This matches the crime incident mannequin completely.

Tweedie regression combines Poisson and Gamma distribution processes to mannequin the variety of incidents and the scale (threat rating) of every incident. For example:

  • Poisson course of: in window 6pm-9pm on December tenth, 2025 what number of incidents occurred in H3 index 89283082873ffff?
  • Gamma distribution: how extreme was every occasion that occurred in H3 index 89283082873ffff between 6pm-9pm on December tenth, 2025?

Why This Issues?

A concrete instance from the information illustrates why this framing is essential.
Within the Presidio, there was a single, uncommon high-severity incident that scored near 9/10. In distinction, a block close to 300 Hyde Avenue within the Tenderloin has hundreds of incidents over time, however with a decrease common severity. Tweedie breaks it down as:

Anticipated threat = E[#incidents] × E[severity]
# Presidio
E[#] ≈ ~0
E[severity] = excessive
→ Anticipated threat ≈ nonetheless ~0
# Tenderloin
E[#] = excessive
E[severity] = medium
→ Anticipated threat = massive

Subsequently, if the high-risk occasions are likely to occur extra usually in Presidio then it would modify the anticipated threat accordingly and lift the output scores. Tweedie handles the goal’s zero‑heavy, proper‑skewed distribution and the enter options we mentioned earlier simply clarify variation in that concentrate on.

Framing the Consequence

The result’s a mannequin that predicts anticipated threat, not conditional severity and never binary security labels. This distinction issues. It avoids overreacting to uncommon however excessive occasions, whereas nonetheless reflecting sustained patterns that emerge over time.

Remaining Steps + Deployments

To deliver the mannequin to life, I used the Google Maps API to construct an internet site which integrates the maps, routes, and path UI on which I can overlay colours primarily based on the chance scores. I color-coded the segments by taking a percentile of distributions in my information, i.e. rating ≤ P50 = inexperienced (secure), rating ≤ P75 = yellow (reasonably secure), rating ≤ P90 = orange (reasonably dangerous), else purple (dangerous). I additionally added a logic to re-route the person by way of a safer route if the detour will not be over 15% of the unique length. This may be tweaked, however I left it as is for now since with the San Francisco hills a 15% detour may work you numerous.

Picture by writer

I additionally deployed backend on Render and frontend on Vercel.

Placing StreetSense To Use!

And now, going again to the primary instance we checked out — the journey from Chinatown to Market & Van Ness, however now with our new mannequin + software we’ve constructed!

Right here’s how the stroll seems like at 9am on a Tuesday versus 11pm on a Saturday:

Snapshot of my app to illustrate UI/UX + navigating the website.
My software (StreetSense) — Chinatown to Market & Van Ness at 9am on a Tuesday. Screenshot by writer.
My software (StreetSense) — Chinatown to Market & Van Ness at 11pm on a Saturday. Screenshot by writer

Within the first picture, the segments in Chinatown that are inexperienced have a decrease incident and severity depend in comparison with segments that are purple and the information backs it too. The cool half concerning the second picture is that it robotically re-routes the person by way of a route which is safer at 11pm on a Saturday night time. That is the type of contextual decision-making I initially wished for — and the motivation behind constructing StreetSense.

Remaining Ideas and Potential Enhancements

Whereas the present system captures spatial and temporal patterns in historic incidents, there are clear areas for enchancment:

  • incorporating real-time indicators
  • utilizing additional floor reality information to validate and practice
    (a) if an incident was marked as 4/10 threat rating for theft and we are able to discover out by way of the San Francisco database that an arrest was made, we are able to bump it as much as a 5/10
  • make H3 index delicate to neighboring cells — Outer Richmond ~ Central Richmond so the mannequin ought to infer proximity and contextual data must be partially shared
  • Develop spatial options past H3 ID (neighbor aggregation, distance to hotspots, land‑use options).
  • deeper exploration of various strategies of dealing with of incident information + evaluations
    (a) experiment with completely different XGBoost goal capabilities comparable to Pseudo Huber Loss
    (b) Leverage hyperparameter optimization frameworks and consider completely different combos of values
    (c) experiment with neural networks
  • increasing past a single metropolis would all make the mannequin extra strong

Like all mannequin constructed on historic information, StreetSense displays previous patterns relatively than predicting particular person outcomes, and it must be used as a software for context relatively than certainty. In the end, the purpose is to not label locations as secure or unsafe, however to assist folks make extra knowledgeable, situationally conscious selections as they transfer by way of a metropolis.


Information Sources & Licensing

This challenge makes use of publicly accessible information from the San Francisco Open Information Portal:

  • San Francisco Police Division Incident Reviews
    Supply: San Francisco Open Information Portal (https://information.sfgov.org
    License: Open Information Commons Public Area Dedication and License (PDDL)

All datasets used are publicly accessible and permitted for reuse, modification, and industrial use beneath their respective open information licenses.

Acknowledgement & References

I’d prefer to thank the San Francisco Open Information workforce for sustaining high-quality public datasets that make tasks like this attainable.

Extra references that knowledgeable my understanding of the strategies and ideas used on this work embody:

My socials

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles