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.

Share this article

“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

[2211].\begin{bmatrix} 2\\ 2\\ 1\\ 1 \end{bmatrix}.

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 T\mathsf{T} means transpose, which switches a row display to a column display or a column display to a row. Bold x\boldsymbol{x} names the whole vector; the ordinary symbol xjx_j names one coordinate. Thus

x=[x1x2x3x4]andxT=[x1x2x3x4]\boldsymbol{x}= \begin{bmatrix}x_1\\x_2\\x_3\\x_4\end{bmatrix} \quad\text{and}\quad \boldsymbol{x}^{\mathsf{T}}= \begin{bmatrix}x_1&x_2&x_3&x_4\end{bmatrix}

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 fjf_j names feature jj, an encoder ϕ\phi can map an item dd to

ϕ(d)=[f1(d)f2(d)⋮fn(d)]∈Rn.\phi(d)= \begin{bmatrix} f_1(d)\\ f_2(d)\\ \vdots\\ f_n(d) \end{bmatrix} \in\mathbb{R}^{n}.

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

(ai,model,data,vector).(\texttt{ai},\texttt{model},\texttt{data},\texttt{vector}).

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:

x1=count⁡(ai)=2,x2=count⁡(model)=2,x3=count⁡(data)=1,x4=count⁡(vector)=1.\begin{aligned} x_1&=\operatorname{count}(\texttt{ai})=2,\\ x_2&=\operatorname{count}(\texttt{model})=2,\\ x_3&=\operatorname{count}(\texttt{data})=1,\\ x_4&=\operatorname{count}(\texttt{vector})=1. \end{aligned}

Therefore,

ϕ(d)=[2211]∈R4.\phi(d)= \begin{bmatrix} 2\\2\\1\\1 \end{bmatrix} \in\mathbb{R}^{4}.
A fixed feature order turns four named token counts into four coordinates. Arrows preserve the contract between each label and its value.
Document-to-feature-vector pipelineThe document AI models use data, AI models use vectors is normalized and counted against the ordered features ai, model, data, and vector. The resulting column vector contains 2, 2, 1, and 1.1 · DocumentAI models use data.AI models usevectors.lowercasemodels → modelvectors → vector2 · Ordered features1 · ai22 · model23 · data14 · vector13 · Column vector2211shape 4 × 1The values travel with their labels: coordinate 3 means “count of data.”
Semantic alternative: ordered features and counts for the document
CoordinateFeatureCountMeaning
1ai2The normalized token ai appears twice.
2model2The two instances of models become model.
3data1The normalized token data appears once.
4vector1The 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 4×14\times1. 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:

  1. the number of coordinates; and
  2. the feature assigned to each coordinate.

For any two documents dad_a and dbd_b, the value at position jj must be the same kind of measurement fjf_j:

ϕ(da)j=fj(da),ϕ(db)j=fj(db).\phi(d_a)_j=f_j(d_a), \qquad \phi(d_b)_j=f_j(d_b).

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 [1,1,1,0]T[1,1,1,0]^\mathsf{T} 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 x\boldsymbol{x} belongs to R4\mathbb{R}^{4}. Mathematics usually displays its coordinates as a 4×14\times1 column matrix:

[2211]∈R4×1.\begin{bmatrix}2\\2\\1\\1\end{bmatrix}\in\mathbb{R}^{4\times1}.

Transposing that column display produces a row vector:

[2211]∈R1×4.\begin{bmatrix}2&2&1&1\end{bmatrix}\in\mathbb{R}^{1\times4}.

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.

RepresentationWritten formShapeOrientation
Column vector[2,2,1,1]T[2,2,1,1]^\mathsf{T}4×14\times1four rows, one column
Row vector[2,2,1,1][2,2,1,1]1×41\times4one row, four columns
NumPy 1-D arraynp.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

F=(ai,model,data,vector)F=(\texttt{ai},\texttt{model},\texttt{data},\texttt{vector})

and stores [2,2,1,1]T[2,2,1,1]^\mathsf{T}. Another pipeline declares

G=(data,vector,ai,model)G=(\texttt{data},\texttt{vector},\texttt{ai},\texttt{model})

but passes the same number sequence without reordering it. Under GG, 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 FF to GG must move each value with its label:

feature order1234Fai:2model:2data:1vector:1Gdata:1vector:1ai:2model:2\begin{array}{c|cccc} \text{feature order} & 1 & 2 & 3 & 4\\\hline F & \texttt{ai}:2 & \texttt{model}:2 & \texttt{data}:1 & \texttt{vector}:1\\ G & \texttt{data}:1 & \texttt{vector}:1 & \texttt{ai}:2 & \texttt{model}:2 \end{array}

so the vector in GG order is [1,1,2,2]T[1,1,2,2]^\mathsf{T}.

Two other boundary cases sharpen the definition:

  • If a document contains none of the vocabulary terms, it maps to the zero vector [0,0,0,0]T[0,0,0,0]^\mathsf{T}. Its dimension remains 4; zeros are coordinate values, not missing coordinates.
  • When n=1n=1, a row vector and column vector both have shape 1×11\times1 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 jj 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 x∈R3\boldsymbol{x}\in\mathbb{R}^{3} and the vector’s dimension is 3.

Match positions to feature labels in order:

  1. x1=4x_1=4 means four clicks;
  2. x2=0x_2=0 means zero purchases; and
  3. x3=−2x_3=-2 means the returns feature has value −2-2 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, ai

Count them in vocabulary order, not document order:

x1=count⁡(ai)=1,x2=count⁡(model)=1,x3=count⁡(data)=2,x4=count⁡(vector)=0.\begin{aligned} x_1&=\operatorname{count}(\texttt{ai})=1,\\ x_2&=\operatorname{count}(\texttt{model})=1,\\ x_3&=\operatorname{count}(\texttt{data})=2,\\ x_4&=\operatorname{count}(\texttt{vector})=0. \end{aligned}

Therefore the document vector is x=[1,1,2,0]T\boldsymbol{x}=[1,1,2,0]^\mathsf{T}. 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:

cat:3,dog:1,bird:0.\texttt{cat}:3,\qquad \texttt{dog}:1,\qquad \texttt{bird}:0.

Now read those values in the second service’s order:

(bird,cat,dog)⟶[0,3,1]T.(\texttt{bird},\texttt{cat},\texttt{dog}) \longrightarrow [0,3,1]^\mathsf{T}.

Sending [3,1,0]T[3,1,0]^\mathsf{T} 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:

ai:1,model:1,data:1,vector:0.\texttt{ai}:1,\quad \texttt{model}:1,\quad \texttt{data}:1,\quad \texttt{vector}:0.

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 R4\mathbb{R}^{4}. Each coordinate exists and has value zero:

0=[0000].\boldsymbol{0}= \begin{bmatrix}0\\0\\0\\0\end{bmatrix}.

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.

Sources

  1. Mathematics for Machine Learning companion website
  2. Mathematics for Machine Learning book PDF
  3. NumPy documentation: the N-dimensional array
  4. NumPy documentation: numpy.reshape
  5. NumPy documentation: numpy.transpose
  6. scikit-learn documentation: CountVectorizer