如何从 arff 文件中删除特定属性并生成修改后的 arff?
How to remove particular attributes from arff file and produce modified arff?
(非手动)我有 96 个特征,想从 arff 中删除大约 20 个特征并生成修改后的 arff。使用 weka 进行特征选择现在想删除那些不太重要的特征。谁能为此建议代码
给你...只需更改源文件路径和目标文件路径...
import java.io.File;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
import weka.core.converters.ArffSaver;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Remove;
public class Convert4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
ArffLoader loader2= new ArffLoader();
loader2.setSource(new File("C:/Users/RAHUL/Desktop/stack.arff"));
Instances data2= loader2.getDataSet();
//Load Arff
String[] options = new String[2];
options[0] = "-R"; // "range"
options[1] = "1"; // first attribute
Remove remove = new Remove(); // new instance of filter
remove.setOptions(options); // set options
remove.setInputFormat(data2); // inform filter about dataset **AFTER** setting options
Instances newData2 = Filter.useFilter(data2, remove); // apply filter
ArffSaver saver = new ArffSaver();
saver.setInstances(newData2);
saver.setFile(new File("C:/Users/RAHUL/Desktop/stack2.arff"));
saver.writeBatch();
}
catch (Exception e)
{}
}
}
干杯:)
这里有简短的答案,请查看
BufferedReader datafile = new BufferedReader(new FileReader("bbcsport.arff"));
BufferedReader attrfile = new BufferedReader(new FileReader("attributes.txt"));
Instances data = new Instances(datafile);
List<Integer> myList = new ArrayList<Integer>();
String line;
while ((line = attrfile.readLine()) != null) {
for (n = 0; n < data.numAttributes(); n++) {
if (data.attribute(n).name().equalsIgnoreCase(line)) {
if(!myList.contains(n))
myList.add(n);
}
}
}
int[] attrs = myList.stream().mapToInt(i -> i).toArray();
Remove remove = new Remove();
remove.setAttributeIndicesArray(attrs);
remove.setInvertSelection(false);
remove.setInputFormat(data); // init filter
Instances filtered = Filter.useFilter(data, remove);
(非手动)我有 96 个特征,想从 arff 中删除大约 20 个特征并生成修改后的 arff。使用 weka 进行特征选择现在想删除那些不太重要的特征。谁能为此建议代码
给你...只需更改源文件路径和目标文件路径...
import java.io.File;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
import weka.core.converters.ArffSaver;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Remove;
public class Convert4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
ArffLoader loader2= new ArffLoader();
loader2.setSource(new File("C:/Users/RAHUL/Desktop/stack.arff"));
Instances data2= loader2.getDataSet();
//Load Arff
String[] options = new String[2];
options[0] = "-R"; // "range"
options[1] = "1"; // first attribute
Remove remove = new Remove(); // new instance of filter
remove.setOptions(options); // set options
remove.setInputFormat(data2); // inform filter about dataset **AFTER** setting options
Instances newData2 = Filter.useFilter(data2, remove); // apply filter
ArffSaver saver = new ArffSaver();
saver.setInstances(newData2);
saver.setFile(new File("C:/Users/RAHUL/Desktop/stack2.arff"));
saver.writeBatch();
}
catch (Exception e)
{}
}
}
干杯:)
这里有简短的答案,请查看
BufferedReader datafile = new BufferedReader(new FileReader("bbcsport.arff"));
BufferedReader attrfile = new BufferedReader(new FileReader("attributes.txt"));
Instances data = new Instances(datafile);
List<Integer> myList = new ArrayList<Integer>();
String line;
while ((line = attrfile.readLine()) != null) {
for (n = 0; n < data.numAttributes(); n++) {
if (data.attribute(n).name().equalsIgnoreCase(line)) {
if(!myList.contains(n))
myList.add(n);
}
}
}
int[] attrs = myList.stream().mapToInt(i -> i).toArray();
Remove remove = new Remove();
remove.setAttributeIndicesArray(attrs);
remove.setInvertSelection(false);
remove.setInputFormat(data); // init filter
Instances filtered = Filter.useFilter(data, remove);