stevenpawley / Pyspatialml

Machine learning modelling for spatial data
GNU General Public License v3.0
145 stars 29 forks source link

Apply standardscaler to raster stack #40

Closed ggous closed 2 years ago

ggous commented 2 years ago

Hello,

I have trained a model based on a csv file.

I have used the StandardScaler from scikit.

scaler = StandardScaler() x_train = scaler.fit_transform(x_train)

I am saving the scaler:

pickle.dump(scaler, open("./scaler", "wb"))

Now, in order to predict, my test data is a stacked tif image.

I am loading:

preds = pml.Raster('./masked.tif')

How can I apply the same standardization on this raster stack?

Thanks!

stevenpawley commented 2 years ago

Hello,

Sorry for the delayed response. For cases where you would like to apply a specific function to a Raster object, other than what is supported in the provided methods like Raster.predict, the general case is to use Raster.apply.

That said, your question relating to applying a transformer directly to the Raster object is interesting and it might be nice to add a transform method to the Raster object that supports scikit-learn transformers. I'll try to get back to this over the next week or so, but for now just use Raster.apply.

stevenpawley commented 2 years ago

I've added a new method, alter which is a convenience method to apply a fitted scikit-learn transformer to a Raster object. The name alter was used because unfortunately transform is alter used extensively though the package for the CRS, and also in rasterio.

ggous commented 2 years ago

Very nice! Thanks!

Στις Σάβ, 30 Ιουλ 2022, 07:50 ο χρήστης Steven Pawley < @.***> έγραψε:

I've added a new method, alter which is a convenience method to apply a fitted scikit-learn transformer to a Raster object. The name alter was used because unfortunately transform is alter used extensively though the package for the CRS, and also in rasterio.

— Reply to this email directly, view it on GitHub https://github.com/stevenpawley/Pyspatialml/issues/40#issuecomment-1200089219, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVH7ATQMSAJQI4BRXQ5BSSTVWSYAJANCNFSM5TFU7Y4A . You are receiving this because you authored the thread.Message ID: @.***>