weka.filters.Filter Java Examples

The following examples show how to use weka.filters.Filter. 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: Dl4JMlpFilterTest.java    From wekaDeeplearning4j with GNU General Public License v3.0 6 votes vote down vote up
protected void checkLayer(Dl4jMlpClassifier clf, Instances instances, String[] transformationLayerNames,
    String clfPath, boolean useZooModel) throws Exception {
  Instances activationsExpected = clf.getActivationsAtLayers(transformationLayerNames, instances);
  Dl4jMlpFilter filter = new Dl4jMlpFilter();
  // Load the MNIST III if we're being called on the MNIST dataset (dataset is in meta format (String, class))
  if (ImageInstanceIterator.isMetaArff(instances))
    filter.setInstanceIterator(DatasetLoader.loadMiniMnistImageIterator());
  filter.setSerializedModelFile(new File(clfPath));
  filter.setTransformationLayerNames(transformationLayerNames);
  filter.setInputFormat(instances);
  filter.setPoolingType(PoolingType.NONE);

  Instances activationsActual = Filter.useFilter(instances, filter);

  for (int i = 0; i < activationsActual.size(); i++) {
    Instance expected = activationsExpected.get(i);
    Instance actual = activationsActual.get(i);
    for (int j = 0; j < expected.numAttributes(); j++) {
      assertEquals(expected.value(j), actual.value(j), 1e-6);
    }
  }
}
 
Example #2
Source File: PrepareClassAttributes.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (args.length != 3)
    throw new IllegalArgumentException("Required parameters: <input> <attribute_indices> <output>");

  System.out.println("Loading input data: " + args[0]);
  Instances input = DataSource.read(args[0]);

  System.out.println("Applying filter using indices: " + args[1]);
  MekaClassAttributes filter = new MekaClassAttributes();
  filter.setAttributeIndices(args[1]);
  filter.setInputFormat(input);
  Instances output = Filter.useFilter(input, filter);

  System.out.println("Saving filtered data to: " + args[2]);
  ArffSaver saver = new ArffSaver();
  saver.setFile(new File(args[2]));
  DataSink.write(saver, output);
}
 
Example #3
Source File: Filter.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Filters an entire set of instances through a filter and returns
 * the new set. 
 *
 * @param data the data to be filtered
 * @param filter the filter to be used
 * @return the filtered set of data
 * @throws Exception if the filter can't be used successfully
 */
public static Instances useFilter(Instances data,
		    Filter filter) throws Exception {
  /*
  System.err.println(filter.getClass().getName() 
                     + " in:" + data.numInstances());
  */
  for (int i = 0; i < data.numInstances(); i++) {
    filter.input(data.instance(i));
  }
  filter.batchFinished();
  Instances newData = filter.getOutputFormat();
  Instance processed;
  while ((processed = filter.output()) != null) {
    newData.add(processed);
  }

  /*
  System.err.println(filter.getClass().getName() 
                     + " out:" + newData.numInstances());
  */
  return newData;
}
 
Example #4
Source File: SentimentAnalyser.java    From sentiment-analysis with Apache License 2.0 6 votes vote down vote up
/**Decides upon a "disagreed" document by applying the learned model based on the last 1,000 "agreed" documents.*/
private String clarifyOnSlidingWindow(String tweet){
	String out = "";
       double[] instanceValues = new double[train.numAttributes()];
       instanceValues[0] = train.attribute(0).addStringValue(tweet);
	train.add(new SparseInstance(1.0, instanceValues));
	try {
		stwv.setInputFormat(train);
		Instances newData = Filter.useFilter(train, stwv);
		Instances train_ins = new Instances(newData, 0, train.size()-1);
		Instances test_ins = new Instances(newData, train.size()-1, 1);
		Classifier mnb = (Classifier)new NaiveBayesMultinomial();
		mnb.buildClassifier(train_ins);
		double[] preds = mnb.distributionForInstance(test_ins.get(0));
		if (preds[0]>0.5)
			out = "positive";
		else
			out = "negative";
	} catch (Exception e) {
		e.printStackTrace();
	}
	train.remove(train.numInstances()-1);
	return out;
}
 
