Construct a counter with keys the labels of the classifier and
values the score (unnormalized log probability) of each class.
765651
public Counter<L> scoresOf(Datum<L, F> example) {
    if(example instanceof RVFDatum<?, ?>)return scoresOfRVFDatum((RVFDatum<L,F>)example);
    Collection<F> feats = example.asFeatures();
    int[] features = new int[feats.size()];
    int i = 0;
    for (F f : feats) {
      int index = featureIndex.indexOf(f);
      if (index >= 0) {
        features[i++] = index;
      } else {
        //System.err.println("FEATURE LESS THAN ZERO: " + f);
      }
    }
    int[] activeFeatures = new int[i];
    System.arraycopy(features, 0, activeFeatures, 0, i);
    Counter<L> scores = new ClassicCounter<L>();
    for (L lab : labels()) {
      scores.setCount(lab, scoreOf(activeFeatures, lab));
    }
    return scores;
  }
37917
def log_loss(preds, labels):
    """"""
    log_likelihood = np.sum(labels * np.log(preds)) / len(preds)
    return -log_likelihood
1175497
function zadd(key /* score-1, member-1, score-N, member-N, req */) {
  var args = slice.call(arguments, 1)
    , req = typeof args[args.length-1] === 'object' ? args.pop() : null
    , val = this.getKey(key, req);

  if(val === undefined) {
    val = new SortedSet();
    this.setKey(key, val, undefined, undefined, undefined, req);
  }

  return val.zadd(args);
}
78771
def set_classifier_interface_params(spec, features, class_labels,
        model_accessor_for_class_labels, output_features = None):
    
    # Normalize the features list.
    features = _fm.process_or_validate_features(features)

    if class_labels is None:
        raise ValueError("List of class labels must be provided.")

    n_classes = len(class_labels)

    output_features = _fm.process_or_validate_classifier_output_features(output_features, class_labels)

    if len(output_features) == 1:
        predicted_class_output, pred_cl_type = output_features[0]
        score_output = None
    elif len(output_features) == 2:
        predicted_class_output, pred_cl_type = output_features[0]
        score_output, score_output_type = output_features[1]
    else:
        raise ValueError("Provided output classes for a classifier must be "
                "a list of features, predicted class and (optionally) class_score.")

    spec.description.predictedFeatureName = predicted_class_output

    # Are they out of order?
    if not (pred_cl_type == datatypes.Int64() or pred_cl_type == datatypes.String()):
        raise ValueError("Provided predicted class output type not Int64 or String (%s)."
                % repr(pred_cl_type))

    if score_output is not None:
        if not isinstance(score_output_type, datatypes.Dictionary):
            raise ValueError("Provided class score output type not a Dictionary (%s)."
                    % repr(score_output_type))

        if score_output_type.key_type != pred_cl_type:
            raise ValueError(("Provided class score output (%s) key_type (%s) does not "
                    "match type of class prediction (%s).")
                    % (score_output, repr(score_output_type.key_type), repr(pred_cl_type)))

        spec.description.predictedProbabilitiesName = score_output

    # add input
    for index, (cur_input_name, input_type) in enumerate(features):
        input_ = spec.description.input.add()
        input_.name = cur_input_name
        datatypes._set_datatype(input_.type, input_type)

    # add output
    for index, (cur_output_name, output_type) in enumerate(output_features):
        output_ = spec.description.output.add()
        output_.name = cur_output_name
        datatypes._set_datatype(output_.type, output_type)

    # Worry about the class labels
    if pred_cl_type == datatypes.String():
        try:
            for c in class_labels:
                getattr(spec, model_accessor_for_class_labels).stringClassLabels.vector.append(str(c))
        # Not all the classifiers have class labels; in particular the pipeline
        # classifier.  Thus it's not an error if we can't actually set them.
        except AttributeError:
            pass

    else:
        for c in class_labels:
            conv_error = False
            try:
                if not (int(c) == c):
                    conv_error = True
            except:
                conv_error = True

            if conv_error:
                raise TypeError(("Cannot cast '%s' class to an int type " % str(c))
                + "(class type determined by type of first class).")

            try:
                getattr(spec, model_accessor_for_class_labels).int64ClassLabels.vector.append(int(c))
            # Not all the classifiers have class labels; in particular the pipeline
            # classifier.  Thus it's not an error if we can't actually set them.
            except AttributeError:
                break

    # And we are done!
    return spec
