Big Query

K-Nearest Neighbor Classify

Height(in cms)Weight(in kgs)T Shirt Size
15858M
15859M
15863M
16059M
16060M
16361M
16064L
16364L
16561L
16562L
16565L
16862L
16863L
16866L
17063L
17064L
17068L

Euclidean Distance

$$d(x,y)=\sum{(x_i - y_i)^2}$$

Predict

New customer named 'Monica' has height 161cm and weight 61kg

Height(in cms)Weight(in kgs)T Shirt SizeDistanceSrNO
15858M4.2
15859M3.6
15863M3.6
16059M2.23
16060M1.41
16361M2.23
16064L2.02
16364L3.25
16561L3.6
16562L4.0
16565L4.1
16862L5.7
16863L7.1
16866L7.3
17063L8.6
17064L9.5
17068L11.4

Predict With SQL Big Query

DECLARE H INT64 DEFAULT 161;
DECLARE W INT64 DEFAULT 61;
WITH CTE AS(
  SELECT Shop.Distance(Height, Weight, H, W) AS Dist, Size  FROM Shop.TShirt ORDER BY Dist LIMIT 5
)
SELECT Size FROM CTE GROUP BY Size ORDER BY COUNT(*) DESC LIMIT 1;

Function Predict

CREATE OR REPLACE FUNCTION Shop.PredictKNN(H INT64, W INT64)
RETURNS STRING
AS(
  (WITH CTE AS(
      SELECT Shop.Distance(Height, Weight, H, W) AS Dist, Size  FROM Shop.TShirt ORDER BY Dist LIMIT 5
    )
    SELECT Size FROM CTE GROUP BY Size ORDER BY COUNT(*) DESC LIMIT 1)
);

Procedure Score