我有一个csv文件,有三列:Id,Main_user和Users。Id 是标注,其他两个值都作为要素。现在我想从 csv 加载两个特征(main_user 和用户),将它们矢量化并将它们组装为一个矢量。按照文档中所述使用 HashingTF 后,除了功能"用户"之外,如何添加第二个功能"Main_user"。
DataFrame df = (new CsvParser()).withUseHeader(true).csvFile(sqlContext, csvFile);
Tokenizer tokenizer = new Tokenizer().setInputCol("Users").setOutputCol("words");
DataFrame wordsData = tokenizer.transform(df);
int numFeatures = 20;
HashingTF hashingTF = new HashingTF().setInputCol("words")
.setOutputCol("rawFeatures").setNumFeatures(numFeatures);
好的,
我找到了解决方案。一个接一个地加载列,标记化,hashTF,最后组装它们。我将不胜感激对此的任何改进。
DataFrame df = (new CsvParser()).withUseHeader(true).csvFile(sqlContext, csvFile);
Tokenizer tokenizer = new Tokenizer();
HashingTF hashingTF = new HashingTF();
int numFeatures = 35;
tokenizer.setInputCol("Users")
.setOutputCol("Users_words");
DataFrame df1 = tokenizer.transform(df);
hashingTF.setInputCol("Users_words")
.setOutputCol("rawUsers").setNumFeatures(numFeatures);
DataFrame featurizedData1 = hashingTF.transform(df1);
tokenizer.setInputCol("Main_user")
.setOutputCol("Main_user_words");
DataFrame df2 = tokenizer.transform(featurizedData1);
hashingTF.setInputCol("Main_user_words")
.setOutputCol("rawMain_user").setNumFeatures(numFeatures);
DataFrame featurizedData2 = hashingTF.transform(df2);
// Now Assemble Vectors
VectorAssembler assembler = new VectorAssembler()
.setInputCols(new String[]{"rawUsers", "rawMain_user"})
.setOutputCol("assembeledVector");
DataFrame assembledFeatures = assembler.transform(featurizedData2);