本文介绍了如何在Java中使用StringToWordVector(weka)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的arff文件

@relation hamspam

@attribute text string
@attribute class {ham,spam}

@data
'good',ham
'very good',ham
'bad',spam
'very bad',spam
'very bad, very bad',spam

我想做的是在我的Java程序中使用weka clasiffier对它进行分类,但是我不知道如何使用StringToWordVector对其进行分类。

What i want to do is to classify it with weka clasiffier in my java program, but i don't know how to use StringToWordVector and then classify it.

这是我的代码:

Classifier j48tree = new J48();
Instances train = new Instances(new BufferedReader(new FileReader("data.arff")));

StringToWordVector filter = new StringToWordVector();

接下来要做什么?我不知道该怎么办。

What next?, i don't know what to do..

推荐答案

import weka.core.Instance;
//import required classes
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
import weka.core.stemmers.LovinsStemmer;
import weka.classifiers.meta.FilteredClassifier;
import weka.classifiers.trees.J48;
import weka.filters.unsupervised.attribute.Remove;
import weka.filters.unsupervised.attribute.StringToWordVector;

public class ClassifierWithFilter{

    public static void main(String args[]) throws Exception{
    //load dataset
    DataSource source = new DataSource("/Users/amaryadav/Desktop/spamham.arff");
    Instances dataset = source.getDataSet();
    //set class index to the last attribute
    dataset.setClassIndex(dataset.numAttributes()-1);

    //the base classifier
    J48 tree = new J48();

    //the filter
    StringToWordVector filter = new StringToWordVector();
    filter.setInputFormat(dataset);
    filter.setIDFTransform(true);
    filter.setUseStoplist(true);
    LovinsStemmer stemmer = new LovinsStemmer();
    filter.setStemmer(stemmer);
    filter.setLowerCaseTokens(true);

    //Create the FilteredClassifier object
    FilteredClassifier fc = new FilteredClassifier();
    //specify filter
    fc.setFilter(filter);
    //specify base classifier
    fc.setClassifier(tree);
    //Build the meta-classifier
    fc.buildClassifier(dataset);

    System.out.println(tree.graph());
    System.out.println(tree);
   }
}

此代码使用J48决策树构建经过训练的分类器与spamham.arff。希望有帮助。

This code uses J48 decision tree to build a classifier trained with spamham.arff. Hope that helps.

这篇关于如何在Java中使用StringToWordVector(weka)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 19:55