类weka.core.Attribute源码实例Demo

下面列出了怎么用weka.core.Attribute的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: tsml   文件: ChangeDateFormat.java
/**
  * Set the output format. Changes the format of the specified date
  * attribute.
  */
 private void setOutputFormat() {
   
   // Create new attributes
   FastVector newAtts = new FastVector(getInputFormat().numAttributes());
   for (int j = 0; j < getInputFormat().numAttributes(); j++) {
     Attribute att = getInputFormat().attribute(j);
     if (j == m_AttIndex.getIndex()) {
newAtts.addElement(new Attribute(att.name(), getDateFormat().toPattern()));  
     } else {
newAtts.addElement(att.copy()); 
     }
   }
     
   // Create new header
   Instances newData = new Instances(getInputFormat().relationName(), newAtts, 0);
   newData.setClassIndex(getInputFormat().classIndex());
   m_OutputAttribute = newData.attribute(m_AttIndex.getIndex());
   setOutputFormat(newData);
 }
 
源代码2 项目: AILibs   文件: PerformanceKnowledgeBase.java
private Attribute getAttribute(final ComponentInstance ci, final Parameter parameter) {
	IParameterDomain domain = parameter.getDefaultDomain();
	if (domain instanceof CategoricalParameterDomain) {
		CategoricalParameterDomain catDomain = (CategoricalParameterDomain) domain;
		return new Attribute(ci.getComponent().getName() + "::" + parameter.getName(), Arrays.asList(catDomain.getValues()));
	} else if (domain instanceof NumericParameterDomain) {
		NumericParameterDomain numDomain = (NumericParameterDomain) domain;
		String range = "[" + numDomain.getMin() + "," + numDomain.getMax() + "]";
		Properties prop = new Properties();
		prop.setProperty("range", range);
		ProtectedProperties metaInfo = new ProtectedProperties(prop);
		return new Attribute(ci.getComponent().getName() + "::" + parameter.getName(), metaInfo);
	} else {
		return null;
	}
}
 
源代码3 项目: tsml   文件: Reorder.java
/**
  * Sets the format of the input instances.
  *
  * @param instanceInfo an Instances object containing the input instance
  * structure (any instances contained in the object are ignored - only the
  * structure is required).
  * @return true if the outputFormat may be collected immediately
  * @throws Exception if a problem occurs setting the input format
  */
 public boolean setInputFormat(Instances instanceInfo) throws Exception {
   super.setInputFormat(instanceInfo);
   
   FastVector attributes = new FastVector();
   int outputClass = -1;
   m_SelectedAttributes = determineIndices(instanceInfo.numAttributes());
   for (int i = 0; i < m_SelectedAttributes.length; i++) {
     int current = m_SelectedAttributes[i];
     if (instanceInfo.classIndex() == current) {
outputClass = attributes.size();
     }
     Attribute keep = (Attribute)instanceInfo.attribute(current).copy();
     attributes.addElement(keep);
   }
   
   initInputLocators(instanceInfo, m_SelectedAttributes);

   Instances outputFormat = new Instances(instanceInfo.relationName(),
				   attributes, 0); 
   outputFormat.setClassIndex(outputClass);
   setOutputFormat(outputFormat);
   
   return true;
 }
 
源代码4 项目: tsml   文件: MultivariateProcessing.java
public static void exampleUsage(){
//        ECGActivities
            Instances train,test;
            train=DatasetLoading.loadData("Z:\\Data\\MultivariateTSCProblems\\ECGActivities\\ECGActivities_TRAIN");
            test=DatasetLoading.loadData("Z:\\Data\\MultivariateTSCProblems\\ECGActivities\\ECGActivities_TEST");

//        Instances[] split=InstanceTools.resampleTrainAndTestInstances(train, test, 1);
            Instances[] split=MultivariateInstanceTools.resampleMultivariateTrainAndTestInstances(train, test, 1);
            System.out.println("IS it relational ? "+split[0].checkForAttributeType(Attribute.RELATIONAL));
            System.out.println("Fold 1 TRAIN num instances "+split[0].numInstances()+" Num atts ="+(split[0].numAttributes()-1));
//        System.out.println(split[0]+"");
            System.out.println("Fold 1 TRAIN  instance 1 num dimensions "+split[0].instance(0).relationalValue(0).numInstances()+" series length "+split[0].instance(0).relationalValue(0).numAttributes());
            for(Instance ins:split[0])
                System.out.println("Fold TRAIN  instance num dimensions "+ins.relationalValue(0).numInstances()+" series length "+ins.relationalValue(0).numAttributes());

        }
 
