LightMySky

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.

No account needed. Progress saves in this browser.

What a learner can do afterwards

  • 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

1 · Read

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.

rooms: 2area: 75 m2district: Baytarget price: $180000
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.

Bay: 1Hill: 0Park: 0
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.

Try it together

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.

40 - 20 = 20100 - 20 = 8020 / 80 = 0.25

Use the same fitted minimum and maximum for every value in this column.

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.

split train and testfit scaler on trainingapply same scaler to both

The test set does not choose the scaling values.

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.

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.

Seven additional questions and three readings written for this dataset. Corpus records were studied for concepts and question formats; the replacement text is independently written.

More to read

Choose the meaning of a row

Before building a prediction table, finish this sentence: one prediction is about one _____. The answer might be a delivery, a plant or a cinema showing. That unit determines what each row describes.

Keep input values in the feature matrix X. Keep the value to predict in y, with one target for each feature row. A rectangular table alone does not guarantee that the pairing is correct. Sorting the inputs without sorting their targets can attach one example's answer to another example's measurements.

Columns also need meanings. A room count is a numerical quantity. A district number may be a category label, even though it uses digits. Subtracting two such labels need not describe a meaningful difference between districts.

For a numeric-input method, one-hot encoding gives each known category its own flag column. A matching flag contains 1; the others contain 0. Replacing a category column can increase the number of columns without changing the number of examples.

CSV files store fields separated by commas, often with a header naming the columns. Quoting allows a field to contain a comma. Importing a CSV does not decide which fields are features, which field is the target or what the row unit should be.

Check when an input becomes available

A bakery plans to predict its daily sales before opening. Its purchase log contains many records per day, but the prediction concerns a day's total. Build one example per day and combine records at that level.

Now give each possible input a time. Stock counted before opening and orders already received are available for the prediction. A total calculated from closing receipts is later information. Using it as an opening-time input would make the exercise differ from the prediction the bakery actually wants to make.

The sales total is still needed during training, but as the target. Keeping a value in y does not make it available as an input in X. An identifier can help join records while remaining outside the measured inputs.

Missing entries need a separate decision. Possible plans include replacing values, excluding affected rows or choosing a model that handles missing values. There is no rule that every incomplete row must always be deleted. Removing rows can change which examples remain represented.

If a replacement value is learned from the data, learn it from training rows. Held-out test rows should not choose that value. Keep any row exclusions matched between X and y.

Units, scaling and repeated measurements

Suppose a distance adds absolute differences across its feature columns. A difference of 6000 dollars contributes more than a difference of 3 schooling years when both numbers are used raw. This comparison concerns those two rows. A large income range does not mean that income differs between every pair.

Scaling changes the numerical contributions. Min-max scaling subtracts a column's training minimum and divides by its training maximum minus minimum. A constant column has a zero range, so that formula needs a separate handling rule.

Separate training and test rows before fitting the scaler. Learn its values from training rows, then apply the same calculation to both sets. Do not fit a new scaler on the test set. A later value outside the training range may produce a result below 0 or above 1.

Scaling is a preparation choice for the model and task. It does not establish which feature is most useful, and not every model requires comparable feature ranges.

Also check whether two columns repeat a measurement. If milliseconds equals seconds times 1000 in every row, the milliseconds column can be recovered exactly. It adds no independent measurement. Keeping both columns may still alter a distance because that measurement contributes through two columns.

2 · Watch

Take it off screen

Print a worksheetA4 with an answer key page for grown-ups. No screen, no internet.

Where it sits

Then practise

15 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.

Spotted a problem on this page? Tell us
Data as a Matrix: Rows, Features and the Target · Computing, ages 18 to 20 · LightMySky