Các đặc trưng thời gian

Feature Engineering với PySpark

John Hogue

Lead Data Scientist, General Mills

Tính chu kỳ của mọi thứ

Hình về thời gian

Feature Engineering với PySpark

Chọn đúng cấp độ

Doanh số theo ngày

Feature Engineering với PySpark

Chọn đúng cấp độ

Doanh số theo tháng

Feature Engineering với PySpark

Xem trường ngày như kiểu Date...

from pyspark.sql.functions import to_date

# Cast the data type to Date
df = df.withColumn('LISTDATE', to_date('LISTDATE'))
# Inspect the field
df[['LISTDATE']].show(2)
+----------+
|  LISTDATE|
+----------+
|2017-07-14|
|2017-10-08|
+----------+
only showing top 2 rows
Feature Engineering với PySpark

Thành phần thời gian

from pyspark.sql.functions import year, month

# Create a new column of year number
df = df.withColumn('LIST_YEAR', year('LISTDATE'))
# Create a new column of month number
df = df.withColumn('LIST_MONTH', month('LISTDATE'))
from pyspark.sql.functions import dayofmonth, weekofyear

# Create new columns of the day number within the month
df = df.withColumn('LIST_DAYOFMONTH', dayofmonth('LISTDATE'))
# Create new columns of the week number within the year
df = df.withColumn('LIST_WEEKOFYEAR', weekofyear('LISTDATE'))
Feature Engineering với PySpark

Chỉ số thời gian cơ bản

Số ngày trên thị trường

from pyspark.sql.functions import datediff

# Calculate difference between two date fields
df.withColumn('DAYSONMARKET', datediff('OFFMARKETDATE', 'LISTDATE'))
Feature Engineering với PySpark

Đặc trưng độ trễ

Giọt cà phê trong cốc

window()

Trả về một bản ghi dựa trên một nhóm bản ghi

lag(col, count=1)

Trả về giá trị lùi lại theo số hàng trước hàng hiện tại

Feature Engineering với PySpark

Đặc trưng độ trễ với PySpark

from pyspark.sql.functions import lag
from pyspark.sql.window import Window

# Create Window w = Window().orderBy(m_df['DATE'])
# Create lagged column m_df = m_df.withColumn('MORTGAGE-1wk', lag('MORTGAGE', count=1).over(w))
# Inspect results m_df.show(3)
+----------+------------+----------------+
|      DATE|    MORTGAGE|    MORTGAGE-1wk|
+----------+------------+----------------+
|2013-10-10|        4.23|            null|
|2013-10-17|        4.28|            4.23|
|2013-10-24|        4.13|            4.28|
+----------+------------+----------------+
only showing top 3 rows
Feature Engineering với PySpark

ĐẾN LÚC luyện tập rồi!

Feature Engineering với PySpark

Preparing Video For Download...