Crime Mapping

Crime Mapping Module

This module provides functionality for spatial and temporal crime mapping analysis. It includes utilities for creating grid points, hexagonal grids, and implementing kernel density estimation for crime hotspot detection.

class predspot.crime_mapping.KDE(tfreq, grid, start_time=False, end_time=False, bandwidth='silverman', debug=False)[source]

Bases: SpatioTemporalMapping

Kernel Density Estimation for crime hotspot detection.

Parameters:
  • tfreq (str) – Time frequency (‘M’ for monthly, ‘W’ for weekly, ‘D’ for daily)

  • grid (GeoDataFrame) – Spatial grid for analysis

  • start_time (str or datetime, optional) – Analysis start time

  • end_time (str or datetime, optional) – Analysis end time

  • bandwidth (str or float) – Bandwidth method (‘silverman’ or numeric value)

fit_grid(data_points, as_df=False)[source]

Fit the kernel density estimation to grid points.

Parameters:
  • data_points (GeoDataFrame) – Crime incident points

  • as_df (bool) – If True, return results as DataFrame

Returns:

Density estimates for grid points

Return type:

dict or DataFrame

set_fit_request(*, x: bool | None | str = '$UNCHANGED$') KDE

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

x (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for x parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_transform_request(*, data_points: bool | None | str = '$UNCHANGED$') KDE

Request metadata passed to the transform method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to transform if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to transform.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

data_points (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_points parameter in transform.

Returns:

self – The updated object.

Return type:

object

class predspot.crime_mapping.KGrid(k, tfreq)[source]

Bases: object

fit(data_points)[source]
transform(data_points)[source]
class predspot.crime_mapping.QuadratCount(tfreq, grid, filter_place_ratio=0.9)[source]

Bases: BaseEstimator, TransformerMixin

fit(x=None, y=None)[source]
set_fit_request(*, x: bool | None | str = '$UNCHANGED$') QuadratCount

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

x (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for x parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_transform_request(*, data_points: bool | None | str = '$UNCHANGED$') QuadratCount

Request metadata passed to the transform method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to transform if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to transform.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

data_points (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_points parameter in transform.

Returns:

self – The updated object.

Return type:

object

transform(data_points)[source]
class predspot.crime_mapping.QuadratCount2(tfreq, grid, filter_place_ratio=0.9)[source]

Bases: BaseEstimator, TransformerMixin

set_transform_request(*, data_points: bool | None | str = '$UNCHANGED$') QuadratCount2

Request metadata passed to the transform method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to transform if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to transform.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

data_points (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_points parameter in transform.

Returns:

self – The updated object.

Return type:

object

transform(data_points)[source]
class predspot.crime_mapping.SpatioTemporalMapping(tfreq, grid, start_time=False, end_time=False, debug=False)[source]

Bases: ABC, TransformerMixin, BaseEstimator

Abstract base class for spatio-temporal crime mapping.

Parameters:
  • tfreq (str) – Time frequency (‘M’ for monthly, ‘W’ for weekly, ‘D’ for daily)

  • grid (GeoDataFrame) – Spatial grid for analysis

  • start_time (str or datetime, optional) – Analysis start time

  • end_time (str or datetime, optional) – Analysis end time

fit(x, y=None)[source]
abstract fit_grid(data_points=None)[source]
get_time_data_chunks(data_points)[source]
get_times_no_data(chunks)[source]
set_fit_request(*, x: bool | None | str = '$UNCHANGED$') SpatioTemporalMapping

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

x (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for x parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_transform_request(*, data_points: bool | None | str = '$UNCHANGED$') SpatioTemporalMapping

Request metadata passed to the transform method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to transform if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to transform.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

data_points (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_points parameter in transform.

Returns:

self – The updated object.

Return type:

object

transform(data_points)[source]
predspot.crime_mapping.create_gridhexagonal(bbox, resolution)[source]
predspot.crime_mapping.create_gridpoints(bbox, resolution, return_coords=False, debug=False)[source]

Create a grid of points within a given bounding box.

Parameters:
  • bbox (GeoDataFrame) – Bounding box as a GeoDataFrame

  • resolution (float) – Grid cell size in kilometers

  • return_coords (bool) – If True, returns additional coordinate arrays

  • debug (bool) – Enable debug printing

Returns:

Grid points as GeoDataFrame, optionally with coordinate arrays

Return type:

GeoDataFrame or tuple

predspot.crime_mapping.create_gridsquares(city_shape, resolution=1)[source]

It constructs a grid of square cells.

Parameters:
  • city_shape (GeoDataFrame.) – Corresponds to the boundary geometry in which the grid will be formed.

  • resolution (float, default is 1.) – Space between the square cells.

predspot.crime_mapping.create_hexagon(l, x, y)[source]

Create a hexagonal polygon.

Parameters:
  • l (float) – Length of hexagon side

  • x (float) – X-coordinate of center

  • y (float) – Y-coordinate of center

Returns:

Hexagonal polygon

Return type:

Polygon