我在使用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函数,但我不知道如何使用它。
回答:
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);