Contents

Python NumPy For Your Grandma - 6.5 Challenge: Table Tennis

Setup

After a series of poor life choices, you find yourself managing a recreational table tennis league. There are 10 participants and in an effort to make the first round of matchups as exciting as possible, you develop a model that predicts the score difference for every possible pair of players. That is, you produce a 10x10 matrix where (i,j) represents your prediction for player i’s score minus player j’s score if they were to compete. Given this matrix, determine the “best” matchups for round one - the matchups that minimize the sum of squared point differentials.

import numpy as np

generator = np.random.default_rng(0)
score_diffs = np.round(generator.uniform(low=-15, high=15, size=(10,10)), 2)
np.fill_diagonal(score_diffs, np.nan)
score_diffs[np.triu_indices(10, k=1)] = -score_diffs[np.tril_indices(10, k=-1)]
print(score_diffs)
## [[   nan  -9.48  14.15  11.27  -5.65   3.33  10.95  -2.15   5.34  -2.83]
##  [  9.48    nan   4.86  -8.61   7.82 -11.29  13.24   4.92   2.86   9.04]
##  [-14.15 -11.27    nan  12.28  -2.41   6.04  -5.16  -3.87 -12.81   1.79]
##  [  5.65  -3.33 -10.95    nan -13.64   0.     2.24  -3.61  -7.73   0.08]
##  [  2.15  -5.34   2.83  -4.86    nan  -0.88  -8.57   2.56  -7.03  -6.33]
##  [  8.61  -7.82  11.29 -13.24  -4.92    nan -12.96 -12.82 -14.04  14.56]
##  [ -2.86  -9.04 -12.28   2.41  -6.04   5.16    nan -10.91 -14.44 -13.72]
##  [  3.87  12.81  -1.79  13.64  -0.    -2.24   3.61    nan  10.54 -14.18]
##  [  7.73  -0.08   0.88   8.57  -2.56   7.03   6.33  12.96    nan -11.7 ]
##  [ 12.82  14.04 -14.56  10.91  14.44  13.72 -10.54  14.18  11.7     nan]]

Solution

# How to represent all possible schedules?
# [(0,1), (2,3), (4,5), (6,7), (8,9)]
# [(0,1), (2,3), (4,5), (6,8), (7,9)]
# [(0,1), (2,3), (4,5), (6,9), (7,8)]

# Consider array with [0,1, 2,3, 4,5, 6,7, 8,9]
# Consider all possible permutations
# Pick out permutations where the value at position 0 is less than the value at position 1, val @ 2 < val @ 3, ...

# Build all permutations of [0, 1, 2, .. 9]
from itertools import permutations
perms = np.array(list(permutations([0,1,2,3,4,5,6,7,8,9])))

# Partition into two matrices representing player 1 and player 2
p1 = perms[:, [0,2,4,6,8]]
p2 = perms[:, [1,3,5,7,9]]

# Only retain matchups where player 1 < player 2
keeps = np.all(p1 < p2, axis=1)
p1 = p1[keeps]
p2 = p2[keeps]

# Build a matrix where (i,j) gives the expected point differential
# for the jth pairing in the ith schedule
point_diffs = score_diffs[p1, p2]

# Calculate sum of squared point differentials
schedule_scores = np.sum(point_diffs**2, axis=1)

# Identify the best schedule
best_idx = np.argmin(schedule_scores)
best_schedule = np.vstack((p1[best_idx], p2[best_idx]))

print(best_schedule)
## [[0 3 1 2 4]
##  [7 6 8 9 5]]

Course Curriculum

  1. Introduction
    1.1 Introduction
  2. Basic Array Stuff
    2.1 NumPy Array Motivation
    2.2 NumPy Array Basics
    2.3 Creating NumPy Arrays
    2.4 Indexing 1-D Arrays
    2.5 Indexing Multidimensional Arrays
    2.6 Basic Math On Arrays
    2.7 Challenge: High School Reunion
    2.8 Challenge: Gold Miner
    2.9 Challenge: Chic-fil-A
  3. Intermediate Array Stuff
    3.1 Broadcasting
    3.2 newaxis
    3.3 reshape()
    3.4 Boolean Indexing
    3.5 nan
    3.6 infinity
    3.7 random
    3.8 Challenge: Love Distance
    3.9 Challenge: Professor Prick
    3.10 Challenge: Psycho Parent
  4. Common Operations
    4.1 where()
    4.2 Math Functions
    4.3 all() and any()
    4.4 concatenate()
    4.5 Stacking
    4.6 Sorting
    4.7 unique()
    4.8 Challenge: Movie Ratings
    4.9 Challenge: Big Fish
    4.10 Challenge: Taco Truck
  5. Advanced Array Stuff
    5.1 Advanced Array Indexing
    5.2 View vs Copy
    5.3 Challenge: Population Verification
    5.4 Challenge: Prime Locations
    5.5 Challenge: The Game of Doors
    5.6 Challenge: Peanut Butter
  6. Final Boss
    6.1 as_strided()
    6.2 einsum()
    6.3 Challenge: One-Hot-Encoding
    6.4 Challenge: Cumulative Rainfall
    6.5 Challenge: Table Tennis
    6.6 Challenge: Where’s Waldo
    6.7 Challenge: Outer Product

Additional Content

  1. Python Pandas For Your Grandpa
  2. Neural Networks For Your Dog
  3. Introduction To Google Colab