2017-12-20 8 views
0

私はテキスト分類を行い、パイプラインメソッドを使用してモデルを構築しました。ランダムフォレストクラシファイド - インデックス付きラベルタグを文字列値に戻す

私はデータフレームを使用して作成したトレーニングデータにフィットし、「ラベル」と「文章」という列があります。ラベルには異なる質問タイプがあります。 DFは、パイプラインを作成するためのコード

training = sqlContext.createDataFrame([ 
("DESC:manner", "How did serfdom develop in and then leave Russia ?"), 
("DESC:def", "What does '' extended definition '' mean and how would one a paper on it ? "), 
("HUM:ind", " Who was The Pride of the Yankees ?") 
], ["label", "sentence"]) 

、のように見える -

tokenizer = Tokenizer(inputCol="sentence", outputCol="words") 
wordsData = tokenizer.transform(training) 
hashingTF = HashingTF(inputCol="words", outputCol="rawFeatures", numFeatures=20) 
featurizedData = hashingTF.transform(wordsData) 
idf = IDF(inputCol="rawFeatures", outputCol="features") 
indexer = StringIndexer(inputCol="label", outputCol="idxlabel") 

rf = RandomForestClassifier().setFeaturesCol("features").setLabelCol("idxlabel") 
pipeline = Pipeline(stages=[tokenizer, hashingTF, idf, indexer, rf]) 
model = pipeline.fit(training) 

と予測するためのコードです -

test = sqlContext.createDataFrame([("What is the highest waterfall in the United States ?" ,)], ["sentence"]) 
prediction = model.transform(test) 
selected = prediction.select("sentence", "prediction") 

今、私は、コマンドに「selected.showを与える場合(truncate = False) 'を指定すると、次の形式のデータが表示されます。

+----------------------------------------------------+----------+ 
|Question           |prediction| 
+----------------------------------------------------+----------+ 
|What is the highest waterfall in the United States ?|2.0  | 
+----------------------------------------------------+----------+ 

問題は、予測されたデータをトレーニングデータのようにラベル形式にすることです。しかし、私は倍の形式で値を取得しています。予測値をdoubleからstringに変換するにはどうすればよいですか?

答えて

0

必要な機能を提供するIndexToStringトランスフォーマーがあります。詳細については、スパークソースのスカラーの例を参照してください。

labeler = IndexToString(inputCol="prediction", outputCol="predictedLabel", labels=indexer.labels) 
関連する問題