源代码5 项目: tsml   文件: ActiveHNode.java
@Override
public void updateNode(Instance inst) throws Exception {
  super.updateDistribution(inst);

  for (int i = 0; i < inst.numAttributes(); i++) {
    Attribute a = inst.attribute(i);
    if (i != inst.classIndex()) {
      ConditionalSufficientStats stats = m_nodeStats.get(a.name());
      if (stats == null) {
        if (a.isNumeric()) {
          stats = new GaussianConditionalSufficientStats();
        } else {
          stats = new NominalConditionalSufficientStats();
        }
        m_nodeStats.put(a.name(), stats);
      }

      stats
          .update(inst.value(a),
              inst.classAttribute().value((int) inst.classValue()),
              inst.weight());
    }
  }
}
 
源代码6 项目: tsml   文件: Test.java
/**
 * Returns the test represented by a string in Prolog notation.
 *
 * @return a string representing the test in Prolog notation
 */   
public String toPrologString() {
  Attribute att = m_Dataset.attribute(m_AttIndex);
  StringBuffer str = new StringBuffer();
  String attName = m_Dataset.attribute(m_AttIndex).name();
  if (att.isNumeric()) {
    str = str.append(attName + " ");
    if (m_Not) str = str.append(">= " + Utils.doubleToString(m_Split, 3));
    else str = str.append("< " + Utils.doubleToString(m_Split, 3));
  } else {
    String value = att.value((int)m_Split);
  
    if (value == "false") { str = str.append("not(" + attName + ")"); }      
    else { str = str.append(attName); }
  }
return str.toString();
}
 
源代码7 项目: meka   文件: MLUtils.java
/**
 * InsertZintoD - Insert data Z[][] to Instances D (e.g., as labels).
 * NOTE: Assumes binary labels!
 * @see #addZtoD(Instances, double[][], int)
 */
private static Instances insertZintoD(Instances D, double Z[][]) {

	int L = Z[0].length;

	// add attributes
	for(int j = 0; j < L; j++) {
		D.insertAttributeAt(new Attribute("c"+j,Arrays.asList(new String[]{"0","1"})),j);
	}

	// add values Z[0]...Z[N] to D
	// (note that if D.numInstances() < Z.length, only some are added)
	for(int j = 0; j < L; j++) {
		for(int i = 0; i < D.numInstances(); i++) {
			D.instance(i).setValue(j,Z[i][j] > 0.5 ? 1.0 : 0.0);
		}
	}

	D.setClassIndex(L);
	return D;
}
 
源代码8 项目: NLIWOD   文件: QuestionWord.java
public QuestionWord() {
	
	ArrayList<String> attributeValues = new ArrayList<String>();
	attributeValues.add("Who");
	attributeValues.add("What");
	attributeValues.add("When");
	attributeValues.add("Where");
	attributeValues.add("Which");
	attributeValues.add(Commands);
	attributeValues.add(AuxVerb);
	attributeValues.add("How");
	attributeValues.add("Misc");

	attribute = new Attribute("QuestionWord", attributeValues);
	
}
 
源代码9 项目: AILibs   文件: WekaTimeseriesUtil.java
/**
 * Converts a double[][] matrix (number of instances x number of attributes) to
 * Weka instances without any class attribute.
 *
 * @param matrix
 *            The double[][] matrix storing all the attribute values of the
 *            instances
 * @return Returns the Weka Instances object consisting of all instances and the
 *         attribute values
 */
public static Instances matrixToWekaInstances(final double[][] matrix) {
	final ArrayList<Attribute> attributes = new ArrayList<>();
	for (int i = 0; i < matrix[0].length; i++) {
		final Attribute newAtt = new Attribute("val" + i);
		attributes.add(newAtt);
	}
	Instances wekaInstances = new Instances(I_NAME, attributes, matrix.length);
	for (int i = 0; i < matrix[0].length; i++) {
		final Instance inst = new DenseInstance(1, matrix[i]);
		inst.setDataset(wekaInstances);
		wekaInstances.add(inst);
	}

	return wekaInstances;
}
 
