我是靠谱客的博主 爱听歌航空,最近开发中收集的这篇文章主要介绍spark kmeans java_pyspark:kmeans的分类变量准备,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

我知道Kmeans不适用于分类数据,但我们在spark 1.4中没有太多选项可用于聚类分类数据 . 无论上述问题如何 . 我在下面的代码中遇到错误 . 我从hive读取我的表,在管道中使用onehotencoder,然后将代码发送到Kmeans .

我在运行此代码时遇到错误 . 错误是否可以输入Kmeans的数据类型? doen是否期待numpay Array数据?如果是这样我如何将索引数据传输到numpy数组!?!?所有评论都得到了批准,感谢您的帮助!

我得到的错误:Traceback(最近一次调用最后一次):文件“/usr/hdp/2.3.2.0-2950/spark/python/lib/pyspark.zip/pyspark /daemon.py”,第157行,在manager文件中“/usr/hdp/2.3.2.0-2950/spark/python/lib/pyspark.zip/pyspark/daemon.py”,第61行,在工作文件“/usr/hdp/2.3.2.0-2950/spark/python /lib/pyspark.zip/pyspark/worker.py“,第136行,在main中如果read_int(infile)== SpecialLengths.END_OF_STREAM:文件”/usr/hdp/2.3.2.0-2950/spark/python/lib/pyspark .zip / pyspark / serializers.py“,第544行,在read_int中引发EOFError EOFError文件”“,第1行Traceback(最近一次调用最后一次):

我的代码:

#aline will be passed in from another rdd

aline=["xxx","yyy"]

# get data from Hive table & select the column & convert back to Rdd

rddRes2=hCtx.sql("select XXX, YYY from table1 where xxx <> ''")

rdd3=rddRes2.rdd

#fill the NA values with "none"

Rdd4=rdd3.map(lambda line: [x if len(x) else 'none' for x in line])

# convert it back to Df

DataDF=Rdd4.toDF(aline)

# Indexers encode strings with doubles

string_indexers=[

StringIndexer(inputCol=x,outputCol="idx_{0}".format(x))

for x in DataDF.columns if x not in '' ]

encoders=[

OneHotEncoder(inputCol="idx_{0}".format(x),outputCol="enc_{0}".format(x))

for x in DataDF.columns if x not in ''

]

# Assemble multiple columns into a single vector

assembler=VectorAssembler(

inputCols=["enc_{0}".format(x) for x in DataDF.columns if x not in ''],

outputCol="features")

pipeline= Pipeline(stages=string_indexers+encoders+[assembler])

model=pipeline.fit(DataDF)

indexed=model.transform(DataDF)

labeled_points=indexed.select("features").map(lambda row: LabeledPoint(row.features))

# Build the model (cluster the data)

clusters = KMeans.train(labeled_points, 3, maxIterations=10,runs=10, initializationMode="random")

最后

以上就是爱听歌航空为你收集整理的spark kmeans java_pyspark:kmeans的分类变量准备的全部内容,希望文章能够帮你解决spark kmeans java_pyspark:kmeans的分类变量准备所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部