<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Irakoze Deborah</title>
    <description>The latest articles on DEV Community by Irakoze Deborah (@ira_deb).</description>
    <link>https://dev.to/ira_deb</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4154031%2F059e848d-7404-4f73-8e3c-b5788cd76cde.jpg</url>
      <title>DEV Community: Irakoze Deborah</title>
      <link>https://dev.to/ira_deb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ira_deb"/>
    <language>en</language>
    <item>
      <title>My First Steps into Computer Vision: Teaching a Computer to Understand Images</title>
      <dc:creator>Irakoze Deborah</dc:creator>
      <pubDate>Tue, 13 Oct 2026 13:46:28 +0000</pubDate>
      <link>https://dev.to/ira_deb/my-first-steps-into-computer-vision-teaching-a-computer-to-understand-images-2cdk</link>
      <guid>https://dev.to/ira_deb/my-first-steps-into-computer-vision-teaching-a-computer-to-understand-images-2cdk</guid>
      <description>&lt;p&gt;My First Steps into Computer Vision: Teaching a Computer to Understand Images&lt;/p&gt;

&lt;p&gt;Have you ever wondered how your phone recognises a face, how a self-driving car detects pedestrians, or how a computer can identify objects in a photograph?&lt;/p&gt;

&lt;p&gt;These are examples of computer vision, a field of artificial intelligence that helps computers extract meaningful information from images and videos.&lt;/p&gt;

&lt;p&gt;During my introduction to computer vision, I learned that computers do not see images the same way humans do. They process numerical data, discover patterns, and use those patterns to make predictions.&lt;/p&gt;

&lt;p&gt;In this article, I will explore how computers represent images, how filters detect edges, how neural networks classify images, and how object detection can locate objects in a scene. I will also discuss some challenges that arise when applying computer vision to real-world problems.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How Does a Computer See an Image?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When humans look at a photograph, we can immediately recognise colours, people, vehicles, and other objects. A computer, however, processes an image as a collection of pixels.&lt;/p&gt;

&lt;p&gt;Each pixel contains numerical information. In a typical RGB colour image, each pixel has three values representing red, green, and blue intensity.&lt;/p&gt;

&lt;p&gt;For example, the following Python code loads an image and examines its pixel values.&lt;/p&gt;

&lt;p&gt;Python example: Reading image pixels&lt;br&gt;
from PIL import Image&lt;br&gt;
import numpy as np&lt;/p&gt;

&lt;h1&gt;
  
  
  Load an image
&lt;/h1&gt;

&lt;p&gt;image = Image.open("my_image.jpg").convert("RGB")&lt;/p&gt;

&lt;h1&gt;
  
  
  Convert the image into a NumPy array
&lt;/h1&gt;

&lt;p&gt;pixels = np.array(image)&lt;/p&gt;

&lt;h1&gt;
  
  
  Display image information
&lt;/h1&gt;

&lt;p&gt;print("Image shape:", pixels.shape)&lt;br&gt;
print("First pixel:", pixels[0, 0])&lt;br&gt;
print("Minimum value:", pixels.min())&lt;br&gt;
print("Maximum value:", pixels.max())&lt;/p&gt;

&lt;p&gt;Install the required libraries using:&lt;/p&gt;

&lt;p&gt;pip install pillow numpy&lt;/p&gt;

&lt;p&gt;The image shape gives us its height, width, and colour channels. For example, an image with dimensions 600 × 400 pixels and three colour channels contains 720,000 individual channel values.&lt;/p&gt;

&lt;p&gt;This demonstrates an important concept: an image that looks simple to us can contain a large amount of numerical information.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How Do Image Filters Find Edges?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Images contain different patterns, textures, and boundaries. Sometimes, we want to highlight particular features to make an image easier to analyse.&lt;/p&gt;

&lt;p&gt;Image filters can help us reduce noise, smooth images, sharpen details, or detect edges.&lt;/p&gt;

