本文整理汇总了Java中weka.core.Instances.insertAttributeAt方法的典型用法代码示例。如果您正苦于以下问题:Java Instances.insertAttributeAt方法的具体用法?Java Instances.insertAttributeAt怎么用?Java Instances.insertAttributeAt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weka.core.Instances
的用法示例。
在下文中一共展示了Instances.insertAttributeAt方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateMore
import weka.core.Instances; //导入方法依赖的package包/类
public Instances generateMore(int number, int existedNum,
Instances header) {
ArrayList<Attribute> localAtts = new ArrayList<Attribute>();
Enumeration<Attribute> enu = header.enumerateAttributes();
while (enu.hasMoreElements()) {
localAtts.add(enu.nextElement());
}
Instances samplePoints = LHSInitializer.getMultiDimContinuous(
localAtts, number + existedNum, false);
samplePoints.insertAttributeAt(header.classAttribute(),
samplePoints.numAttributes());
samplePoints.setClassIndex(samplePoints.numAttributes() - 1);
return samplePoints;
}
示例2: testCOMT2
import weka.core.Instances; //导入方法依赖的package包/类
public static void testCOMT2() throws Exception{
BestConf bestconf = new BestConf();
Instances trainingSet = DataIOFile.loadDataFromArffFile("data/trainingBestConf0.arff");
trainingSet.setClassIndex(trainingSet.numAttributes()-1);
Instances samplePoints = LHSInitializer.getMultiDimContinuous(bestconf.getAttributes(), InitialSampleSetSize, false);
samplePoints.insertAttributeAt(trainingSet.classAttribute(), samplePoints.numAttributes());
samplePoints.setClassIndex(samplePoints.numAttributes()-1);
COMT2 comt = new COMT2(samplePoints, COMT2Iteration);
comt.buildClassifier(trainingSet);
Evaluation eval = new Evaluation(trainingSet);
eval.evaluateModel(comt, trainingSet);
System.err.println(eval.toSummaryString());
Instance best = comt.getInstanceWithPossibleMaxY(samplePoints.firstInstance());
Instances bestInstances = new Instances(trainingSet,2);
bestInstances.add(best);
DataIOFile.saveDataToXrffFile("data/trainingBestConf_COMT2.arff", bestInstances);
//now we output the training set with the class value updated as the predicted value
Instances output = new Instances(trainingSet, trainingSet.numInstances());
Enumeration<Instance> enu = trainingSet.enumerateInstances();
while(enu.hasMoreElements()){
Instance ins = enu.nextElement();
double[] values = ins.toDoubleArray();
values[values.length-1] = comt.classifyInstance(ins);
output.add(ins.copy(values));
}
DataIOFile.saveDataToXrffFile("data/trainingBestConf0_predict.xrff", output);
}
示例3: collectPerfs
import weka.core.Instances; //导入方法依赖的package包/类
@Override
public Instances collectPerfs(Instances samplePoints, String perfAttName) {
Instances retVal = null;
if(samplePoints.attribute(perfAttName) == null){
Attribute performance = new Attribute(perfAttName);
samplePoints.insertAttributeAt(performance, samplePoints.numAttributes());
}
File perfFolder = new File(perfsfilepath);
int tot=0;
if(perfFolder.exists()){
//let's get all the name set for the sample points
Iterator<Instance> itr = samplePoints.iterator();
TreeSet<String> insNameSet = new TreeSet<String>();
HashMap<String, Integer> mapping = new HashMap<String, Integer>();
int pos=0;
while(itr.hasNext()){
String mdstr = getMD5(itr.next());
insNameSet.add(mdstr);
mapping.put(mdstr, new Integer(pos++));
}
//now we collect
File[] perfFiles = perfFolder.listFiles(new PerfsFileFilter(insNameSet));
tot = perfFiles.length;
if(tot > 0) isInterrupt = true;
for(int i=0;i<tot;i++){
Instance ins = samplePoints.get(mapping.get(perfFiles[i].getName()));
double[] results = getPerf(perfFiles[i].getAbsolutePath());
if(results!=null){
ins.setValue(samplePoints.numAttributes()-1, results[0]);
}
}
}
retVal = samplePoints;
retVal.setClassIndex(retVal.numAttributes()-1);
System.out.println("Total number of collected performances is : "+tot);
return retVal;
}
示例4: generateDecisionTree
import weka.core.Instances; //导入方法依赖的package包/类
protected Classifier generateDecisionTree(AbstractClusterer clusterer, MarkovAttributeSet aset, Instances data) throws Exception {
// We need to create a new Attribute that has the ClusterId
Instances newData = data; // new Instances(data);
newData.insertAttributeAt(new Attribute("ClusterId"), newData.numAttributes());
Attribute cluster_attr = newData.attribute(newData.numAttributes()-1);
assert(cluster_attr != null);
assert(cluster_attr.index() > 0);
newData.setClass(cluster_attr);
// We will then tell the Classifier to predict that ClusterId based on the MarkovAttributeSet
ObjectHistogram<Integer> cluster_h = new ObjectHistogram<Integer>();
for (int i = 0, cnt = newData.numInstances(); i < cnt; i++) {
// Grab the Instance and throw it at the the clusterer to get the target cluster
Instance inst = newData.instance(i);
int c = (int)clusterer.clusterInstance(inst);
inst.setClassValue(c);
cluster_h.put(c);
} // FOR
System.err.println("Number of Elements: " + cluster_h.getValueCount());
System.err.println(cluster_h);
NumericToNominal filter = new NumericToNominal();
filter.setInputFormat(newData);
newData = Filter.useFilter(newData, filter);
String output = this.catalog_proc.getName() + "-labeled.arff";
FileUtil.writeStringToFile(output, newData.toString());
LOG.info("Wrote labeled data set to " + output);
// Decision Tree
J48 j48 = new J48();
String options[] = {
"-S", Integer.toString(this.rand.nextInt()),
};
j48.setOptions(options);
// Make sure we add the ClusterId attribute to a new MarkovAttributeSet so that
// we can tell the Classifier to classify that!
FilteredClassifier fc = new FilteredClassifier();
MarkovAttributeSet classifier_aset = new MarkovAttributeSet(aset);
classifier_aset.add(cluster_attr);
fc.setFilter(classifier_aset.createFilter(newData));
fc.setClassifier(j48);
// Bombs away!
fc.buildClassifier(newData);
return (fc);
}
示例5: runExp
import weka.core.Instances; //导入方法依赖的package包/类
public Instances runExp(Instances samplePoints, String perfAttName){
Instances retVal = null;
if(samplePoints.attribute(perfAttName) == null){
Attribute performance = new Attribute(perfAttName);
samplePoints.insertAttributeAt(performance, samplePoints.numAttributes());
}
int pos = samplePoints.numInstances();
int count = 0;
for (int i = 0; i < pos; i++) {
Instance ins = samplePoints.get(i);
HashMap hm = new HashMap();
int tot = 0;
for (int j = 0; j < ins.numAttributes(); j++) {
hm.put(ins.attribute(j).name(), ins.value(ins.attribute(j)));
}
boolean testRet;
if (Double.isNaN(ins.value(ins.attribute(ins.numAttributes() - 1)))) {
testRet = this.startTest(hm, i, isInterrupt);
double y = 0;
if (!testRet) {// the setting does not work, we skip it
y = -1;
count++;
if (count >= targetTestErrorNum) {
System.out.println("There must be somthing wrong with the system. Please check and restart.....");
System.exit(1);
}
} else {
y = getPerformanceByType(performanceType);
count = 0;
}
ins.setValue(samplePoints.numAttributes() - 1, y);
writePerfstoFile(ins);
} else {
continue;
}
}
retVal = samplePoints;
retVal.setClassIndex(retVal.numAttributes()-1);
return retVal;
}
示例6: ConstructionInstances
import weka.core.Instances; //导入方法依赖的package包/类
public Instances ConstructionInstances(Instances data) throws Exception{
String tweet, tweetS, tweetLem;
Instances newData = new Instances(data);
if (prop.getProperty("SyntacticFeatures.countElongatedWords").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_numberElongatedWords"), newData.numAttributes());
if (prop.getProperty("SyntacticFeatures.presencePunctuation").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_punctuation"), newData.numAttributes());
if (prop.getProperty("SyntacticFeatures.countCapitalizations").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_allCaps"), newData.numAttributes());
if (prop.getProperty("SyntacticFeatures.presenceSmileys").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_emoticonePos"), newData.numAttributes());
if (prop.getProperty("SyntacticFeatures.presenceSmileys").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_emoticoneNeg"), newData.numAttributes());
if (prop.getProperty("SyntacticFeatures.countHashtags").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_hashtags"), newData.numAttributes());
if (prop.getProperty("SyntacticFeatures.countNegators").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_countNegators"), newData.numAttributes());
if (prop.getProperty("SyntacticFeatures.presencePartOfSpeechTags").equalsIgnoreCase("yes")) for (int j=0; j<POS.size(); j++) newData.insertAttributeAt(new Attribute("__"+POS.get(j)), newData.numAttributes());
// Lexicons
if (prop.getProperty("Lexicons.feelPol").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_countPosFEEL"), newData.numAttributes());
if (prop.getProperty("Lexicons.feelPol").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_countNegFEEL"), newData.numAttributes());
if (prop.getProperty("Lexicons.polarimotsPol").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_countPosPolarimots"), newData.numAttributes());
if (prop.getProperty("Lexicons.polarimotsPol").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_countNegPolarimots"), newData.numAttributes());
if (prop.getProperty("Lexicons.polarimotsPol").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_countNeuPolarimots"), newData.numAttributes());
if (prop.getProperty("Lexicons.affectsPol").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_countPosAffects"), newData.numAttributes());
if (prop.getProperty("Lexicons.affectsPol").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_countNegAffects"), newData.numAttributes());
if (prop.getProperty("Lexicons.affectsPol").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_countNeuAffects"), newData.numAttributes());
if (prop.getProperty("Lexicons.dikoPol").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_countPosDiko"), newData.numAttributes());
if (prop.getProperty("Lexicons.dikoPol").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_countNegDiko"), newData.numAttributes());
if (prop.getProperty("Lexicons.dikoPol").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_countNeuDiko"), newData.numAttributes());
if (prop.getProperty("Lexicons.feelEmo").equalsIgnoreCase("yes")) for (int i=0; i<nbClassesFEEL; i++) newData.insertAttributeAt(new Attribute("_countEmotionFEEL"+(i+1)), newData.numAttributes());
if (prop.getProperty("Lexicons.affectsEmo").equalsIgnoreCase("yes")) for (int i=0; i<nbClassesAffects; i++) newData.insertAttributeAt(new Attribute("_countEmotionAffects"+(i+1)), newData.numAttributes());
if (prop.getProperty("Lexicons.dikoEmo").equalsIgnoreCase("yes")) for (int i=0; i<nbClassesDiko; i++) newData.insertAttributeAt(new Attribute("_countEmotionDiko"+(i+1)), newData.numAttributes());
if (prop.getProperty("Lexicons.incongruity").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_countIncongruity"), newData.numAttributes());
if (prop.getProperty("Lexicons.incongruityAll").equalsIgnoreCase("yes")) newData.insertAttributeAt(new Attribute("_countIncongruityAll"), newData.numAttributes());
// Data
for (int i=0; i<newData.numInstances(); i++){
tweet = newData.instance(i).stringValue(data.attribute("_text"));
tweetS = tweet;
// Preprocessings1
if (prop.getProperty("Preprocessings.normalizeHyperlinks").equalsIgnoreCase("yes")) tweet = Pretraitements.ReplaceLink(tweet);
if (prop.getProperty("Preprocessings.normalizeEmails").equalsIgnoreCase("yes")) tweet = Pretraitements.ReplaceMail(tweet);
if (prop.getProperty("Preprocessings.replacePseudonyms").equalsIgnoreCase("yes")) tweet = Pretraitements.ReplaceUserTag(tweet);
if (prop.getProperty("Preprocessings.normalizeSlang").equalsIgnoreCase("yes")) tweet = Pretraitements.ReplaceArgots(tweet);
// Syntactic features
if (prop.getProperty("SyntacticFeatures.countElongatedWords").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_numberElongatedWords"), ca.ElongatedWords(tweet));
if (prop.getProperty("SyntacticFeatures.presencePunctuation").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_punctuation"), (tweet.contains("!") || tweet.contains("?"))? 1 : 0);
if (prop.getProperty("SyntacticFeatures.countCapitalizations").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_allCaps"), ca.AllCaps(tweet));
if (prop.getProperty("SyntacticFeatures.presenceSmileys").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_emoticonePos"), ((ca.EmoticonesPos(tweet))? 1 : 0));
if (prop.getProperty("SyntacticFeatures.presenceSmileys").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_emoticoneNeg"), ((ca.EmoticonesNeg(tweet))? 1 : 0));
if (prop.getProperty("SyntacticFeatures.countHashtags").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_hashtags"), ca.CountHashtag(tweet));
if (prop.getProperty("SyntacticFeatures.countNegators").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_countNegators"), (ca.CountNegation(tweet)));
if (prop.getProperty("SyntacticFeatures.presencePartOfSpeechTags").equalsIgnoreCase("yes")) for (int j=0; j<POS.size(); j++) newData.instance(i).setValue(newData.attribute("__"+POS.get(j)), ca.POS(tweet,POS.get(j)));
// Preprocessings2
if (prop.getProperty("Preprocessings.lowercase").equalsIgnoreCase("yes")) tweet = tweet.toLowerCase();
if (prop.getProperty("Preprocessings.lemmatize").equalsIgnoreCase("yes")) tweet = ca.Lemmatiser(tweet);
newData.instance(i).setValue(newData.attribute("_text"), tweet);
// Lexicons
tweetLem = tweetS.toLowerCase();
tweetLem = ca.Lemmatiser(tweetLem);
if (prop.getProperty("Lexicons.feelPol").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_countPosFEEL"), ca.ComputePosFEEL(tweetLem));
if (prop.getProperty("Lexicons.feelPol").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_countNegFEEL"), ca.ComputeNegFEEL(tweetLem));
if (prop.getProperty("Lexicons.polarimotsPol").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_countPosPolarimots"), ca.ComputePosPolarimots(tweetLem));
if (prop.getProperty("Lexicons.polarimotsPol").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_countNegPolarimots"), ca.ComputeNegPolarimots(tweetLem));
if (prop.getProperty("Lexicons.polarimotsPol").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_countNeuPolarimots"), ca.ComputeNeuPolarimots(tweetLem));
if (prop.getProperty("Lexicons.affectsPol").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_countPosAffects"), ca.ComputePosAffects(tweetLem));
if (prop.getProperty("Lexicons.affectsPol").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_countNegAffects"), ca.ComputeNegAffects(tweetLem));
if (prop.getProperty("Lexicons.affectsPol").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_countNeuAffects"), ca.ComputeNeuAffects(tweetLem));
if (prop.getProperty("Lexicons.dikoPol").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_countPosDiko"), ca.ComputePosDiko(tweetLem));
if (prop.getProperty("Lexicons.dikoPol").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_countNegDiko"), ca.ComputeNegDiko(tweetLem));
if (prop.getProperty("Lexicons.dikoPol").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_countNeuDiko"), ca.ComputeNeuDiko(tweetLem));
if (prop.getProperty("Lexicons.feelEmo").equalsIgnoreCase("yes")) for (int j=0; j<nbClassesFEEL; j++) newData.instance(i).setValue(newData.attribute("_countEmotionFEEL"+(j+1)), ca.ComputeEmotionFEEL(tweetLem, j));
if (prop.getProperty("Lexicons.affectsEmo").equalsIgnoreCase("yes")) for (int j=0; j<nbClassesAffects; j++) newData.instance(i).setValue(newData.attribute("_countEmotionAffects"+(j+1)), ca.ComputeEmotionAffects(tweetLem, j));
if (prop.getProperty("Lexicons.dikoEmo").equalsIgnoreCase("yes")) for (int j=0; j<nbClassesDiko; j++) newData.instance(i).setValue(newData.attribute("_countEmotionDiko"+(j+1)), ca.ComputeEmotionDiko(tweetLem, j));
if (prop.getProperty("Lexicons.incongruity").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_countIncongruity"), ca.ComputeIncongruity(tweetLem));
if (prop.getProperty("Lexicons.incongruityAll").equalsIgnoreCase("yes")) newData.instance(i).setValue(newData.attribute("_countIncongruityAll"), ca.ComputeIncongruityAll(tweetLem));
}
return newData;
}