How to use date type in weka in java code?

淺唱寂寞╮ 提交于 2019-12-07 18:06:49

问题


I am trying to create training instances with one date attribute and one numeric attribute. I get error because of my date value. The error is: java.lang.IllegalArgumentException: Attribute neither nominal nor string!

I guess I do not understand how to deal with this format. I have searched for it a lot but all of the examples are about using it in arff file and I couldn't find any example for java code. I will be so thankful if you could tell where I am going wrong or send me some links which could guide me through this problem.

Here is the code:

    Attribute dateTimeAttribute = new Attribute("dateTime","yyyy-MM-dd HH:mm:ss");
    Attribute valueAttribute = new Attribute("value");

    FastVector fvWekaAttributesLinear = new FastVector(2);  

    fvWekaAttributesLinear.addElement(dateTimeAttribute);
    fvWekaAttributesLinear.addElement(valueAttribute); 

    Instances isTrainingSet = new Instances("Relation", fvWekaAttributesLinear, 100000); 
    isTrainingSet.setClassIndex(1);

    Instance ins = new Instance(2);

    ins.setValue((Attribute)fvWekaAttributesLinear.elementAt(0), "2009-07-15 10:00:00");


    ins.setValue((Attribute)fvWekaAttributesLinear.elementAt(1), 0.5); 

回答1:


This is how I solved the problem:

    Attribute dateTimeAttribute = new Attribute("dateTime","yyyy-MM-dd HH:mm");
    Attribute valueAttribute = new Attribute("value");

    FastVector fvWekaAttributesLinear = new FastVector(2);          
    fvWekaAttributesLinear.addElement(dateTimeAttribute);
    fvWekaAttributesLinear.addElement(valueAttribute); 

    Instances isTrainingSet = new Instances("Relation", fvWekaAttributesLinear, 100000);
    double[] attValues = new double[isTrainingSet.numAttributes()];

    attValues[0] = isTrainingSet.attribute("dateTime").parseDate("2009-07-15 10:00");
    attValues[1] = 0.5;

Here is a link that I found useful: http://zitnik.si/wordpress/2011/09/25/quick-intro-to-weka/



来源:https://stackoverflow.com/questions/19759031/how-to-use-date-type-in-weka-in-java-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!