Example #5
Source File: LMTNode.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
    *Determines the optimum number of LogitBoost iterations to perform by building a standalone logistic 
    *regression function on the training data. Used for the heuristic that avoids cross-validating this
    *number again at every node.
    *@param data training instances for the logistic model
    *@throws Exception if something goes wrong
    */
   protected int tryLogistic(Instances data) throws Exception{

//convert nominal attributes
Instances filteredData = new Instances(data);	
NominalToBinary nominalToBinary = new NominalToBinary();			
nominalToBinary.setInputFormat(filteredData);
filteredData = Filter.useFilter(filteredData, nominalToBinary);	

LogisticBase logistic = new LogisticBase(0,true,m_errorOnProbabilities);

//limit LogitBoost to 200 iterations (speed)
logistic.setMaxIterations(200);
       logistic.setWeightTrimBeta(getWeightTrimBeta()); // Not in Marc's code. Added by Eibe.
       logistic.setUseAIC(getUseAIC());
logistic.buildClassifier(filteredData);

//return best number of iterations
return logistic.getNumRegressions(); 
   }
 
Example #6
Source File: SummaryStats.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) {
/**Debug code to test SummaryStats generation: **/
	
		
            try{
                Instances test=DatasetLoading.loadDataNullable("C:\\Users\\ajb\\Dropbox\\TSC Problems\\Beef\\Beef_TRAIN");
//                Instances filter=new SummaryStats().process(test);
               SummaryStats m=new SummaryStats();
               m.setInputFormat(test);
               Instances filter=Filter.useFilter(test,m);
               System.out.println(filter);
            }
            catch(Exception e){
               System.out.println("Exception thrown ="+e);
               e.printStackTrace();
               
            }
        }
 
Example #7
Source File: Filter.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
  * runs the filter instance with the given options.
  * 
  * @param filter	the filter to run
  * @param options	the commandline options
  */
 public static void runFilter(Filter filter, String[] options) {
   try {
     if (Utils.getFlag('b', options)) {
Filter.batchFilterFile(filter, options);
     } else {
Filter.filterFile(filter, options);
     }
   } catch (Exception e) {
     if (    (e.toString().indexOf("Help requested") == -1) 
   && (e.toString().indexOf("Filter options") == -1) )
e.printStackTrace();
     else
System.err.println(e.getMessage());
   }
 }
 
Example #8
Source File: AddCluster.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
  * filters all attributes that should be ignored.
  * 
  * @param data	the data to filter
  * @return		the filtered data
  * @throws Exception	if filtering fails
  */
 protected Instances removeIgnored(Instances data) throws Exception {
   Instances result = data;
   
   if (m_IgnoreAttributesRange != null || data.classIndex() >= 0) {
     m_removeAttributes = new Remove();
     String rangeString = "";
     if (m_IgnoreAttributesRange != null) {
rangeString += m_IgnoreAttributesRange.getRanges();
     }
     if (data.classIndex() >= 0) {
if (rangeString.length() > 0) {
  rangeString += "," + (data.classIndex() + 1);
} else {
  rangeString = "" + (data.classIndex() + 1);
}
     }
     ((Remove) m_removeAttributes).setAttributeIndices(rangeString);
     ((Remove) m_removeAttributes).setInvertSelection(false);
     m_removeAttributes.setInputFormat(data);
     result = Filter.useFilter(data, m_removeAttributes);
   }
   
   return result;
 }
 
Example #9
Source File: Cluster.java    From chuidiang-ejemplos with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Instances data = GenerateTestVessels.getData();
    data.setClassIndex(-1); // No class index.

    Remove rm = new Remove();
    rm.setAttributeIndices("1");
    rm.setInputFormat(data);
    data = Filter.useFilter(data,rm);
    System.out.println(data);


    EM cw = new EM();

    cw.buildClusterer(data);
    System.out.println(cw);

    System.out.println(cw.clusterInstance(data.firstInstance()));
}
 
Example #10
Source File: PartitionedMultiFilter.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * returns the filter classname and the options as one string.
 *
 * @param filter	the filter to get the specs for
 * @return		the classname plus options
 */
