Lab 4 of 4about 45 minutes
Nearest digits, in fewer numbers
It uses what you learned in SVD and low-rank approximation and High-dimensional space.
Start here
Run itThe 1,797 handwritten digits, each a row of 64 numbers, with the digit it shows in labels. distances gives every row's distance to every other row at once, as a 1,797 by 1,797 table: row i, column j is how far digit i is from digit j.
1One digit's nearest
Run itThe video's board: digit 3's distance to every digit, then the six nearest. Run the cell.
The first is digit 3 itself, at 0. The next five are all threes.
2Every digit's nearest
Your turnNow every digit at once. np.fill_diagonal puts infinity where a digit meets itself, so no digit is its own nearest. Keep the position of each digit's nearest other digit in nearest, then the share of digits whose nearest shows the same digit in share.
Hint
argmin(axis=1) gives the position of the smallest number in each row. Then compare labels at those positions with labels.
One way to do it
far = distances(digits)
np.fill_diagonal(far, np.inf)
nearest = far.argmin(axis=1)
share = (labels[nearest] == labels).mean()
print(round(share, 3))3Digits against random points
Your turnRandom points of 64 numbers are all about as far apart. Write nearest_to_farthest(rows): for each row, its nearest other row's distance divided by its farthest, then the median of those, the typical row's, with np.median. Compare the digits with random rows of 64 numbers from 0 to 16, the digits' own range.
Hint
Take the farthest before you fill the diagonal with infinity. Then the smallest of each row, divided by its farthest, and the median of all of them.
One way to do it
def nearest_to_farthest(rows):
far = distances(rows)
farthest = far.max(axis=1)
np.fill_diagonal(far, np.inf)
return np.median(far.min(axis=1) / farthest)
rng = np.random.default_rng(0)
random_rows = rng.random((1797, 64)) * 16
print(round(nearest_to_farthest(digits), 2), round(nearest_to_farthest(random_rows), 2))4The digits' strongest directions
Your turnFrom SVD and low-rank approximation
Take the average digit away from every digit, then split the result with SVD. full_matrices=False keeps only what a matrix of 64 columns needs: without it, U is a 1,797 by 1,797 matrix that nobody needs. Draw the singular values: they fall, strongest first.
Hint
np.linalg.svd takes the matrix, and full_matrices=False after it.
One way to do it
centered = digits - digits.mean(axis=0)
U, S, Vt = np.linalg.svd(centered, full_matrices=False)
print(U.shape, S.shape, Vt.shape, S[:5].round())
plt.plot(S)5Ten numbers for each digit
Your turnFrom SVD and low-rank approximation
Keep each digit as its numbers along the 10 strongest directions: the first 10 columns of U, each times its singular value, as on the video's board. Keep them in kept. Each direction is a row of Vt, not a column, and centered @ Vt[:10].T gives the same numbers.
Hint
U[:, :10] takes the first ten columns, and * S[:10] scales each by its strength.
One way to do it
kept = U[:, :10] * S[:10]
print(kept.shape)6The nearest, in ten numbers
On your ownFrom SVD and low-rank approximation and High-dimensional space
On your own now. Find each digit's nearest other digit again, from kept alone, and keep the share whose nearest shows the same digit in share_10.
Hint
Step 2, with kept in place of digits.
One way to do it
far_10 = distances(kept)
np.fill_diagonal(far_10, np.inf)
share_10 = (labels[far_10.argmin(axis=1)] == labels).mean()
print(round(share_10, 3))7The fewest numbers
On your ownFrom SVD and low-rank approximation and High-dimensional space
How few numbers a digit can you keep, and still find the same digit as the nearest 95 times in 100? Try 1, 2, 3 and on, and keep the first count that reaches 0.95 in fewest.
Hint
Put step 6 in a function of the count, and loop the count up from 1 until the share reaches 0.95.
One way to do it
def share_with(count):
far = distances(U[:, :count] * S[:count])
np.fill_diagonal(far, np.inf)
return (labels[far.argmin(axis=1)] == labels).mean()
fewest = next(count for count in range(1, 65) if share_with(count) >= 0.95)
print(fewest, round(share_with(fewest), 3))Run on Pyodide 314, Python 3.14, NumPy 2.4, scikit-learn 1.8 and matplotlib 3.10. Checked on .
