net.sf.javaml.core.Instance Java Examples

The following examples show how to use net.sf.javaml.core.Instance. 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: JMLNeurophClassifier.java    From NeurophFramework with Apache License 2.0 6 votes vote down vote up
/**
 * Classifies instance as one of possible classes
 * @param instnc Instance to classify
 * @return Object class as Object
 */
@Override
public Object classify(Instance instnc) {
    
    double[] item = convertInstanceToDoubleArray(instnc);

    // set neural network input
    neuralNet.setInput(item);
    // calculate neural network output
    neuralNet.calculate();

    // find neuron with highest output
    Neuron maxNeuron = null;
    double maxOut = Double.NEGATIVE_INFINITY;
    for (Neuron neuron : neuralNet.getOutputNeurons()) {
        if (neuron.getOutput() > maxOut) {
            maxNeuron = neuron;
            maxOut = neuron.getOutput();
        }
    }

    // and return its label
    return maxNeuron.getLabel();
}
 
Example #2
Source File: JMLNeurophClassifier.java    From NeurophFramework with Apache License 2.0 6 votes vote down vote up
/**
 * Calculates predict values for every possible class that
 * instance can be classified as that
 * @param instnc Instance
 * @return Map<Object, Double>
 */
@Override
public Map<Object, Double> classDistribution(Instance instnc) {
    
    // Convert instance to double array
    double[] item = convertInstanceToDoubleArray(instnc);
    
    // set neural network input
    neuralNet.setInput(item);
    // calculate neural network output
    neuralNet.calculate();

    // find neuron with highest output
    Map<Object, Double> possibilities = new HashMap<Object, Double>();

    for (Neuron neuron : neuralNet.getOutputNeurons()) {
        possibilities.put(neuron.getLabel(), neuron.getOutput());
    }
    
    return possibilities;
}
 
Example #3
Source File: KMeans2.java    From HMMRATAC with GNU General Public License v3.0 6 votes vote down vote up
public void makeClusteredList2Signals(Dataset[] clusters,HashMap<Integer,MatrixNodeForKMeans> map){
	clusterList = new ArrayList<ATACClusterNode>();
	ATACClusterNode temp = null;
	covMat = new ArrayList<double[][]>();
	for (int i = 0; i < clusters.length;i++){
		Dataset cluster = clusters[i];
		StorelessCovariance cov = new StorelessCovariance(2);
		
		for (int x = 0; x< cluster.size();x++){
			Instance ins = cluster.get(x);
			MatrixNodeForKMeans node = map.get(ins.getID());
			temp = new ATACClusterNode(node.getChrom(),node.getPos(),node.getEnrich1(),node.getEnrich2(),
					node.getEnrich3(),node.getIndex(),i);
			clusterList.add(temp);
			double[] row1 = new double[2];
			row1[0] = node.getEnrich1();
			row1[1] = node.getEnrich2();
			cov.increment(row1);
		}
		double[][] covM = cov.getCovarianceMatrix().getData();
		covMat.add(covM);
	}
	clusters = null; map = null;
	Collections.sort(clusterList,ATACClusterNode.positionComparator);
}
 
Example #4
Source File: KMeans2.java    From HMMRATAC with GNU General Public License v3.0 6 votes vote down vote up
public void makeClusteredList3Signals(Dataset[] clusters,HashMap<Integer,MatrixNodeForKMeans> map){
	clusterList = new ArrayList<ATACClusterNode>();
	ATACClusterNode temp = null;
	covMat = new ArrayList<double[][]>();
	for (int i = 0; i < clusters.length;i++){
		Dataset cluster = clusters[i];
		StorelessCovariance cov = new StorelessCovariance(3);
		
		for (int x = 0; x< cluster.size();x++){
			Instance ins = cluster.get(x);
			MatrixNodeForKMeans node = map.get(ins.getID());
			temp = new ATACClusterNode(node.getChrom(),node.getPos(),node.getEnrich1(),node.getEnrich2(),
					node.getEnrich3(),node.getIndex(),i);
			clusterList.add(temp);
			double[] row1 = new double[3];
			row1[0] = node.getEnrich1();
			row1[1] = node.getEnrich2();
			row1[2] = node.getEnrich3();
			cov.increment(row1);
		}
		double[][] covM = cov.getCovarianceMatrix().getData();
		covMat.add(covM);
	}
	clusters = null; map = null;
	Collections.sort(clusterList,ATACClusterNode.positionComparator);
}
 
Example #5
Source File: JMLNeurophClassifier.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
/**
 * Convert instance attribute values to double array values
 * @param instnc Instance to convert
 * @return double[]
 */
private double[] convertInstanceToDoubleArray(Instance instnc) {
    Iterator attributeIterator = instnc.iterator();

    double[] item = new double[instnc.noAttributes()];
    int index = 0;

    while (attributeIterator.hasNext()) {
        Double attrValue = (Double) attributeIterator.next();
        item[index] = attrValue.doubleValue();
        index++;
    }
    
    return item;
}
 
Example #6
Source File: JMLNeurophSample.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
/**
 * Prints Java-ML data set
 *
 * @param jmlDataset Dataset Java-ML data set
 */
public static void printDataset(Dataset jmlDataset) {
    System.out.println("JML dataset");
    Iterator iterator = jmlDataset.iterator();

    while (iterator.hasNext()) {
        Instance instance = (Instance) iterator.next();
        System.out.println("inputs");
        System.out.println(instance.values());
        System.out.println(instance.classValue());
    }
}
 
