/*
@param features - A list of string-double pairs representing the valued features for a classification instance
@return string-double pair list - A list of pairs that include each label and a posterior probability mass
490129
public List<StringDoublePair> classifyValuedInstanceDistribution(List<StringDoublePair> features) {
    List<scala.Tuple2<String,Double>> nfs = new ArrayList<scala.Tuple2<String,Double>>();
    for (StringDoublePair el : features) {
      nfs.add(new scala.Tuple2<String,Double>(el.getString(), el.getDouble()));
    }
    List<scala.Tuple2<String,Double>> r = maxEnt.decodeValuedInstanceAsDistribution(nfs);
    List<StringDoublePair> res = new ArrayList<StringDoublePair>();
    for (scala.Tuple2<String,Double> el : r) {
      res.add(new StringDoublePair(el._1, el._2));
    }
    return res;
  }
805295
public String topFeaturesToString(List<Triple<F,L,Double>> topFeatures)
  {
    // find longest key length (for pretty printing) with a limit
    int maxLeng = 0;
    for (Triple<F,L,Double> t : topFeatures) {
      String key = "(" + t.first + "," + t.second + ")";
      int leng = key.length();
      if (leng > maxLeng) {
        maxLeng = leng;
      }
    }
    maxLeng = Math.min(64, maxLeng);

    // set up pretty printing of weights
    NumberFormat nf = NumberFormat.getNumberInstance();
    nf.setMinimumFractionDigits(4);
    nf.setMaximumFractionDigits(4);
    if (nf instanceof DecimalFormat) {
      ((DecimalFormat) nf).setPositivePrefix(" ");
    }

    //print high weight features to a String
    StringBuilder sb = new StringBuilder();
    for (Triple<F,L,Double> t : topFeatures) {
      String key = "(" + t.first + "," + t.second + ")";
      sb.append(StringUtils.pad(key, maxLeng));
      sb.append(" ");
      double cnt = t.third();
      if (Double.isInfinite(cnt)) {
        sb.append(cnt);
      } else {
        sb.append(nf.format(cnt));
      }
      sb.append("\n");
    }
    return sb.toString();
  }
574177
public void selectFeatures(int numFeatures, double[] scores) {

    List<ScoredObject<F>> scoredFeatures = new ArrayList<ScoredObject<F>>();

    for (int i = 0; i < scores.length; i++) {
      scoredFeatures.add(new ScoredObject<F>(featureIndex.get(i), scores[i]));
    }

    Collections.sort(scoredFeatures, ScoredComparator.DESCENDING_COMPARATOR);
    Index<F> newFeatureIndex = new HashIndex<F>();
    for (int i = 0; i < scoredFeatures.size() && i < numFeatures; i++) {
      newFeatureIndex.add(scoredFeatures.get(i).object());
      //System.err.println(scoredFeatures.get(i));
    }

    for (int i = 0; i < size; i++) {
      int[] newData = new int[data[i].length];
      int curIndex = 0;
      for (int j = 0; j < data[i].length; j++) {
        int index;
        if ((index = newFeatureIndex.indexOf(featureIndex.get(data[i][j]))) != -1) {
          newData[curIndex++] = index;
        }
      }
      int[] newDataTrimmed = new int[curIndex];
      System.arraycopy(newData, 0, newDataTrimmed, 0, curIndex);
      data[i] = newDataTrimmed;
    }
    featureIndex = newFeatureIndex;
  }
344679
protected List<Feature> convertFeatures(FeatureCollection<SimpleFeatureType, SimpleFeature> features,
			int maxCoordinates, double startDistance) throws IOException, GeomajasException {
		FeatureIterator<SimpleFeature> iterator = features.features();
		List<Feature> dtoFeatures = new ArrayList<Feature>();
		FeatureConverter converter = new FeatureConverter();
		while (iterator.hasNext()) {
			try {
				SimpleFeature feature = iterator.next();
				dtoFeatures.add(converter.toDto(feature, maxCoordinates));
			} catch (Exception e) {
				continue;
			}
		}
		iterator.close();
		return dtoFeatures;
	}
1119425
public FeatureList getFeatureList() throws IOException, ServerException {

        if (this.session.featureList != null) {
            return this.session.featureList;
        }

        // TODO: this can also be optimized. Instead of parsing the
        // reply after it is reveiced, we can parse it as it is
        // received.
        Reply featReply = null;
        try {
            featReply = controlChannel.execute(Command.FEAT);

            if (featReply.getCode() != 211) {
                throw ServerException.embedUnexpectedReplyCodeException(
                                  new UnexpectedReplyCodeException(featReply),
                                  "Server refused returning features");
            }
        } catch (FTPReplyParseException rpe) {
            throw ServerException.embedFTPReplyParseException(rpe);
        } catch (UnexpectedReplyCodeException urce) {
            throw ServerException.embedUnexpectedReplyCodeException(
                                urce,
                                "Server refused returning features");
        }

        this.session.featureList = new FeatureList(featReply.getMessage());

        return session.featureList;
    }
147631
public int[] columnMapping( String[] features ) {
    int[] map = new int[_colNames.length];
    for( int i=0; i<_colNames.length; i++ ) {
      map[i] = -1;              // Assume it is missing
      for( int j=0; j<features.length; j++ ) {
        if( _colNames[i].equals(features[j]) ) {
          if( map[i] != -1 ) throw new IllegalArgumentException("duplicate feature "+_colNames[i]);
          map[i] = j;
        }
      }
      if( map[i] == -1 ) Log.warn(Sys.SCORM,"Model feature "+_colNames[i]+" not in the provided feature list from the data");
    }
    return map;
  }
642106
def feature_selection(self, data, labels, weights, num_features, method):
        """"""
        if method == 'none':
            return np.array(range(data.shape[1]))
        elif method == 'forward_selection':
            return self.forward_selection(data, labels, weights, num_features)
        elif method == 'highest_weights':
            clf = Ridge(alpha=0, fit_intercept=True,
                        random_state=self.random_state)
            clf.fit(data, labels, sample_weight=weights)
            feature_weights = sorted(zip(range(data.shape[0]),
                                         clf.coef_ * data[0]),
                                     key=lambda x: np.abs(x[1]),
                                     reverse=True)
            return np.array([x[0] for x in feature_weights[:num_features]])
        elif method == 'lasso_path':
            weighted_data = ((data - np.average(data, axis=0, weights=weights))
                             * np.sqrt(weights[:, np.newaxis]))
            weighted_labels = ((labels - np.average(labels, weights=weights))
                               * np.sqrt(weights))
            nonzero = range(weighted_data.shape[1])
            _, coefs = self.generate_lars_path(weighted_data,
                                               weighted_labels)
            for i in range(len(coefs.T) - 1, 0, -1):
                nonzero = coefs.T[i].nonzero()[0]
                if len(nonzero) <= num_features:
                    break
            used_features = nonzero
            return used_features
        elif method == 'auto':
            if num_features <= 6:
                n_method = 'forward_selection'
            else:
                n_method = 'highest_weights'
            return self.feature_selection(data, labels, weights,
                                          num_features, n_method)
639494
public Pair<List<SuggestedReplacement>, SortedMap<String, Float>> computeFeatures(List<String> suggestions, String word, AnalyzedSentence sentence, int startPos) {
    if (suggestions.isEmpty()) {
      return Pair.of(Collections.emptyList(), Collections.emptySortedMap());
    }
    if (topN <= 0) {
      topN = suggestions.size();
    }
    List<String> topSuggestions = suggestions.subList(0, Math.min(suggestions.size(), topN));
    //EditDistance<Integer> levenshteinDistance = new LevenshteinDistance(4);
    EditDistance levenstheinDistance = new EditDistance(word, EditDistance.DistanceAlgorithm.Damerau);
    SimilarityScore<Double> jaroWrinklerDistance = new JaroWinklerDistance();
    List<Feature> features = new ArrayList<>(topSuggestions.size());

    for (String candidate : topSuggestions) {
      double prob1 = languageModel.getPseudoProbability(Collections.singletonList(candidate)).getProb();
      double prob3 = LanguageModelUtils.get3gramProbabilityFor(language, languageModel, startPos, sentence, candidate);
      //double prob4 = LanguageModelUtils.get4gramProbabilityFor(language, languageModel, startPos, sentence, candidate);
      long wordCount = ((BaseLanguageModel) languageModel).getCount(candidate);
      int levenstheinDist = levenstheinDistance.compare(candidate, 3);
      double jaroWrinklerDist = jaroWrinklerDistance.apply(word, candidate);
      DetailedDamerauLevenstheinDistance.Distance detailedDistance = DetailedDamerauLevenstheinDistance.compare(word, candidate);

      features.add(new Feature(prob1, prob3, wordCount, levenstheinDist, detailedDistance, jaroWrinklerDist, candidate));
    }
    if (!"noop".equals(score)) {
      features.sort(Feature::compareTo);
    }
    //logger.trace("Features for '%s' in '%s': %n", word, sentence.getText());
    //features.stream().map(Feature::toString).forEach(logger::trace);
    List<String> words = features.stream().map(Feature::getWord).collect(Collectors.toList());

    // compute general features, not tied to candidates
    SortedMap<String, Float> matchData = new TreeMap<>();
    matchData.put("candidateCount", (float) words.size());

    List<SuggestedReplacement> suggestionsData = features.stream().map(f -> {
      SuggestedReplacement s = new SuggestedReplacement(f.getWord());
      s.setFeatures(f.getData());
      return s;
    }).collect(Collectors.toList());
    return Pair.of(suggestionsData, matchData);
  }
1232505
double[][] trainWeights(int[][] data, int[] labels) {
    if (tuneSigma) {
      tuneSigma(data, labels);
    }
    if (VERBOSE) {
      System.err.println("NB CF: " + data.length + " data items ");
      for (int i = 0; i < data.length; i++) {
        System.err.print("Datum " + i + ": " + labels[i] + ":");
        for (int j = 0; j < data[i].length; j++) {
          System.err.print(" " + data[i][j]);
        }
        System.err.println();
      }
    }
    int numFeatures = numFeatures();
    int numClasses = numClasses();
    double[][] weights = new double[numFeatures][numClasses];
    // find P(C|F)/P(C)
    int num = 0;
    double[] numc = new double[numClasses];
    double n = 0;   // num active features in whole dataset
    double[] n_c = new double[numClasses];  // num active features in class c items
    double[] n_f = new double[numFeatures]; // num data items for which feature is active
    double[][] n_fc = new double[numFeatures][numClasses];  // num times feature active in class c
    for (int d = 0; d < data.length; d++) {
      num++;
      numc[labels[d]]++;
      for (int i = 0; i < data[d].length; i++) {
        n++;
        n_c[labels[d]]++;
        n_f[data[d][i]]++;
        n_fc[data[d][i]][labels[d]]++;
      }
    }
    for (int c = 0; c < numClasses; c++) {
      for (int f = 0; f < numFeatures; f++) {
        if (interpretAlwaysOnFeatureAsPrior && n_f[f] == data.length) {
          // interpret always on feature as prior!
          weights[f][c] = Math.log(numc[c] / num);
        } else {
          // p_c_f = (N(f,c)+k)/(N(f)+|C|k) = Paddk(c|f)
          // set lambda = log (P()/P())
          double p_c = (n_c[c] + epsilon) / (n + numClasses * epsilon);
          double p_c_f = (n_fc[f][c] + sigma) / (n_f[f] + sigma * numClasses);
          if (VERBOSE) {
            System.err.println("Prob ratio(f=" + f + ",c=" + c + ") = " + p_c_f / p_c + " (nc=" + n_c[c] + ", nf=" + n_f[f] + ", nfc=" + n_fc[f][c] + ")");
          }
          weights[f][c] = Math.log(p_c_f / p_c);
        }
      }
    }
    return weights;
  }
789782
@SuppressWarnings("static-method")
	public void getImportedFeatures(List<Class<?>> features) {
		features.add(AtomicLongCastExtensions.class);
		features.add(AtomicIntegerCastExtensions.class);
		features.add(AtomicDoubleCastExtensions.class);
		features.add(BigIntegerCastExtensions.class);
		features.add(BigDecimalCastExtensions.class);
		features.add(NumberCastExtensions.class);
		features.add(PrimitiveByteCastExtensions.class);
		features.add(PrimitiveDoubleCastExtensions.class);
		features.add(PrimitiveFloatCastExtensions.class);
		features.add(PrimitiveIntCastExtensions.class);
		features.add(PrimitiveLongCastExtensions.class);
		features.add(PrimitiveShortCastExtensions.class);
	}
540289
def get_feature(name):
    """"""
    if name == 'css':
        return CSSFeatures()
    elif name == 'kohlschuetter':
        return KohlschuetterFeatures()
    elif name == 'readability':
        return ReadabilityFeatures()
    elif name == 'weninger':
        return WeningerFeatures()
    elif name == 'clustered_weninger':
        return ClusteredWeningerFeatures()
    else:
        raise ValueError('invalid feature name: "{}"'.format(name))
468379
protected static void addFeature(String feature, FeatureMap featureMap, List<Integer> featureList)
    {
        int featureId = featureMap.idOf(feature);
        if (featureId != -1)
            featureList.add(featureId);
    }
511117
def _format_feature(feature, weight, hl_spaces):
    # type: (...) -> str
    
    if isinstance(feature, FormattedFeatureName):
        return feature.format()
    elif (isinstance(feature, list) and
            all('name' in x and 'sign' in x for x in feature)):
        return _format_unhashed_feature(feature, weight, hl_spaces=hl_spaces)
    else:
        return _format_single_feature(feature, weight, hl_spaces=hl_spaces)
176157
public FeatureShape getFeatureShape(String database, String table, long featureId) {
        Map<Long, FeatureShape> featureIds = getFeatureIds(database, table);
        FeatureShape featureShape = getFeatureShape(featureIds, featureId);
        return featureShape;
    }
68180
private FeatureInfo getFeatureInfo(String name,
                                       Map<String, ProductInfo> productInfos,
                                       Map<String, FeatureInfo> featuresBySymbolicName) {
        String productName, featureName;
        int index = name.indexOf(':');
        if (index == -1) {
            FeatureInfo featureInfo = featuresBySymbolicName.get(name);
            if (featureInfo != null) {
                return featureInfo;
            }

            productName = ManifestFileProcessor.CORE_PRODUCT_NAME;
            featureName = name;
        } else {
            productName = name.substring(0, index);
            featureName = name.substring(index + 1);
        }

        ProductInfo product = productInfos.get(productName);
        return product == null ? null : product.featuresByShortName.get(featureName);
    }
450623
function FeatureCollection(featureCollection) {
        crs(featureCollection);
        bbox(featureCollection);
        if (featureCollection.properties !== undefined) {
            errors.push({
                message: 'FeatureCollection object cannot contain a "properties" member',
                line: featureCollection.__line__
            });
        }
        if (featureCollection.coordinates !== undefined) {
            errors.push({
                message: 'FeatureCollection object cannot contain a "coordinates" member',
                line: featureCollection.__line__
            });
        }
        if (!requiredProperty(featureCollection, 'features', 'array')) {
            if (!everyIs(featureCollection.features, 'object')) {
                return errors.push({
                    message: 'Every feature must be an object',
                    line: featureCollection.__line__
                });
            }
            featureCollection.features.forEach(Feature);
        }
    }
527166
private Map<Long, FeatureShape> getFeatureIds(Map<String, Map<Long, FeatureShape>> tables, String table) {

        Map<Long, FeatureShape> featureIds = tables.get(table);
        if (featureIds == null) {
            featureIds = new HashMap<>();
            tables.put(table, featureIds);
        }
        return featureIds;
    }