Loading W Code...
6
Topics
Level 0
Foundation
🇮🇳 Indian Context
Delhi NCR housing, document similarity for Hindi/English, and cricket analytics!
Foundation
import numpy as np
print("=" * 55)
print("VECTORS IN ML")
print("=" * 55)
# Define vectors
u = np.array([3, 4])
v = np.array([1, 2])
# Operations
print(f"\nu = {u}")
print(f"v = {v}")
print(f"\nu + v = {u + v}")
print(f"3 × u = {3 * u}")
# Dot Product
dot = np.dot(u, v)
print(f"\nu · v = {dot}")
print(f"Manual: {u[0]*v[0]} + {u[1]*v[1]} = {u[0]*v[0] + u[1]*v[1]}")
# Magnitude
mag_u = np.linalg.norm(u)
mag_v = np.linalg.norm(v)
print(f"\n‖u‖ = √({u[0]}² + {u[1]}²) = √{u[0]**2 + u[1]**2} = {mag_u:.4f}")
# Cosine Similarity
cos_sim = dot / (mag_u * mag_v)
angle = np.degrees(np.arccos(cos_sim))
print(f"\nCosine Similarity = {cos_sim:.4f}")
print(f"Angle between u and v = {angle:.1f}°")
# ML Example: Linear Prediction
print("\n" + "=" * 55)
print("ML: PREDICTION = DOT PRODUCT")
print("=" * 55)
features = np.array([1500, 3, 5]) # sqft, bedrooms, age
weights = np.array([0.05, 15, -2]) # learned weights
bias = 40
prediction = np.dot(features, weights) + bias
print(f"\nFeatures: Area={features[0]}sqft, BHK={features[1]}, Age={features[2]}yr")
print(f"Weights: {weights}")
print(f"Bias: {bias}")
print(f"Price = w·x + b = {np.dot(features, weights)} + {bias} = ₹{prediction} Lakhs")
# Cosine Similarity for NLP
print("\n📊 Indian Example: Document Similarity")
doc1 = np.array([3, 1, 0, 2]) # word counts: [cricket, movie, food, india]
doc2 = np.array([2, 0, 0, 3])
doc3 = np.array([0, 2, 3, 0])
sim_12 = np.dot(doc1, doc2) / (np.linalg.norm(doc1) * np.linalg.norm(doc2))
sim_13 = np.dot(doc1, doc3) / (np.linalg.norm(doc1) * np.linalg.norm(doc3))
print(f"Sports article vs India article: {sim_12:.4f}")
print(f"Sports article vs Food article: {sim_13:.4f}")
print(f"Higher similarity = more related content")