Time Features

PySpark के साथ Feature Engineering

John Hogue

Lead Data Scientist, General Mills

वस्तुओं की चक्रीय प्रकृति

समय की छवि

PySpark के साथ Feature Engineering

सही स्तर चुनना

दैनिक बिक्री

PySpark के साथ Feature Engineering

सही स्तर चुनना

मासिक बिक्री

PySpark के साथ Feature Engineering

डेट फ़ील्ड्स को तारीख़ की तरह ट्रीट करना...

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
PySpark के साथ Feature Engineering

समय के अवयव

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'))
PySpark के साथ Feature Engineering

मूलभूत समय-आधारित मैट्रिक्स

मार्केट पर दिन की छवि

from pyspark.sql.functions import datediff

# Calculate difference between two date fields
df.withColumn('DAYSONMARKET', datediff('OFFMARKETDATE', 'LISTDATE'))
PySpark के साथ Feature Engineering

Lagging फीचर्स

कप में कॉफ़ी की बूँद

window()

रिकॉर्ड्स के समूह के आधार पर एक रिकॉर्ड लौटाता है

lag(col, count=1)

वह मान लौटाता है जो मौजूदा पंक्ति से उतनी पंक्तियाँ पहले ऑफ़सेट है

PySpark के साथ Feature Engineering

PySpark तरीके से Lagging फीचर्स

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
PySpark के साथ Feature Engineering

It's TIME to practice!

PySpark के साथ Feature Engineering

Preparing Video For Download...