---
title: "Data as a Matrix: Rows, Features and the Target"
description: "For tabular supervised learning, arrange one row per example and one column per input feature. Keep targets in a separate vector with matching row order. Preparing numeric inputs can involve encoding "
canonical: https://lightmysky.com/learn/computing/data-as-a-matrix-rows-features-and-the-target-mt_KEN-TU1Gpn
source: https://lightmysky.com/learn/computing/data-as-a-matrix-rows-features-and-the-target-mt_KEN-TU1Gpn.md
retrieved: 2026-11-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# Data as a Matrix: Rows, Features and the Target

For tabular supervised learning, arrange one row per example and one column per input feature. Keep targets in a separate vector with matching row order. Preparing numeric inputs can involve encoding categories, choosing the row unit, and scaling features when the method depends on their scale.

Subject: Computing · Area: Machine Learning · Ages 18 to 20
Page: https://lightmysky.com/learn/computing/data-as-a-matrix-rows-features-and-the-target-mt_KEN-TU1Gpn

## Ready when they can

- Turn a table with categories into a numeric feature matrix and separate target vector
- Explain how measurement units affect distance and how column scaling changes it
- Choose what one row represents for a prediction question and keep its target matched to it

## Lesson: Shaping data into rows and columns

For tabular supervised learning, X is a feature matrix: one row per example and one column per input feature. A cell holds one value. A separate target vector y holds one answer per row, in the same order. For house sales, rooms, area and district can be inputs; price is the target. Here, row and column numbers start at 1. Some code instead indexes from 0.

*(drawing: These values describe one house. Rooms, area and district go in X; its price goes in y.)*

A numeric matrix needs numeric entries, though some tools accept categories directly. Here we use one-hot encoding: replace a category column with one flag column per known category. Write 1 for a match and 0 otherwise. For districts Bay, Hill and Park, a Bay row becomes (1, 0, 0). With no missing or unknown categories, exactly one flag is 1. Count the original columns needing encoding separately from the flag columns produced.

*(drawing: One district column becomes three flag columns. Keep the other feature columns.)*

For the distance rule used here, add the absolute differences in the features. An income gap of thousands can outweigh a schooling gap of a few years. Ranges alone do not tell you the contribution for every pair: equal incomes give an income gap of zero. Scaling changes the relative sizes of these contributions. Many distance-based methods benefit from scaling, but not every model needs it.

**Example.** Min-max scaling uses (value - minimum) / (maximum - minimum). Take the minimum and maximum from training rows. With minimum 20 and maximum 100, a value of 40 becomes (40 - 20) / (100 - 20) = 0.25. The endpoints map to 0 and 1. This formula needs a nonzero range; handle a constant column separately.

Let the prediction choose the row unit. A final height per plant needs one row per plant. A purchase target per visit needs one row per visit. A total-sales target per cinema showing needs one row per showing; combine visit records at that level. Put inputs in X and the matching target in y. When you sort or filter feature rows, move or remove their targets too, so each target stays with its example.

Use inputs available when the prediction is made. For a shop's daily sales prediction at opening, opening stock and advance bookings may be inputs. Closing receipts arrive later, so exclude them. Daily sales belongs in y. An ID used only as a record label is not a measured input.

Training rows fit the model; test rows check it on held-out examples. Split these sets before fitting a scaler. Learn minima and maxima from training rows only, then reuse those values for both sets and later inputs. Fitting on test rows lets held-out information affect preparation. A test value outside the training range may scale below 0 or above 1.

Changing units does not create a new measurement. If a delay column in milliseconds is exactly a seconds column multiplied by 1000, every milliseconds value can be recovered from the seconds value. It adds no independent information. Keeping both columns can still change a model's weighting, because a distance calculation may count that measurement twice.

**Recap.** Choose the row unit, encode categories for numeric inputs, keep targets matched and separate, and fit any needed scaling on training rows.

*Lesson, practice questions and flashcards written for this dataset. Corpus records informed the review of concepts and question formats; their prose and code are not reproduced.*

## Practice

15 questions on this page, each with its working shown.

## Optional connections

- [Matrix multiplication and what it represents](https://lightmysky.com/learn/mathematics/matrix-multiplication-and-what-it-represents-mt_0fJOMPlCxr)
- [Lists: Many Values Under One Name](https://lightmysky.com/learn/computing/lists-many-values-under-one-name-mt_ipB6kmyK85)
- [Vectors: Notation, Addition and Scalar Multiples](https://lightmysky.com/learn/mathematics/vectors-notation-addition-and-scalar-multiples-mt_NkqmrL7pLe)

## Opens up

- [Clustering: Structure Without Labels](https://lightmysky.com/learn/computing/clustering-structure-without-labels-mt_BAMGOlWgRY)
- [Linear Regression by Least Squares](https://lightmysky.com/learn/computing/linear-regression-by-least-squares-mt_QRRkpIrwWs)
- [Regularisation: Charging for Complexity](https://lightmysky.com/learn/computing/regularisation-charging-for-complexity-mt_RVgsDP0XzP)
