2017-05-26 184 views
1
Dataset<Row> dataFrame = ... ; 
StringIndexerModel labelIndexer = new StringIndexer() 
       .setInputCol("label") 
       .setOutputCol("indexedLabel") 
       .fit(dataFrame); 

VectorIndexerModel featureIndexer = new VectorIndexer() 
       .setInputCol("s") 
       .setOutputCol("indexedFeatures") 
       .setMaxCategories(4) 
       .fit(dataFrame); 
IndexToString labelConverter = new IndexToString() 
       .setInputCol("prediction") 
       .setOutputCol("predictedLabel") 
       .setLabels(labelIndexer.labels()); 

什麼是StringIndexer,VectorIndexer,IndexToString和它們之間的區別是什麼?我應該如何以及何時使用它們?什麼是StringIndexer,VectorIndexer以及如何使用它們?

回答

1

我知道只有那些二:

StringIndexer和VectorIndexer

StringIndexer:

  • 單個列到索引列轉換(類似於R中的一個因素列)

VectorIndexer:

  • 用於對featuresCol列中的分類預測變量進行索引。請記住featuresCol是由矢量組成的單個列(請參閱featuresCol和labelCol)。每一行都是一個包含每個預測變量值的向量。
  • 如果您有字符串類型預測變量,您首先需要使用StringIndexer對這些列進行索引。 featuresCol包含矢量,矢量不包含字符串值。

看看這裏例如:https://mingchen0919.github.io/learning-apache-spark/StringIndexer-and-VectorIndexer.html

+0

鏈接已損壞。 –

+0

我發現該頁面被移動到https://github.com/MingChen0919/learning-apache-spark - 一個非常好的資源! –

0

字符串索引 - 如果你想要的機器學習算法來識別列分類變量使用它,或者如果想將文本數據轉換爲數字數據保持分類上下文。

e,g將天數(星期一,星期二...)轉換爲數字表示。

向量索引器 - 如果我們不知道數據傳入的類型,使用這個。所以我們使用Vector Indexer將區分分類數據和非分類數據的邏輯分離爲算法。

e,g - 來自第三方API的數據,其中隱藏數據並直接攝入訓練模型。

索引器到字符串 - 正好與字符串索引器相反,如果使用字符串索引器對最終輸出列進行索引,現在我們要將其數字表示轉換回文本,以便更好地理解結果。

相關問題