本文整理汇总了Java中weka.core.Instance.setValue方法的典型用法代码示例。如果您正苦于以下问题:Java Instance.setValue方法的具体用法?Java Instance.setValue怎么用?Java Instance.setValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weka.core.Instance
的用法示例。
在下文中一共展示了Instance.setValue方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: Main
import weka.core.Instance; //导入方法依赖的package包/类
public Main() {
try {
BufferedReader datafile;
datafile = readDataFile("camping.txt");
Instances data = new Instances(datafile);
data.setClassIndex(data.numAttributes() - 1);
Instances trainingData = new Instances(data, 0, 14);
Instances testingData = new Instances(data, 14, 5);
Evaluation evaluation = new Evaluation(trainingData);
SMO smo = new SMO();
smo.buildClassifier(data);
evaluation.evaluateModel(smo, testingData);
System.out.println(evaluation.toSummaryString());
// Test instance
Instance instance = new DenseInstance(3);
instance.setValue(data.attribute("age"), 78);
instance.setValue(data.attribute("income"), 125700);
instance.setValue(data.attribute("camps"), 1);
instance.setDataset(data);
System.out.println("The instance: " + instance);
System.out.println(smo.classifyInstance(instance));
} catch (Exception ex) {
ex.printStackTrace();
}
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:30,代码来源:Main-SVG.java
示例2: classifySentence
import weka.core.Instance; //导入方法依赖的package包/类
public SentenceType classifySentence(Sentence sentence) {
SpeechActsClassifier.Features features = speechActsClassifier.classifyFeatures(sentence);
Instance inst = new DenseInstance(6);
inst.setDataset(dataSet);
inst.setValue(0, features.getSentenceLength());
inst.setValue(1, features.getNumberOfNouns());
inst.setValue(2, (features.isEndingInNounOrAdjective() ? 1 : 0));
inst.setValue(3, (features.isBeginningInVerb() ? 1 : 0));
inst.setValue(4, features.getCountOfWhMarkers());
inst.setValue(5, Utils.missingValue());
try {
return SentenceType.valueOf(classifier.classifyInstance(inst));
} catch (Exception e) {
throw new RuntimeException("Can't classify");
}
}
示例3: classifyQuestion
import weka.core.Instance; //导入方法依赖的package包/类
public QuestionType classifyQuestion(Sentence sentence) {
if (!sentence.isQuestion()) {
return QuestionType.NA;
}
QuestionTypeClassifier.Features features = questionTypeClassifier.classifyFeatures(sentence);
Instance inst = new DenseInstance(5);
inst.setDataset(dataSet);
inst.setValue(0, features.getWhWord());
inst.setValue(1, features.getWhWordPos());
inst.setValue(2, features.getPosOfNext());
inst.setValue(3, features.getRootPos());
inst.setValue(4, Utils.missingValue());
try {
int ndx = (int) classifier.classifyInstance(inst);
return QuestionType.valueOf(ndx);
} catch (Exception e) {
throw new RuntimeException("Not classified");
}
}
示例4: getInstanceObject
import weka.core.Instance; //导入方法依赖的package包/类
public static Instance getInstanceObject (String[] instanceText,
String[] globalFeatureVector, String docID, String classValue, Instances ds) throws Exception {
FeatureVector instanceFeatureVector = getInstanceFeatureVector(instanceText,
globalFeatureVector, docID);
Instance instance = new Instance(globalFeatureVector.length + 2);
instance.setDataset(ds);
instance.setValue(0, docID);
for(int i = 0; i < globalFeatureVector.length; i++) {
double value = 0;
if(instanceFeatureVector.m_FeatureVector[0].containsKey(i)) {
value = instanceFeatureVector.m_FeatureVector[0].get(i);
}
instance.setValue(i + 1, value);
}
instance.setValue(globalFeatureVector.length + 1, classValue);
return new SparseInstance(instance);
}
示例5: getTestInstance
import weka.core.Instance; //导入方法依赖的package包/类
private Instance getTestInstance(
String binding, String multicolor, String genre) {
Instance instance = new DenseInstance(3);
instance.setDataset(trainingData);
instance.setValue(trainingData.attribute(0), binding);
instance.setValue(trainingData.attribute(1), multicolor);
instance.setValue(trainingData.attribute(2), genre);
return instance;
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:10,代码来源:BookDecisionTree.java
示例6: convertToArff
import weka.core.Instance; //导入方法依赖的package包/类
public static Instances convertToArff(List<Document> dataSet, List<String> vocabulary, String fileName) {
int dataSetSize = dataSet.size();
/* Create features */
ArrayList<Attribute> attributes = new ArrayList<>();
for (int i = 0; i < vocabulary.size(); i++) {
attributes.add(new Attribute("word_" + i));
}
Attribute classAttribute = new Attribute("Class");
attributes.add(classAttribute);
/* Add examples */
System.out.println("Building instances...");
Instances trainingDataSet = new Instances(fileName, attributes, 0);
for (int k = 0; k < dataSetSize; k++) {
Document document = dataSet.get(k);
Instance example = new DenseInstance(attributes.size());
for (int i = 0; i < vocabulary.size(); i++) {
String word = vocabulary.get(i);
example.setValue(i, Collections.frequency(document.getTerms(), word));
}
example.setValue(classAttribute, document.getDocumentClass());
trainingDataSet.add(example);
int progress = (int) ((k * 100.0) / dataSetSize);
System.out.printf("\rPercent completed: %3d%%", progress);
}
trainingDataSet.setClass(classAttribute);
System.out.println();
System.out.println("Writing to file ...");
try {
ArffSaver saver = new ArffSaver();
saver.setInstances(trainingDataSet);
saver.setFile(new File(fileName));
saver.writeBatch();
} catch (IOException e) {
e.printStackTrace();
}
return trainingDataSet;
}
示例7: reloadSeries
import weka.core.Instance; //导入方法依赖的package包/类
private void reloadSeries(Number xValue, Number yValue) {
try {
Instance instance = new DenseInstance(NUMBER_OF_CLASSES);
instance.setDataset(data);
instance.setValue(0, xValue.doubleValue());
instance.setValue(1, yValue.doubleValue());
double predictedClass = tree.classifyInstance(instance);
instance.setValue(2, predictedClass);
data.add(instance);
reloadSeries();
} catch (Exception e) {
e.printStackTrace();
}
}
示例8: collectPerfs
import weka.core.Instance; //导入方法依赖的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;
}
示例9: add
import weka.core.Instance; //导入方法依赖的package包/类
void add(T elt, Instance inst) {
inst.setValue(0, elements.size());
elements.add(elt);
super.add(inst);
}
示例10: train
import weka.core.Instance; //导入方法依赖的package包/类
private void train() throws Exception{
models = new M5P[ModelNum];
for(int i=0;i<ModelNum;i++){
models[i] = buildModel(labeledInstances, M[i]);
}
for(int i=0;i<this.comtIterations;i++){
ArrayList<Instance>[] InstancePiSet = new ArrayList[ModelNum];
for(int j=0;j<ModelNum;j++)
InstancePiSet[j] = new ArrayList<Instance>();
for(int m=0;m<ModelNum;m++){
double maxDelta = 0;
Instance maxDeltaXY = null;
Enumeration<Instance> enu = this.unlabeledInstances.enumerateInstances();
while(enu.hasMoreElements()){
Instance ulIns = enu.nextElement();
Instances omega = getSiblings(models[m], ulIns);
double y = models[m].classifyInstance(ulIns);
if(indexOfClass==-1)
indexOfClass = labeledInstances.classIndex();
ulIns.setValue(indexOfClass, y);
Instances instancesPi = new Instances(models[m].getM5RootNode().zyqGetTrainingSet());
instancesPi.add(ulIns);
M5P modelPi = buildModel(instancesPi, M[m]);
double delta = computeOmegaDelta(models[m],modelPi,omega);
if(maxDelta<delta){
maxDelta = delta;
maxDeltaXY = ulIns;
}
}
//now check facts about delta
if(maxDelta>0){
InstancePiSet[m].add(maxDeltaXY);
this.unlabeledInstances.delete(this.unlabeledInstances.indexOf(maxDeltaXY));
}
}//check for both model
boolean toExit = true;
for(int m=0;m<ModelNum;m++){
if(InstancePiSet[m].size()>0){
toExit = false;
break;
}
}
if(toExit)
break;
else{
//update the models
int toGen = 0;
for(int m=0;m<ModelNum;m++){
Instances set = models[m].getM5RootNode().zyqGetTrainingSet();
toGen += InstancePiSet[m].size();
for(Instance ins : InstancePiSet[m])
set.add(ins);
models[m] = buildModel(set, M[m]);
}
//Replenish pool U' to size p
Instances toAdd = retrieveMore(toGen);
unlabeledInstances.addAll(toAdd);
}//we will go to another round of iteration
}//iterate for a number of rounds or break out on empty InstancesPiSets
//now we have the model as y = 0.5*sum(models[m].predict(x))
}
示例11: runExp
import weka.core.Instance; //导入方法依赖的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;
}