源代码10 项目: NLIWOD   文件: Analyzer.java
/**
 * Retrieves the occurrences of all part-of-speech tags from the question. Then iterates over the HashMap and sets all 36 attributes for the instance.
 * @param tmpInstance
 * @param analyzer
 * @param q
 */
private void analyzePOS(Instance tmpInstance, PartOfSpeechTags analyzer, String q) {
	@SuppressWarnings("unchecked")
	LinkedHashMap<String,Integer> map = (LinkedHashMap<String, Integer>) analyzer.analyze(q);
	
	ArrayList<Attribute> attributes = analyzer.getAttributes();
	for(String ind: map.keySet()) {
		Attribute a = null;
		//searches for the attribute object of the HashMap entry
		for(Attribute att: attributes) {
			if(att.name().equals(ind)) {
				a = att;
				break;
			}
		}
		tmpInstance.setValue(a, map.get(ind));
	}
}
 
源代码11 项目: AILibs   文件: DataSetUtilsTest.java
public void cifar10InstancesAttributesTest() {
    ArrayList<Attribute> atts = new ArrayList<>();
    for (int i = 0; i < 32 * 32 * 3 + 1; i++) {
        atts.add(new Attribute("blub" + i));
    }
    Instances instances = new Instances("test", atts, 1);
    DenseInstance inst = new DenseInstance(atts.size());
    for (int i = 0; i < inst.numAttributes(); i++) {
        inst.setValue(i, 1d);
    }
    inst.setDataset(instances);
    instances.add(inst);

    INDArray result = DataSetUtils.cifar10InstanceToMatrix(inst);
    Assert.assertArrayEquals(new long[]{32, 32, 3}, result.shape());
}
 
源代码12 项目: tsml   文件: CheckClassifier.java
/**
 * Begin the tests, reporting results to System.out
 */
public void doTests() {

  if (getClassifier() == null) {
    println("\n=== No classifier set ===");
    return;
  }
  println("\n=== Check on Classifier: "
      + getClassifier().getClass().getName()
      + " ===\n");

  // Start tests
  m_ClasspathProblems = false;
  println("--> Checking for interfaces");
  canTakeOptions();
  boolean updateableClassifier = updateableClassifier()[0];
  boolean weightedInstancesHandler = weightedInstancesHandler()[0];
  boolean multiInstanceHandler = multiInstanceHandler()[0];
  println("--> Classifier tests");
  declaresSerialVersionUID();
  testToString();
  testsPerClassType(Attribute.NOMINAL,    updateableClassifier, weightedInstancesHandler, multiInstanceHandler);
  testsPerClassType(Attribute.NUMERIC,    updateableClassifier, weightedInstancesHandler, multiInstanceHandler);
  testsPerClassType(Attribute.DATE,       updateableClassifier, weightedInstancesHandler, multiInstanceHandler);
  testsPerClassType(Attribute.STRING,     updateableClassifier, weightedInstancesHandler, multiInstanceHandler);
  testsPerClassType(Attribute.RELATIONAL, updateableClassifier, weightedInstancesHandler, multiInstanceHandler);
}
 
/**
 * Tests whether there are errors when training with a small example given as a
 * Instances object and performance values where all values are set.
 * @throws AlgorithmException
 *
 * @throws Exception
 */
