การพยากรณ์ CTR ด้วย Machine Learning ใน Python
Kevin Huo
Instructor
print(df.hour.head(1))
14102101
df['hour'] = pd.to_datetime(
df['hour'], format = '%y%m%d%H')
df['hour_of_day'] = df['hour'].dt.hour
print(df.hour.head(1))
2014-10-21 01:00:0
print(df.groupby('hour_of_day')
['click'].sum())
click
hour_of_day
1 1092
2 6546
ฟีเจอร์ประเภท categorical ต้องแปลงเป็นตัวเลขก่อน
Hash function: แปลงค่า input ใด ๆ ให้เป็นจำนวนเต็ม และคืนค่าเดิมเสมอสำหรับ input เดิม
Lambda function: lambda x: f(x)
ใช้ hash function ผ่าน f(x) = hash(x) ดังนี้:
df['site_id'] = df['site_id'].apply(lambda x: hash(x))
83a0ad1a -> -9161053084583616050
85f751fd-> 818242008494177460
count() และ nunique():df['ad_type'].count()
50000
df['ad_type'].nunique()
31

การเพิ่มฟีเจอร์ช่วยเพิ่มพลังในการพยากรณ์
ตัวอย่างฟีเจอร์ใหม่: จำนวน impression ตาม device_id (ผู้ใช้) และ search_engine_type:
df['device_id_count'] = df.groupby('device_id')['click'].transform("count")
df['search_engine_type_count'] = df.groupby('search_engine_type')['click'].transform("count")
print(df.head(1))
... device_id_count search_engine_type_count
... 40862 47710
การพยากรณ์ CTR ด้วย Machine Learning ใน Python