我是机器学习的新手。我正在尝试使用MLlib的决策树。
有人可以帮我解决如何为多类别分类准备mllib决策树的输入文件。
我的csv文件格式为
标签,文字
label_1, text of label 1
label_2, text of label 2
label_3, text of label 3
mllib输入所需的格式为libsvm或labeledpoint fileformat中不允许使用文本。问题是如何将文本映射到mllib所需的基于数字数据的文件格式,然后解释结果。我正在使用java实现。
此致
答案 0 :(得分:0)
您需要编写这样的映射器来解析csv文件
public class TokensToLabeledPoints implements Function<String[], LabeledPoint> {
int responseIndex;
TokensToLabeledPoints(int index) {
this.responseIndex = index;
}
@Override
public LabeledPoint call(String[] tokens) throws Exception {
double y = Double.parseDouble(tokens[responseIndex]);
double[] x = new double[tokens.length];
for (int i = 0; i < tokens.length; ++i) {
if (responseIndex != i) {
x[i] = Double.parseDouble(tokens[i]);
}
}
return new LabeledPoint(y, Vectors.dense(x));
}
}
答案 1 :(得分:0)
text to numeric是bag of words,tf-idf的一个广泛而复杂的主题。 一般来说,您需要:
基于原始文件,词汇:
label_1, word1, word2,...
label_2, word1, word2,...
label_3, word1, word2,...
此外,基于TfxIdf背后的频率和数学,您可以创建特征空间(通常比单词包含更好的结果)
label_1, feature1, feature2,...
label_2, feature1, feature2,...
label_3, feature1, feature2,...
在Spark中,请查看:http://spark.apache.org/docs/latest/mllib-feature-extraction.html#tf-idf,或者您可能希望使用word2vec等工具
祝你好运!