68.2k views
1 vote
Finish the PYTHON CODE below. Your job is to use data x and y to check for misclassifications. x is the predicted values and y is the true values. w has shape (3,3), x has shape (2,30), and y has shape (1,30). You have a numpy array called history that is filled with weight history (w). Use numpy arrays to check for the number of misclassifications and store the weights (w) into the list "mismatches". PLEASE DO NOT USE THE ANSWER THAT IS ALREADY ON , IT IS WRONG!

mismatches = []
for w in history:

1 Answer

3 votes

Final answer:

To check for misclassifications, multiply each weight matrix w from history with the predicted values x, compare with true values y and count misclassifications. If any, add the particular w to the mismatches list.

Step-by-step explanation:

To finish the Python code for checking misclassifications, we must use matrix multiplication to apply the weights from the history array to the predicted values x and then compare the resulting predictions with the true values y. If a prediction does not match the true value, it is counted as a misclassification. The matrix of weights w that resulted in a misclassification is then stored in the mismatches list.

The procedure is as follows:

  • For each weight matrix w in the history array, multiply x by w to get the predicted outcomes.
  • Compare the predictions to the true values y by checking if the sign of the predictions matches that of the true values.
  • Count the number of misclassified predictions.
  • If there are misclassifications, add the weight matrix to the mismatches list.

The Python code would look something like this:

mismatches = []
for w in history:
predictions = np.dot(w.T, x)
misclassified = np.sum(predictions * y <= 0)
if misclassified > 0:
mismatches.append(w)

User Aman Chawla
by
8.4k points

Related questions

1 answer
5 votes
130k views