# Q3B · Co-occurrence matrix and cosine

Subject: Natural Language Processing

Exam: TEE

Reader: https://deep-learning-study-room.vercel.app/nlp/#tee/mocks/timed-final-2025/final-2025-q3b

Canonical: https://deep-learning-study-room.vercel.app/read/nlp/tee/mocks/timed-final-2025--final-2025-q3b/

TEE · actual previous paper · 100 marks · 180 minutes

Question. Consider the given paragraph: The teacher wrote on the board. The students listened to the teacher. The board was full of notes. The student asked a question. The teacher answered from the book.  
Target words: teacher, student, board, book.  
Context words: wrote, listened, notes, question.  
i. Construct a co-occurrence matrix for the target and context words. Take the window size as one whole sentence.  
ii. Evaluate the similarity between the vectors of the target words “teacher-student” and “teacher-board” using the cosine similarity.  
iii. Plot the target word vectors by considering only two dimensions: “wrote” and “notes”. [10 marks]

### Answer

### Given

Five sentences, four target rows and four context columns. Treat students and student as the same target lemma, and count each target–context pair once when both occur in a sentence.

### To find

The 4 × 4 sentence-window co-occurrence matrix, two cosine similarities and the requested 2-D plot.

### Formula: which one and why



$$
M_{ij}=\sum_{s=1}^{5}\mathbf{1}[\text{target}_i\text{ and context}_j\text{ occur in sentence }s]
$$



The indicator is 1 when both words appear in that sentence and 0 otherwise. Each matrix row is the target word’s vector: its list of four context counts.



$$
\cos(\mathbf a,\mathbf b)=\frac{\mathbf a\cdot\mathbf b}{\|\mathbf a\|\|\mathbf b\|}
$$



The dot product multiplies corresponding coordinates and adds them. The length is the square root of the sum of squared coordinates. Cosine compares directions while allowing different vector lengths.

### Solution

### Step 1. Record the contribution of each sentence

| Sentence | Targets present | Relevant context | Counts added |
| --- | --- | --- | --- |
| 1 | teacher, board | wrote | teacher–wrote +1; board–wrote +1 |
| 2 | student, teacher | listened | student–listened +1; teacher–listened +1 |
| 3 | board | notes | board–notes +1 |
| 4 | student | question | student–question +1 |
| 5 | teacher, book | none | No requested context count |

### Step 2. Construct every requested column

| Target | wrote | listened | notes | question |
| --- | --- | --- | --- | --- |
| teacher | 1 | 1 | 0 | 0 |
| student | 0 | 1 | 0 | 1 |
| board | 1 | 0 | 1 | 0 |
| book | 0 | 0 | 0 | 0 |

### Step 3. Calculate teacher–student



$$
\mathbf t\cdot\mathbf s=(1)(0)+(1)(1)+(0)(0)+(0)(1)=1
$$





$$
\|\mathbf t\|=\sqrt{1^2+1^2+0^2+0^2}=\sqrt2
$$





$$
\|\mathbf s\|=\sqrt{0^2+1^2+0^2+1^2}=\sqrt2
$$





$$
\cos(\mathbf t,\mathbf s)=\frac1{\sqrt2\sqrt2}=\frac12=0.5
$$



### Step 4. Calculate teacher–board



$$
\mathbf t\cdot\mathbf b=(1)(1)+(1)(0)+(0)(1)+(0)(0)=1
$$





$$
\|\mathbf b\|=\sqrt{1^2+0^2+1^2+0^2}=\sqrt2
$$





$$
\cos(\mathbf t,\mathbf b)=\frac1{\sqrt2\sqrt2}=0.5
$$



### Step 5. Select only the two requested plot coordinates

| Word | x = wrote | y = notes |
| --- | --- | --- |
| teacher | 1 | 0 |
| student | 0 | 0 |
| board | 1 | 1 |
| book | 0 | 0 |

001122teacherstudent, bookboardwrotenotes

The plot uses only the requested two coordinates; cosine uses all context columns.

### Final answer

Both requested cosine similarities are **0.5**. Student and book overlap at the origin of the 2-D projection, but their complete four-dimensional vectors differ. The book row is zero, so a cosine involving book would be undefined without an explicit convention.

**Yaad rakho:** Har sentence mein target aur context saath? Ek count. Cosine ke liye poori row lo, plot ke liye sirf do columns.
