我们如何使用从 pyspark.ml 获得的 RandomForestClassifier 进行预测
How can we predict using RandomForestClassifier obtained from pyspark.ml
我正在做一个文本分类,我已经使用管道方法建立了一个模型。我已经创建了 RF 分类器对象并设置了我在之前的步骤中获得的特征列和标签列(步骤未显示)。
我正在拟合我使用数据框创建的训练数据,它有列 "labels" 和 "sentences"。标签是不同的问题类型。 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"])
管道的代码是,
rf = RandomForestClassifier().setFeaturesCol("features").setLabelCol("idxlabel")
pipeline = Pipeline(stages=[pos, tokenizer, hashingTF, idf, indexer,rf])
model = pipeline.fit(training)
所以现在我可以使用以下代码获得预测,
prediction = model.transform(test)
selected = prediction.select("sentence","prediction")
我可以执行 select() 操作来获取预测标签。
但对于我的用例,有一个来自 Kinesis 的数据流,它只是句子(纯字符串)。对于每个句子,我都必须预测标签。但是现在我在执行 dir(model) 时找不到任何 predict() 函数。从pyspark.ml获取的RandomForestClassifier怎么没有predict()方法呢?如果没有,我如何才能成功执行我的用例?我需要 predict() 方法来满足要求。如果不是 RF,我应该使用什么 ML 算法?我做错了什么吗?任何人都可以提出建议吗?任何帮助表示赞赏。我的环境是 Spark 1.6 和 Python 2.7.
于是我想通了,没有可以使用的predict()方法。因此,我们需要使用 transform() 方法来进行预测。只需删除标签列并创建一个新的数据框。例如,就我而言,我做到了,
pred = sqlContext.createDataFrame([("What are liver enzymes ?" ,)], ["sentence"])
prediction = model.transform(pred)
然后我们可以使用 select() 方法找到预测。至少现在,这个解决方案对我来说很成功。如果有任何更正或比这更好的方法,请告诉我。
我也在做同样的问题。你能告诉我管道阶段的 "pos"(词性) 是什么以及你是如何得到它的吗?还有你是如何准备测试数据的。下面是我的代码 -
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)
如果我做错了什么请告诉我。
我正在做一个文本分类,我已经使用管道方法建立了一个模型。我已经创建了 RF 分类器对象并设置了我在之前的步骤中获得的特征列和标签列(步骤未显示)。
我正在拟合我使用数据框创建的训练数据,它有列 "labels" 和 "sentences"。标签是不同的问题类型。 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"])
管道的代码是,
rf = RandomForestClassifier().setFeaturesCol("features").setLabelCol("idxlabel")
pipeline = Pipeline(stages=[pos, tokenizer, hashingTF, idf, indexer,rf])
model = pipeline.fit(training)
所以现在我可以使用以下代码获得预测,
prediction = model.transform(test)
selected = prediction.select("sentence","prediction")
我可以执行 select() 操作来获取预测标签。
但对于我的用例,有一个来自 Kinesis 的数据流,它只是句子(纯字符串)。对于每个句子,我都必须预测标签。但是现在我在执行 dir(model) 时找不到任何 predict() 函数。从pyspark.ml获取的RandomForestClassifier怎么没有predict()方法呢?如果没有,我如何才能成功执行我的用例?我需要 predict() 方法来满足要求。如果不是 RF,我应该使用什么 ML 算法?我做错了什么吗?任何人都可以提出建议吗?任何帮助表示赞赏。我的环境是 Spark 1.6 和 Python 2.7.
于是我想通了,没有可以使用的predict()方法。因此,我们需要使用 transform() 方法来进行预测。只需删除标签列并创建一个新的数据框。例如,就我而言,我做到了,
pred = sqlContext.createDataFrame([("What are liver enzymes ?" ,)], ["sentence"])
prediction = model.transform(pred)
然后我们可以使用 select() 方法找到预测。至少现在,这个解决方案对我来说很成功。如果有任何更正或比这更好的方法,请告诉我。
我也在做同样的问题。你能告诉我管道阶段的 "pos"(词性) 是什么以及你是如何得到它的吗?还有你是如何准备测试数据的。下面是我的代码 -
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)
如果我做错了什么请告诉我。