1266214
def process_or_validate_classifier_output_features(
        output_features, class_labels, supports_class_scores = True):

    

    def raise_error(msg):

        raise ValueError("Classifier error: %s" % msg)

    class_labels = list(class_labels)

    # First, we need to determine the type of the classes.
    _int_types = _integer_types + (bool, _np.bool_, _np.int32, _np.int64)

    if all(isinstance(cl, _int_types) for cl in class_labels):
        output_class_type = datatypes.Int64()

    elif all(isinstance(cl, _string_types) for cl in class_labels):
        output_class_type = datatypes.String()

    else:
        raise ValueError('Class labels must be all of type int or all of type string.')

    if output_features is None:

        out = [("classLabel", output_class_type)]

        if supports_class_scores:
            out += [("classProbability", datatypes.Dictionary(output_class_type))]

    elif isinstance(output_features, _string_types):

        out = [(output_features, output_class_type)]

        if supports_class_scores:
            out += [("classProbability", datatypes.Dictionary(output_class_type))]

    elif (isinstance(output_features, (list, tuple))
            and all(isinstance(fn, _string_types) for fn in output_features)
            and len(output_features) == 2):

        if supports_class_scores:
            out = [(output_features[0], output_class_type),
                   (output_features[1], datatypes.Dictionary(output_class_type))]
        else:
            raise ValueError("Classifier model (as trained) does not support output scores for classes.")

    elif is_valid_feature_list(output_features):

        output_features = [(k, datatypes._normalize_datatype(dt)) for k, dt in output_features]

        if len(output_features) == 1 or not supports_class_scores:
            if not output_features[0][1] == output_class_type:
                raise ValueError("Type of output class feature does not match type of class labels.")

        else:
            # Make sure the first two output features specified give the output
            # class field and the output class scores dictionary field
            if (isinstance(output_features[0][1], datatypes.Dictionary)
                    and isinstance(output_features[1][1], output_class_type)):

                output_features[0], output_features[1] = output_features[1], output_features[0]

            if not isinstance(output_features[1][1], datatypes.Dictionary):
                raise_error("Output features class scores should be dictionary type.")

            if output_features[1][1].key_type != output_class_type:
                raise_error("Class scores dictionary key type does not match type of class labels.")

            if output_features[0][1] != output_class_type:
                raise_error("Specified type of output class does not match type of class labels.")

        # NOTE: We are intentionally allowing the case where additional fields are allowed
        # beyond the original two features.

        out = output_features

    else:
        raise_error("Form of output features not recognized")

    return out
1130583
def ReliefF_compute_scores(inst, attr, nan_entries, num_attributes, mcmap, NN, headers, class_type, X, y, labels_std, data_type):
    
    scores = np.zeros(num_attributes)
    for feature_num in range(num_attributes):
        scores[feature_num] += compute_score(attr, mcmap, NN, feature_num, inst,
                                             nan_entries, headers, class_type, X, y, labels_std, data_type)
    return scores
619147
def labels_at(y_true, y_score, proportion, normalize=False):
    '''
        
    '''
    # Get indexes of scores sorted in descending order
    indexes = np.argsort(y_score)[::-1]

    # Sort true values in the same order
    y_true_sorted = y_true[indexes]

    # Grab top x proportion of true values
    cutoff_index = max(int(len(y_true_sorted) * proportion) - 1, 0)
    # add one to index to grab values including that index
    y_true_top = y_true_sorted[:cutoff_index+1]

    # Count the number of non-nas in the top x proportion
    # we are returning a count so it should be an int
    values = int((~np.isnan(y_true_top)).sum())

    if normalize:
        values = float(values)/(~np.isnan(y_true)).sum()

    return values
740739
def calculate_probabilities(tokens)
      # P(class|words) = P(w1,...,wn|class) * P(class) / P(w1,...,wn)
      #                = argmax P(w1,...,wn|class) * P(class)
      #
      # P(wi|class) = (count(wi, class) + k)/(count(w,class) + kV)
      prob_numerator = {}
      v_size = vocab.size

      cat_prob = Math.log(1 / data.categories.count.to_f)
      total_example_count = data.total_examples.to_f

      data.each do |category|
        unless assume_uniform
          cat_prob = Math.log(data.example_count(category) / total_example_count)
        end

        log_probs = 0
        denominator = (data.token_count(category) + @k * v_size).to_f
        tokens.each do |token|
          numerator = data.count_of_token_in_category(category, token) + @k
          log_probs += Math.log( numerator / denominator )
        end
        prob_numerator[category] = log_probs + cat_prob
      end
      normalize(prob_numerator)
    end
1196100
def counter2trend(counter)
      self.new({
        enterprise: counter.enterprise,
        score: score(counter.divergence),
        uid: counter.uid,
        ts: counter.ts
      })
    end