&lt;p&gt;One useful technique is edge detection. It identifies areas where pixel intensity changes significantly, helping reveal the boundaries of objects.&lt;/p&gt;

&lt;p&gt;For example, edge detection can highlight the outline of a vehicle, a building, or a person.&lt;/p&gt;

&lt;p&gt;Python example: Detecting edges&lt;/p&gt;

&lt;p&gt;import cv2&lt;br&gt;
import matplotlib.pyplot as plt&lt;/p&gt;

&lt;h1&gt;
  
  
  Load the image
&lt;/h1&gt;

&lt;p&gt;image = cv2.imread("my_image.jpg")&lt;/p&gt;

&lt;p&gt;if image is None:&lt;br&gt;
    raise FileNotFoundError("Image not found. Check the filename.")&lt;/p&gt;

&lt;h1&gt;
  
  
  Convert BGR to RGB for correct display
&lt;/h1&gt;

&lt;p&gt;image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)&lt;/p&gt;

&lt;h1&gt;
  
  
  Convert the image to grayscale
&lt;/h1&gt;

&lt;p&gt;gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)&lt;/p&gt;

&lt;h1&gt;
  
  
  Detect edges using the Canny algorithm
&lt;/h1&gt;

&lt;p&gt;edges = cv2.Canny(gray, 100, 200)&lt;/p&gt;

&lt;h1&gt;
  
  
  Display the original image and its edges
&lt;/h1&gt;

&lt;p&gt;plt.figure(figsize=(10, 4))&lt;/p&gt;

&lt;p&gt;plt.subplot(1, 2, 1)&lt;br&gt;
plt.imshow(image)&lt;br&gt;
plt.title("Original Image")&lt;br&gt;
plt.axis("off")&lt;/p&gt;

&lt;p&gt;plt.subplot(1, 2, 2)&lt;br&gt;
plt.imshow(edges, cmap="gray")&lt;br&gt;
plt.title("Detected Edges")&lt;br&gt;
plt.axis("off")&lt;/p&gt;

&lt;p&gt;plt.tight_layout()&lt;br&gt;
plt.show()&lt;/p&gt;

&lt;p&gt;Install the required libraries:&lt;/p&gt;

&lt;p&gt;pip install opencv-python matplotlib&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpn6muqyosin2c5lwsuyl.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpn6muqyosin2c5lwsuyl.jpg" alt=" " width="800" height="554"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This example shows how a computer can transform an ordinary photograph into a representation that highlights its boundaries.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What Is a Convolutional Neural Network?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Image filters can reveal useful features, but modern computer vision also relies heavily on deep learning.&lt;/p&gt;

&lt;p&gt;A Convolutional Neural Network (CNN) is a neural network architecture commonly used to process images. It learns patterns from training examples rather than depending entirely on manually designed rules.&lt;/p&gt;

&lt;p&gt;Early layers may learn to respond to edges and simple textures. Deeper layers can combine these features into more complex patterns.&lt;/p&gt;

&lt;p&gt;For example, a trained model may learn visual patterns that help distinguish a bus from a car.&lt;/p&gt;

&lt;p&gt;We can experiment with a pretrained image-classification model using TensorFlow.&lt;/p&gt;

&lt;p&gt;Python example: Classifying an image&lt;br&gt;
import tensorflow as tf&lt;/p&gt;

&lt;h1&gt;
  
  
  Load a pretrained MobileNetV2 model
&lt;/h1&gt;

&lt;p&gt;model = tf.keras.applications.MobileNetV2(&lt;br&gt;
    weights="imagenet"&lt;br&gt;
)&lt;/p&gt;

&lt;h1&gt;
  
  
  Load and resize the image
&lt;/h1&gt;

&lt;p&gt;image = tf.keras.utils.load_img(&lt;br&gt;
    "my_image.jpg",&lt;br&gt;
    target_size=(224, 224)&lt;br&gt;
)&lt;/p&gt;

