Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Vector vs raster in machine learning: geometry, pixels and feature vectors

Last updated: 5 Oct 20266 min read
tutorial
BeginnerBy AITrove Editorial

Vector geometry describes shapes with coordinates; raster data records values in a fixed grid. An ML feature vector is a third concept: an ordered list of numeric features.

Separate the three meanings

A delivery-zone boundary represented by polygon coordinates is vector geometry. A satellite tile or scanned receipt is raster data because values occupy rows and columns of cells. In machine learning, a feature vector is an ordered numeric array for one example; it can be built from either source. The word “vector” therefore needs context. Location contracts specify units and coordinate systems for geometry, while a feature vector specifies feature order and transformation.

Choose a representation for the task

Use vector geometry when exact boundaries, vertices and topology matter, such as whether a pickup point lies inside a service zone. Use a raster for dense measurements across space, such as image pixels, temperature cells or a segmentation mask. In a receipt model, a rectangular field annotation may be stored as four coordinates, while the scanned page and pixel-level target mask are rasters. Annotation geometry must remain aligned with the image revision.

Understand conversion loss

Rasterizing a polygon assigns each cell a class or value at a selected resolution. Narrow edges can disappear and boundary positions shift when cell size changes. Vectorizing a mask approximates its pixel boundary with coordinates, but cannot recover detail that was never sampled. Save resolution, origin, coordinate frame and class semantics with the output. A model trained at one scale may misread masks generated at another; resizing labels requires an appropriate categorical interpolation method.

Apply it in an ML pipeline

For a warehouse coverage model, retain service areas as vector polygons for eligibility tests, then sample an aligned raster of demand density to create image-like features. For a scanned invoice, store the document raster, text-box coordinates and field values separately. A numerical feature vector might combine OCR confidence, page dimensions and merchant history; it is not a vector drawing. Feature availability still determines whether each value may enter a real prediction.

Check one concrete box

In a 47-column by 29-row receipt image, a half-open box from x=8 to x=31 and y=6 to y=19 covers 23 columns and 13 rows, or 299 pixels. Convert it to a mask, then verify the count and alignment against the image. If a resized image is used, transform the box coordinates first; copying the old box onto the new grid silently corrupts the training label.

Implementation

python
def box_to_mask(left, top, right, bottom, width, height):
    if min(width, height) < 0 or left > right or top > bottom:
        raise ValueError("invalid image or box")
    mask = [[0] * width for _ in range(height)]
    x_start, x_end = max(0, min(width, left)), max(0, min(width, right))
    y_start, y_end = max(0, min(height, top)), max(0, min(height, bottom))
    for pixel_row in range(y_start, y_end):
        mask[pixel_row][x_start:x_end] = [1] * max(0, x_end - x_start)
    return mask

receipt_mask = box_to_mask(8, 6, 31, 19, 47, 29)
assert sum(map(sum, receipt_mask)) == 299

Performance and operating cost

Allocating a W-by-H mask costs O(W × H) time and space; writing a bounded box adds O(A) work for A covered cells. A sparse polygon uses space proportional to its vertices, but point-in-polygon tests and rasterization have their own costs. At large image sizes, tile or stream masks instead of allocating full-resolution copies for every annotation.

Common Mistakes

  • Do not call a numerical feature vector a vector graphic.
  • Do not compare raster masks without matching resolution, origin and class meaning.
  • Do not assume rasterization preserves a narrow boundary exactly.

Read next

Continue the workflow: Principal components and variance retention.

machine-learning
guide
Storage details