使用Pyspark从Spark数据帧创建标签点

使用Pyspark从Spark数据帧创建标签点,pyspark,rdd,apache-spark-mllib,random-forest,Pyspark,Rdd,Apache Spark Mllib,Random Forest,我有一个spark数据帧,其中包含两个coulmn“标签”和“稀疏向量”,这是在对tweet语料库应用Countvectorizer后获得的 当尝试训练随机森林回归模型时,我发现它只接受类型标签点 有人知道如何将我的spark数据帧转换为LabeledPoint吗?您使用的是哪个spark版本。Spark使用Spark ml而不是mllib from pyspark.ml.feature import CountVectorizer from pyspark.ml.classification

我有一个spark数据帧,其中包含两个coulmn“标签”和“稀疏向量”,这是在对tweet语料库应用Countvectorizer后获得的

当尝试训练随机森林回归模型时,我发现它只接受类型标签点


有人知道如何将我的spark数据帧转换为LabeledPoint吗?您使用的是哪个spark版本。Spark使用Spark ml而不是mllib

from pyspark.ml.feature import CountVectorizer
from pyspark.ml.classification import RandomForestClassifier
from pyspark.sql import functions as F

# Input data: Each row is a bag of words with a ID.
df = sqlContext.createDataFrame([
    (0, "a b c".split(" ")),
    (1, "a b b c a".split(" "))
], ["id", "words"])

# fit a CountVectorizerModel from the corpus.
cv = CountVectorizer(inputCol="words", outputCol="features", vocabSize=3, minDF=2.0)

model = cv.fit(df)

result = model.transform(df).withColumn('label', F.lit(0))
rf = RandomForestClassifier(labelCol="label", featuresCol="features", numTrees=10)
rf.fit(result)
如果您坚持使用mllib:

from pyspark.mllib.regression import LabeledPoint
from pyspark.mllib.tree import RandomForest

rdd = result \ 
          .rdd \
          .map(lambda row: LabeledPoint(row['label'], row['features'].toArray()))
RandomForest.trainClassifier(rdd, 2, {}, 3)

嗨,我使用的是spark 2.3.0版本。嗨,我使用的是同一版本。你能用ml模块代替ml吗?带标签的点与rdd一起工作。你能帮我设置它的参数吗?事实上,在mlli中,我使用了Train Regressionor,但在ml的例子中,它是不同的类。这是pyspark.ml.regression.RandomforestRegressionr(self,featuresCol=“features”,labelCol=“label”,predictionCol=“prediction”,maxDepth=5,maxBins=32,minInstancesPerNode=1,MinInfo=0.0,maxMemoryInMB=256,cacheNodeIds=False,checkpointInterval=10,Purchy=“variance”,subsamplingRate=1.0,seed=None,numTrees=20,featureSubsetStrategy=“auto”),您应该将df转换为rdd,然后从mllib.Returnal导入标记点。答案已更新。它是否支持数据帧或标签点类型??