Appendix Q — Air quality and house price model inference
Wrap into a custom class dealing with the lag computation.
import pickleimport geopandas as gpdimport libpysalfrom demoland_engine.indicators import Model
/var/folders/2f/fhks6w_d0k556plcv3rfmshw0000gn/T/ipykernel_3696/2799141644.py:2: UserWarning: Shapely 2.0 is installed, but because PyGEOS is also installed, GeoPandas will still use PyGEOS by default for now. To force to use and test Shapely 2.0, you have to set the environment variable USE_PYGEOS=0. You can do this before starting the Python process, or in your code before importing geopandas:
import os
os.environ['USE_PYGEOS'] = '0'
import geopandas
In a future release, GeoPandas will switch to using Shapely by default. If you are using PyGEOS directly (calling PyGEOS functions on geometries from GeoPandas), this will then stop working and you are encouraged to migrate from PyGEOS to Shapely 2.0 (https://shapely.readthedocs.io/en/latest/migration_pygeos.html).
import geopandas as gpd
/Users/martin/mambaforge/envs/demoland/lib/python3.11/site-packages/libpysal/weights/weights.py:172: UserWarning: The weights matrix is not fully connected:
There are 3 disconnected components.
warnings.warn(message)
Create object.
aqm = Model(W, air_quality)
Save the custom predictor class to a pickle.
withopen(f"{data_folder}/models/air_quality_predictor.pickle", "wb") as f: pickle.dump(aqm, f)
Q.1.1 England-wide model
Load the sklearn model
withopen(f"{data_folder}/models/air_quality_model_nc_urbanities.pickle", "rb") as f: air_quality = pickle.load(f)
/Users/martin/mambaforge/envs/demoland/lib/python3.11/site-packages/libpysal/weights/weights.py:172: UserWarning: The weights matrix is not fully connected:
There are 3 disconnected components.
warnings.warn(message)
Create object.
aqm = Model(W, air_quality)
Save the custom predictor class to a pickle.
withopen(f"{data_folder}/models/air_quality_predictor_nc_urbanities.pickle", "wb") as f: pickle.dump(aqm, f)
Q.2 House price
Load the sklearn model
withopen(f"{data_folder}/models/house_price_model.pickle", "rb") as f: house_price = pickle.load(f)
withopen(f"{data_folder}/models/house_price_predictor.pickle", "wb") as f: pickle.dump(hpm, f)
Q.2.1 England-wide model
Load the sklearn model
withopen(f"{data_folder}/models/house_price_model_england_no_london.pickle", "rb") as f: house_price = pickle.load(f)
Create a wrapper class computing the lag.
hpm = Model(W, house_price)
Save the custom predictor class to a pickle.
withopen(f"{data_folder}/models/house_price_predictor_england_no_london.pickle", "wb") as f: pickle.dump(hpm, f)
Q.3 Using the class for prediction
To use the class for prediction, load the pickle and call predict on a data frame with explanatory variables (either default or reflecting a scenario).
withopen(f"{data_folder}/models/air_quality_predictor.pickle", "rb") as f: aqm2 = pickle.load(f)