Question
We will now be using the top 2 values of predictions_top5 to produce the confusion matrix. We will create the confusion matrix by considering the
We will now be using the top 2 values of predictions_top5 to produce the confusion matrix. We will create the confusion matrix by considering the correct section jj and the two sections with the two highest scores i1,i2i1,i2 where score(i1)>score(i2)score(i1)>score(i2).
- A prediction is correct if j=i1j=i1. correct examples are ignored.
- The predicion i2i2 is confused with i1i1 if j=i2j=i2
- If ji1ji1 and ji2ji2 then the example is ignored.
Examples
We consider two scenarios where the possible sectors/categories are (0,1,2,3)
Scenario 1
Sample Input y_label = 3 top2_predictions = [2, 3]
Output confusion_matrix: [0 0 0 0] [0 0 0 0] [0 0 0 1] [0 0 0 0] Explanation: The first example has top two predictions: (2, 3) and the label 3. So we add 1 to the position (2,3).
Scenario 2
Sample Input Say we have 7 sample prediction values y_label = [3, 2, 3, 1, 0, 2, 2] top2_predictions = [[2, 3], [0, 3], [2, 3], [1, 2], [3, 0], [2, 0], [3, 2]]
Output confusion_matrix: [0 0 0 0] [0 0 0 0] [0 0 0 2] [1 0 1 0]
Explanation: The second example has 7 sample predictions:
- In two scenarios 2 is predicted in place of 3 and the cell (2,3) is incremented twice.
- In two other scenarios we have a case where the second prediction is right. The corresponding cells {(3,0) and (3,2)} are incremented once each.
- In two scenarios, the first element is the correct prediction. Thus no cell is incremented.
- In one scenario, neither of the top 2 predictions is correct. Thus no cell is incremented.
Note: y_label is the same as y_valid here.
All but one of the lines of the function have been filled. You need only fill the one missing line that updates the confusion matrix if a confusion (i.e., sector that should be labeled top but is predicted second) happenes during a cycle
def get_confusion_matrix(predictions_top5, y_valid):
confusion_matrix = np.zeros((11,11), dtype=int64)
i=0
for entry in predictions_top5[:, :2]:
if entry[1] == y_valid[i]:
# update confusion_matrix
###
### YOUR CODE HERE
###
i += 1
return confusion_matrix
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started