在Pyspark中转换为编码循环特征



我试图将月份、弱和年中的日期列转换为周期性特征(sin,cos(,我的python代码如下所示:

def encode(data, col, max_val):
data[col + '_sin'] = np.sin(2 * np.pi * data[col]/max_val)
data[col + '_cos'] = np.cos(2 * np.pi * data[col]/max_val)
return data 

pyspark中的代码如下:

df = df.withColumn('month_sin',np.sin(2 * np.pi * df['month']/12)) 

我得到这个错误

TypeError: loop of ufunc does not support argument 0 of type Column which has no callable sin method

月份的列类型是integer,我将其转换为float和double,但没有帮助。

注意:该列没有零(0(值。

您必须使用PySparksin和pythonmath.pi而不是np

最新更新