Weka属性既不是标称值也不是字符串

时间:2019-02-01 18:03:13

标签: android machine-learning attributes weka java-api

我正在尝试从android接受单个用户输入,并预测个人是否患有癌症,我正在通过java中的API从weka应用朴素贝叶斯。我所有的功能都是分类的,因此我已将用户输入转换为String并试图将数据提供给Weka Attribute,例如此示例如何在Java代码中的weka中使用日期类型?

但我收到错误,这是“错误是:java.lang.IllegalArgumentException异常:属性既不标称也不字符串”,的printStackTrace()从该代码行,其中i有评论引发此错误消息“//的printStackTrace从抛出错误此行”,请检查。我在代码中找不到任何错误,下面是我的代码,部分代码是从本教程中收集的:https://www.youtube.com/watch?v=bNmQNXS1xfg&t=419s。请提前帮助我解决此问题,

  

公共类MainActivity扩展了AppCompatActivity {

private Spinner spinner1,spinner3,spinner4,spinner5,spinner6,spinner7,spinner8,spinner9,spinner10,spinner11,spinner12;
private Button btnSubmit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    addListenerOnButton();
    addListenerOnSpinnerItemSelection();
}


// add items into spinner dynamically



public void addListenerOnSpinnerItemSelection() {
    spinner1 = findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
// get the selected dropdown list value

public void addListenerOnButton() {
    spinner1 = findViewById(R.id.spinner1);
    spinner3 = findViewById(R.id.spinner3);
    spinner4 = findViewById(R.id.spinner4);
    spinner5 = findViewById(R.id.spinner5);
    spinner6 = findViewById(R.id.spinner6);
    spinner7 = findViewById(R.id.spinner7);
    spinner8 = findViewById(R.id.spinner8);
    spinner9 = findViewById(R.id.spinner9);
    spinner10 = findViewById(R.id.spinner10);
    spinner11 = findViewById(R.id.spinner11);
    spinner12 = findViewById(R.id.spinner12);


    btnSubmit = findViewById(R.id.btnSubmit);
    btnSubmit.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String menarche = String.valueOf(spinner1.getSelectedItem());
            String oral = String.valueOf(spinner3.getSelectedItem());
            String diet = String.valueOf(spinner4.getSelectedItem());
            String breast = String.valueOf(spinner5.getSelectedItem());
            String cervical = String.valueOf(spinner6.getSelectedItem());
            String history = String.valueOf(spinner7.getSelectedItem());
            String education = String.valueOf(spinner8.getSelectedItem());
            String aohusband = String.valueOf(spinner9.getSelectedItem());
            String menopause = String.valueOf(spinner10.getSelectedItem());
            String foodfat = String.valueOf(spinner11.getSelectedItem());
            String abortion = String.valueOf(spinner12.getSelectedItem());

            int counter = 0;

            try {
                int features = 12;
                int num_instances = 1;

                Attribute menarche1 = new Attribute("menarche11");
                Attribute oral1 = new Attribute("oral11");
                Attribute diet1 = new Attribute("diet11");
                Attribute breast1 = new Attribute("breast11");
                Attribute cervical1 = new Attribute("cervical11");
                Attribute history1 = new Attribute("history11");
                Attribute education1 = new Attribute("education11");
                Attribute aohusband1 = new Attribute("aohusband11");
                Attribute menopause1 = new Attribute("menopause11");
                Attribute foodfat1 = new Attribute("foodfat11");
                Attribute abortion1 = new Attribute("abortion11");
                Attribute ovarian1 = new Attribute("ovarian11");

                //FastVector fvwekaAttributes = new FastVector(features);

                ArrayList<Attribute>  fvwekaAttributes = new ArrayList<Attribute>(12);


                fvwekaAttributes.add(0,menarche1);
                fvwekaAttributes.add(1,oral1);
                fvwekaAttributes.add(2,diet1);
                fvwekaAttributes.add(3,breast1);
                fvwekaAttributes.add(4,cervical1);
                fvwekaAttributes.add(5,history1);
                fvwekaAttributes.add(6,education1);
                fvwekaAttributes.add(7,aohusband1);
                fvwekaAttributes.add(8,menopause1);
                fvwekaAttributes.add(9,foodfat1);
                fvwekaAttributes.add(10,abortion1);
                fvwekaAttributes.add(11,ovarian1);


                Instances trainingset = new Instances("string",fvwekaAttributes,num_instances);

                trainingset.setClassIndex(11);

                Instance iExample = new DenseInstance(12);

                //printStackTrace Throws error from this line

                iExample.setValue(menarche1, 1);


                iExample.setValue(oral1, 1);
                iExample.setValue(diet1, 1);
                iExample.setValue(breast1, 1);
                iExample.setValue(cervical1, 1);
                iExample.setValue(history1, 1);
                iExample.setValue(education1, 1);
                iExample.setValue(aohusband1, 1);
                iExample.setValue(menopause1, 1);
                iExample.setValue(foodfat1, 1);
                iExample.setValue(abortion1, 1);
                //iExample.setValue(ovarian1, "?");

                trainingset.add(iExample);



                Classifier cls = (Classifier) weka.core.SerializationHelper.read(getAssets().open("NaiveBayes.model"));


                Toast.makeText(MainActivity.this, "hoynai ", Toast.LENGTH_LONG).show();

                double pred = cls.classifyInstance(trainingset.instance(0));

                int predict = (int) (pred*100);



                Toast.makeText(MainActivity.this, "predicted probability: " + String.valueOf(predict) + "%", Toast.LENGTH_LONG).show();



            } catch (Exception e) {
                e.printStackTrace();


                Toast.makeText(MainActivity.this, "error: " + String.valueOf(e) + "%", Toast.LENGTH_LONG).show();

            }


        }
    });
}
}

0 个答案:

没有答案