original_docstring: Takes predicted and actual y arrays. Returns F1-score.
  
          :param pred: ndarray, predictions.
          :param true: ndarray, true y values.
          :param weighted: bool, If True, 'weighted' score is returned, 'macro' otherwise.
          :param print_detailed: If True, redirects labels precision, recall, F1-score to stdout.
          :return: float, F1-score metrics.
          
docstring: Takes predicted and actual y arrays. Returns F1-score.
docstring_tokens: Takes predicted and actual y arrays . Returns F1 - score .
code: def f1_score(pred, true, weighted=False, print_detailed=False):
          """Takes predicted and actual y arrays. Returns F1-score.
  
          :param pred: ndarray, predictions.
          :param true: ndarray, true y values.
          :param weighted: bool, If True, 'weighted' score is returned, 'macro' otherwise.
          :param print_detailed: If True, redirects labels precision, recall, F1-score to stdout.
          :return: float, F1-score metrics.
          """
          pred = pred.flatten()
          true = true.flatten()
          joined = np.c_[pred, true]
  
          labels, num_of_samples = np.unique(true, return_counts=True)
          precision, recall, f1score = [], [], []
          for label in labels:
              predicted = joined[joined[:, 0] == label]  # true positive + false positive
              predicted_true = len(predicted[predicted[:, 1] == label])  # true positives
              actual = len(joined[joined[:, 1] == label])  # true positive + false negative
  
              if predicted_true == 0:
                  precision.append(0)
                  recall.append(0)
                  f1score.append(0)
                  continue
  
              prec = predicted_true / len(predicted)
              rec = predicted_true / actual
              precision.append(prec)
              recall.append(rec)
              f1score.append((2 * prec * rec) / (prec + rec))
  
          if print_detailed:
              print("Precision per class: {}\n"
                    "Recall per class: {}\n"
                    "F1-score per class: {}".format(precision, recall, f1score))
  
          if weighted:
              return round(np.array(f1score) @ num_of_samples / sum(num_of_samples) * 100, 2)
  
          return round(sum(f1score) / len(f1score) * 100, 2)
code_tokens: def f1_score ( pred , true , weighted = False , print_detailed = False ) : pred = pred . flatten ( ) true = true . flatten ( ) joined = np . c_ [ pred , true ] labels , num_of_samples = np . unique ( true , return_counts = True ) precision , recall , f1score = [ ] , [ ] , [ ] for label in labels : predicted = joined [ joined [ : , 0 ] == label ] predicted_true = len ( predicted [ predicted [ : , 1 ] == label ] ) actual = len ( joined [ joined [ : , 1 ] == label ] ) if predicted_true == 0 : precision . append ( 0 ) recall . append ( 0 ) f1score . append ( 0 ) continue prec = predicted_true / len ( predicted ) rec = predicted_true / actual precision . append ( prec ) recall . append ( rec ) f1score . append ( ( 2 * prec * rec ) / ( prec + rec ) ) if print_detailed : print ( "Precision per class: {}\n" "Recall per class: {}\n" "F1-score per class: {}" . format ( precision , recall , f1score ) ) if weighted : return round ( np . array ( f1score ) @ num_of_samples / sum ( num_of_samples ) * 100 , 2 ) return round ( sum ( f1score ) / len ( f1score ) * 100 , 2 )
short_docstring: Takes predicted and actual y arrays.
short_docstring_tokens: Takes predicted and actual y arrays .
comment: """Takes predicted and actual y arrays. Returns F1-score.
  
          :param pred: ndarray, predictions.
          :param true: ndarray, true y values.
          :param weighted: bool, If True, 'weighted' score is returned, 'macro' otherwise.
          :param print_detailed: If True, redirects labels precision, recall, F1-score to stdout.
          :return: float, F1-score metrics.
          """$SEP$# true positive + false positive$SEP$# true positives$SEP$# true positive + false negative