501410
def _save(self, counters):
        
        if self._logger == None:
            return
        if len(counters) == 0:
            return

        # Sort counters by name
        counters = sorted(counters, key=LogCounters._get_counter_name)

        for counter in counters:
            self._logger.info("counters", self._counter_to_string(counter))
29896
def verify_consistency(predictions, scores, proba, opt_class):
    
    if predictions.shape != scores.shape:
        raise ValueError("predictions and scores arrays have inconsistent " +
                         "dimensions.")

    n_class = scores.shape[1] if scores.ndim > 1 else 1

    # If proba not given, default to False for all classifiers
    if proba is None:
        proba = np.repeat(False, n_class)

    # If opt_class is not given, default to True for all classifiers
    if opt_class is None:
        opt_class = np.repeat(True, n_class)

    # Convert to numpy arrays if necessary
    proba = np.array(proba, dtype=bool, ndmin=1)
    opt_class = np.array(opt_class, dtype=bool, ndmin=1)

    if np.sum(opt_class) < 1:
        raise ValueError("opt_class should contain at least one True value.")

    if predictions.shape[1] != len(proba):
        raise ValueError("mismatch in shape of proba and predictions.")
    if predictions.shape[1] != len(opt_class):
        raise ValueError("mismatch in shape of opt_class and predictions.")

    for m in range(n_class):
        if (np.any(np.logical_or(scores[:,m] < 0, scores[:,m] > 1)) and proba[m]):
            warnings.warn("scores fall outside the [0,1] interval for " +
                          "classifier {}. Setting proba[m]=False.".format(m))
            proba[m] = False

    return proba, opt_class
1031914
def run_ideal_classifier(args={}):
  
  numObjects = args.get("numObjects", 10)
  numLocations = args.get("numLocations", 10)
  numFeatures = args.get("numFeatures", 10)
  numPoints = args.get("numPoints", 10)
  trialNum = args.get("trialNum", 42)
  useLocation = args.get("useLocation", 1)
  numColumns = args.get("numColumns", 1)
  objects = createObjectMachine(
    machineType="simple",
    numInputBits=20,
    sensorInputSize=150,
    externalInputSize=2400,
    numCorticalColumns=numColumns,
    numFeatures=numFeatures,
    numLocations=numLocations,
    seed=trialNum
  )
  random.seed(trialNum)

  objects.createRandomObjects(numObjects, numPoints=numPoints,
                              numLocations=numLocations,
                              numFeatures=numFeatures)

  objectSDRs = objects.provideObjectsToLearn()
  objectNames = objectSDRs.keys()
  featureWidth = objects.sensorInputSize
  locationWidth = objects.externalInputSize

  # compute the number of sensations across all objects
  numInputVectors = numPoints * numObjects

  if useLocation:
    inputWidth = featureWidth + locationWidth
  else:
    inputWidth = featureWidth

  # create "training" dataset
  data = np.zeros((numInputVectors, inputWidth))
  label = np.zeros((numInputVectors, numObjects))
  k = 0
  for i in range(numObjects):
    numSensations = len(objectSDRs[objectNames[i]])
    for j in range(numSensations):
      activeBitsFeature = np.array(list(objectSDRs[objectNames[i]][j][0][1]))
      data[k, activeBitsFeature] = 1
      if useLocation:
        activeBitsLocation = np.array(list(objectSDRs[objectNames[i]][j][0][0]))
        data[k, featureWidth + activeBitsLocation] = 1
      label[k, i] = 1
      k += 1

  # enumerate number of distinct "words".
  # Note: this could be done much more easily if we simply use the
  # location/feature pairs that are stored in the object machine.
  wordList = np.zeros((0, inputWidth), dtype='int32')
  featureList = np.zeros((numInputVectors,))
  for i in range(numInputVectors):
    index = findWordInVocabulary(data[i, :], wordList)
    if index is not None:
      featureList[i] = index
    else:
      newWord = np.zeros((1, inputWidth), dtype='int32')
      newWord[0, :] = data[i, :]
      wordList = np.concatenate((wordList, newWord))
      featureList[i] = wordList.shape[0] - 1

  numWords = wordList.shape[0]

  # convert objects to vectorized word representations
  storedObjectRepresentations = np.zeros((numObjects, numWords), dtype=np.int32)
  k = 0
  for i in range(numObjects):
    numSensations = len(objectSDRs[objectNames[i]])
    for j in range(numSensations):
      index = findWordInVocabulary(data[k, :], wordList)
      storedObjectRepresentations[i, index] += 1
      k += 1

  # Cool plot of feature vectors
  # plt.figure()
  # plt.imshow(np.transpose(storedObjectRepresentations))
  # plt.xlabel('Object #')
  # plt.ylabel('Word #')
  # plt.title("Object representations")

  # Create random order of sensations for each object
  objectSensations = []
  for i in range(numObjects):
    senseList = []
    wordIndices = np.where(storedObjectRepresentations[i, :])[0]
    # An object can have multiple instances of a word, in which case we
    # add all of them
    for w in wordIndices:
      senseList.extend(storedObjectRepresentations[i, w]*[w])
    random.shuffle(senseList)
    objectSensations.append(senseList)

  # plot accuracy as a function of number of sensations
  accuracyList = []
  classificationOutcome = np.zeros((numObjects, numPoints+1))
  for sensationNumber in range(1, numPoints+1):
    bowVectorsTest = np.zeros((numObjects, numWords), dtype=np.int32)
    for objectId in range(numObjects):
      # No. sensations for object objectId
      sensations = objectSensations[objectId]
      numPointsToInclude = computeUniquePointsSensed(numColumns,
                                             len(sensations), sensationNumber)
      for j in range(numPointsToInclude):
        index = sensations[j]
        bowVectorsTest[objectId, index] += 1

    # Count the number of correct classifications.
    # A correct classification is where object i is unambiguously recognized.
    numCorrect = 0
    for i in range(numObjects):
      overlaps = classifierPredict(bowVectorsTest[i, :], storedObjectRepresentations)
      bestOverlap = max(overlaps)
      outcome = ( (overlaps[i] == bestOverlap) and
                  len(np.where(overlaps==bestOverlap)[0]) == 1)
      numCorrect += outcome
      classificationOutcome[i, sensationNumber] = outcome
    accuracy = float(numCorrect) / numObjects
    accuracyList.append(accuracy)

  convergencePoint = np.zeros((numObjects, ))
  for i in range(numObjects):
    if np.max(classificationOutcome[i, :])>0:
      convergencePoint[i] = locateConvergencePoint(classificationOutcome[i, :])
    else:
      convergencePoint[i] = 11

  args.update({"accuracy": accuracyList})
  args.update({"numTouches": range(1, 11)})
  args.update({"convergencePoint": np.mean(convergencePoint)})
  args.update({"classificationOutcome": classificationOutcome})
  print "objects={}, features={}, locations={}, distinct words={}, numColumns={}".format(
    numObjects, numFeatures, numLocations, numWords, numColumns),
  print "==> convergencePoint:", args["convergencePoint"]

  return args
