【问题标题】:Scaling training data in a weka classifier在 weka 分类器中缩放训练数据
【发布时间】:2013-08-30 09:24:36
【问题描述】:

我在训练集上使用 weka 分类器,但我想在构建模型之前对其进行缩放。问题是我不知道该怎么做。这是构建分类器并执行预测的代码。 “trainPath”和“predictPath”中的文件是 arff 格式。

void classify(String trainPath, String predictPath) {
    try {
        DataSource trainData = new DataSource(trainPath);
        Instances train = trainData.getDataSet();
        if(train.classIndex() == -1)
            train.setClassIndex(train.numAttributes() -1);

        DataSource predictData = new DataSource(predictPath);
        Instances predict = predictData.getDataSet();
        if(predict.classIndex() == -1)
            predict.setClassIndex(predict.numAttributes() -1);


        Classifier cls = new LibSVM();
        cls.buildClassifier(train);

        Instances labeled = new Instances(predict);
        for (int c=0; c<predict.numInstances(); c++) {
            double clsLabel = cls.classifyInstance(predict.instance(c));
            labeled.instance(c).setClassValue(clsLabel);
        }

        BufferedWriter bw = new BufferedWriter(new FileWriter("files/labeled.arff"));
        bw.write(labeled.toString());
        bw.newLine();
        bw.flush();
        bw.close();

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

}

我知道 Libsvm 中存在 svm-scale 功能,但我不知道如何使用它。

【问题讨论】:

    标签: java machine-learning scaling weka


    【解决方案1】:

    Weka 特别为您提供数据预处理方法

    • weka.filters.unsupervised.attribute.Normalize
    • weka.filters.unsupervised.attribute.Standardize

    规范化器的示例用法,它将数据缩放到[0,1] 间隔(默认情况下):

    Normalize norm = new Normalize();
    norm.setInputFormat(train);
    Instances processed_train = Filter.useFilter(train, norm);
    

    【讨论】:

      猜你喜欢
      • 2022-11-17
      • 2014-04-21
      • 2016-04-04
      • 2014-11-27
      • 2016-07-03
      • 2015-03-28
      • 2016-09-25
      • 2021-12-07
      • 2015-01-13
      相关资源
      最近更新 更多