Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save kristiyanto/f027d3d2ffd4b5386de99c4d57d4fee9 to your computer and use it in GitHub Desktop.

Select an option

Save kristiyanto/f027d3d2ffd4b5386de99c4d57d4fee9 to your computer and use it in GitHub Desktop.
Fintech Engagement: feature computation
class f_user_daysSinceLastTransc(Transformer):
''' Feature description: Number of days since last transaction.
'''
@keyword_only
def __init__(self, transc_table, **kwargs):
super(f_user_daysSinceLastTransc, self).__init__()
self.transc_table = transc_table
self.feature_names = "user_daysSinceLastTransc"
def _transform(self, X):
latest_date = (self.transc_table
.agg(f.max("created_date"))
.collect()[0][0])
last_transction = (self.transc_table.withColumn("rn", f.row_number().over(
Window.partitionBy("user_id").orderBy(f.desc("created_date"))))
.where("rn==1")
.withColumn(self.feature_names,
f.datediff(f.lit(latest_date), col("created_date")))
.select("user_id", self.feature_names))
return X.join(last_transction, ["user_id"], "left")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment