我在进行频繁模式分析,需要一些关于输入类型的帮助。
首先,我使用stringindexer将我的分类变量转换为数字。
之后,我为每个分类值创建一个唯一数字,如下所示:
add_100=udf(lambda x:x+100,returnType=FloatType())add_1000=udf(lambda x:x+1000,returnType=FloatType())df = df.select('cat_var_1', add_1000('cat_var_2').alias('cat_var_2_final'), add_10000('cat_var_3').alias('cat_var_3_final'))
我的下一步是创建一个包含特征的向量:
featuresCreator = ft.VectorAssembler(inputCols=[col for col in features], outputCol='features')df=featuresCreator.transform(df)
最后,我尝试拟合我的模型:
from pyspark.ml.fpm import FPGrowthfpGrowth = FPGrowth(itemsCol="features", minSupport=0.5, minConfidence=0.6)model = fpGrowth.fit(df)
然后得到了这个错误:
u’requirement failed: The input column must be ArrayType, but got org.apache.spark.ml.linalg.VectorUDT@3bfc3ba7.
所以,问题是,我如何将我的向量转换为数组?或者,还有其他方法可以解决这个问题吗?
回答:
我认为,你不需要使用udf来创建唯一数字。你可以直接使用withColumn,如下所示:
df = df.withColumn('cat_var_2_final',df['cat_var_2']+100).withColumn('cat_var_3_final',df['cat_var_3']+1000)
另外,如果你只为FPGrowth模型准备这些数据,我们也可以跳过VectorAssembler,直接使用udf创建Array特征,如下所示:
udf1 = udf(lambda c1,c2,c3 : (c1,c2,c3),ArrayType(IntegerType()))df = df.withColumn('features',udf1(df['cat_var_1'],df['cat_var_2_final'],df['cat_var_3_final']))