&lt;h1&gt;
  
  
  Convert the image to an array
&lt;/h1&gt;

&lt;p&gt;image_array = tf.keras.utils.img_to_array(image)&lt;br&gt;
image_array = tf.expand_dims(image_array, axis=0)&lt;/p&gt;

&lt;h1&gt;
  
  
  Prepare the image for MobileNetV2
&lt;/h1&gt;

&lt;p&gt;image_array = tf.keras.applications.mobilenet_v2.preprocess_input(&lt;br&gt;
    image_array&lt;br&gt;
)&lt;/p&gt;

&lt;h1&gt;
  
  
  Predict the image category
&lt;/h1&gt;

&lt;p&gt;predictions = model.predict(image_array)&lt;/p&gt;

&lt;h1&gt;
  
  
  Display the three most likely categories
&lt;/h1&gt;

&lt;p&gt;results = tf.keras.applications.mobilenet_v2.decode_predictions(&lt;br&gt;
    predictions,&lt;br&gt;
    top=3&lt;br&gt;
)&lt;/p&gt;

&lt;p&gt;for _, label, score in results[0]:&lt;br&gt;
    print(f"{label}: {score:.2%}")&lt;/p&gt;

&lt;p&gt;Install TensorFlow using:&lt;/p&gt;

&lt;p&gt;pip install tensorflow&lt;/p&gt;

&lt;p&gt;The model uses pretrained ImageNet weights and returns its top three predicted categories with scores. Those scores are model outputs, not guarantees that a prediction is correct.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuzmpx6xog8drudy364ef.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuzmpx6xog8drudy364ef.jpg" alt=" " width="799" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This experiment demonstrates how a pretrained model can use learned visual patterns to make predictions about an image.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Classification, Object Detection, and Segmentation&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Computer vision includes several tasks that may appear similar but serve different purposes.&lt;/p&gt;

&lt;p&gt;Image classification&lt;/p&gt;

&lt;p&gt;Classification predicts a category for an image or image region.&lt;/p&gt;

&lt;p&gt;For example, a model might predict that a photograph contains a bus.&lt;/p&gt;

&lt;p&gt;Object detection&lt;/p&gt;

&lt;p&gt;Object detection identifies objects and estimates their locations, commonly using rectangular bounding boxes.&lt;/p&gt;

&lt;p&gt;For example, a model might identify several vehicles and people in a street photograph.&lt;/p&gt;

&lt;p&gt;Image segmentation&lt;/p&gt;

&lt;p&gt;Segmentation assigns labels to individual pixels or regions, depending on the task. It can help distinguish the road from vehicles, pedestrians, and other parts of a scene.&lt;/p&gt;

&lt;p&gt;These tasks answer different questions:&lt;/p&gt;

&lt;p&gt;Classification: What is in the image?&lt;br&gt;
Detection: What objects are present, and where are they?&lt;br&gt;
Segmentation: Which pixels belong to each object or region?&lt;br&gt;
Python example: Detecting objects with YOLO&lt;/p&gt;

&lt;p&gt;YOLO is a family of object-detection models designed to identify objects and their locations in images.&lt;/p&gt;

&lt;p&gt;from ultralytics import YOLO&lt;/p&gt;

&lt;h1&gt;
  
  
  Load a pretrained object-detection model
&lt;/h1&gt;

&lt;p&gt;model = YOLO("yolo11n.pt")&lt;/p&gt;

&lt;h1&gt;
  
  
  Run detection on an image
&lt;/h1&gt;

&lt;p&gt;results = model("my_image.jpg")&lt;/p&gt;

&lt;h1&gt;
  
  
  Display the detection results
&lt;/h1&gt;

&lt;p&gt;results[0].show()&lt;/p&gt;

&lt;h1&gt;
  
  
  Print each detected object's label and confidence
&lt;/h1&gt;

