Spatial Column Transformer

The SpatialColumnTransformer shares spatial information with multiple transformers, applying transformations to different columns and concatenating the results.

The following table describes the main methods of the SpatialColumnTransformer class.

Method Description
fit Calls the fit method of all the transformers.
transform Calls the transform method of each transformer and concatenates the results.
fit_transform Fits all the transformers, transforms the data, and concatenates the results.

See the SpatialColumnTransformer class in Python API Reference for Oracle Spatial AI for more information.

The following example uses the block_groups SpatialDataFrame and SpatialColumnTransformer to concatenate the output from two different transformers. The first is a SimpleImputer from scikit-learn, and the second is a SpatialLagTransformer applied to HOUSE_VALUE and MEDIAN_INCOME columns. The final result contains five columns.

from oraclesai.pipeline import SpatialColumnTransformer
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 SpatialColumnTransformer to concatenate column subsets
slag_column_transformer = SpatialColumnTransformer([
    ("imputer", SimpleImputer(), ["MEAN_AGE", "HOUSE_VALUE", "MEDIAN_INCOME"]),
    ("spatial_lag", spatial_lag_transformer, ["HOUSE_VALUE",  "MEDIAN_INCOME"])])
 
# Print the final result
print("\n=================== X transformed ============================")
print(slag_column_transformer.fit_transform(X)[:5, :])

The first three columns of the transformed data represent the output of the first transformer, and the other two columns represent the output of the second transformer.

=========================== 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 6.23460000e+05
  7.92068000e+04]
 [3.88231812e+01 8.36300000e+05 6.07240000e+04 8.20100000e+05
  9.82008000e+04]
 [4.78076096e+01 1.12630000e+06 8.25380000e+04 1.22280000e+06
  1.14899600e+05]
 [4.65636330e+01 9.60400000e+05 1.43661000e+05 1.04664000e+06
  1.16867800e+05]
 [5.11550865e+01 1.01090000e+06 1.23977000e+05 1.14368000e+06
  1.45833400e+05]]