@Test
public void testTrainWithInstancesNoUnsetValues() throws AlgorithmException  {
	ArrayList<Attribute> attInfo = new ArrayList<>();
	attInfo.add(new Attribute("Att1"));
	attInfo.add(new Attribute("Att2"));
	attInfo.add(new Attribute("Att3"));
	attInfo.add(new Attribute("Target"));
	Instances data = new Instances("Train", attInfo, 5);
	data.add(new DenseInstance(1, new double[] { 1, -1, -1, 5 }));
	data.add(new DenseInstance(1, new double[] { 1, -1, -1, 7 }));
	data.add(new DenseInstance(1, new double[] { -1, 1, -1, 3 }));
	data.add(new DenseInstance(1, new double[] { -1, -1, 1, 10 }));
	data.add(new DenseInstance(1, new double[] { -1, 1, 1, 30 }));
	data.setClassIndex(attInfo.size() - 1);

	this.logger.info(MSG_BUILD, data);

	boolean successfulTraining = false;
	RandomTreePerformanceBasedFeatureGenerator treeFeatureGen = new RandomTreePerformanceBasedFeatureGenerator();
	treeFeatureGen.disallowNonOccurence();
	treeFeatureGen.setOccurenceValue(1);
	treeFeatureGen.setNonOccurenceValue(-1);
	treeFeatureGen.train(data);

	this.logger.info(MSG_GEN, treeFeatureGen);

	successfulTraining = true;


	assertEquals(true, successfulTraining);
}
 
源代码14 项目: tsml   文件: CheckEstimator.java
int getSetType() throws Exception {			
     int sum = 0;
     int type = -1;
     if (nominal) { sum ++; type = Attribute.NOMINAL; }
     if (numeric) { sum ++; type = Attribute.NUMERIC; }
     if (string) { sum ++; type = Attribute.STRING; }
     if (date) { sum ++; type = Attribute.DATE; }
     if (relational) { sum ++; type = Attribute.RELATIONAL; }
     if (sum > 1)
throw new Exception("Expected to have only one type set used wrongly.");
     if (type < 0)
throw new Exception("No type set.");
     return type;
   }
 
源代码15 项目: meka   文件: ARAMNetworkSparseV.java
/**
 * Classifies the given test instance. The instance has to belong to a
 * dataset when it's being classified. Note that a classifier MUST
 * implement either this or distributionForInstance().
 *
 * @param instance the instance to be classified
 * @return the predicted most likely class for the instance or 
 * Instance.missingValue() if no prediction is made
 * @exception Exception if an error occurred during the prediction
 */
public double classifyInstance(Instance instance) throws Exception {

double[] dist = distributionForInstance(instance);
if (dist == null) {
	throw new Exception("Null distribution predicted");
}
switch (instance.classAttribute().type()) {
	case Attribute.NOMINAL:
		double max = 0;
		int maxIndex = 0;
	
		for (int i = 0; i < dist.length; i++) {
			if (dist[i] > max) {
				maxIndex = i;
				max = dist[i];
			}
		}
		if (max > 0) {
			return maxIndex;
		} else {
		    //return Instance.missingValue();
		}
	case Attribute.NUMERIC:
		return dist[0];
default:
    return -1;
}

}
 
源代码16 项目: AILibs   文件: PerformanceKnowledgeBase.java
public Instance getInstanceForIndividualCI(final String benchmarkName, final ComponentInstance ci, final double score) {
	Instances instancesInd = this.performanceInstancesIndividualComponents.get(benchmarkName).get(ci.getComponent().getName());
	DenseInstance instanceInd = new DenseInstance(instancesInd.numAttributes());
	for (int i = 0; i < instancesInd.numAttributes() - 1; i++) {
		Attribute attr = instancesInd.attribute(i);
		String attrFQN = attr.name();
		String attrName = attrFQN.substring(attrFQN.indexOf("::") + 2);
		Parameter param = ci.getComponent().getParameterWithName(attrName);
		String value;
		if (ci.getParametersThatHaveBeenSetExplicitly().contains(param)) {
			value = ci.getParameterValues().get(param.getName());
		} else {
			value = param.getDefaultValue().toString();
		}
		if (value != null) {
			if (param.isCategorical()) {
				boolean attrContainsValue = false;
				Enumeration<Object> possibleValues = attr.enumerateValues();
				while (possibleValues.hasMoreElements() && !attrContainsValue) {
					Object o = possibleValues.nextElement();
					if (o.equals(value)) {
						attrContainsValue = true;
					}
				}
				if (attrContainsValue) {
					instanceInd.setValue(attr, value);
				}
			} else if (param.isNumeric()) {
				double finalValue = Double.parseDouble(value);
				instanceInd.setValue(attr, finalValue);
			}
		}
	}
	Attribute scoreAttrInd = instancesInd.classAttribute();
	instanceInd.setValue(scoreAttrInd, score);
	return instanceInd;
}
 