&lt;p&gt;for box in results[0].boxes:&lt;br&gt;
    class_id = int(box.cls[0])&lt;br&gt;
    confidence = float(box.conf[0])&lt;br&gt;
    label = model.names[class_id]&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(f"{label}: {confidence:.2%}")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Install Ultralytics using:&lt;/p&gt;

&lt;p&gt;pip install ultralytics&lt;/p&gt;

&lt;p&gt;The pretrained model weights may download when you run the example for the first time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhiw5v3p6wtxe9s2ve4up.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhiw5v3p6wtxe9s2ve4up.jpg" alt=" " width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Seeing those boxes makes the difference between classification and object detection easier to understand. The model does not simply predict what might be in the photograph; it also estimates where detected objects are located.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Why Do Computer Vision Models Make Mistakes?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One important lesson is that computer vision models are not perfect.&lt;/p&gt;

&lt;p&gt;A model might confuse objects that look similar, miss an object in poor lighting, or perform poorly on images that differ from its training data.&lt;/p&gt;

&lt;p&gt;For example, an object detector trained on clear photographs may struggle with a blurry image taken at night.&lt;/p&gt;

&lt;p&gt;Confidence thresholds also affect detection results. A lower threshold can produce more detections, including incorrect ones. A higher threshold can remove weak predictions but may also cause genuine objects to be missed.&lt;/p&gt;

&lt;p&gt;This means that a model's output should be evaluated rather than accepted automatically.&lt;/p&gt;

&lt;p&gt;When developing computer vision applications, it is important to test different conditions, examine incorrect predictions, and understand the limitations of the model.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Why Does Local Data Matter?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Computer vision can support many useful applications in African communities.&lt;/p&gt;

&lt;p&gt;In agriculture, models could help researchers identify plant diseases. In transportation, detection systems could help analyse traffic. In healthcare, computer vision could assist qualified professionals in examining medical images.&lt;/p&gt;

&lt;p&gt;However, a model trained mainly on images from other regions may not perform equally well on local data.&lt;/p&gt;

&lt;p&gt;Differences in lighting, road conditions, clothing, crop varieties, building styles, and camera quality can affect its predictions.&lt;/p&gt;

&lt;p&gt;This makes local data collection and testing important. A model should be evaluated using representative examples from the environment where it will actually be used.&lt;/p&gt;

&lt;p&gt;For sensitive applications such as healthcare, predictions also need appropriate professional oversight.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What I Learned&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;My introduction to computer vision helped me connect several important ideas.&lt;/p&gt;

&lt;p&gt;First, images can be represented as numerical arrays. Second, filters can highlight useful visual features. Third, CNNs learn patterns from data, while classification, detection, and segmentation solve different problems.&lt;/p&gt;

&lt;p&gt;I also learned that computer vision is not only about building a model. It involves preparing data, testing predictions, identifying mistakes, and understanding how the system behaves in real-world situations.&lt;/p&gt;

&lt;p&gt;The most interesting part is seeing how mathematical operations and code can turn image pixels into useful information.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;Computer vision is an exciting field because it gives computers the ability to analyse images and videos in increasingly useful ways.&lt;/p&gt;

&lt;p&gt;From examining individual pixel values to detecting objects with pretrained models, each step reveals another part of how machines process visual information.&lt;/p&gt;

&lt;p&gt;There is still much to learn, especially about improving model accuracy, working with diverse datasets, and developing responsible applications.&lt;/p&gt;

&lt;p&gt;I look forward to exploring more computer vision projects and understanding how these techniques can help solve practical problems in my community.&lt;/p&gt;

