Java Code Examples for weka.core.Instance#relationalValue()
The following examples show how to use
weka.core.Instance#relationalValue() .
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: MIOptimalBall.java From tsml with GNU General Public License v3.0 | 6 votes |
/** * Calculate the distance from one data point to a bag * * @param center the data point in instance space * @param bag the bag * @return the double value as the distance. */ public double minBagDistance (Instance center, Instance bag){ double distance; double minDistance = Double.MAX_VALUE; Instances temp = bag.relationalValue(1); //calculate the distance from the data point to each instance in the bag and return the minimum distance for (int i=0; i<temp.numInstances(); i++){ distance =0; for (int j=0; j<center.numAttributes(); j++) distance += (center.value(j)-temp.instance(i).value(j))*(center.value(j)-temp.instance(i).value(j)); if (minDistance>distance) minDistance = distance; } return Math.sqrt(minDistance); }
Example 2
Source File: MultivariateProcessing.java From tsml with GNU General Public License v3.0 | 6 votes |
/**A getting started with relational attributes in Weka. Once you have the basics * there are a range of tools for manipulating them in * package utilities.multivariate_tools * * See https://weka.wikispaces.com/Multi-instance+classification * for more * */ public static void gettingStarted(){ //Load a multivariate data set String path="\\\\cmptscsvr.cmp.uea.ac.uk\\ueatsc\\Data\\Multivariate\\univariateConcatExample"; Instances train =DatasetLoading.loadData(path); System.out.println(" univariate data = "+train); path="\\\\cmptscsvr.cmp.uea.ac.uk\\ueatsc\\Data\\Multivariate\\multivariateConcatExample"; train =DatasetLoading.loadData(path); System.out.println(" multivariate data = "+train); //Recover the first instance Instance first=train.instance(0); //Split into separate dimensions Instances split=first.relationalValue(0); System.out.println(" A single multivariate case split into 3 instances with no class values= "+split); for(Instance ins:split) System.out.println("Dimension of first case =" +ins); //Extract as arrays double[][] d = new double[split.numInstances()][]; for(int i=0;i<split.numInstances();i++) d[i]=split.instance(i).toDoubleArray(); }
Example 3
Source File: MultivariateProcessing.java From tsml with GNU General Public License v3.0 | 6 votes |
public static void summariseData(){ String path="Z:\\Data\\MultivariateTSCProblems\\"; OutFile out=new OutFile("Z:\\Data\\MultivariateTSCProblems\\SummaryData.csv"); out.writeLine("problem,numTrainCases,numTestCases,numDimensions,seriesLength,numClasses"); for(String prob: DatasetLists.mtscProblems2018){ Instances train =DatasetLoading.loadData(path+prob+"\\"+prob+"_TRAIN"); Instances test =DatasetLoading.loadData(path+prob+"\\"+prob+"_TEST"); System.out.println("PROBLEM "+prob); System.out.println("Num train instances ="+train.numInstances()); System.out.println("Num test instances ="+test.numInstances()); System.out.println("num attributes (should be 2!)="+train.numAttributes()); System.out.println("num classes="+train.numClasses()); Instance temp=train.instance(0); Instances x= temp.relationalValue(0); System.out.println(" number of dimensions "+x.numInstances()); System.out.println(" number of attributes per dimension "+x.numAttributes()); out.writeLine(prob+","+train.numInstances()+","+test.numInstances()+","+x.numInstances()+","+x.numAttributes()+","+train.numClasses()); // System.out.println(" Object type ="+x); } }
Example 4
Source File: DataProcessing.java From tsml with GNU General Public License v3.0 | 6 votes |
/**A getting started with relational attributes in Weka. Once you have the basics * there are a range of tools for manipulating them in * package utilities.multivariate_tools * * See https://weka.wikispaces.com/Multi-instance+classification * for more * */ public static void gettingStarted(){ //Load a multivariate data set String path="\\\\cmptscsvr.cmp.uea.ac.uk\\ueatsc\\Data\\Multivariate\\univariateConcatExample"; Instances train = DatasetLoading.loadDataNullable(path); System.out.println(" univariate data = "+train); path="\\\\cmptscsvr.cmp.uea.ac.uk\\ueatsc\\Data\\Multivariate\\multivariateConcatExample"; train = DatasetLoading.loadDataNullable(path); System.out.println(" multivariate data = "+train); //Recover the first instance Instance first=train.instance(0); //Split into separate dimensions Instances split=first.relationalValue(0); System.out.println(" A single multivariate case split into 3 instances with no class values= "+split); for(Instance ins:split) System.out.println("Dimension of first case =" +ins); //Extract as arrays double[][] d = new double[split.numInstances()][]; for(int i=0;i<split.numInstances();i++) d[i]=split.instance(i).toDoubleArray(); }
Example 5
Source File: DataProcessing.java From tsml with GNU General Public License v3.0 | 6 votes |
public static void testSimpleClassifier() throws Exception{ String path="Z:\\Data\\MultivariateTSCProblems\\"; for(int i=15;i<DatasetLists.mtscProblems2018.length;i++){ String prob=DatasetLists.mtscProblems2018[i]; System.out.println("PROBLEM "+prob); Instances train =DatasetLoading.loadDataNullable(path+prob+"\\"+prob+"_TRAIN"); Instances test =DatasetLoading.loadDataNullable(path+prob+"\\"+prob+"_TEST"); System.out.println("Num train instances ="+train.numInstances()); System.out.println("Num test instances ="+test.numInstances()); System.out.println("num attributes (should be 2!)="+train.numAttributes()); System.out.println("num classes="+train.numClasses()); Instance temp=train.instance(0); Instances x= temp.relationalValue(0); System.out.println(" number of dimensions "+x.numInstances()); System.out.println(" number of attributes per dimension "+x.numAttributes()); NN_ED_I nb = new NN_ED_I(); nb.buildClassifier(train); double a=ClassifierTools.accuracy(test, nb); System.out.println("Problem ="+prob+" 1-NN ED accuracy ="+a); } }
Example 6
Source File: DataProcessing.java From tsml with GNU General Public License v3.0 | 6 votes |
public static void summariseMultivariateData(){ String path="Z:\\Data\\MultivariateTSCProblems\\"; OutFile out=new OutFile("Z:\\Data\\MultivariateTSCProblems\\SummaryData.csv"); out.writeLine("problem,numTrainCases,numTestCases,numDimensions,seriesLength,numClasses"); for(int i=0;i<DatasetLists.mtscProblems2018.length;i++){ String prob=DatasetLists.mtscProblems2018[i]; System.out.println("PROBLEM "+prob); Instances train =DatasetLoading.loadDataNullable(path+prob+"\\"+prob+"_TRAIN"); Instances test =DatasetLoading.loadDataNullable(path+prob+"\\"+prob+"_TEST"); System.out.println("Num train instances ="+train.numInstances()); System.out.println("Num test instances ="+test.numInstances()); System.out.println("num attributes (should be 2!)="+train.numAttributes()); System.out.println("num classes="+train.numClasses()); Instance temp=train.instance(0); Instances x= temp.relationalValue(0); System.out.println(" number of dimensions "+x.numInstances()); System.out.println(" number of attributes per dimension "+x.numAttributes()); out.writeLine(prob+","+train.numInstances()+","+test.numInstances()+","+x.numInstances()+","+x.numAttributes()+","+train.numClasses()); // System.out.println(" Object type ="+x); } }
Example 7
Source File: CitationKNN.java From tsml with GNU General Public License v3.0 | 6 votes |
/** * Wether the instances of two exemplars are or are not equal * @param exemplar1 first exemplar * @param exemplar2 second exemplar * @return if the instances of the exemplars are equal or not */ public boolean equalExemplars(Instance exemplar1, Instance exemplar2){ if(exemplar1.relationalValue(1).numInstances() == exemplar2.relationalValue(1).numInstances()){ Instances instances1 = exemplar1.relationalValue(1); Instances instances2 = exemplar2.relationalValue(1); for(int i = 0; i < instances1.numInstances(); i++){ Instance instance1 = instances1.instance(i); Instance instance2 = instances2.instance(i); for(int j = 0; j < instance1.numAttributes(); j++){ if(instance1.value(j) != instance2.value(j)){ return false; } } } return true; } return false; }
Example 8
Source File: CitationKNN.java From tsml with GNU General Public License v3.0 | 6 votes |
/** * Updates the normalization of each attribute. * * @param bag the exemplar to update the normalization for */ public void updateNormalization(Instance bag){ int i, k; double min, max; Instances instances; Instance instance; // compute the min/max of each feature for (i = 0; i < m_TrainBags.attribute(1).relation().numAttributes(); i++) { min = m_Min[i] / m_MinNorm; max = m_Max[i] / m_MaxNorm; instances = bag.relationalValue(1); for (k=0;k<instances.numInstances();k++) { instance = instances.instance(k); if(instance.value(i) < min) min = instance.value(i); if(instance.value(i) > max) max = instance.value(i); } m_Min[i] = min * m_MinNorm; m_Max[i] = max * m_MaxNorm; m_Diffs[i]= max * m_MaxNorm - min * m_MinNorm; } }
Example 9
Source File: MINND.java From tsml with GNU General Public License v3.0 | 6 votes |
/** * Updates the minimum and maximum values for all the attributes * based on a new exemplar. * * @param ex the new exemplar */ private void updateMinMax(Instance ex) { Instances insts = ex.relationalValue(1); for (int j = 0;j < m_Dimension; j++) { if (insts.attribute(j).isNumeric()){ for(int k=0; k < insts.numInstances(); k++){ Instance ins = insts.instance(k); if(!ins.isMissing(j)){ if (Double.isNaN(m_MinArray[j])) { m_MinArray[j] = ins.value(j); m_MaxArray[j] = ins.value(j); } else { if (ins.value(j) < m_MinArray[j]) m_MinArray[j] = ins.value(j); else if (ins.value(j) > m_MaxArray[j]) m_MaxArray[j] = ins.value(j); } } } } } }
Example 10
Source File: MIDD.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Computes the distribution for a given exemplar * * @param exmp the exemplar for which distribution is computed * @return the distribution * @throws Exception if the distribution can't be computed successfully */ public double[] distributionForInstance(Instance exmp) throws Exception { // Extract the data Instances ins = exmp.relationalValue(1); if(m_Filter!=null) ins = Filter.useFilter(ins, m_Filter); ins = Filter.useFilter(ins, m_Missing); int nI = ins.numInstances(), nA = ins.numAttributes(); double[][] dat = new double [nI][nA]; for(int j=0; j<nI; j++){ for(int k=0; k<nA; k++){ dat[j][k] = ins.instance(j).value(k); } } // Compute the probability of the bag double [] distribution = new double[2]; distribution[0]=0.0; // log-Prob. for class 0 for(int i=0; i<nI; i++){ double exp = 0.0; for(int r=0; r<nA; r++) exp += (m_Par[r*2]-dat[i][r])*(m_Par[r*2]-dat[i][r])* m_Par[r*2+1]*m_Par[r*2+1]; exp = Math.exp(-exp); // Prob. updated for one instance distribution[0] += Math.log(1.0-exp); } distribution[0] = Math.exp(distribution[0]); distribution[1] = 1.0-distribution[0]; return distribution; }
Example 11
Source File: DataProcessing.java From tsml with GNU General Public License v3.0 | 5 votes |
public static void formatCricket(){ String path="Z:\\Data\\Multivariate Working Area\\Cricket\\"; Instances[] data=new Instances[6]; data[0]=DatasetLoading.loadDataNullable(path+"CricketXLeft.arff"); data[1]=DatasetLoading.loadDataNullable(path+"CricketYLeft.arff"); data[2]=DatasetLoading.loadDataNullable(path+"CricketZLeft.arff"); data[3]=DatasetLoading.loadDataNullable(path+"CricketXRight.arff"); data[4]=DatasetLoading.loadDataNullable(path+"CricketYRight.arff"); data[5]=DatasetLoading.loadDataNullable(path+"CricketZRight.arff"); Instances all=MultivariateInstanceTools.mergeToMultivariateInstances(data); OutFile out=new OutFile(path+"Cricket.arff"); System.out.println("Cricket number of instances ="+all.numInstances()); Instance temp=all.instance(0); Instances x= temp.relationalValue(0); System.out.println(" number of dimensions "+x.numInstances()); System.out.println(" number of attributes per dimension "+x.numAttributes()); out.writeString(all+""); Instances[] split= MultivariateInstanceTools.resampleMultivariateInstances(all, 0, 0.6); System.out.println("Train size ="+split[0].numInstances()); System.out.println("Test size ="+split[1].numInstances()); out=new OutFile(path+"Cricket_TRAIN.arff"); out.writeString(split[0]+""); out=new OutFile(path+"Cricket_TEST.arff"); out.writeString(split[1]+""); }
Example 12
Source File: DataProcessing.java From tsml with GNU General Public License v3.0 | 5 votes |
public static void makeSingleDimensionFiles(String path, String[] probs,boolean overwrite){ for(String prob: probs){ System.out.println("Processing "+prob); if(prob.equals("InsectWingbeat")||prob.equals("FaceDetection")|| prob.equals("DuckDuckGeese")) continue; File f= new File(path+prob+"\\"+prob+"Dimension"+(1)+"_TRAIN.arff"); if(f.exists()&&!overwrite) continue; Instances train =DatasetLoading.loadDataNullable(path+prob+"\\"+prob+"_TRAIN"); Instances test =DatasetLoading.loadDataNullable(path+prob+"\\"+prob+"_TEST"); System.out.println("PROBLEM "+prob); System.out.println("Num train instances ="+train.numInstances()); System.out.println("Num test instances ="+test.numInstances()); System.out.println("num attributes (should be 2!)="+train.numAttributes()); System.out.println("num classes="+train.numClasses()); Instance temp=train.instance(0); Instances x= temp.relationalValue(0); System.out.println(" number of dimensions "+x.numInstances()); System.out.println(" number of attributes per dimension "+x.numAttributes()); Instances[] splitTest=MultivariateInstanceTools.splitMultivariateInstances(test); Instances[] splitTrain=MultivariateInstanceTools.splitMultivariateInstances(train); System.out.println(" Num split files ="+splitTest.length); for(int i=0;i<splitTrain.length;i++){ System.out.println("Number of test instances = "+splitTest[i].numInstances()); OutFile outTrain=new OutFile(path+prob+"\\"+prob+"Dimension"+(i+1)+"_TRAIN.arff"); outTrain.writeLine(splitTrain[i].toString()+""); OutFile outTest=new OutFile(path+prob+"\\"+prob+"Dimension"+(i+1)+"_TEST.arff"); outTest.writeLine(splitTest[i].toString()+""); } // System.out.println(" Object type ="+x); } }
Example 13
Source File: MIEMDD.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Computes the distribution for a given exemplar * * @param exmp the exemplar for which distribution is computed * @return the distribution * @throws Exception if the distribution can't be computed successfully */ public double[] distributionForInstance(Instance exmp) throws Exception { // Extract the data Instances ins = exmp.relationalValue(1); if (m_Filter != null) ins = Filter.useFilter(ins, m_Filter); ins = Filter.useFilter(ins, m_Missing); int nI = ins.numInstances(), nA = ins.numAttributes(); double[][] dat = new double [nI][nA]; for (int j = 0; j < nI; j++){ for (int k=0; k<nA; k++){ dat[j][k] = ins.instance(j).value(k); } } //find the concept instance in the exemplar double min = Double.MAX_VALUE; double maxProb = -1.0; for (int j = 0; j < nI; j++){ double exp = 0.0; for (int k = 0; k<nA; k++) // for each attribute exp += (dat[j][k]-m_Par[k*2])*(dat[j][k]-m_Par[k*2])*m_Par[k*2+1]*m_Par[k*2+1]; //the probability can be calculated as Math.exp(-exp) //to find the maximum Math.exp(-exp) is equivalent to find the minimum of (exp) if (exp < min) { min = exp; maxProb = Math.exp(-exp); //maximum probability of being positive } } // Compute the probability of the bag double[] distribution = new double[2]; distribution[1] = maxProb; distribution[0] = 1.0 - distribution[1]; //mininum prob. of being negative return distribution; }
Example 14
Source File: MultivariateProcessing.java From tsml with GNU General Public License v3.0 | 5 votes |
public static void formatCricket(){ String path="Z:\\Data\\Multivariate Working Area\\Cricket\\"; Instances[] data=new Instances[6]; data[0]=DatasetLoading.loadData(path+"CricketXLeft.arff"); data[1]=DatasetLoading.loadData(path+"CricketYLeft.arff"); data[2]=DatasetLoading.loadData(path+"CricketZLeft.arff"); data[3]=DatasetLoading.loadData(path+"CricketXRight.arff"); data[4]=DatasetLoading.loadData(path+"CricketYRight.arff"); data[5]=DatasetLoading.loadData(path+"CricketZRight.arff"); Instances all=MultivariateInstanceTools.mergeToMultivariateInstances(data); OutFile out=new OutFile(path+"Cricket.arff"); System.out.println("Cricket number of instances ="+all.numInstances()); Instance temp=all.instance(0); Instances x= temp.relationalValue(0); System.out.println(" number of dimensions "+x.numInstances()); System.out.println(" number of attributes per dimension "+x.numAttributes()); out.writeString(all+""); Instances[] split= MultivariateInstanceTools.resampleMultivariateInstances(all, 0, 0.6); System.out.println("Train size ="+split[0].numInstances()); System.out.println("Test size ="+split[1].numInstances()); out=new OutFile(path+"Cricket_TRAIN.arff"); out.writeString(split[0]+""); out=new OutFile(path+"Cricket_TEST.arff"); out.writeString(split[1]+""); }
Example 15
Source File: MultivariateProcessing.java From tsml with GNU General Public License v3.0 | 5 votes |
public static void makeSingleDimensionFiles(){ String path="Z:\\Data\\MultivariateTSCProblems\\"; for(String prob: DatasetLists.mtscProblems2018){ File f= new File(path+prob+"\\"+prob+"Dimension"+(1)+"_TRAIN.arff"); if(!f.exists()){ Instances train =DatasetLoading.loadData(path+prob+"\\"+prob+"_TRAIN"); Instances test =DatasetLoading.loadData(path+prob+"\\"+prob+"_TEST"); System.out.println("PROBLEM "+prob); System.out.println("Num train instances ="+train.numInstances()); System.out.println("Num test instances ="+test.numInstances()); System.out.println("num attributes (should be 2!)="+train.numAttributes()); System.out.println("num classes="+train.numClasses()); Instance temp=train.instance(0); Instances x= temp.relationalValue(0); System.out.println(" number of dimensions "+x.numInstances()); System.out.println(" number of attributes per dimension "+x.numAttributes()); Instances[] splitTest=MultivariateInstanceTools.splitMultivariateInstances(test); Instances[] splitTrain=MultivariateInstanceTools.splitMultivariateInstances(train); System.out.println(" Num split files ="+splitTest.length); for(int i=0;i<splitTrain.length;i++){ System.out.println("Number of test instances = "+splitTest[i].numInstances()); OutFile outTrain=new OutFile(path+prob+"\\"+prob+"Dimension"+(i+1)+"_TRAIN.arff"); outTrain.writeLine(splitTrain[i].toString()+""); OutFile outTest=new OutFile(path+prob+"\\"+prob+"Dimension"+(i+1)+"_TEST.arff"); outTest.writeLine(splitTest[i].toString()+""); } } // System.out.println(" Object type ="+x); } }
Example 16
Source File: MDD.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Computes the distribution for a given exemplar * * @param exmp the exemplar for which distribution is computed * @return the distribution * @throws Exception if the distribution can't be computed successfully */ public double[] distributionForInstance(Instance exmp) throws Exception { // Extract the data Instances ins = exmp.relationalValue(1); if(m_Filter!=null) ins = Filter.useFilter(ins, m_Filter); ins = Filter.useFilter(ins, m_Missing); int nI = ins.numInstances(), nA = ins.numAttributes(); double[][] dat = new double [nI][nA]; for(int j=0; j<nI; j++){ for(int k=0; k<nA; k++){ dat[j][k] = ins.instance(j).value(k); } } // Compute the probability of the bag double [] distribution = new double[2]; distribution[1]=0.0; // Prob. for class 1 for(int i=0; i<nI; i++){ double exp = 0.0; for(int r=0; r<nA; r++) exp += (m_Par[r*2]-dat[i][r])*(m_Par[r*2]-dat[i][r])/ ((m_Par[r*2+1])*(m_Par[r*2+1])); exp = Math.exp(-exp); // Prob. updated for one instance distribution[1] += exp/(double)nI; distribution[0] += (1.0-exp)/(double)nI; } return distribution; }
Example 17
Source File: TestUtil.java From wekaDeeplearning4j with GNU General Public License v3.0 | 4 votes |
/** * Creates a relational test dataset */ public static Instances makeTestDatasetRelational( int seed, int numInstances, int numClasses, int classType, int classIndex, int numRelationalNominal, int numRelationalString, int numRelationalNumeric, int numInstancesRelational) throws Exception { TestInstances testset = new TestInstances(); testset.setSeed(seed); testset.setNumInstances(numInstances); testset.setNumClasses(numClasses); testset.setClassType(classType); testset.setClassIndex(classIndex); testset.setNumClasses(numClasses); testset.setMultiInstance(false); testset.setNumNominal(0); testset.setNumNumeric(0); testset.setNumString(0); testset.setNumDate(0); testset.setNumRelational(1); testset.setNumRelationalNominal(numRelationalNominal); testset.setNumRelationalString(numRelationalString); testset.setNumRelationalNumeric(numRelationalNumeric); testset.setNumInstancesRelational(numInstancesRelational); final Instances generated = testset.generate(); // Remove random instances Random rand = new Random(42); for (Instance datum : generated) { final Instances rel = datum.relationalValue(0); RemovePercentage rp = new RemovePercentage(); rp.setInputFormat(rel); rp.setPercentage(rand.nextDouble() * 100); final Instances rel2 = Filter.useFilter(rel, rp); final int i = generated.attribute(0).addRelation(rel2); datum.setValue(0, i); } return generated; }
Example 18
Source File: TestUtil.java From wekaDeeplearning4j with GNU General Public License v3.0 | 4 votes |
/** * Creates a relational test dataset */ public static Instances makeTestDatasetRelational( int seed, int numInstances, int numClasses, int classType, int classIndex, int numRelationalNominal, int numRelationalString, int numRelationalNumeric, int numInstancesRelational) throws Exception { TestInstances testset = new TestInstances(); testset.setSeed(seed); testset.setNumInstances(numInstances); testset.setNumClasses(numClasses); testset.setClassType(classType); testset.setClassIndex(classIndex); testset.setNumClasses(numClasses); testset.setMultiInstance(false); testset.setNumNominal(0); testset.setNumNumeric(0); testset.setNumString(0); testset.setNumDate(0); testset.setNumRelational(1); testset.setNumRelationalNominal(numRelationalNominal); testset.setNumRelationalString(numRelationalString); testset.setNumRelationalNumeric(numRelationalNumeric); testset.setNumInstancesRelational(numInstancesRelational); final Instances generated = testset.generate(); // Remove random instances Random rand = new Random(42); for (Instance datum : generated) { final Instances rel = datum.relationalValue(0); RemovePercentage rp = new RemovePercentage(); rp.setInputFormat(rel); rp.setPercentage(rand.nextDouble() * 100); final Instances rel2 = Filter.useFilter(rel, rp); final int i = generated.attribute(0).addRelation(rel2); datum.setValue(0, i); } return generated; }