源代码17 项目: tsml   文件: TSBF.java
Instances formatProbabilityInstances(double[][] probs,Instances data){
        int numClasses=data.numClasses();
        int numFeatures=(numClasses-1)*numSubSeries;
        //Set up instances size and format. 
        FastVector atts=new FastVector();
        String name;
        for(int j=0;j<numFeatures;j++){
                name = "ProbFeature"+j;
                atts.addElement(new Attribute(name));
        }
        //Get the class values as a fast vector			
        Attribute target =data.attribute(data.classIndex());
       FastVector vals=new FastVector(target.numValues());
        for(int j=0;j<target.numValues();j++)
                vals.addElement(target.value(j));
        atts.addElement(new Attribute(data.attribute(data.classIndex()).name(),vals));
//create blank instances with the correct class value                
        Instances result = new Instances("SubsequenceIntervals",atts,data.numInstances());
        result.setClassIndex(result.numAttributes()-1);
        for(int i=0;i<data.numInstances();i++){
            double cval=data.instance(i).classValue();
            DenseInstance in=new DenseInstance(result.numAttributes());
            in.setValue(result.numAttributes()-1,cval);
            int pos=0;
            for(int j=0;j<numSubSeries;j++){
                for(int k=0;k<numClasses-1;k++)
                    in.setValue(pos++, probs[j+numSubSeries*i][k]);
            }           
            result.add(in);
        }
        return result;
    }
 
public ThreeWayMNBTrainer(String outputModel) {
    classifier = new NaiveBayesMultinomialText();
    modelFile = outputModel;

    ArrayList<Attribute> atts = new ArrayList<Attribute>(2);
    ArrayList<String> classVal = new ArrayList<String>();
    classVal.add(SentimentClass.ThreeWayClazz.NEGATIVE.name());
    classVal.add(SentimentClass.ThreeWayClazz.POSITIVE.name());
    atts.add(new Attribute("content",(ArrayList<String>)null));
    atts.add(new Attribute("@@[email protected]@",classVal));

    dataRaw = new Instances("TrainingInstances",atts,10);
}
 
源代码19 项目: tsml   文件: NaiveBayes.java
/**
  * Returns a description of the classifier in the old format.
  *
  * @return a description of the classifier as a string.
  */
 protected String toStringOriginal() {
   
   StringBuffer text = new StringBuffer();

   text.append("Naive Bayes Classifier");
   if (m_Instances == null) {
     text.append(": No model built yet.");
   } else {
     try {
for (int i = 0; i < m_Distributions[0].length; i++) {
  text.append("\n\nClass " + m_Instances.classAttribute().value(i) +
                     ": Prior probability = " + Utils.
                     doubleToString(m_ClassDistribution.getProbability(i),
                                    4, 2) + "\n\n");
  Enumeration enumAtts = m_Instances.enumerateAttributes();
  int attIndex = 0;
  while (enumAtts.hasMoreElements()) {
    Attribute attribute = (Attribute) enumAtts.nextElement();
    if (attribute.weight() > 0) {
      text.append(attribute.name() + ":  " 
                         + m_Distributions[attIndex][i]);
    }
    attIndex++;
  }
}
     } catch (Exception ex) {
text.append(ex.getMessage());
     }
   }

   return text.toString();
 }
 
源代码20 项目: AILibs   文件: InteractingFeatures.java
private Instances getEmptyDataset() {
	if (!this.isPrepared) {
		throw new IllegalStateException("Cannot get empty dataset before preparation");
	}
	ArrayList<Attribute> attributes = new ArrayList<>();
	for (Pair<Integer, Integer> pair : SetUtil.cartesianProduct(this.indicesToInteract, this.indicesToInteract)) {
		if (pair.getX() < pair.getY()) {
			attributes.add(new Attribute("interaction_" + pair.getX() + "_" + pair.getY(), false));
		}
	}
	return new Instances("interaction", attributes, 0);
}
 
