您好我正在尝试在java中实现我自己的分类器。这是我到目前为止所获得的:
import weka.core.*;
public class RandomProbability extends Classifier {
Instances data;
public RandomProbability ()
{
/*DataSource d = new DataSource("C:\\Program Files\\Weka-3-6\\data\\labor.arff");
data=((Object) d).getSourceData();*/
DataSource source = null;
try {
source = new DataSource("C:\\Program Files\\Weka-3-6\\data\\labor.arff");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Instances instances = source.getDataSet();
//instances.setClassIndex(instances.numAttributes() - 1);
// Print header and instances.
System.out.println("\nDataset:\n");
System.out.println(instances);
现在问题是我无法对数据集中的数据进行分类(好的或坏的)。 我在尝试访问此代码中的单个实例时需要帮助。
答案 0 :(得分:0)
您可以使用instanceOf运算符,如下所示:
int countA = 0, countB=0;
double pred;
for (int i=0;i<57;i++) {
pred = classifyInstance(instances.instance(i));
System.out.println("===== Classified instance =====");
System.out.println("Class predicted:" + instances.classAttribute().value((int) pred));
if (instances.classAttribute().value((int) pred).toString().equals("bad")) {
countB++;
} else {
countA++;
}
}
答案 1 :(得分:0)
package dm;
//import javax.activation.DataSource;
import weka.core.converters.ConverterUtils.DataSource;
import weka.classifiers.*;
import weka.core.*;
import org.jfree.data.*;
import weka.core.*;
public class RandomProbability extends Classifier {
Instances data;
public RandomProbability ()
{
/*DataSource d = new DataSource("C:\\Program Files\\Weka-3-6\\data\\labor.arff");
data=((Object) d).getSourceData();*/
DataSource source = null;
try {
source = new DataSource("C:\\Program Files\\Weka-3-6\\data\\labor.arff");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Instances instances = source.getDataSet();
instances.setClassIndex(instances.numAttributes() - 1);
// Print header and instances.
System.out.println("\nDataset:\n");
System.out.println(instances);
int total=instances.numInstances();
System.out.println("total"+total);
Attribute attr= instances.attribute(16);
System.out.println("attr"+attr);
// checking class
int countA = 0, countB=0;
double pred;
for (int i=0;i<57;i++)
{
pred=0;
pred = classifyInstance(instances.instance(i));
System.out.println("===== Classified instance =====");
System.out.println("Class predicted:" + instances.classAttribute().value((int) pred));
if (instances.classAttribute().value((int) pred).toString().equals("bad"))
{
countB++;
}
else {
countA++;
}
}
System.out.println("good instances"+countA);
System.out.println("bad instances"+ countB);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void buildClassifier (Instances data)
{
}
public double classifyInstance (Instance inst)
{
return 0;
}
}