cc.mallet.util.Randoms Java Examples

The following examples show how to use cc.mallet.util.Randoms. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FixedSparseRandomProjection.java    From anchor with MIT License 6 votes vote down vote up
public static void main (String[] args) throws Exception {
	//Alphabet vocabulary = AlphabetFactory.loadFromFile(new File(args[0]));
	InstanceList instances = InstanceList.load(new File(args[0]));

	FixedSparseRandomProjection matrix = new FixedSparseRandomProjection(instances.getDataAlphabet(), 1000, 10, new Randoms());

	matrix.load(instances);

	/*
	  for (int row = 0; row < matrix.numWords; row++) {
	  Formatter out = new Formatter();
	  for (int col = 0; col < matrix.numWords; col++) {
	  out.format("%.9f ", matrix.weights[row][col]);
	  }
	  System.out.println(out);
	  }
	*/

}
 
Example #2
Source File: SparseRandomProjection.java    From anchor with MIT License 6 votes vote down vote up
public static void main (String[] args) throws Exception {
	//Alphabet vocabulary = AlphabetFactory.loadFromFile(new File(args[0]));
	InstanceList instances = InstanceList.load(new File(args[0]));

	SparseRandomProjection matrix = new SparseRandomProjection(instances.getDataAlphabet(), 1000, 10, new Randoms());

	matrix.load(instances);

	/*
	  for (int row = 0; row < matrix.numWords; row++) {
	  Formatter out = new Formatter();
	  for (int col = 0; col < matrix.numWords; col++) {
	  out.format("%.9f ", matrix.weights[row][col]);
	  }
	  System.out.println(out);
	  }
	*/

}
 
Example #3
Source File: GaussianRandomProjection.java    From anchor with MIT License 6 votes vote down vote up
public GaussianRandomProjection (Alphabet a, int randomProjections, Randoms random) {
	vocabulary = a;
	numWords = vocabulary.size();
	numColumns = randomProjections;
	weights = new double[numWords][numColumns];
	
	wordCounts = new int[numWords];
	documentFrequencies = new int[numWords];
	rowSums = new double[numWords];

	projectionMatrix = new double[numWords][randomProjections];

	for (int word = 0; word < numWords; word++) {
		for (int col = 0; col < randomProjections; col++) {
			projectionMatrix[word][col] = random.nextGaussian();
		}
	}
}
 
Example #4
Source File: GaussianRandomProjection.java    From anchor with MIT License 6 votes vote down vote up
public static void main (String[] args) throws Exception {
	//Alphabet vocabulary = AlphabetFactory.loadFromFile(new File(args[0]));
	InstanceList instances = InstanceList.load(new File(args[0]));

	GaussianRandomProjection matrix = new GaussianRandomProjection(instances.getDataAlphabet(), 1000, new Randoms());

	matrix.load(instances);

	/*
	  for (int row = 0; row < matrix.numWords; row++) {
	  Formatter out = new Formatter();
	  for (int col = 0; col < matrix.numWords; col++) {
	  out.format("%.9f ", matrix.weights[row][col]);
	  }
	  System.out.println(out);
	  }
	*/

}
 
Example #5
Source File: ReferencesClassifierTrainer.java    From bluima with Apache License 2.0 5 votes vote down vote up
public static Trial testTrainSplit(InstanceList instances) {

        InstanceList[] instanceLists = instances.split(new Randoms(),
                new double[] { 0.9, 0.1, 0.0 });

        // LOG.debug("{} training instance, {} testing instances",
        // instanceLists[0].size(), instanceLists[1].size());

        @SuppressWarnings("rawtypes")
        ClassifierTrainer trainer = new MaxEntTrainer();
        Classifier classifier = trainer.train(instanceLists[TRAINING]);
        return new Trial(classifier, instanceLists[TESTING]);
    }
 
Example #6
Source File: SparseRandomProjection.java    From anchor with MIT License 5 votes vote down vote up
public SparseRandomProjection (Alphabet a, int randomProjections, int sparsity, Randoms random) {
	System.err.println("starting a sparse RP");

	vocabulary = a;
	numWords = vocabulary.size();
	numColumns = randomProjections;
	weights = new double[numWords][numColumns];
	
	wordCounts = new int[numWords];
	documentFrequencies = new int[numWords];
	rowSums = new double[numWords];

	projectionMatrix = new byte[numWords][randomProjections];

	squareRootSparsity = Math.sqrt(sparsity);

	// We sample random values for each cell. With probability 1/2s we sample a 1...
	double positiveCutoff = 0.5 / sparsity;

	// ... with probability 1/2s we sample a -1 ...
	double negativeCutoff = 1.0 - positiveCutoff;

	// ... and in the middle we leave a zero.

	for (int word = 0; word < numWords; word++) {
		for (int col = 0; col < randomProjections; col++) {
			double sample = random.nextUniform();
			if (sample < positiveCutoff) {
				projectionMatrix[word][col] = 1;
			}
			else if (sample > negativeCutoff) {
				projectionMatrix[word][col] = -1;
			}
		}
	}
}
 
Example #7
Source File: FixedSparseRandomProjection.java    From anchor with MIT License 4 votes vote down vote up
public FixedSparseRandomProjection (Alphabet a, int randomProjections, int nonZeros, Randoms random) {
	System.err.println("Calculating a sparse random projection");

	vocabulary = a;
	numWords = vocabulary.size();
	numColumns = randomProjections;
	this.nonZeros = nonZeros;
	weights = new double[numWords][numColumns];
	
	wordCounts = new int[numWords];
	documentFrequencies = new int[numWords];
	rowSums = new double[numWords];

	projectionMatrix = new int[numWords][nonZeros];

	//squareRootSparsity = Math.sqrt((double) nonZeros / randomProjections);
	squareRootSparsity = Math.sqrt((double) randomProjections / nonZeros);

	// Make an array with the numbers 0 .. (dictionary size - 1)
	int[] allProjectionIndices = new int[randomProjections];
	for (int i = 0; i < randomProjections; i++) {
		allProjectionIndices[i] = i;
	}

	for (int word = 0; word < numWords; word++) {
		// For each word we're going to randomly shuffle the projection indices array up to the first [nonZeros] entries.
		for (int col = 0; col < nonZeros; col++) {
			// Randomly swap the current index with one further down the array
			int swapCol = col + random.nextInt(randomProjections - col);
			int temp = allProjectionIndices[swapCol];
			allProjectionIndices[swapCol] = allProjectionIndices[col];
			allProjectionIndices[col] = temp;

			// And use the swapped index for the projection index.
			projectionMatrix[word][col] = allProjectionIndices[col];
			// Use a second range of values for indices that should be negated.
			if (random.nextUniform() > 0.5) {
				projectionMatrix[word][col] += randomProjections;
			}
		}
	}
}