源代码21 项目: wekaDeeplearning4j   文件: ZooModelTest.java
private Instances shrinkInstances(Instances data) {
    ArrayList<Attribute> atts = new ArrayList<>();
    for (int i = 0; i < data.numAttributes(); i++) {
        atts.add(data.attribute(i));
    }
    Instances shrunkenData = new Instances("shrinked", atts, 10);
    shrunkenData.setClassIndex(1);
    for (int i = 0; i < 10; i++) {
        Instance inst = data.get(i);
        inst.setClassValue(i % 10);
        inst.setDataset(shrunkenData);
        shrunkenData.add(inst);
    }
    return shrunkenData;
}
 
源代码22 项目: tsml   文件: PACF.java
@Override
protected Instances determineOutputFormat(Instances inputFormat)
                throws Exception {
//Check capabilities for the filter. Can only handle real valued, no missing.       
    getCapabilities().testWithFail(inputFormat);

    seriesLength=inputFormat.numAttributes();	
    if(inputFormat.classIndex()>=0)
        seriesLength--;
    if(maxLag>seriesLength-endTerms)
        maxLag=seriesLength-endTerms;
    if(maxLag<0)
        maxLag=inputFormat.numAttributes()-1;
    //Set up instances size and format. 
    ArrayList<Attribute> atts=new ArrayList<>();
    String name;
    for(int i=0;i<maxLag;i++){
        name = "PACF_"+i;
        atts.add(new Attribute(name));
    }
    if(inputFormat.classIndex()>=0){	//Classification set, set class 
      //Get the class values 		
        Attribute target =inputFormat.attribute(inputFormat.classIndex());
        ArrayList<String> vals=new ArrayList<>(target.numValues());
        for(int i=0;i<target.numValues();i++)
            vals.add(target.value(i));
        atts.add(new Attribute(inputFormat.attribute(inputFormat.classIndex()).name(),vals));
    }	
    Instances result = new Instances("PACF"+inputFormat.relationName(),atts,inputFormat.numInstances());
    if(inputFormat.classIndex()>=0)
        result.setClassIndex(result.numAttributes()-1);
    return result;	
}
 
源代码23 项目: tsml   文件: PowerSpectrum.java
@Override
protected Instances determineOutputFormat(Instances inputFormat)
throws Exception {
    //Set up instances size and format.
    int length=(fftFilter.findLength(inputFormat));
    length/=2;
ArrayList<Attribute> atts=new ArrayList<>();
    String name;
    for(int i=0;i<length;i++){
            name = "PowerSpectrum_"+i;
            atts.add(new Attribute(name));
    }

    if(inputFormat.classIndex()>=0){	//Classification set, set class 
            //Get the class values as a fast vector			
            Attribute target =inputFormat.attribute(inputFormat.classIndex());

            ArrayList<String> vals=new ArrayList(target.numValues());
            for(int i=0;i<target.numValues();i++)
                    vals.add(target.value(i));
            atts.add(new Attribute(inputFormat.attribute(inputFormat.classIndex()).name(),vals));
    }	
    Instances result = new Instances("PowerSpectrum"+inputFormat.relationName(),atts,inputFormat.numInstances());
    if(inputFormat.classIndex()>=0)
            result.setClassIndex(result.numAttributes()-1);

    return result;
}
 
源代码24 项目: anthelion   文件: AnthOnlineClassifier.java
/**
 * Internal function which initialized the {@link Instances} used by the
 * {@link Classifier} wrapped by the {@link AnthOnlineClassifier} class.
 */
private void initInstances() {
	// gather attributes
	ArrayList<Attribute> attributes = new ArrayList<Attribute>();
	ArrayList<String> allowedClasses = new ArrayList<String>();
	allowedClasses.add("sem");
	allowedClasses.add("nonsem");
	Attribute classAttribute = new Attribute("class", allowedClasses);
	attributes.add(classAttribute);
	// this looks somehow stupid to me :/
	List<String> vector = null;
	attributes.add(new Attribute("domain", vector));
	attributes.add(new Attribute("sempar"));
	attributes.add(new Attribute("nonsempar"));
	attributes.add(new Attribute("semsib"));
	attributes.add(new Attribute("nonsemsib"));
	for (int i = 0; i < hashTrickSize; i++) {
		// the boolAttValues here should not be necessary but based on some
		// runtime experiements they make a (slight) difference as it is not
		// possible to create directly boolean attributes. The time to
		// define a split is reduced by doing this with nominal.
		attributes.add(new Attribute(getAttributeNameOfHash(i),
				boolAttValues));
	}
	// now we create the Instances
	instances = new Instances("Anthelion", attributes, 1);
	instances.setClass(classAttribute);
	attributesIndex = new HashMap<String, Integer>();
	for (int i = 0; i < attributes.size(); i++) {
		attributesIndex.put(attributes.get(i).name(), i);
	}
	// set dimension (class + domain + 4xgraph + hashes)
	dimension = 1 + 1 + 4 + hashTrickSize;
	// init replacement array
	replaceMissingValues = new double[dimension];
	for (int i = 0; i < dimension; i++) {
		replaceMissingValues[i] = 0.0;
	}
}
 
源代码25 项目: sentiment-analysis   文件: SentimentAnalyser.java
/**Constructor. 
 * "main_folder" is provided in order to define the initial directory to work on; 
 * "useSW" refers to whether the training should be made on the most recent 1,000 tweets or no.
 * @throws Exception */
public SentimentAnalyser(String main_folder, boolean useSW, String test_dataset) throws Exception{
	tr = new Trainer(main_folder);			//tr.train();
	pc = new PolarityClassifier(main_folder, tr.getTextAttributes(), tr.getFeatureAttributes(), tr.getComplexAttributes());
	tp = new TweetPreprocessor(main_folder);
	initializeFilter();
	
	useSlidingWindow = useSW;
       ArrayList<Attribute> attributes = new ArrayList<Attribute>();
       ArrayList<String> classVal = new ArrayList<String>();
       classVal.add("positive");
       classVal.add("negative");
       attributes.add(new Attribute("text",(ArrayList<String>)null));
       attributes.add(new Attribute("sentimentClassValue",classVal));
	train = new Instances("somerel", attributes, 0);
	train.setClassIndex(1);
	test = new Instances("somerel", attributes, 0);
	test.setClassIndex(1);
	
	if (useSlidingWindow == false){
		multiNB = (Classifier) weka.core.SerializationHelper.read(main_folder+"/test_models/"+test_dataset+".model");
		BufferedReader rd = new BufferedReader(new FileReader(new File(main_folder+"test_models/"+test_dataset+"-attributes.tsv")));
		train_attributes = new DualHashBidiMap<String, Integer>();
		String inline;
		int cnt = 0;
		while ((inline=rd.readLine())!=null){
			train_attributes.put(inline, cnt);
			cnt++;
		}
		rd.close();
		BufferedReader frd = new BufferedReader(new FileReader(new File(main_folder+"test_models/"+test_dataset+"-attributes.arff")));
		training_text = new Instances(frd);
		frd.close();
	}
}
 
源代码26 项目: mzmine2   文件: ClusteringTask.java
/**
 * Creates the weka data set for clustering of variables (metabolites)
 *
 * @param rawData Data extracted from selected Raw data files and rows.
 * @return Weka library data set
 */
private Instances createVariableWekaDataset(double[][] rawData) {
  FastVector attributes = new FastVector();

  for (int i = 0; i < this.selectedRawDataFiles.length; i++) {
    String varName = "Var" + i;
    Attribute var = new Attribute(varName);
    attributes.addElement(var);
  }

  if (clusteringStep.getModule().getClass().equals(HierarClusterer.class)) {
    Attribute name = new Attribute("name", (FastVector) null);
    attributes.addElement(name);
  }
  Instances data = new Instances("Dataset", attributes, 0);

  for (int i = 0; i < selectedRows.length; i++) {
    double[] values = new double[data.numAttributes()];
    System.arraycopy(rawData[i], 0, values, 0, rawData[0].length);

    if (clusteringStep.getModule().getClass().equals(HierarClusterer.class)) {
      DecimalFormat twoDForm = new DecimalFormat("#.##");
      double MZ = Double.valueOf(twoDForm.format(selectedRows[i].getAverageMZ()));
      double RT = Double.valueOf(twoDForm.format(selectedRows[i].getAverageRT()));
      String rowName = "MZ->" + MZ + "/RT->" + RT;
      values[data.numAttributes() - 1] = data.attribute("name").addStringValue(rowName);
    }
    Instance inst = new SparseInstance(1.0, values);
    data.add(inst);
  }
  return data;
}
 