Example #7
Source File: JMLNeurophSample.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
/**
 * Converts Java-ML data set to Map
 *
 * @param jmlDataset Dataset Java-ML data set
 * @return Map converted from Java-ML data set
 */
private static Map<double[], String> convertJMLDatasetToMap(Dataset jmlDataset) {

    //number of attributes without class attribute
    int numOfAttributes = jmlDataset.noAttributes();

    //initialize map
    Map<double[], String> itemClassMap = new HashMap<double[], String>();

    //iterate through jml dataset
    for (Instance dataRow : jmlDataset) {

        //initialize double array for values from dataset
        double[] values = new double[numOfAttributes];
        int ind = 0;

        //iterate through values in dataset instance an adding them in double array
        for (Double val : dataRow) {
            values[ind] = val;
            ind++;
        }

        //put attribute values and class value in map
        itemClassMap.put(values, dataRow.classValue().toString());
    }
    return itemClassMap;
}
 
Example #8
Source File: JMLNeurophSample.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
/**
 * Test JMLNeurophClassifier
 *
 * @param jmlDataset Dataset Java-ML data set
 */
private static void testJMLNeurophClassifier(Dataset jmlDataset) {
    MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(4, 16, 3);
    
    // set labels for output neurons
    neuralNet.getOutputNeurons().get(0).setLabel("Setosa");
    neuralNet.getOutputNeurons().get(1).setLabel("Versicolor");
    neuralNet.getOutputNeurons().get(2).setLabel("Virginica");

    // initialize NeurophJMLClassifier
    JMLNeurophClassifier jmlnClassifier = new JMLNeurophClassifier(neuralNet);

    // Process Java-ML data set
    jmlnClassifier.buildClassifier(jmlDataset);

    // test item
    //double[] item = {5.1, 3.5, 1.4, 0.2}; // normalized item is below
    double[] item = {-0.27777777777777773, 0.1249999999999999, -0.4322033898305085, -0.45833333333333337};

    // Java-ML instance out of test item
    Instance instance = new DenseInstance(item);

    // why are these not normalised?
    System.out.println("NeurophJMLClassifier - classify of {0.22222222222222213, 0.6249999999999999, 0.06779661016949151, 0.04166666666666667}");
    System.out.println(jmlnClassifier.classify(instance));
    System.out.println("NeurophJMLClassifier - classDistribution of {0.22222222222222213, 0.6249999999999999, 0.06779661016949151, 0.04166666666666667}");
    System.out.println(jmlnClassifier.classDistribution(instance));
}
 
Example #9
Source File: DomDistance.java    From apogen with Apache License 2.0 4 votes vote down vote up
public Dataset createDataset() {

		for (String k : domDistancesMap.keySet()) {

			Collection<BigDecimal> v = domDistancesMap.get(k).values();
			double[] features = new double[v.size()];
			int count = 0;

			for (BigDecimal bd : v) {
				features[count] = bd.doubleValue();
				count++;
			}

			Instance instance = new DenseInstance(features, k);
			data.add(instance);

		}

		return data;

	}
 
Example #10
Source File: UrlDistance.java    From apogen with Apache License 2.0 4 votes vote down vote up
/**
 * create the URL distances matrix
 * 
 * @return
 */
public Dataset createDataset() {

	for (String k : urlDistancesMap.keySet()) {

		Collection<BigDecimal> v = urlDistancesMap.get(k).values();
		double[] features = new double[v.size()];
		int count = 0;

		for (BigDecimal bd : v) {
			features[count] = bd.doubleValue();
			count++;
		}

		Instance instance = new DenseInstance(features, k);
		data.add(instance);

	}

	return data;

}
 
Example #11
Source File: WordFrequency.java    From apogen with Apache License 2.0 4 votes vote down vote up
/**
 * create the dataset for body frequencies
 * 
 * @return
 */
public Dataset createDatasetBody() {

	for (String k : wordsBodyFrequenciesMap.keySet()) {

		Collection<BigDecimal> v = wordsBodyFrequenciesMap.get(k).values();
		double[] features = new double[v.size()];
		int count = 0;

		for (BigDecimal bd : v) {
			features[count] = bd.doubleValue();
			count++;
		}

		Instance instance = new DenseInstance(features, k);
		dataBody.add(instance);

	}

	return dataBody;

}
 
Example #12
Source File: WordFrequency.java    From apogen with Apache License 2.0 4 votes vote down vote up
/**
 * create the dataset for thal frequencies
 * 
 * @return
 */
public Dataset createDatasetThal() {

	for (String k : wordsThalFrequenciesMap.keySet()) {

		Collection<BigDecimal> v = wordsThalFrequenciesMap.get(k).values();
		double[] features = new double[v.size()];
		int count = 0;

		for (BigDecimal bd : v) {
			features[count] = bd.doubleValue();
			count++;
		}

		Instance instance = new DenseInstance(features, k);
		dataThal.add(instance);

	}

	return dataThal;

}
 
Example #13
Source File: TagFrequency.java    From apogen with Apache License 2.0 4 votes vote down vote up
/**
 * exports the tags frequencies map in a Java-ML Dataset
 * 
 * @return
 */
public Dataset createDataset() {

	for (String k : tagsFrequenciesMap.keySet()) {

		Collection<BigDecimal> v = tagsFrequenciesMap.get(k).values();
		double[] features = new double[v.size()];
		int count = 0;

		for (BigDecimal bd : v) {
			features[count] = bd.doubleValue();
			count++;
		}

		Instance instance = new DenseInstance(features, k);
		data.add(instance);

	}

	return data;

}