本文整理汇总了Java中weka.core.DenseInstance类的典型用法代码示例。如果您正苦于以下问题:Java DenseInstance类的具体用法?Java DenseInstance怎么用?Java DenseInstance使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DenseInstance类属于weka.core包,在下文中一共展示了DenseInstance类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: Main
import weka.core.DenseInstance; //导入依赖的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.DenseInstance; //导入依赖的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.DenseInstance; //导入依赖的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: createInstance
import weka.core.DenseInstance; //导入依赖的package包/类
public Instance createInstance(List<String> featureNames, I key) {
double[] vals = new double[featureNames.size()];
for (int i = 0; i < featureNames.size(); i++) {
Feature<Object> f = this.featureValues.get(key).get(featureNames.get(i));
if (f != null)
vals[i] = f.getDoubleValue();
else {
Class<Object> type = features.get(featureNames.get(i));
if (type.equals(Double.class) || type.equals(Float.class) || type.equals(Integer.class))
vals[i] = Double.NaN;
if (type.equals(Boolean.class) || type.equals(String.class))
vals[i] = 0;
}
}
return new DenseInstance(1.0, vals);
}
示例5: createXInstances
import weka.core.DenseInstance; //导入依赖的package包/类
public static Instances createXInstances(XYForRegression xy) {
// build attributes
ArrayList<Attribute> attributes = new ArrayList<Attribute>();
for (String attrName : xy.getAttrNames() ) {
// attribute name is the polynomial power
Attribute xattr = new Attribute(attrName);
attributes.add(xattr);
}
double[][] Xarr = xy.getX();
int numInstances = Xarr.length;
int numAttr = Xarr[0].length;
Instances Xinst = new Instances("X", attributes, numInstances);
// fill instances
for (int i = 0; i < numInstances; ++i) {
Instance row = new DenseInstance(numAttr);
for (int j = 0; j < numAttr; ++j) {
row.setValue(j, Xarr[i][j]);
}
Xinst.add(row);
}
return Xinst;
}
示例6: createArff
import weka.core.DenseInstance; //导入依赖的package包/类
public Instances createArff(String comment) {
ArrayList<Attribute> atts = new ArrayList<Attribute>();
ArrayList<String> classes = new ArrayList<String>();
classes.add("good");
classes.add("bad");
atts.add(new Attribute("text", (ArrayList<String>) null));
// make sure that the name of the class attribute is unlikely to
// clash with any attribute created via the StringToWordVector filter
atts.add(new Attribute("@@[email protected]@", classes));
Instances data = new Instances("weka_SO_comments_model", atts, 0);
data.setClassIndex(data.numAttributes() - 1);
DenseInstance instance = new DenseInstance(2);
Attribute messageAtt = data.attribute("text");
instance.setValue(messageAtt, messageAtt.addStringValue(comment));
data.add(instance);
// System.out.println(data.toString());
return data;
}
示例7: train
import weka.core.DenseInstance; //导入依赖的package包/类
public void train(TrainData data, TermVocabulary vocab) throws Exception {
this.vocab = vocab;
Instances trainset = buildInstances();
for (Pair<String, BytecodeData.MethodData> p : data.data()) {
double[] instV = seqToFeats(p.getRight().getExtOpcodeSeq());
instV[instV.length - 1] = (double) vocab.termToPos(p.getKey());
Instance inst = new DenseInstance(1.0, instV);
inst.setDataset(trainset);
trainset.add(inst);
}
nb = new NaiveBayes();
nb.buildClassifier(trainset);
}
示例8: predict
import weka.core.DenseInstance; //导入依赖的package包/类
public Map<String, Double> predict(BytecodeData.MethodData method) throws Exception {
if (testHeader == null)
testHeader = buildInstances();
double[] instV = seqToFeats(method.getExtOpcodeSeq());
instV[instV.length - 1] = 0.0;
Instance inst = new DenseInstance(1.0, instV);
inst.setDataset(testHeader);
testHeader.add(inst);
double[] probs = nb.distributionForInstance(inst);
Map<String, Double> result = new HashMap<>();
int i = 0;
for (String term : vocab.terms()) {
result.put(term, probs[i]);
i++;
}
testHeader.clear();
return result;
}
示例9: train
import weka.core.DenseInstance; //导入依赖的package包/类
public void train(TrainData data, TermVocabulary vocab) throws Exception {
this.vocab = vocab;
Instances trainset = buildInstances();
for (Pair<String, BytecodeData.MethodData> p : data.data()) {
double[] instV = seqToFeats(p.getRight().getExtOpcodeSeq());
instV[instV.length - 1] = (double) vocab.termToPos(p.getKey());
Instance inst = new DenseInstance(1.0, instV);
inst.setDataset(trainset);
trainset.add(inst);
}
frf = new FastRandomForest();
frf.setSeed(1993);
// frf.setMaxDepth(20);
// frf.setNumThreads(1);
frf.setNumTrees(numTrees);
frf.buildClassifier(trainset);
}
示例10: predict
import weka.core.DenseInstance; //导入依赖的package包/类
public Map<String, Double> predict(BytecodeData.MethodData method) throws Exception {
if (testHeader == null)
testHeader = buildInstances();
double[] instV = seqToFeats(method.getExtOpcodeSeq());
instV[instV.length - 1] = 0.0;
Instance inst = new DenseInstance(1.0, instV);
inst.setDataset(testHeader);
testHeader.add(inst);
double[] probs = frf.distributionForInstance(inst);
Map<String, Double> result = new HashMap<>();
int i = 0;
for (String term : vocab.terms()) {
result.put(term, probs[i]);
i++;
}
testHeader.clear();
return result;
}
示例11: launchMoveCentroids
import weka.core.DenseInstance; //导入依赖的package包/类
/**
* Launch the move centroids tasks
*
* @param clusters the cluster centroids
* @return the number of empty clusters
*/
protected int launchMoveCentroids(Instances[] clusters) {
int emptyClusterCount = 0;
List<Future<double[]>> results = new ArrayList<Future<double[]>>();
for (int i = 0; i < m_NumClusters; i++) {
if (clusters[i].numInstances() == 0) {
emptyClusterCount++;
} else {
Future<double[]> futureCentroid =
m_executorPool.submit(new KMeansComputeCentroidTask(i, clusters[i]));
results.add(futureCentroid);
}
}
try {
for (Future<double[]> d : results) {
m_ClusterCentroids.add(new DenseInstance(1.0, d.get()));
}
} catch (Exception ex) {
ex.printStackTrace();
}
return emptyClusterCount;
}
示例12: addTrainingInstanceFromMouseLocation
import weka.core.DenseInstance; //导入依赖的package包/类
/**
* Adds a training instance to our dataset, based on the coordinates of the
* mouse on the panel. This method sets the x and y attributes and the class
* (as defined by classAttIndex), and sets all other values as Missing.
*
* @param mouseX the x coordinate of the mouse, in pixels.
* @param mouseY the y coordinate of the mouse, in pixels.
* @param classAttIndex the index of the attribute that is currently selected
* as the class attribute.
* @param classValue the value to set the class to in our new point.
*/
public void addTrainingInstanceFromMouseLocation(int mouseX, int mouseY,
int classAttIndex, double classValue) {
// convert to coordinates in the training instance space.
double x = convertFromPanelX(mouseX);
double y = convertFromPanelY(mouseY);
// build the training instance
Instance newInstance = new DenseInstance(m_trainingData.numAttributes());
for (int i = 0; i < newInstance.numAttributes(); i++) {
if (i == classAttIndex) {
newInstance.setValue(i, classValue);
} else if (i == m_xAttribute) {
newInstance.setValue(i, x);
} else if (i == m_yAttribute) {
newInstance.setValue(i, y);
} else {
newInstance.setMissing(i);
}
}
// add it to our data set.
addTrainingInstance(newInstance);
}
示例13: makeOutputInstance
import weka.core.DenseInstance; //导入依赖的package包/类
/**
* Make an output instance given an input one
*
* @param inputI the input instance to process
* @return the output instance with substrings replaced
*/
public Instance makeOutputInstance(Instance inputI) {
double[] vals = new double[m_outputStructure.numAttributes()];
String[] stringVals = new String[m_outputStructure.numAttributes()];
for (int i = 0; i < inputI.numAttributes(); i++) {
if (inputI.attribute(i).isString() && !inputI.isMissing(i)) {
stringVals[i] = inputI.stringValue(i);
} else {
vals[i] = inputI.value(i);
}
}
for (SubstringReplacerMatchRule mr : m_matchRules) {
mr.apply(stringVals);
}
for (int i = 0; i < m_outputStructure.numAttributes(); i++) {
if (m_outputStructure.attribute(i).isString() && stringVals[i] != null) {
m_outputStructure.attribute(i).setStringValue(stringVals[i]);
}
}
Instance result = new DenseInstance(inputI.weight(), vals);
result.setDataset(m_outputStructure);
return result;
}
示例14: fillFeatureVector
import weka.core.DenseInstance; //导入依赖的package包/类
public Instance fillFeatureVector(NGGFeatureVector vSource, Instances data) {
double[] values = new double[data.numAttributes()];
values[0] = vSource.getContainmentSimilarityArrayAtIndex(0);
values[1] = vSource.getSizeSimilarityArrayAtIndex(0);
values[2] = vSource.getValueSimilarityArrayAtIndex(0);
values[3] = vSource.getNVSArrayAtIndex(0);
values[4] = vSource.getContainmentSimilarityArrayAtIndex(1);
values[5] = vSource.getSizeSimilarityArrayAtIndex(1);
values[6] = vSource.getValueSimilarityArrayAtIndex(1);
values[7] = vSource.getNVSArrayAtIndex(1);
values[8] = data.attribute(8).indexOfValue(vSource.getLabel());
Instance inst = new DenseInstance(1.0, values);
return inst;
}
示例15: decideCentroid
import weka.core.DenseInstance; //导入依赖的package包/类
public Instance decideCentroid(double[] vals,Instances members){
Instance inst=new DenseInstance(vals.length);
int q;
for(q=0;q<vals.length;q++){
inst.setValue(q, vals[q]);
}
double minDistance=Double.MAX_VALUE;
double tempDistance;
int instanceID=0;
int i;
for(i=0;i<members.numInstances();i++){
tempDistance=myDistance(inst,members.instance(i));
if(tempDistance<minDistance ){
minDistance=tempDistance;
instanceID=i;
}
}
return members.instance(instanceID);
}