geovalidate.MultinomialSampler

class geovalidate.MultinomialSampler(n_samples=500, quasi_random=None, random_state=None)[source]

Sample n_samples points via two-stage multinomial allocation.

Stage 1 – allocate sample counts across classes: Sum weights within each labels group to get a per-class total weight W_k. Draw class sample counts jointly from

(n_1, n_2, …, n_K) ~ Multinomial(n_samples, W_k / ΣW_k)

so counts are stochastic but always sum exactly to n_samples.

Stage 2 – place points within each class: Sample n_k points uniformly at random from within the union of geometries belonging to class k.

When weights are omitted every geometry / pixel is assigned weight 1, so W_k equals the number of observations in class k.

This differs from StratifiedClassSampler, which uses the deterministic largest-remainder (Hamilton) method for allocation.

Parameters:
n_samples : int, default 500

Total number of points to generate.

quasi_random : str or None

Low-discrepancy sequence for within-class coordinate generation.

random_state : int, RandomState instance, or None

Examples

GeoDataFrame path:

pts = MultinomialSampler(n_samples=500).sample(
    gdf.geometry, gdf["lc_class"], gdf["area_ha"]
)

Raster path – read bands yourself:

with rasterio.open("landcover.tif") as ds:
    pts = MultinomialSampler(n_samples=500).sample(
        ds, ds.read(1), ds.read(2)   # class band, weight band
    )
__init__(n_samples=500, quasi_random=None, random_state=None)[source]

Methods

__init__([n_samples, quasi_random, random_state])

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

sample(geometry[, labels, weights])

Generate multinomially allocated class samples.

set_params(**params)

Set the parameters of this estimator.

get_metadata_routing()[source]

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:

routing – A MetadataRequest encapsulating routing information.

Return type:

MetadataRequest

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters:
deep : bool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params – Parameter names mapped to their values.

Return type:

dict

sample(geometry, labels=None, weights=None)[source]

Generate multinomially allocated class samples.

Parameters:
geometry : GeoSeries | GeoDataFrame | rasterio.DatasetReader | path-like

Spatial source.

labels : array-like of shape (n,) or 2-D ndarray, required

Class label per geometry (GDF) or 2-D integer class array with shape (nrows, ncols) (raster, e.g. ds.read(1)).

weights : array-like of shape (n,) or 2-D ndarray, optional

Non-negative weight per geometry (GDF) or per pixel (raster). When None, all weights default to 1 so class counts are proportional to class size.

Returns:

Columns: geometry, class_label.

Return type:

geopandas.GeoDataFrame

set_params(**params)[source]

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**params : dict

Estimator parameters.

Returns:

self – Estimator instance.

Return type:

estimator instance