mlresearch.utils.image_to_dataframe

mlresearch.utils.image_to_dataframe(X, y=None, bands=None, target_feature='target')[source]

Converts an image array (height, width, bands) to a pandas dataframe (height * width, bands). If y is not None, a feature with name target_feature will be added to the dataset.

Note

Some workflows use image arrays of format (bands, width, height). In that case, you can simply transpose the image before calling this function.

Parameters:
Xarray-like of shape (h, w, b)

Matrix containing the image data.

yarray-like of shape (h, w), default=None

The target values (class labels) as integers or strings.

bandsarray-like of shape (b,), default=None

The names of the bands in the image.

target_featurestr, default=”target”

Target feature name.

Returns:
df_imagepd.DataFrame, shape (h * w, b[+1])

Dataframe with pixel coordinates (h, w) as index, counting from the top left corner.

Examples

>>> import numpy as np
>>> X = np.random.default_rng(42).random((4,5,3))
>>> y = np.random.default_rng(42).integers(0,2,4*5).reshape(4,5)
>>> image_to_dataframe(X).head(5)
...             0         1         2
... h w
... 0 0  0.773956  0.438878  0.858598
...   1  0.697368  0.094177  0.975622
...   2  0.761140  0.786064  0.128114
...   3  0.450386  0.370798  0.926765
...   4  0.643865  0.822762  0.443414
>>> image_to_dataframe(X, y, bands=["r","g", "b"], target_feature="classes").head(5)
...             r         g         b  classes
... h w
... 0 0  0.773956  0.438878  0.858598        0
...   1  0.697368  0.094177  0.975622        1
...   2  0.761140  0.786064  0.128114        1
...   3  0.450386  0.370798  0.926765        0
...   4  0.643865  0.822762  0.443414        0