All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ciir.umass.edu.features.SumNormalizor Maven / Gradle / Ivy

There is a newer version: 2.10.1
Show newest version
/*===============================================================================
 * Copyright (c) 2010-2012 University of Massachusetts.  All Rights Reserved.
 *
 * Use of the RankLib package is subject to the terms of the software license set
 * forth in the LICENSE file included with this software, and also available at
 * http://people.cs.umass.edu/~vdang/ranklib_license.html
 *===============================================================================
 */

package ciir.umass.edu.features;

import java.util.Arrays;

import ciir.umass.edu.learning.DataPoint;
import ciir.umass.edu.learning.RankList;
import ciir.umass.edu.utilities.RankLibError;

/**
 * @author vdang
 */
public class SumNormalizor extends Normalizer {
    @Override
    public void normalize(final RankList rl) {
        if (rl.size() == 0) {
            throw RankLibError.create("Error in SumNormalizor::normalize(): The input ranked list is empty");
        }
        final int nFeature = rl.getFeatureCount();
        final double[] norm = new double[nFeature];
        Arrays.fill(norm, 0);
        for (int i = 0; i < rl.size(); i++) {
            final DataPoint dp = rl.get(i);
            for (int j = 1; j <= nFeature; j++) {
                norm[j - 1] += Math.abs(dp.getFeatureValue(j));
            }
        }
        for (int i = 0; i < rl.size(); i++) {
            final DataPoint dp = rl.get(i);
            for (int j = 1; j <= nFeature; j++) {
                if (norm[j - 1] > 0) {
                    dp.setFeatureValue(j, (float) (dp.getFeatureValue(j) / norm[j - 1]));
                }
            }
        }
    }

    @Override
    public void normalize(final RankList rl, int[] fids) {
        if (rl.size() == 0) {
            throw RankLibError.create("Error in SumNormalizor::normalize(): The input ranked list is empty");
        }

        //remove duplicate features from the input @fids ==> avoid normalizing the same features multiple times
        fids = removeDuplicateFeatures(fids);

        final double[] norm = new double[fids.length];
        Arrays.fill(norm, 0);
        for (int i = 0; i < rl.size(); i++) {
            final DataPoint dp = rl.get(i);
            for (int j = 0; j < fids.length; j++) {
                norm[j] += Math.abs(dp.getFeatureValue(fids[j]));
            }
        }
        for (int i = 0; i < rl.size(); i++) {
            final DataPoint dp = rl.get(i);
            for (int j = 0; j < fids.length; j++) {
                if (norm[j] > 0) {
                    dp.setFeatureValue(fids[j], (float) (dp.getFeatureValue(fids[j]) / norm[j]));
                }
            }
        }
    }

    @Override
    public String name() {
        return "sum";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy