Skip to content

Homework 1

concept and math

alt text

  1. grayscale only: \(d = 15*15 = 225\)
  2. RGB: \(d = 15*15*3 = 675\)

alt text

\[Euclidean distance = \sqrt{(1-3)^2 + (-2 - 2)^2 + (1 - 1)^2} = \sqrt{20} = 2\sqrt{5}\]

alt text

  1. the error rate is \(P(error) = 0.25 * 0.5 + 0.25 * 0.8 + 0.25 * 0.8 + 0.25 * 0.9 = 0.75\)
  2. if the classifier always return same label, it should return A, and error rate is \(0.5\)

alt text

P1 = (0.5, 0.5), P2 = (0.5, 1.5)

  1. the label point for \(\begin{bmatrix}0.5 \\ 0.5\end{bmatrix}\) is 2
    1. \(\sqrt{1 + 0} = 1\) < \(\sqrt{1 + 1} = \sqrt{2}\), so the label \(\begin{bmatrix}0.5 \\ 0.5\end{bmatrix}\) is closer, lebel = 2
    2. the label \(\begin{bmatrix}0.5 \\ 1.5\end{bmatrix}\) is closer, lebel = 1
    3. it will never predict class 3
    4. for a point (x,y), \(l(P_1)^2 = (x - 0.5)^2 + (y - 0.5)^2\), \(l(P_2)^2 = (x - 0.5)^2 + (y - 1.5)^2\), so \(P_1 < P_2\) when \(y < 1\), \(P_1 > P_2\) when \(y > 1\), and \(P_1 = P_2\) when \(y = 1\). So the decision boundary is the line \(y = 1\). therefore, the error rate is 0.5

alt text alt text

the point is located at (3.5, 4.5) the most close point is (4,4), with label star the next close point is (2,4), square the third close point is (4,6), square 4th close point is (2,6), star 5th close point is (4,2), square

  1. 1-NN: star
  2. 3-NN: square
  3. 5-NN: square

coding

alt text

  1. the test point 100:

    alt text

    the nearest neighbor image:

    alt text

    the prediction is successful

  2. here is the confusion matrix:

[[ 99   0   0   0   0   1   0   0   0   0]
 [  0 100   0   0   0   0   0   0   0   0]
 [  0   1  94   1   0   0   0   3   1   0]
 [  0   0   2  91   2   4   0   0   1   0]
 [  0   0   0   0  97   0   0   0   0   3]
 [  1   0   0   0   0  98   0   0   0   1]
 [  0   0   0   0   0   1  99   0   0   0]
 [  0   4   0   0   1   0   0  94   0   1]
 [  2   0   1   1   1   0   1   1  92   1]
 [  1   1   1   1   2   1   0   3   0  90]]

the most accurate class is 1, the lest accurate class is 9

  1. the average grayscale for each images is:

[45.12679672241211, 19.94109535217285, 38.56541061401367, 36.835243225097656, 31.114784240722656, 35.164283752441406, 36.48517990112305, 29.564733505249023, 39.26533126831055, 32.149410247802734]

here is the image of the average grayscale for each digit:

alt text

alt text

alt text

alt text

alt text

alt text

alt text

alt text

alt text

alt text


alt text

alt text

  1. error rate for l1 is: 0.21666666666666667, error rate for l2 is: 0.23333333333333334

  2. confusion matrix for l1 is:
    [[14.  0.  2.]
    [ 9.  9.  0.]
    [ 1.  1. 24.]]
    confusion matrix for l2 is:
    [[12.  1.  3.]
    [ 9.  9.  0.]
    [ 1.  0. 25.]]