源代码27 项目: tsml   文件: JRip.java
/** 
 * Constructor
 */
public NominalAntd(Attribute a){ 
  super(a);    
  int bag = att.numValues();
  accurate = new double[bag];
  coverage = new double[bag];
}
 
源代码28 项目: tsml   文件: PowerCepstrum.java
@Override
protected Instances determineOutputFormat(Instances inputFormat) throws Exception {

    //Set up instances size and format.

    int length=(fftFilter.findLength(inputFormat));
    length/=2;
    ArrayList<Attribute> atts=new ArrayList<>();
    String name;
    for(int i=0;i<length;i++){
        name = "PowerSpectrum_"+i;
        atts.add(new Attribute(name));
    }

    if(inputFormat.classIndex()>=0){	//Classification set, set class
        //Get the class values as a fast vector
        Attribute target =inputFormat.attribute(inputFormat.classIndex());

        ArrayList<String> vals=new ArrayList<>(target.numValues());
        for(int i=0;i<target.numValues();i++)
            vals.add(target.value(i));
        atts.add(new Attribute(inputFormat.attribute(inputFormat.classIndex()).name(),vals));
    }

    Instances result = new Instances("Cepstrum"+inputFormat.relationName(),atts,inputFormat.numInstances());
    if(inputFormat.classIndex()>=0)
        result.setClassIndex(result.numAttributes()-1);

    return result;
}
 
源代码29 项目: tsml   文件: ARMA.java
@Override
    protected Instances determineOutputFormat(Instances inputFormat)
                    throws Exception {
            //Check all attributes are real valued, otherwise throw exception
            for(int i=0;i<inputFormat.numAttributes();i++)
                    if(inputFormat.classIndex()!=i)
                            if(!inputFormat.attribute(i).isNumeric())
                                    throw new Exception("Non numeric attribute not allowed in ACF");

            if(inputFormat.classIndex()>=0)	//Classification set, dont transform the target class!
                    maxLag=(inputFormat.numAttributes()-1>maxLag)?maxLag:inputFormat.numAttributes()-1;
            else
                    maxLag=(inputFormat.numAttributes()>maxLag)?maxLag:inputFormat.numAttributes();
            //Set up instances size and format. 
            ArrayList<Attribute> atts=new ArrayList<>();
            String name;
            for(int i=0;i<maxLag;i++){
                    name = "ARMA_"+i;
                    atts.add(new Attribute(name));
            }
            if(inputFormat.classIndex()>=0){	//Classification set, set class 
//Get the class values as a fast vector			
                    Attribute target =inputFormat.attribute(inputFormat.classIndex());

                    ArrayList<String> vals=new ArrayList<>(target.numValues());
                    for(int i=0;i<target.numValues();i++)
                            vals.add(target.value(i));
                    atts.add(new Attribute(inputFormat.attribute(inputFormat.classIndex()).name(),vals));

            }	
            Instances result = new Instances("ARMA"+inputFormat.relationName(),atts,inputFormat.numInstances());
            if(inputFormat.classIndex()>=0)
                    result.setClassIndex(result.numAttributes()-1);
            return result;	}
 
源代码30 项目: meka   文件: DataTableModel.java
/**
 * returns the TYPE of the attribute at the given position
 *
 * @param rowIndex the index of the row
 * @param columnIndex the index of the column
 * @return the attribute type
 */
public int getType(int rowIndex, int columnIndex) {
	int result;

	result = Attribute.STRING;

	if ((rowIndex < 0) && columnIndex > 0 && columnIndex < getColumnCount()) {
		result = m_Data.attribute(columnIndex - 1).type();
	} else if ((rowIndex >= 0) && (rowIndex < getRowCount())
		&& (columnIndex > 0) && (columnIndex < getColumnCount())) {
		result = m_Data.instance(rowIndex).attribute(columnIndex - 1).type();
	}

	return result;
}