protected String getFilterSpec(Filter filter) {
  String        result;

  if (filter == null) {
    result = "";
  }
  else {
    result  = filter.getClass().getName();
    if (filter instanceof OptionHandler)
      result += " "
        + Utils.joinOptions(((OptionHandler) filter).getOptions());
  }

  return result;
}
 
Example #11
Source File: LibLINEAR.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * turns on nominal to binary filtering
 * if there are not only numeric attributes
 */
private Instances nominalToBinary( Instances insts ) throws Exception {
  boolean onlyNumeric = true;
  for (int i = 0; i < insts.numAttributes(); i++) {
    if (i != insts.classIndex()) {
      if (!insts.attribute(i).isNumeric()) {
        onlyNumeric = false;
        break;
      }
    }
  }

  if (!onlyNumeric) {
    m_NominalToBinary = new NominalToBinary();
    m_NominalToBinary.setInputFormat(insts);
    insts = Filter.useFilter(insts, m_NominalToBinary);
  }
  return insts;
}
 
Example #12
Source File: MIOptimalBall.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Computes the distribution for a given multiple instance
 *
 * @param newBag the instance for which distribution is computed
 * @return the distribution
 * @throws Exception if the distribution can't be computed successfully
 */
public double[] distributionForInstance(Instance newBag)
  throws Exception {  

  double [] distribution = new double[2];	
  double distance; 
  distribution[0]=0;	 
  distribution[1]=0;

  Instances insts = new Instances(newBag.dataset(),0);
  insts.add(newBag);  

  // Filter instances 
  insts= Filter.useFilter( insts, m_ConvertToSI); 	
  if (m_Filter!=null) 
    insts = Filter.useFilter(insts, m_Filter);     

  //calculate the distance from each single instance to the ball center
  int numInsts = insts.numInstances(); 		
  insts.deleteAttributeAt(0); //remove the bagIndex attribute, no use for the distance calculation

  for (int i=0; i<numInsts; i++){
    distance =0;	   
    for (int j=0; j<insts.numAttributes()-1; j++)
      distance += (insts.instance(i).value(j) - m_Center[j])*(insts.instance(i).value(j)-m_Center[j]);  

    if (distance <=m_Radius*m_Radius){  // check whether this single instance is inside the ball
      distribution[1]=1.0;  //predicted as a positive bag   	  
      break;
    }	
  }

  distribution[0]= 1-distribution[1]; 

  return distribution; 
}
 
Example #13
Source File: TransformExamples.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public static Instances acfTransform(Instances data){
       ACF acf=new ACF();
acf.setMaxLag(data.numAttributes()/4);
       Instances acfTrans=null;
       try{
           acf.setInputFormat(data);
           acfTrans=Filter.useFilter(data, acf);
       }catch(Exception e){
               System.out.println(" Exception in ACF harness="+e);
	e.printStackTrace();
              System.exit(0);
       }
     
           return acfTrans;
   }
 
Example #14
Source File: MIEMDD.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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 #15
Source File: MIDD.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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 #16
Source File: LMTNode.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
    * Returns a numeric version of a set of instances.
    * All nominal attributes are replaced by binary ones, and the class variable is replaced
    * by a pseudo-class variable that is used by LogitBoost.
    */
   protected Instances getNumericData(Instances train) throws Exception{

Instances filteredData = new Instances(train);	
m_nominalToBinary = new NominalToBinary();			
m_nominalToBinary.setInputFormat(filteredData);
filteredData = Filter.useFilter(filteredData, m_nominalToBinary);	

return super.getNumericData(filteredData);
   }
 
Example #17
Source File: FilteredCollectiveClassifier.java    From collective-classification-weka-package with GNU General Public License v3.0 5 votes vote down vote up
/**
 * builds the necessary CollectiveInstances from the given Instances
 * @throws Exception if anything goes wrong
 */
