from pyspark import SparkContext, HiveContext
from pyspark.mllib.regression import LabeledPoint
from pyspark.mllib.classification import LogisticRegressionWithLBFGS
table_name = "MyTable"
target_col = "MyTargetCol"
sc = SparkContext()
hc = HiveContext(sc)
# get the table from the hive context
df = hc.table(table_name)
# reorder columns so that we know the index of the target column
df = df.select(target_col, *[col for col in dataframe.columns if col != target_col])
# map through the data to produce an rdd of labeled points
rdd_of_labeled_points = df.map(lambda row: LabeledPoint(row[0], row[1:]))
# use the rdd as input to a model
model = LogisticRegressionWithLBFGS.train(rdd_of_labeled_points)
1条答案
按热度按时间jum4pzuy1#
这个问题的最佳解决方案可能是使用ml库及其模型,因为它们直接作用于Dataframe。
http://spark.apache.org/docs/latest/api/python/pyspark.ml.html?highlight=ml#module-pyspark.ml.分类
然而,mlapi还没有达到与mllib的功能奇偶性,您需要的东西可能丢失了。因此,我们在工作流中通过调用hive上下文检索到的Dataframe上的Map来解决这个问题。
请记住,无论何时使用python进行Map,都需要将数据从jvm封送到python vm,因此性能会受到影响。我们发现使用Map对性能的影响对于我们的数据来说可以忽略不计,但是您的里程数可能会有所不同。