Pipeline สำหรับตรวจจับ SMS สแปม
ห่างหายจากข้อมูล SMS ไปนานพอสมควร ครั้งก่อนเราได้ทำขั้นตอนต่อไปนี้
- แบ่งข้อความออกเป็น token
- ลบ stop words ออก
- ใช้ hashing trick
- แปลงข้อมูลจากการนับความถี่เป็น IDF และ
- ฝึกโมเดล Logistic Regression
แต่ละขั้นตอนเหล่านี้ทำแยกกันทีละขั้น ซึ่งเหมาะมากที่จะนำ pipeline มาใช้!
คลาส Pipeline และ LogisticRegression ถูก import เข้า session ไว้แล้ว ไม่ต้องกังวลในส่วนนี้
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Machine Learning with PySpark
คำแนะนำการฝึกหัด
- สร้าง object สำหรับแบ่งข้อความออกเป็น token
- สร้าง object สำหรับลบ stop words โดยใช้เมธอด
getOutputCol()บน object ก่อนหน้าแทนการระบุชื่อ input column โดยตรง - สร้าง object สำหรับใช้ hashing trick และแปลงข้อมูลเป็น TF-IDF โดยใช้เมธอด
getOutputCol()อีกครั้ง - สร้าง pipeline ที่รวมทุกขั้นตอนข้างต้น พร้อมกับ object สำหรับสร้างโมเดล Logistic Regression
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
from pyspark.ml.feature import Tokenizer, StopWordsRemover, HashingTF, IDF
# Break text into tokens at non-word characters
tokenizer = ____(inputCol='text', outputCol='words')
# Remove stop words
remover = ____(inputCol=____, outputCol='terms')
# Apply the hashing trick and transform to TF-IDF
hasher = ____(inputCol=____, outputCol="hash")
idf = ____(inputCol=____, outputCol="features")
# Create a logistic regression object and add everything to a pipeline
logistic = LogisticRegression()
pipeline = Pipeline(stages=[____, ____, ____, ____, logistic])