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_kpoints uniformly at random from within the union of geometries belonging to classk.When weights are omitted every geometry / pixel is assigned weight 1, so
W_kequals the number of observations in classk.This differs from
StratifiedClassSampler, which uses the deterministic largest-remainder (Hamilton) method for allocation.- Parameters:¶
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 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
MetadataRequestencapsulating routing information.- Return type:¶
MetadataRequest
-
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:¶