@Override
protected void generateSets() throws Exception {
  super.generateSets();

  // setup filter
  m_Filter.setInputFormat(m_Trainset);

  // filter datasets
  m_TrainsetNew = Filter.useFilter(m_Trainset, m_Filter);
  m_TestsetNew  = Filter.useFilter(m_Testset,  m_Filter);
}
 
Example #18
Source File: TweetToEmbeddingsFeatureVectorTest.java    From AffectiveTweets with GNU General Public License v3.0 5 votes vote down vote up
/** Creates a default TweetToEmbeddingsFeatureVector filter */
   public Filter getFilter() {
Filter f = null;

// Check to see if the test is run from directory containing build_package.xml
if ((new File(".." + File.separator + "AffectiveTweets" + File.separator + "build_package.xml")).exists()) {
    File backup = weka.core.WekaPackageManager.PACKAGES_DIR;
    weka.core.WekaPackageManager.PACKAGES_DIR = new java.io.File(".."); // So that default lexicon, etc., is found.
    f = new TweetToEmbeddingsFeatureVector();
    weka.core.WekaPackageManager.PACKAGES_DIR = backup;
} else {
    f = new TweetToEmbeddingsFeatureVector(); // Hope that the package is installed.
}
return f;
   }
 
Example #19
Source File: PLSClassifier.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Set the PLS filter (only used for setup).
 *
 * @param value	the kernel filter.
 * @throws Exception	if not PLSFilter
 */
public void setFilter(Filter value) throws Exception {
  if (!(value instanceof PLSFilter))
    throw new Exception("Filter has to be PLSFilter!");
  else
    m_Filter = (PLSFilter) value;
}
 
Example #20
Source File: MDD.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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 #21
Source File: NBTreeNoSplit.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Build the no-split node
 *
 * @param instances an <code>Instances</code> value
 * @exception Exception if an error occurs
 */
public final void buildClassifier(Instances instances) throws Exception {
  m_nb = new NaiveBayesUpdateable();
  m_disc = new Discretize();
  m_disc.setInputFormat(instances);
  Instances temp = Filter.useFilter(instances, m_disc);
  m_nb.buildClassifier(temp);
  if (temp.numInstances() >= 5) {
    m_errors = crossValidate(m_nb, temp, new Random(1));
  }
  m_numSubsets = 1;
}
 
Example #22
Source File: MekaClassAttributesTest.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a specialized MekaClassAttributes.
 *
 * @param range the range of attributes to use
 */
public Filter getFilter(String range) {
	MekaClassAttributes af = new MekaClassAttributes();
	try {
		af.setAttributeIndices(range);
	}
	catch (Exception e) {
		fail("Failed to set set range '" + range + "': " + e);
	}
	return af;
}
 
Example #23
Source File: PreprocessTab.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Filters the data with the specified filter.
 *
 * @param filter the filter to push the data through
 * @param newName the new relation name, null if to keep current
 */
protected void filterData(final Filter filter, final String newName) {
	Runnable	run;

	log("Filtering data: " + OptionUtils.toCommandLine(filter));

	run = new Runnable() {
		@Override
		public void run() {
			try {
				String relName = getData().relationName();
				filter.setInputFormat(getData());
				Instances filtered = Filter.useFilter(getData(), filter);
				if (newName == null)
					filtered.setRelationName(relName);
				else
					filtered.setRelationName(newName);
				getOwner().notifyTabsDataChanged(PreprocessTab.this, filtered);
				setData(filtered);
				log("Data filtered successfully!");
			}
			catch (Exception e) {
				throw new IllegalStateException(e);
			}
		}
	};
	start(run);
}
 
Example #24
Source File: WekaFeatureSelectionTest.java    From Java-Data-Science-Cookbook with MIT License 5 votes vote down vote up
public void selectFeaturesWithFilter(){
	weka.filters.supervised.attribute.AttributeSelection filter = new weka.filters.supervised.attribute.AttributeSelection();
    CfsSubsetEval eval = new CfsSubsetEval();
    BestFirst search = new BestFirst();
    filter.setEvaluator(eval);
    filter.setSearch(search);
    try {
		filter.setInputFormat(iris);
		Instances newData = Filter.useFilter(iris, filter);
		System.out.println(newData);
	} catch (Exception e) {
	}
}
 
