java - 如何将属性类型更改为字符串(WEKA - CSV 到 ARFF)

标签 java csv weka document-classification arff

我正在尝试使用 WEKA 库制作垃圾短信分类器。我有一个带有“标签”和“文本”标题的 CSV 文件。当我使用下面的代码时,它会创建一个具有两个属性的 ARFF 文件:

@attribute label {ham,spam}
@attribute text {'Go until jurong point','Ok lar...', etc.}

目前,文本属性似乎被格式化为一个名义属性,每条消息的文本作为一个值。但我需要文本属性是一个字符串属性,而不是所有实例中所有文本的列表。将文本属性作为字符串将允许我使用 StringToWordVector 过滤器来训练分类器。

// load CSV
CSVLoader loader = new CSVLoader();
loader.setSource(new File(args[0]));
Instances data = loader.getDataSet();

// save ARFF
ArffSaver saver = new ArffSaver();
saver.setInstances(data);
saver.setFile(new File(args[1]));
saver.setDestination(new File(args[1]));
saver.writeBatch();

我知道我可以像这样创建一个字符串属性:

Attribute tmp = new Attribute("tmp", (FastVector) null);

但我不知道如何替换当前属性,或者在读取 CSV 之前设置属性类型。

我尝试插入一个新的字符串属性并删除当前的名义属性,但这会删除所有 SMS 文本。我也尝试使用 renameAttributeValue ,但这似乎不适用于更改属性类型。

编辑: 我怀疑这个 NominalToString filter会完成这项工作,但我不确定如何使用它。

如有任何建议,我们将不胜感激。谢谢!

最佳答案

这成功了。它改变了文本属性类型,但没有改变标签属性类型(虽然我不确定为什么它做了一个而不是另一个)。

NominalToString filter1 = new NominalToString();
filter1.setInputFormat(data);
data = Filter.useFilter(data, filter1);

有个小提示here

By default, non-numerical attributes get imported as NOMINAL attributes, which is not necessarily desired for textual data, especially if one wants to use the StringToWordVector filter. In order to change the attribute to STRING, one can run the NominalToString filter (package weka.filters.unsupervised.attribute) on the data, specifying the attribute index or range of indices that should be converted (NB: this filter does not exclude the class attribute from conversion!).

关于java - 如何将属性类型更改为字符串(WEKA - CSV 到 ARFF),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17179879/

相关文章:

java - 泛型函数的迭代

sql - 通过 Management Studio 从 SQL Server 2012 导出到 .CSV

machine-learning - WEKA LibSVM 成本权重参数

java - 使用Weka将文件拆分为测试文件和训练文件

java - 如何使用 jackson 从嵌套的 Json 字符串中获取 2 个值

java - 打印没有页面的 Jasper Report

java - 访问谷歌日历时出现空指针异常

python - 如何将大 CSV 文件导入 python 中的 Sqlite3?

r - 读取csv文件,在一列中包含数字和字符串

mysql - 无法使用 Weka 连接到 MySQL