Math for AI course · Lesson 14 of 180
Vectors as AI Data: How Features Turn a Document Into Numbers
Learn to read row and column vectors, build a document feature vector by hand and in NumPy, and catch shape or feature-order mistakes.
“AI models use data. AI models use vectors.” A reader sees a sentence. A numerical model needs a fixed set of positions containing numbers. One simple encoding counts selected words and produces
Those four numbers are not self-explanatory. They become useful only after an
encoding rule says that position 1 counts ai, position 2 counts model,
position 3 counts data, and position 4 counts vector. The ordered numbers,
together with that coordinate meaning, form a feature vector.
By the end of this lesson, you will be able to read row and column vectors, interpret their coordinates as features, encode a small document by hand and in NumPy, and diagnose code whose values are right but whose feature order or array shape is wrong.
Learning outcome: Read row and column vectors and interpret coordinates as features.
This lesson returns to Lesson 5: Tensor Indices for AI. There, an index answered “which position?” Here, the positions travel together as one representation. The official Mathematics for Machine Learning companion develops row and column vector notation in Section 2.2. The document, encoding rule, calculation, code, diagram, and assessments below are original.
A vector needs both values and coordinate meaning
The superscript means transpose, which switches a row display to a column display or a column display to a row. Bold names the whole vector; the ordinary symbol names one coordinate. Thus
contain the same four coordinate values but have different orientations.
For AI data, coordinates usually represent agreed features rather than anonymous slots. A feature is a measurable property supplied to a model. It might be a word count, pixel intensity, temperature reading, or learned signal. If names feature , an encoder can map an item to
This equation separates two jobs. The encoder decides how to measure each feature. The vector preserves the resulting measurements in a fixed order. Every document must use the same feature at the same coordinate if a later model is to compare or combine those values consistently.
Build a document vector coordinate by coordinate
Encode this document:
AI models use data. AI models use vectors.
Use the ordered vocabulary
For this deliberately small example, the normalization rule lowercases the
text, removes punctuation, changes models to model, and changes vectors
to vector. It ignores use because that word has no coordinate in the fixed
vocabulary. Real text systems need a documented tokenizer and vocabulary; this
toy rule is visible so every count can be audited.
The normalized tokens are
ai, model, use, data, ai, model, use, vector
Now calculate one coordinate at a time:
Therefore,
| Coordinate | Feature | Count | Meaning |
|---|---|---|---|
| 1 | ai | 2 | The normalized token ai appears twice. |
| 2 | model | 2 | The two instances of models become model. |
| 3 | data | 1 | The normalized token data appears once. |
| 4 | vector | 1 | The token vectors becomes vector. |
The diagram makes the representation contract visible: every count follows its feature label into a coordinate. The column is four entries tall, so its shape is . It still represents one document, not four documents.
Why fixed coordinates make numerical learning possible
A model cannot attach one behavior to coordinate 3 if that coordinate means
data for one document and vector for another. A feature map therefore fixes
two things across examples:
- the number of coordinates; and
- the feature assigned to each coordinate.
For any two documents and , the value at position must be the same kind of measurement :
This is the mechanism behind familiar text-vectorization software. The
official scikit-learn
CountVectorizer
documentation describes a vocabulary as a mapping from terms to feature
indices. Its transform method uses that vocabulary to create a document-term
count array with shape (n_samples, n_features): one row per document and one
column per vocabulary feature.
The count vector is useful precisely because it is selective. It preserves how often the chosen features occur. It discards the original word order, sentence boundaries, and any token absent from the vocabulary. The two documents
ai model data
data model ai
both map to under this encoding. Equal vectors mean equal retained counts, not equal prose or equal meaning.
Row and column are shapes, not reading directions
The abstract vector belongs to . Mathematics usually displays its coordinates as a column matrix:
Transposing that column display produces a row vector:
The first shape has four rows and one column; the second has one row and four
columns. Neither orientation changes the coordinate-to-feature assignment.
The first entry still belongs to ai, the second to model, and so on.
| Representation | Written form | Shape | Orientation |
|---|---|---|---|
| Column vector | four rows, one column | ||
| Row vector | one row, four columns | ||
| NumPy 1-D array | np.array([2, 2, 1, 1]) | (4,) | no row/column axis yet |
That last line is an implementation boundary worth remembering. A
one-dimensional NumPy array has one axis of length 4. It is not a two-dimensional
row or column array. NumPy’s official
ndarray documentation
defines shape as a tuple containing the size of each array dimension, while
ndim counts the axes. For shape == (4,), there is only one axis.
NumPy makes orientation explicit by adding an axis
This executable example mirrors the hand calculation:
from collections import Counter
import numpy as np
document = "AI models use data. AI models use vectors."
vocabulary = ("ai", "model", "data", "vector")
raw_tokens = document.lower().replace(".", "").split()
normalization = {"models": "model", "vectors": "vector"}
tokens = [normalization.get(token, token) for token in raw_tokens]
counts = Counter(tokens)
x = np.array([counts[feature] for feature in vocabulary], dtype=np.int64)
column = x.reshape(-1, 1)
row = x.reshape(1, -1)
print("features:", vocabulary)
print("values:", x.tolist())
print("1-D shape:", x.shape)
print("column shape:", column.shape)
print("row shape:", row.shape)
print("x.T shape:", x.T.shape)
The verified output is:
features: ('ai', 'model', 'data', 'vector')
values: [2, 2, 1, 1]
1-D shape: (4,)
column shape: (4, 1)
row shape: (1, 4)
x.T shape: (4,)
The official
numpy.reshape
documentation says that a compatible reshape returns an array with a new shape
without changing its data. Here, reshape(-1, 1) asks NumPy to infer the number
of rows and create one column; reshape(1, -1) asks for one row and infers the
number of columns.
The last output exposes a common bug. The official
numpy.transpose
documentation specifies that transposing a 1-D array returns an unchanged
view. There is no second axis to swap. Use x[:, np.newaxis] or
x.reshape(-1, 1) to create shape (4, 1), and use x[np.newaxis, :] or
x.reshape(1, -1) to create shape (1, 4).
Three correct-looking arrays can describe different data
Shape is only half the contract. Feature order supplies the other half. Suppose one pipeline declares
and stores . Another pipeline declares
but passes the same number sequence without reordering it. Under , the first
coordinate claims two occurrences of data, which is false for the document.
The array has the expected length and dtype, so shape validation alone will not
catch the semantic error.
A proper change from to must move each value with its label:
so the vector in order is .
Two other boundary cases sharpen the definition:
- If a document contains none of the vocabulary terms, it maps to the zero vector . Its dimension remains 4; zeros are coordinate values, not missing coordinates.
- When , a row vector and column vector both have shape after transposition. The single-coordinate case hides the visible orientation difference, so it is a poor shape-debugging example.
The representation is ready only when values, feature labels, and expected shape agree. Lesson 15 will add vectors component by component. That operation will make sense because coordinate in both inputs refers to the same kind of signal—the invariant established here.
Check your understanding
Question 1
Let x = [4, 0, −2]ᵀ, where the ordered features are (clicks, purchases, returns). State the vector’s dimension and interpret each coordinate.
Show the step-by-step solution
There are three ordered coordinates, so and the vector’s dimension is 3.
Match positions to feature labels in order:
- means four clicks;
- means zero purchases; and
- means the returns feature has value under the stated encoding.
The negative third value may require domain context—it might be a centered or net-change feature rather than a raw count—but it does not change the vector’s dimension or coordinate order.
Question 2
Using the ordered vocabulary (ai, model, data, vector), encode “Data guides a model; data helps AI.” Count only the four vocabulary terms after lowercasing and removing punctuation.
Show the step-by-step solution
After lowercasing and removing punctuation, the relevant normalized tokens are
data, model, data, aiCount them in vocabulary order, not document order:
Therefore the document vector is . The absent vocabulary term contributes a zero value; its coordinate is still present.
Question 3
A vector [3, 1, 0]ᵀ uses feature order (cat, dog, bird). A second service expects (bird, cat, dog). What values must be sent, and why is sending [3, 1, 0]ᵀ unchanged wrong?
Show the step-by-step solution
First pair each original value with its label:
Now read those values in the second service’s order:
Sending unchanged would assert bird: 3, cat: 1, and
dog: 0. The shape would pass, but every coordinate would carry the wrong
feature meaning.
Question 4
Classify the shapes (5,), (1, 5), and (5, 1) as a NumPy 1-D array, row array, or column array. Then state which are two-dimensional.
Show the step-by-step solution
Count the entries in each shape tuple:
(5,)contains one axis of length 5, so it is a NumPy 1-D array with no explicit row or column orientation.(1, 5)contains two axes: one row and five columns. It is a row array.(5, 1)contains two axes: five rows and one column. It is a column array.
The last two are two-dimensional because their shape tuples contain two axis sizes. All three hold five scalar values, so stored value count alone does not determine orientation.
Question 5
Why does x.T fail to turn x = np.array([2, 2, 1, 1]) into a column array? Give one expression that does create shape (4, 1).
Show the step-by-step solution
The array x has shape (4,), which means it has only one axis. Transpose can
reorder existing axes, but there is no second axis to exchange with the first.
Therefore x.T.shape remains (4,).
Add an axis explicitly:
column = x[:, np.newaxis]
print(column.shape) # (4, 1)The slice selects all four values along the existing axis, while np.newaxis
creates a second axis of length 1. x.reshape(-1, 1) is an equivalent shape
construction for this example.
Question 6
Two sentences map to the same count vector [1, 1, 1, 0]ᵀ under the vocabulary (ai, model, data, vector). What can you conclude, and what can you not conclude?
Show the step-by-step solution
Coordinate equality lets us conclude that the encoder retained the same four counts for both sentences:
We cannot conclude that the original sentences use the same word order, grammar, extra words, or meaning. The count representation discards those properties. Equality of encoded vectors is equality of the retained features, not proof that the source documents are identical.
Question 7
A document contains none of the four vocabulary terms and maps to [0, 0, 0, 0]ᵀ. Has the vector lost its dimension? Explain the result and one limitation.
Show the step-by-step solution
No. The vector still has four ordered coordinates, so it remains an element of . Each coordinate exists and has value zero:
The result says only that none of the selected vocabulary features occurred under the encoder’s rules. It does not say that the document was empty or meaningless. The document may contain many out-of-vocabulary words, which this representation deliberately ignores.