Spark LogisticRegression示例不对测试数据进行分类

时间:2016-04-26 12:45:50

标签: apache-spark logistic-regression apache-spark-mllib

spark documentation包含以下逻辑回归示例:

from pyspark.ml import Pipeline
from pyspark.ml.classification import LogisticRegression
from pyspark.ml.feature import HashingTF, Tokenizer
from pyspark.sql import Row

# Prepare training documents from a list of (id, text, label) tuples.
LabeledDocument = Row("id", "text", "label")
training = sqlContext.createDataFrame([
    (0L, "a b c d e spark", 1.0),
    (1L, "b d", 0.0),
    (2L, "spark f g h", 1.0),
    (3L, "hadoop mapreduce", 0.0)], ["id", "text", "label"])

# Configure an ML pipeline, which consists of tree stages: tokenizer, hashingTF, and lr.
tokenizer = Tokenizer(inputCol="text", outputCol="words")
hashingTF = HashingTF(inputCol=tokenizer.getOutputCol(), outputCol="features")
lr = LogisticRegression(maxIter=10, regParam=0.01)
pipeline = Pipeline(stages=[tokenizer, hashingTF, lr])

# Fit the pipeline to training documents.
model = pipeline.fit(training)

# Prepare test documents, which are unlabeled (id, text) tuples.
test = sqlContext.createDataFrame([
    (4L, "spark i j k"),
    (5L, "l m n"),
    (6L, "mapreduce spark"),
    (7L, "apache hadoop")], ["id", "text"])

# Make predictions on test documents and print columns of interest.
prediction = model.transform(test)
selected = prediction.select("id", "text", "prediction")
for row in selected.collect():
    print(row)

由于行0L2L标记为1.0,因此我预计行4L6L也会被预测为1.0。但是,这是我收到的输出:

Row(id=4, text=u'spark i j k', prediction=0.0)
Row(id=5, text=u'l m n', prediction=0.0)
Row(id=6, text=u'mapreduce spark', prediction=0.0)
Row(id=7, text=u'apache hadoop', prediction=0.0)

任何人都可以让我知道为什么没有行预测为1.0?

0 个答案:

没有答案