Spatial Feature Union
The SpatialFeatureUnion estimator shares spatial properties
with multiple transformers and concatenates the results.
The following table describes the main methods of the
SpatialFeatureUnion class.
| Method | Description |
|---|---|
fit |
Calls the fit method of all the
transformers.
|
transform |
Calls the transform method of each
transformer and concatenate the results.
|
fit_transform |
Fits all the transformers, transforms the data, and concatenates the results. |
See the SpatialFeatureUnion class in Python API Reference for Oracle Spatial AI for more information.
The following example uses the block_groups
SpatialDataFrame and SpatialFeatureUnion to
concatenate the output from two transformers. The first is a
SimpleImputer from scikit-learn, and the second is
a SpatialLagTransformer. The dataset has three columns, excluding the
geometries, so the final result contains six columns.
from oraclesai.pipeline import SpatialFeatureUnion
from oraclesai.weights import KNNWeightsDefinition
from oraclesai.preprocessing import SpatialLagTransformer
from sklearn.impute import SimpleImputer
# Define training variables
X = block_groups[['MEAN_AGE', 'HOUSE_VALUE', "MEDIAN_INCOME", "geometry"]]
# Print X
print("=========================== X =================================")
print(X.get_values()[:5,:])
# Define spatial weights
weights_definition = KNNWeightsDefinition(k=5)
# Define a Spatial Lag Transformer
spatial_lag_transformer = SpatialLagTransformer(spatial_weights_definition=weights_definition)
# Use SpatialFeatureUnion to concatenate the output from all the transformers
slag_feature_union = SpatialFeatureUnion([("imputer", SimpleImputer()),
("spatial_lag", spatial_lag_transformer)])
# Print the final result
print("\n=================== X transformed ============================")
print(slag_feature_union.fit_transform(X)[:5, :])The first three columns of the transformed data represent the output from the
SpatialImputer, and the other three represent the output from the
SpatialLagTransformer.
=========================== X =================================
[[4.75847626e+01 4.56300000e+05 5.38280000e+04]
[3.88231812e+01 8.36300000e+05 6.07240000e+04]
[4.78076096e+01 1.12630000e+06 8.25380000e+04]
[4.65636330e+01 9.60400000e+05 1.43661000e+05]
[5.11550865e+01 1.01090000e+06 1.23977000e+05]]
=================== X transformed ============================
[[4.75847626e+01 4.56300000e+05 5.38280000e+04 4.03809292e+01
6.23460000e+05 7.92068000e+04]
[3.88231812e+01 8.36300000e+05 6.07240000e+04 3.95882790e+01
8.20100000e+05 9.82008000e+04]
[4.78076096e+01 1.12630000e+06 8.25380000e+04 4.69466225e+01
1.22280000e+06 1.14899600e+05]
[4.65636330e+01 9.60400000e+05 1.43661000e+05 4.25439751e+01
1.04664000e+06 1.16867800e+05]
[5.11550865e+01 1.01090000e+06 1.23977000e+05 4.43390564e+01
1.14368000e+06 1.45833400e+05]]