Example #25
Source File: FilteredSubsetEval.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Parses a given list of options. <p/>
  *
  <!-- options-start -->
  * Valid options are: <p/>
  * 
  * <pre> -W &lt;evaluator specification&gt;
  *  Full name of base evaluator to use, followed by evaluator options.
  *  eg: "weka.attributeSelection.CfsSubsetEval -L"</pre>
  * 
  * <pre> -F &lt;filter specification&gt;
  *  Full class name of filter to use, followed
  *  by filter options.
  *  eg: "weka.filters.supervised.instance.SpreadSubsample -M 1"</pre>
  * 
  <!-- options-end -->  
  *
  * @param options the list of options as an array of strings
  * @throws Exception if an option is not supported
  */
 public void setOptions(String[] options) throws Exception {
   String evaluator = Utils.getOption('W', options);
   
   if (evaluator.length() > 0) { 
     String[] evaluatorSpec = Utils.splitOptions(evaluator);
     if (evaluatorSpec.length == 0) {
       throw new IllegalArgumentException("Invalid evaluator specification string");
     }
     
     String evaluatorName = evaluatorSpec[0];
     evaluatorSpec[0] = "";
     setSubsetEvaluator((ASEvaluation)Utils.forName(SubsetEvaluator.class,
                                                    evaluatorName, evaluatorSpec));

   } else {      
     setSubsetEvaluator(new CfsSubsetEval());
   }

   // Same for filter
   String filterString = Utils.getOption('F', options);
   if (filterString.length() > 0) {
     String [] filterSpec = Utils.splitOptions(filterString);
     if (filterSpec.length == 0) {
throw new IllegalArgumentException("Invalid filter specification string");
     }
     String filterName = filterSpec[0];
     filterSpec[0] = "";
     setFilter((Filter) Utils.forName(Filter.class, filterName, filterSpec));
   } else {
     setFilter(new weka.filters.supervised.instance.SpreadSubsample());
   }
 }
 
Example #26
Source File: Dl4JMlpFilterTest.java    From wekaDeeplearning4j with GNU General Public License v3.0 5 votes vote down vote up
protected void checkZooModelMNIST(AbstractZooModel zooModel, boolean isMeta) throws Exception {
  Dl4jMlpFilter myFilter = new Dl4jMlpFilter();
  Dl4jMlpClassifier clf = new Dl4jMlpClassifier();
  Instances instances = null;
  AbstractInstanceIterator iterator = null;
  // Load the MNIST meta arff with ImageInstanceIterator
  if (isMeta) {
    instances = DatasetLoader.loadMiniMnistMeta();
    iterator = DatasetLoader.loadMiniMnistImageIterator();
  } else {
    // Load the Convolutional version of MNIST
    instances = DatasetLoader.loadMiniMnistArff();
    iterator = new ConvolutionInstanceIterator();
    ((ConvolutionInstanceIterator) iterator).setNumChannels(1);
    ((ConvolutionInstanceIterator) iterator).setHeight(28);
    ((ConvolutionInstanceIterator) iterator).setWidth(28);
  }

  clf.setInstanceIterator(iterator);
  myFilter.setInstanceIterator(iterator);

  // Testing pretrained model, no point training for 1 epoch
  clf.setNumEpochs(0);
  clf.setZooModel(zooModel);
  clf.buildClassifier(instances);

  Instances activationsExpected = clf.getActivationsAtLayers(new String[] { zooModel.getFeatureExtractionLayer() }, instances);

  myFilter.setZooModelType(zooModel);
  myFilter.setInputFormat(instances);
  Instances activationsActual = Filter.useFilter(instances, myFilter);

  for (int i = 0; i < activationsActual.size(); i++) {
    Instance expected = activationsExpected.get(i);
    Instance actual = activationsActual.get(i);
    for (int j = 0; j < expected.numAttributes(); j++) {
      assertEquals(expected.value(j), actual.value(j), 1e-6);
    }
  }
}
 
Example #27
Source File: PrincipalComponents.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Fill the correlation matrix
 */
