FeatureArrayEstimator
sklearn_raster.FeatureArrayEstimator ¶
Bases: Generic[EstimatorType], BaseEstimator
An estimator wrapper with overriden methods for n-dimensional feature arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wrapped_estimator
|
BaseEstimator
|
An sklearn-compatible estimator. Supported methods will be overriden to work with n-dimensional feature arrays. If the estimator is already fit, it will be reset and a warning will be raised. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
n_features_in_ |
int
|
The number of features used to fit the estimator. |
n_targets_in_ |
int
|
The number of targets used to fit the estimator. |
feature_names_in_ |
list of str
|
The names of features used to fit the estimator. If the estimator is fit without feature names, e.g. using a Numpy array, this is an empty list. |
target_names_in_ |
list of str
|
The names of targets used to fit the estimator. If the estimator is fit without target names, e.g. using a Numpy array, this is an empty list. |
Examples:
Instantiate an sklearn estimator, wrap it with a FeatureArrayEstimator, then
fit as usual:
>>> from sklearn.neighbors import KNeighborsRegressor
>>> from sklearn_raster.datasets import load_swo_ecoplot
>>> X_img, X, y = load_swo_ecoplot(as_dataset=True)
>>> est = FeatureArrayEstimator(KNeighborsRegressor(n_neighbors=3)).fit(X, y)
Use the fitted FeatureArrayEstimator to generate predictions from raster data
stored in Numpy or Xarray types:
Source code in src/sklearn_raster/estimator.py
Attributes¶
Functions¶
fit ¶
fit(X, y=None, **kwargs) -> FeatureArrayEstimator[EstimatorType]
Fit an estimator from a training set (X, y).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
The training input samples. |
array-like
|
y
|
array-like of shape (n_samples,) or (n_samples, n_outputs)
|
The target values (class labels in classification, real numbers in regression). Single-output targets of shape (n_samples, 1) will be squeezed to shape (n_samples,) to allow consistent prediction across all estimators. |
None
|
**kwargs
|
dict
|
Additional keyword arguments passed to the estimator's |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
FeatureArrayEstimator
|
The wrapper around the fitted estimator. |
Source code in src/sklearn_raster/estimator.py
inverse_transform ¶
inverse_transform(X: FeatureArrayType, *, skip_nodata: bool = True, nodata_input: NoDataType | MissingType = MISSING, nodata_output: float | int = nan, ensure_min_samples: int = 1, allow_cast: bool = False, check_output_for_nodata: bool = True, keep_attrs: bool = False, inner_thread_limit: int | None = 1, **inverse_transform_kwargs) -> FeatureArrayType
Apply the inverse transformation to n-dimensional X features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Numpy or Xarray features
|
The n-dimensional input features. Array types should be in the shape (features, ...) while xr.Dataset should include features as variables. Features should correspond with those used to fit the estimator. |
required |
skip_nodata
|
bool
|
If True, NoData and NaN values will be skipped during prediction. This speeds up processing of partially masked features, but may be incompatible if estimators expect a consistent number of input samples. |
True
|
nodata_input
|
float, sequence of floats, dict, or None
|
Values encoded as NoData in the input array to be masked in the output array. These can be defined with:
A value of None disables masking for the selected feature. NaN is always
treated as NoData and does not need to be specified. When |
MISSING
|
nodata_output
|
float or int or tuple
|
NoData samples in the input features will be replaced with this value in the
output features. If the value does not fit the array dtype returned by the
estimator, an error will be raised unless |
nan
|
ensure_min_samples
|
int
|
The minimum number of samples that should be passed to |
1
|
allow_cast
|
bool
|
If True and the estimator output dtype is incompatible with the chosen
|
False
|
check_output_for_nodata
|
bool
|
If True and |
True
|
keep_attrs
|
bool
|
If True and the input is an Xarray object, the output will keep all
attributes of the input features, unless they're set by the estimator (e.g.
|
False
|
inner_thread_limit
|
int or None
|
The maximum number of threads allowed per Dask worker. Higher values can
result in nested parallelism and oversubscription, which may cause
slowdowns, stalls, or system crashes. Use caution when increasing the limit
or disabling it by setting to |
1
|
**inverse_transform_kwargs
|
Additional arguments passed to the estimator's |
{}
|
Returns:
| Type | Description |
|---|---|
Numpy or Xarray features
|
The inverse-transformed features. Array types will be in the shape (features, ...) while xr.Dataset will store features as variables. |
Source code in src/sklearn_raster/estimator.py
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 | |
kneighbors ¶
kneighbors(X: FeatureArrayType, *, n_neighbors: int | None = None, return_distance: Literal[True] = True, skip_nodata: bool = True, nodata_input: NoDataType | MissingType = MISSING, nodata_output: MaybeTuple[float | int] | None = None, ensure_min_samples: int = 1, allow_cast: bool = False, check_output_for_nodata: bool = True, keep_attrs: bool = False, inner_thread_limit: int | None = 1, **kneighbors_kwargs) -> tuple[FeatureArrayType, FeatureArrayType]
kneighbors(X: FeatureArrayType, *, n_neighbors: int | None = None, return_distance: Literal[False] = False, skip_nodata: bool = True, nodata_input: NoDataType | MissingType = MISSING, nodata_output: float | int | None = None, ensure_min_samples: int = 1, allow_cast: bool = False, check_output_for_nodata: bool = True, keep_attrs: bool = False, inner_thread_limit: int | None = 1, **kneighbors_kwargs) -> FeatureArrayType
kneighbors(X: FeatureArrayType, *, n_neighbors: int | None = None, return_distance: bool = True, skip_nodata: bool = True, nodata_input: NoDataType | MissingType = MISSING, nodata_output: MaybeTuple[float | int] | None = None, ensure_min_samples: int = 1, allow_cast: bool = False, check_output_for_nodata: bool = True, keep_attrs: bool = False, inner_thread_limit: int | None = 1, **kneighbors_kwargs) -> FeatureArrayType | tuple[FeatureArrayType, FeatureArrayType]
Find the K-neighbors of each sample in a feature array.
Returns indices of and distances to the neighbors for each pixel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Numpy or Xarray features
|
The n-dimensional input features. Array types should be in the shape (features, ...) while xr.Dataset should include features as variables. Features should correspond with those used to fit the estimator. |
required |
n_neighbors
|
int
|
Number of neighbors required for each sample. The default is the value passed to the wrapped estimator's constructor. |
None
|
return_distance
|
bool
|
If True, return distances to the neighbors of each sample. If False, return indices only. |
True
|
skip_nodata
|
bool
|
If True, NoData and NaN values will be skipped during prediction. This speeds up processing of partially masked features, but may be incompatible if estimators expect a consistent number of input samples. |
True
|
nodata_input
|
float, sequence of floats, dict, or None
|
Values encoded as NoData in the input array to be masked in the output array. These can be defined with:
A value of None disables masking for the selected feature. NaN is always
treated as NoData and does not need to be specified. When |
MISSING
|
nodata_output
|
float or int or tuple
|
NoData samples in the input features will be replaced with this value in the
output targets. If the value does not fit the array dtype returned by the
estimator, an error will be raised unless |
None
|
ensure_min_samples
|
int
|
The minimum number of samples that should be passed to |
1
|
allow_cast
|
bool
|
If True and the estimator output dtype is incompatible with the chosen
|
False
|
check_output_for_nodata
|
bool
|
If True and |
True
|
keep_attrs
|
bool
|
If True and the input is an Xarray object, the output will keep all
attributes of the input features, unless they're set by the estimator (e.g.
|
False
|
inner_thread_limit
|
int or None
|
The maximum number of threads allowed per Dask worker. Higher values can
result in nested parallelism and oversubscription, which may cause
slowdowns, stalls, or system crashes. Use caution when increasing the limit
or disabling it by setting to |
1
|
**kneighbors_kwargs
|
Additional arguments passed to the estimator's |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
neigh_dist |
Numpy or Xarray features
|
Array representing the lengths to neighbors, present if return_distance=True. Array types will be in the shape (neighbor, ...) while xr.Dataset will store neighbors as variables. |
neigh_ind |
Numpy or Xarray features
|
Array representing the nearest neighbor indices in the population matrix. Array types will be in the shape (neighbor, ...) while xr.Dataset will store neighbors as variables. |
Source code in src/sklearn_raster/estimator.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | |
predict ¶
predict(X: FeatureArrayType, *, skip_nodata: bool = True, nodata_input: NoDataType | MissingType = MISSING, nodata_output: float | int = nan, ensure_min_samples: int = 1, allow_cast: bool = False, check_output_for_nodata: bool = True, keep_attrs: bool = False, inner_thread_limit: int | None = 1, **predict_kwargs) -> FeatureArrayType
Predict target(s) for n-dimensional X features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Numpy or Xarray features
|
The n-dimensional input features. Array types should be in the shape (features, ...) while xr.Dataset should include features as variables. Features should correspond with those used to fit the estimator. |
required |
skip_nodata
|
bool
|
If True, NoData and NaN values will be skipped during prediction. This speeds up processing of partially masked arrays, but may be incompatible if estimators expect a consistent number of input samples. |
True
|
nodata_input
|
float, sequence of floats, dict, or None
|
Values encoded as NoData in the input array to be masked in the output array. These can be defined with:
A value of None disables masking for the selected feature. NaN is always
treated as NoData and does not need to be specified. When |
MISSING
|
nodata_output
|
float or int
|
NoData samples in the input features will be replaced with this value in the
output targets. If the value does not fit the array dtype returned by the
estimator, an error will be raised unless |
np.nan
|
ensure_min_samples
|
int
|
The minimum number of samples that should be passed to |
1
|
allow_cast
|
bool
|
If True and the estimator output dtype is incompatible with the chosen
|
False
|
check_output_for_nodata
|
bool
|
If True and |
True
|
keep_attrs
|
bool
|
If True and the input is an Xarray object, the output will keep all
attributes of the input features, unless they're set by the estimator (e.g.
|
False
|
inner_thread_limit
|
int or None
|
The maximum number of threads allowed per Dask worker. Higher values can
result in nested parallelism and oversubscription, which may cause
slowdowns, stalls, or system crashes. Use caution when increasing the limit
or disabling it by setting to |
1
|
**predict_kwargs
|
Additional arguments passed to the estimator's |
{}
|
Returns:
| Type | Description |
|---|---|
Numpy or Xarray features
|
The predicted values. Array types will be in the shape (targets, ...) while xr.Dataset will store targets as variables. |
Source code in src/sklearn_raster/estimator.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
predict_proba ¶
predict_proba(X: FeatureArrayType, *, skip_nodata: bool = True, nodata_input: NoDataType | MissingType = MISSING, nodata_output: float | int = nan, ensure_min_samples: int = 1, allow_cast: bool = False, check_output_for_nodata: bool = True, keep_attrs: bool = False, inner_thread_limit: int | None = 1, **predict_proba_kwargs) -> FeatureArrayType
Predict class probabilities for n-dimensional X features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Numpy or Xarray features
|
The n-dimensional input features. Array types should be in the shape (features, ...) while xr.Dataset should include features as variables. Features should correspond with those used to fit the estimator. |
required |
skip_nodata
|
bool
|
If True, NoData and NaN values will be skipped during prediction. This speeds up processing of partially masked arrays, but may be incompatible if estimators expect a consistent number of input samples. |
True
|
nodata_input
|
float, sequence of floats, dict, or None
|
Values encoded as NoData in the input array to be masked in the output array. These can be defined with:
A value of None disables masking for the selected feature. NaN is always
treated as NoData and does not need to be specified. When |
MISSING
|
nodata_output
|
float or int
|
NoData samples in the input features will be replaced with this value in the
output targets. If the value does not fit the array dtype returned by the
estimator, an error will be raised unless |
np.nan
|
ensure_min_samples
|
int
|
The minimum number of samples that should be passed to |
1
|
allow_cast
|
bool
|
If True and the estimator output dtype is incompatible with the chosen
|
False
|
check_output_for_nodata
|
bool
|
If True and |
True
|
keep_attrs
|
bool
|
If True and the input is an Xarray object, the output will keep all
attributes of the input features, unless they're set by the estimator (e.g.
|
False
|
inner_thread_limit
|
int or None
|
The maximum number of threads allowed per Dask worker. Higher values can
result in nested parallelism and oversubscription, which may cause
slowdowns, stalls, or system crashes. Use caution when increasing the limit
or disabling it by setting to |
1
|
**predict_proba_kwargs
|
Additional arguments passed to the estimator's |
{}
|
Returns:
| Type | Description |
|---|---|
Numpy or Xarray features
|
The predicted class probabilities. Array types will be in the shape (classes, ...) while xr.Dataset will store classes as variables. |
Source code in src/sklearn_raster/estimator.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | |
transform ¶
transform(X: FeatureArrayType, *, skip_nodata: bool = True, nodata_input: NoDataType | MissingType = MISSING, nodata_output: float | int = nan, ensure_min_samples: int = 1, allow_cast: bool = False, check_output_for_nodata: bool = True, keep_attrs: bool = False, inner_thread_limit: int | None = 1, **transform_kwargs) -> FeatureArrayType
Apply the transformation to n-dimensional X features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Numpy or Xarray features
|
The n-dimensional input features. Array types should be in the shape (features, ...) while xr.Dataset should include features as variables. Features should correspond with those used to fit the estimator. |
required |
skip_nodata
|
bool
|
If True, NoData and NaN values will be skipped during prediction. This speeds up processing of partially masked features, but may be incompatible if estimators expect a consistent number of input samples. |
True
|
nodata_input
|
float, sequence of floats, dict, or None
|
Values encoded as NoData in the input array to be masked in the output array. These can be defined with:
A value of None disables masking for the selected feature. NaN is always
treated as NoData and does not need to be specified. When |
MISSING
|
nodata_output
|
float or int or tuple
|
NoData samples in the input features will be replaced with this value in the
output features. If the value does not fit the array dtype returned by the
estimator, an error will be raised unless |
nan
|
ensure_min_samples
|
int
|
The minimum number of samples that should be passed to |
1
|
allow_cast
|
bool
|
If True and the estimator output dtype is incompatible with the chosen
|
False
|
check_output_for_nodata
|
bool
|
If True and |
True
|
keep_attrs
|
bool
|
If True and the input is an Xarray object, the output will keep all
attributes of the input features, unless they're set by the estimator (e.g.
|
False
|
inner_thread_limit
|
int or None
|
The maximum number of threads allowed per Dask worker. Higher values can
result in nested parallelism and oversubscription, which may cause
slowdowns, stalls, or system crashes. Use caution when increasing the limit
or disabling it by setting to |
1
|
**transform_kwargs
|
Additional arguments passed to the estimator's |
{}
|
Returns:
| Type | Description |
|---|---|
Numpy or Xarray features
|
The transformed features. Array types will be in the shape (features, ...)
while xr.Dataset will store features as variables, with the feature names
based on the estimator's |
Source code in src/sklearn_raster/estimator.py
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 | |