gpt4 book ai didi

java - 测试和训练集中不同数量的特征

转载 作者:行者123 更新时间:2023-11-30 11:12:11 25 4
gpt4 key购买 nike

我正在尝试构建一个线性 svm 分类器来对未知测试数据进行分类。

但是,由于文本文档没有固定长度,如何保证新文档具有相同的特征长度?

Src 和 Dest 的属性数量不同:2 != 1484

 LibSVM classifier = new LibSVM();
classifier.setKernelType(new SelectedTag(LibSVM.KERNELTYPE_LINEAR, LibSVM.TAGS_KERNELTYPE));
classifier.buildClassifier(data1);


System.out.println("done");
data2.setClassIndex(data2.numAttributes() - 1);
double res = classifier.classifyInstance(data2.instance(0));

数据 2

@data
'This is a string!','?'

无论如何,我可以构建一个与当前模型具有相同数量属性的特征向量吗?或者除此之外还有其他解决方案吗?

最佳答案

我怀疑这是否可行,因为 SVM 只能处理数字数据。如果你想使用字符串,你要么必须使用 another kernel ,或使用过滤器将您的字符串数据转换为数字数据。

我建议你试试 StringToWordVector过滤器:

Converts String attributes into a set of attributes representing word occurrence (depending on the tokenizer) information from the text contained in the strings. The set of words (attributes) is determined by the first batch filtered (typically training data).

正如该过滤器的描述所说:您首先对训练数据进行批量过滤,这将初始化过滤器。如果您随后将过滤器应用于您的测试数据(即使是新的未知数据),结果将始终与您过滤的训练数据兼容。

最大的问题是您的模型是否必须在程序终止后继续存在。如果没有,没问题。

Instances train = ...   // from somewhere
Instances test = ... // from somewhere
Standardize filter = new Standardize();
filter.setInputFormat(train); // initializing the filter once with training set
Instances newTrain = Filter.useFilter(train, filter); // configures the Filter based on train instances and returns filtered instances
Instances newTest = Filter.useFilter(test, filter); // create new test set

( source )

由于你的过滤器已经在你的训练数据上初始化,你现在可以通过重复最后一行将它应用于任何看起来像未过滤训练集的数据集

Instances newTest2 = Filter.useFilter(test2, filter);    // create another new test set

如果您想保存您的模型并在您的应用程序多次运行期间反复应用它,您应该使用 FilteredClassifier。 (看看 this answer ,我在其中解释了 FilteredClassifier 的用法。)tl;dr:过滤器是分类器的一部分,可以与其一起序列化,保留对输入的转换数据。

关于java - 测试和训练集中不同数量的特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27031265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com