Copy docker run --rm zchencow/innozverse-python:latest python3 -c "
import numpy as np
# Step 3: np.einsum — tensor contractions
print('=== einsum ===')
# Dot product: sum over shared index j
A = np.array([[1.0, 2.0], [3.0, 4.0]])
B = np.array([[5.0, 6.0], [7.0, 8.0]])
print('A @ B (einsum ij,jk->ik):')
print(np.einsum('ij,jk->ik', A, B)) # == A @ B
# Outer product: i,j -> ij (no shared index)
prices = np.array([864.0, 49.99, 99.99])
discounts = np.array([0.05, 0.10, 0.15])
outer = np.einsum('i,j->ij', prices, discounts)
print(f'Outer product (prices × discounts):\n{np.round(outer, 2)}')
# Trace (diagonal sum)
print(f'Trace of A: {np.einsum(\"ii\", A)} (== {np.trace(A)})')
# Batch matrix multiply: (B, M, K) × (B, K, N) → (B, M, N)
batch = np.random.rand(4, 3, 5)
weights = np.random.rand(4, 5, 2)
result = np.einsum('bik,bkj->bij', batch, weights)
print(f'Batch matmul: {batch.shape} × {weights.shape} → {result.shape}')
# Step 4: np.vectorize
@np.vectorize
def categorise(price: float, stock: int) -> str:
if stock == 0: return 'oos'
if price > 500: return 'premium'
if price > 100: return 'mid'
return 'budget'
prices = np.array([864.0, 49.99, 99.99, 29.99, 1299.0])
stocks = np.array([15, 80, 0, 0, 5 ])
cats = categorise(prices, stocks)
print()
print(f'=== vectorize ===')
print(f'Categories: {cats}')
# Step 5: Structured arrays
print()
print('=== Structured Arrays ===')
dtype = np.dtype([
('id', 'u4'), # uint32
('name', 'U20'), # unicode string, max 20 chars
('price', 'f8'), # float64
('stock', 'i4'), # int32
('rating', 'f4'), # float32
])
products = np.array([
(1, 'Surface Pro', 864.0, 15, 4.8),
(2, 'Surface Pen', 49.99, 80, 4.6),
(3, 'Office 365', 99.99, 999, 4.5),
(4, 'USB-C Hub', 29.99, 0, 4.2),
(5, 'Surface Book', 1299.0, 5, 4.9),
], dtype=dtype)
print(f'dtype size: {products.dtype.itemsize} bytes/record')
print(f'All names: {products[\"name\"]}')
print(f'Mean price: \${products[\"price\"].mean():.2f}')
# Sorting by field
by_price = np.sort(products, order='price')
print(f'By price: {by_price[\"name\"]}')
# Filter
in_stock_mask = products['stock'] > 0
expensive = products[in_stock_mask & (products['price'] > 100)]
print(f'Expensive in-stock: {expensive[\"name\"]}')
# Step 6: Linear algebra for ML-like operations
print()
print('=== Linear Algebra ===')
# Least squares: fit price = a * rating + b
ratings = products['rating'].astype(float)
prices_ = products['price'].astype(float)
A = np.column_stack([ratings, np.ones(len(ratings))])
result = np.linalg.lstsq(A, prices_, rcond=None)
a, b = result[0]
print(f'Price ≈ {a:.1f} × rating + {b:.1f}')
for r, p in zip(ratings, prices_):
pred = a*r + b
print(f' rating={r} actual=\${p:.2f} predicted=\${pred:.2f}')
# Step 7: Custom ufunc pattern
print()
print('=== Custom Operations ===')
# frompyfunc wraps a Python function into a ufunc
def discount_fn(price, tier):
rates = {'premium': 0.15, 'mid': 0.10, 'budget': 0.05}
return price * (1 - rates.get(tier, 0))
# Use with regular arrays via vectorize
pf = np.vectorize(discount_fn)
prices_arr = np.array([864.0, 49.99, 99.99, 29.99, 1299.0])
tiers_arr = np.array(['premium', 'budget', 'mid', 'budget', 'premium'])
final = pf(prices_arr, tiers_arr).astype(float)
print(f'Final prices: {np.round(final, 2)}')
# Step 8: Capstone — product analytics engine
print()
print('=== Capstone: Analytics Engine ===')
np.random.seed(2026)
N = 10_000
# Simulate product and sales data as arrays (columnar — fast!)
product_ids = np.arange(N)
prices_c = np.random.choice([29.99, 49.99, 99.99, 199.99, 864.0, 1299.0], N)
stocks_c = np.random.randint(0, 200, N)
ratings_c = np.round(np.random.uniform(3.0, 5.0, N), 1)
sales_30d = np.random.randint(0, 100, N)
categories_c = np.random.choice(['Laptop','Accessory','Software','Hardware'], N)
# Vectorized computations — no Python loops
values = prices_c * stocks_c
revenue_30d = prices_c * sales_30d
discount_map = np.where(prices_c > 500, 0.15, np.where(prices_c > 100, 0.10, 0.05))
final_prices = np.round(prices_c * (1 - discount_map), 2)
# Boolean masks
in_stock_c = stocks_c > 0
top_sellers = sales_30d >= np.percentile(sales_30d, 90)
high_rated = ratings_c >= 4.5
print(f'Products analyzed: {N:,}')
print(f'In stock: {in_stock_c.sum():,} ({in_stock_c.mean()*100:.1f}%)')
print(f'Top sellers (90p): {top_sellers.sum():,}')
print(f'High rated (≥4.5): {high_rated.sum():,}')
print(f'Total inventory: \${values.sum():,.2f}')
print(f'Revenue 30d: \${revenue_30d.sum():,.2f}')
print(f'Avg price: \${prices_c.mean():.2f}')
print(f'Avg discount: {discount_map.mean()*100:.1f}%')
# Cross-filter: in stock + top seller + high rated
elite = in_stock_c & top_sellers & high_rated
print(f'Elite products: {elite.sum():,}')
if elite.sum() > 0:
print(f' Avg elite price: \${prices_c[elite].mean():.2f}')
print(f' Avg elite rating: {ratings_c[elite].mean():.2f}')
print(f' Elite revenue: \${revenue_30d[elite].sum():,.2f}')
"