&lt;p&gt;What would you choose for your first computer vision project: image classification, object detection, or segmentation? Share your thoughts in the comments!&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>computervision</category>
    </item>
    <item>
      <title>Teaching a Computer Word Meaning Without a Dictionary</title>
      <dc:creator>Irakoze Deborah</dc:creator>
      <pubDate>Thu, 01 Oct 2026 15:16:58 +0000</pubDate>
      <link>https://dev.to/ira_deb/teaching-a-computer-word-meaning-without-a-dictionary-2ogo</link>
      <guid>https://dev.to/ira_deb/teaching-a-computer-word-meaning-without-a-dictionary-2ogo</guid>
      <description>&lt;p&gt;When I started learning Natural Language Processing (NLP), one question bugged me: &lt;strong&gt;how does a computer understand words?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The honest answer is that it doesn't. Computers only understand numbers. So before a model can work with language, we have to turn words into numbers, and the way we do it makes a huge difference. That is what &lt;strong&gt;word embeddings&lt;/strong&gt; are about.&lt;/p&gt;

&lt;p&gt;In this post I'll explain what they are, show tiny examples you can run in seconds with plain Python, and look at how Word2Vec learns them.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a word embedding?
&lt;/h2&gt;

&lt;p&gt;A word embedding is a word written as a list of numbers (a vector):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat → [0.21, 0.85, -0.13, 0.42, ...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nobody types these numbers in by hand. They are &lt;strong&gt;learned from text&lt;/strong&gt;. The big idea is that words used in similar contexts get similar numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The cat eats fish.
The dog eats meat.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"cat" and "dog" both eat things, so their vectors end up close together. A machine-learning model can then treat them as related instead of as complete strangers. That is why embeddings matter: the model can reuse what it knows about "cat" when it meets "dog".&lt;/p&gt;

&lt;h2&gt;
  
  
  The old way: one-hot encoding
&lt;/h2&gt;

&lt;p&gt;The simplest approach gives each word a vector with a single &lt;code&gt;1&lt;/code&gt; and the rest &lt;code&gt;0&lt;/code&gt;. Let's test how "related" these vectors are. The function below multiplies matching positions and adds them up (a dot product):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;similarity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cat vs dog:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;similarity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cat vs car:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;similarity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cat vs cat:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;similarity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat vs dog: 0
cat vs car: 0
cat vs cat: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A word is only similar to itself. To one-hot encoding, "cat" is as unrelated to "dog" as it is to "car". There is also a practical problem: with a 100,000-word vocabulary, every vector would have 100,000 slots, almost all of them zeros. Embeddings fix both issues by using short, dense vectors (usually 50 to 300 numbers) where distance carries meaning.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why context is so powerful
&lt;/h2&gt;

&lt;p&gt;Before any machine learning, let's see the core idea. This code collects the words that appear in the same sentence as each word:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;sentences&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;the cat eats fish&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;the cat drinks milk&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;the dog eats meat&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;the dog drinks water&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;the car needs fuel&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;sentence&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sentences&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;words&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sentence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setdefault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;discard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cat &amp;amp; dog share:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cat&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;dog&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cat &amp;amp; car share:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cat&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;car&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat &amp;amp; dog share: ['drinks', 'eats', 'the']
cat &amp;amp; car share: ['the']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"cat" and "dog" share three context words, while "cat" and "car" share only one. Just by looking at neighbours, we discovered that cats are closer to dogs than to cars.&lt;/p&gt;

&lt;h2&gt;
  
  
  Word2Vec in 30 seconds
&lt;/h2&gt;

&lt;p&gt;Word2Vec takes this same idea and scales it up. It slides a small &lt;strong&gt;window&lt;/strong&gt; over text and learns from the words nearby. It comes in two flavours:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Skip-gram:&lt;/strong&gt; given a center word, predict its neighbours. (&lt;code&gt;cat&lt;/code&gt; → &lt;code&gt;the&lt;/code&gt;, &lt;code&gt;eats&lt;/code&gt;, &lt;code&gt;fish&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CBOW (Continuous Bag of Words):&lt;/strong&gt; given the neighbours, predict the center word. (&lt;code&gt;the ___ eats fish&lt;/code&gt; → &lt;code&gt;cat&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While the model gets better at these predictions, it adjusts its internal vectors. The useful by-product is that words appearing in similar contexts end up with similar vectors. With millions of sentences, famous results like king − man + woman ≈ queen start to appear.&lt;/p&gt;

&lt;h2&gt;
  
  
  Build a tiny embedding yourself
&lt;/h2&gt;

&lt;p&gt;Real Word2Vec libraries are heavy, so let's build the core idea in plain Python. Each word becomes a vector of counts: how often every other word appears in a sentence with it. Then we compare vectors with &lt;strong&gt;cosine similarity&lt;/strong&gt; (1 means same direction, 0 means nothing in common).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;sentences&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;the cat eats fish&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;the cat drinks milk&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;the dog eats meat&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;the dog drinks water&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;the car needs fuel&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;vocab&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sentences&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;()})&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;vector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;counts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;vocab&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sentences&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;words&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                    &lt;span class="n"&gt;counts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;counts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;vocab&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;cosine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;dot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;dot&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;vocab:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vocab&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cat vector:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;vector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cat&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cat vs dog:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cosine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cat&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;vector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;dog&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cat vs car:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cosine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cat&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;vector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;car&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vocab: ['car', 'cat', 'dog', 'drinks', 'eats', 'fish', 'fuel', 'meat', 'milk', 'needs', 'the', 'water']
cat vector: [0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 2, 0]
cat vs dog: 0.75
cat vs car: 0.41
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike one-hot encoding, "cat" is now much closer to "dog" (0.75) than to "car" (0.41), and nobody told the program what any word means. Word2Vec goes further: instead of raw counts, it learns short dense vectors by predicting words from their neighbours, on millions of sentences.&lt;/p&gt;

&lt;h2&gt;
  
  
  What surprised me
&lt;/h2&gt;

&lt;p&gt;The model is &lt;strong&gt;never told what a word means&lt;/strong&gt;. There is no dictionary, no labels, and no "cat = animal". It only sees which words sit near which, and relationships appear on their own. I assumed a computer would need every word explained to it, so this was the most unintuitive part of the topic for me, and the most exciting once it clicked.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I struggled with
&lt;/h2&gt;

&lt;p&gt;I tried to install &lt;code&gt;gensim&lt;/code&gt;, the popular Word2Vec library, and it failed on Windows with a "Microsoft Visual C++ 14.0 is required" build error. Instead of fighting the installer, I built a small count-based version in plain Python to show the same idea. It was a good reminder that understanding the concept matters more than the library.&lt;/p&gt;

&lt;h2&gt;
  
  
  Your turn: challenges
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Add 3 more sentences about animals. Does cat vs dog go up?&lt;/li&gt;
&lt;li&gt;[ ] Add a sentence like "the car needs oil". How does cat vs car change?&lt;/li&gt;
&lt;li&gt;[ ] Try &lt;code&gt;vector("fish")&lt;/code&gt; vs &lt;code&gt;vector("milk")&lt;/code&gt;. Are they similar?&lt;/li&gt;
&lt;li&gt;[ ] On a machine where it installs, try &lt;code&gt;pip install gensim&lt;/code&gt; and compare with Word2Vec.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Share your numbers in the comments, I'd love to compare!&lt;/p&gt;

&lt;h2&gt;
  
  
  Ideas to explore next
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;GloVe and FastText: how do they differ from Word2Vec?&lt;/li&gt;
&lt;li&gt;Famous analogies like king − man + woman ≈ queen&lt;/li&gt;
&lt;li&gt;Plotting word vectors in 2D with PCA&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Dive into Deep Learning, &lt;em&gt;Word Embedding (word2vec)&lt;/em&gt;: &lt;a href="https://d2l.ai/chapter_natural-language-processing-pretraining/word2vec.html" rel="noopener noreferrer"&gt;https://d2l.ai/chapter_natural-language-processing-pretraining/word2vec.html&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>machinelearning</category>
      <category>nlp</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
