我有一个csv文件,有三列:Id,Main_user和Users。 Id是标签,其他值都是功能。现在我想从csv加载两个功能(main_user和用户),将它们矢量化并将它们组装成一个向量。 在使用documentation中所述的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);
答案 0 :(得分:3)
好的我找到了解决方案。一个接一个地加载列,标记化,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);