837807
def counter(module, name, count=1):
    
    counter = get_metric('counters', module, name, Counter())
    counter.inc(count)
750729
def incr_counter(self, *args, **kwargs):
        
        threshold = kwargs.get("threshold", self.batch_counter_default)
        if len(args) == 2:
            # backwards compatibility with existing hadoop jobs
            group_name, count = args
            key = (group_name,)
        else:
            group, name, count = args
            key = (group, name)

        ct = self._counter_dict.get(key, 0)
        ct += count
        if ct >= threshold:
            new_arg = list(key) + [ct]
            self._incr_counter(*new_arg)
            ct = 0
        self._counter_dict[key] = ct
703153
def groupby_count(i, key=None, force_keys=None):
    
    counter = defaultdict(lambda: 0)
    if not key:
        key = lambda o: o

    for k in i:
        counter[key(k)] += 1

    if force_keys:
        for k in force_keys:
            counter[k] += 0

    return counter.items()
423713
@Deprecated
  public double scoreOf(RVFDatum<L, F> example, L label) {
    int iLabel = labelIndex.indexOf(label);
    double score = 0.0;
    Counter<F> features = example.asFeaturesCounter();
    for (F f : features.keySet()) {
      score += weight(f, iLabel) * features.getCount(f);
    }
    return score + thresholds[iLabel];
  }
1185696
@Override
  public void buildClassifier(Database database, Relation<? extends ClassLabel> labelrep) {
    Object2IntOpenHashMap<ClassLabel> count = new Object2IntOpenHashMap<>();
    for(DBIDIter iter = labelrep.iterDBIDs(); iter.valid(); iter.advance()) {
      count.addTo(labelrep.get(iter), 1);
    }
    int max = Integer.MIN_VALUE;
    double size = labelrep.size();

    distribution = new double[count.size()];
    labels = new ArrayList<>(count.size());
    ObjectIterator<Entry<ClassLabel>> iter = count.object2IntEntrySet().fastIterator();
    for(int i = 0; iter.hasNext(); ++i) {
      Entry<ClassLabel> entry = iter.next();
      distribution[i] = entry.getIntValue() / size;
      labels.add(entry.getKey());
      if(entry.getIntValue() > max) {
        max = entry.getIntValue();
        prediction = entry.getKey();
      }
    }
  }