private void fillCorrelation() throws Exception {
  m_correlation = new double[m_numAttribs][m_numAttribs];
  double [] att1 = new double [m_numInstances];
  double [] att2 = new double [m_numInstances];
  double corr;

  for (int i = 0; i < m_numAttribs; i++) {
    for (int j = 0; j < m_numAttribs; j++) {
      for (int k = 0; k < m_numInstances; k++) {
        att1[k] = m_trainInstances.instance(k).value(i);
        att2[k] = m_trainInstances.instance(k).value(j);
      }
      if (i == j) {
        m_correlation[i][j] = 1.0;
          // store the standard deviation
        m_stdDevs[i] = Math.sqrt(Utils.variance(att1));
      } else {
        corr = Utils.correlation(att1,att2,m_numInstances);
        m_correlation[i][j] = corr;
        m_correlation[j][i] = corr;
      }
    }
  }
  
  // now standardize the input data
  m_standardizeFilter = new Standardize();
  m_standardizeFilter.setInputFormat(m_trainInstances);
  m_trainInstances = Filter.useFilter(m_trainInstances, m_standardizeFilter);
}
 
Example #28
Source File: FilteredAttributeEval.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Get the filter + options as a string
  *
  * @return a String containing the name of the filter + any options
  */
 protected String getFilterSpec() {
   Filter c = getFilter();
   if (c instanceof OptionHandler) {
     return c.getClass().getName() + " "
+ Utils.joinOptions(((OptionHandler)c).getOptions());
   }
   return c.getClass().getName();
 }
 
Example #29
Source File: FilteredAttributeEval.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Parses a given list of options. <p/>
  *
  <!-- options-start -->
  * Valid options are: <p/>
  * 
  * <pre> -W &lt;evaluator specification&gt;
  *  Full name of base evaluator to use, followed by evaluator options.
  *  eg: "weka.attributeSelection.InfoGainAttributeEval -M"</pre>
  * 
  * <pre> -F &lt;filter specification&gt;
  *  Full class name of filter to use, followed
  *  by filter options.
  *  eg: "weka.filters.supervised.instance.SpreadSubsample -M 1"</pre>
  * 
  <!-- options-end -->  
  *
  * @param options the list of options as an array of strings
  * @throws Exception if an option is not supported
  */
 public void setOptions(String[] options) throws Exception {
   String evaluator = Utils.getOption('W', options);
   
   if (evaluator.length() > 0) { 
     String[] evaluatorSpec = Utils.splitOptions(evaluator);
     if (evaluatorSpec.length == 0) {
       throw new IllegalArgumentException("Invalid evaluator specification string");
     }
     
     String evaluatorName = evaluatorSpec[0];
     evaluatorSpec[0] = "";
     setAttributeEvaluator((ASEvaluation)Utils.forName(AttributeEvaluator.class,
                                                       evaluatorName, evaluatorSpec));

   } else {      
     setAttributeEvaluator(new InfoGainAttributeEval());
   }

   // Same for filter
   String filterString = Utils.getOption('F', options);
   if (filterString.length() > 0) {
     String [] filterSpec = Utils.splitOptions(filterString);
     if (filterSpec.length == 0) {
throw new IllegalArgumentException("Invalid filter specification string");
     }
     String filterName = filterSpec[0];
     filterSpec[0] = "";
     setFilter((Filter) Utils.forName(Filter.class, filterName, filterSpec));
   } else {
     setFilter(new weka.filters.supervised.instance.SpreadSubsample());
   }
 }
 
Example #30
Source File: Dl4jMlpClassifier.java    From wekaDeeplearning4j with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Apply the filters to the given Instances
 *
 * @param insts Instances that are going to be filtered
 * @return Filtered Instances
 * @throws Exception Filter could not be applied
 */
protected Instances applyFilters(Instances insts) throws Exception {
  // Filter the instance
  insts = Filter.useFilter(insts, replaceMissingFilter);
  insts = Filter.useFilter(insts, nominalToBinaryFilter);
  if (filter != null) {
    insts = Filter.useFilter(insts, filter);
  }
  return insts;
}