concept precision in category nlp

This is an excerpt from Manning's book Real-World Natural Language Processing.
There are a number of evaluation metrics you can use depending on the nature of your NLP task, but there are some that you need to know no matter what task you are working on—accuracy, precision, recall, and F measure. For now, it suffices to know that they are used to measure how good your classifier is (more details coming in Chapter 4).
Precision is the fraction of instances that the model classifies as positive that are indeed correct. For example, if your spam filter identifies 3 emails as spam, and 2 of them are indeed spam, the precision will be 2/3, or about 66%.
Recall is somewhat opposite of precision. It is the fraction of positive instances in your dataset that are identified as positive by your model. Again, using spam filtering as an example, if there are 3 spam emails in your dataset and your model identifies 2 of them as spam successfully, the recall will be 2/3, or about 66%.
Figure 4.11: Precision and Recall
![]()
Figure 4.11 shows the relationship between predicted and true labels as well as recall and precision. There are four sub-regions in this Venn diagram — True positives, false positives, false negatives, and true negatives. True positives (TP) are instances that are predicted as positive (= spam) and are indeed in the positive class. False positives (FP) are instances that are predicted as positive (= spam) but are actually not in the positive class. These are noises in the prediction, that is, innocent non-spam emails that are mistakenly caught by the spam filter and end up in the “spam” folder of your email client.
Listing 4.1: LSTM sentence classifier
@Model.register("lstm_classifier") class LstmClassifier(Model): #A def __init__(self, word_embeddings: TextFieldEmbedder, encoder: Seq2VecEncoder, vocab: Vocabulary, positive_label: str = '4') -> None: super().__init__(vocab) self.word_embeddings = word_embeddings self.encoder = encoder self.linear = torch.nn.Linear( #B in_features=encoder.get_output_dim(), out_features=vocab.get_vocab_size('labels')) positive_index = vocab.get_token_index( positive_label, namespace='labels') self.accuracy = CategoricalAccuracy() self.f1_measure = F1Measure(positive_index) #C self.loss_function = torch.nn.CrossEntropyLoss() #D def forward(self, #E tokens: Dict[str, torch.Tensor], label: torch.Tensor = None) -> torch.Tensor: mask = get_text_field_mask(tokens) embeddings = self.word_embeddings(tokens) encoder_out = self.encoder(embeddings, mask) logits = self.linear(encoder_out) output = {"logits": logits} #F if label is not None: self.accuracy(logits, label) self.f1_measure(logits, label) output["loss"] = self.loss_function(logits, label) return output def get_metrics(self, reset: bool = False) -> Dict[str, float]: precision, recall, f1_measure = self.f1_measure.get_metric(reset) return {'accuracy': self.accuracy.get_metric(reset), #G 'precision': precision, 'recall': recall, 'f1_measure': f1_measure}