/** * Returns the weight of the given feature. * * @param featureIndex The feature index. * @return The weight of the feature. **/ public double getWeight(int featureIndex) { return getWeight(featureIndex, defaultWeight); }
/** * Takes the dot product of this <code>SparseWeightVector</code> with the argument vector, using * the specified default weight when one is not yet present in this vector. * * @param exampleFeatures The example's feature indices. * @param exampleValues The example's feature values. * @param defaultW The default weight. * @return The computed dot product. **/ public double dot(int[] exampleFeatures, double[] exampleValues, double defaultW) { double sum = 0; for (int i = 0; i < exampleFeatures.length; i++) { double w = getWeight(exampleFeatures[i], defaultW); sum += w * exampleValues[i]; } return sum; }
/** * The strength of each feature in the argument vector is multiplied by the corresponding weight * in this weight vector and the result is returned as an array of arrays. The first array * contains the integer keys of the example's features, as indexed in the lexicon. The second * array gives the double values corresponding to the product of the pairwise multiplication of * the strengths of that feature. * * @param exampleFeatures The example's feature indices. * @param exampleValues The example's feature values. * @param defaultW An initial weight for previously unseen features. * @param inverse When set to <code>true</code> the weight in this vector is inverted before the * multiplication takes place. * @return A new example vector representing the pairwise multiplication. **/ public Object[] pairwiseMultiply(int[] exampleFeatures, double[] exampleValues, double defaultW, boolean inverse) { int resultFeatures[] = new int[exampleFeatures.length]; double resultValues[] = new double[exampleFeatures.length]; for (int i = 0; i < exampleFeatures.length; i++) { int featureIndex = exampleFeatures[i]; double w = getWeight(featureIndex, defaultW); if (inverse) w = 1 / w; resultFeatures[i] = exampleFeatures[i]; resultValues[i] = w * exampleValues[i]; } return new Object[] {resultFeatures, resultValues}; }
/** * Self-modifying vector addition where the argument vector is first scaled by the given factor. * * @param exampleFeatures The example's feature indices. * @param exampleValues The example's feature values. * @param factor The scaling factor. * @param defaultW An initial weight for previously unseen features. **/ public void scaledAdd(int[] exampleFeatures, double[] exampleValues, double factor, double defaultW) { for (int i = 0; i < exampleFeatures.length; i++) { int featureIndex = exampleFeatures[i]; double w = getWeight(featureIndex, defaultW) + factor * exampleValues[i]; setWeight(featureIndex, w, defaultW); } }
/** * Self-modifying vector multiplication where the argument vector is first scaled by the given * factor. * * @param exampleFeatures The example's feature indices. * @param exampleValues The example's feature values. * @param factor The scaling factor. * @param defaultW An initial weight for previously unseen features. **/ public void scaledMultiply(int[] exampleFeatures, double[] exampleValues, double factor, double defaultW) { for (int i = 0; i < exampleFeatures.length; i++) { int featureIndex = exampleFeatures[i]; double s = exampleValues[i]; double multiplier = factor; if (s == 0) multiplier = 1; else if (s != 1) multiplier = Math.pow(factor, s); double w = getWeight(featureIndex, defaultW) * multiplier; setWeight(featureIndex, w, defaultW); } }