Home > database >  how can i sort this output?
how can i sort this output?

Time:01-13

I created an output for incorrect and correct classification. I want to sort the output for correct classifications by prediction_prob. This is my code so far:

for input, prediction_prob, prediction, label in zip(test_text, test_pred_prob, test_pred_class, test_label):
  if prediction == label:
    print(input, '\n\n', prediction_prob, '\n\n', 'has been machine coded as', prediction, 'and should be', label, 'according to human coding.' '\n\n-----\n') 

This is an example of my output:

mailtext1

 tf.Tensor([0.7113831  0.28861693], shape=(2,), dtype=float32) 

 has been machine coded as 0 and should be 0 according to human coding.

-----

mailtext2

 tf.Tensor([0.02235095 0.97764903], shape=(2,), dtype=float32) 

 has been machine coded as 1 and should be 1 according to human coding.

This is my first question, and I am relatively new to python. Thanks for your help in advance.

CodePudding user response:

I understand that uou want to sort zip(test_text, test_pred_prob, test_pred_class, test_label) by the second value in the second argument (test_pred_prob) in the descending order

You can do this like so. First we stub some data

test_text = ['mailtext1', 'mailtext2']
test_pred_prob = [tf.constant(np.array([0.7113831,  0.28861693])), tf.constant(np.array([0.02235095, 0.97764903]))]
test_pred_class = [0,1] 
test_label = [0,1]

Next we zip and sort it

all_test_data = zip(test_text, test_pred_prob, test_pred_class, test_label)
all_test_data_sorted = sorted(all_test_data, reverse = True, key = lambda t: t[1][1])

The sorting function is sorted() and of note is the key argument that picks your correct classification probability. reverse=True as we want a descending order

We can check this list now to see if we got it in the right order:

all_test_data_sorted

produces

[('mailtext2',
  <tf.Tensor: shape=(2,), dtype=float64, numpy=array([0.02235095, 0.97764903])>,
  1,
  1),
 ('mailtext1',
  <tf.Tensor: shape=(2,), dtype=float64, numpy=array([0.7113831 , 0.28861693])>,
  0,
  0)]

as expected. Now you can do your print loop suitably modified to loop over the sorted tuples:

for input, prediction_prob, prediction, label in all_test_data_sorted:
  if prediction == label:
    print(input, '\n\n', prediction_prob, '\n\n', 'has been machine coded as', prediction, 'and should be', label, 'according to human coding.' '\n\n-----\n') 

if you want to sort by the first value in your tensor you would use key = lambda t: t[1][0] in the sorted() function

  •  Tags:  
  • Related