الدرس 1.2 · الكود والأرقام⏱ 12 دقيقة
الفصل 1: Vectors: المعنى كاتجاه في الفضاء
تجربة: أقرب كورس
حوّلنا كل كورس لـ vector بيعدّ الكلمات (24 بُعد)، وقارنّا.
- Python 0.603، و Scratch 0.474: منطقي.
- بس الطبخ 0.354 طلع أقرب من Robotics 0.177!
- السبب: الطبخ فيه «for kids». الطريقة بتعدّ كلمات ومبتفهمش المعنى.
الشرح بالتفصيل
Python🐍 افتح في روبو بايثون
import math
from collections import Counter
docs = {
"python": "python course for kids learn coding with python games",
"scratch": "scratch course for kids drag blocks make games and stories",
"robotics": "robotics course build robots sensors motors and coding",
"cooking": "cooking class for kids bake cakes and cookies",
}
vocab = sorted({w for d in docs.values() for w in d.split()})
def vec(text): # bag of words: كام مرة كل كلمة اتكررت
c = Counter(text.split())
return [c[w] for w in vocab]
def cosine(a, b): # cos الزاوية بين الاتجاهين
dot = sum(x * y for x, y in zip(a, b))
return dot / (math.sqrt(sum(x * x for x in a)) * math.sqrt(sum(y * y for y in b)))
q = vec("coding games for kids")
print(len(vocab), "dimensions")
for name, d in sorted(docs.items(), key=lambda kv: -cosine(q, vec(kv[1]))):
print(f"{name:9} {cosine(q, vec(d)):.3f}")
النتيجة المتوقعة (من الكتاب):
24 dimensions python 0.603 scratch 0.474 cooking 0.354 robotics 0.177
Python و Scratch فوق: منطقي. بس كورس الطبخ طلع أقرب من Robotics! ليه؟ لأن الطبخ فيه «for kids» (كلمتين مشتركين)، والـ Robotics فيه «coding» بس. الطريقة دي بتعدّ كلمات، ومبتفهمش إن «robots» و «coding» من نفس العالم. ده بالظبط اللي الـ embeddings الحديثة بتصلّحه (الجزء 21).
ملاحظةالـ embeddings الحديثة بتحط «robots» و «coding» جنب بعض.

