Punctuation, numbers और tokens
पिछले अध्याय के अंत में आपने SMS संदेशों का एक डेटासेट लोड किया था, जिन्हें "spam" (लेबल 1) या "ham" (लेबल 0) के रूप में टैग किया गया था. अब आप उन्हीं डेटा का उपयोग करके एक क्लासिफायर मॉडल बनाएँगे.
लेकिन पहले आपको SMS संदेशों को इस तरह तैयार करना होगा:
- punctuation और numbers हटाएँ
- tokenize करें (अलग-अलग शब्दों में बाँटें)
- stop words हटाएँ
- hashing trick लगाएँ
- TF-IDF representation में बदलें.
इस अभ्यास में आप punctuation और numbers हटाएँगे, फिर संदेशों को tokenize करेंगे.
SMS डेटा sms के रूप में उपलब्ध है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
PySpark के साथ Machine Learning
अभ्यास निर्देश
- रेगुलर एक्सप्रेशन को रिप्लेस करने वाला फंक्शन और tokenize करने वाली फीचर इम्पोर्ट करें.
textकॉलम से सभी punctuation कैरेक्टर को एक स्पेस से बदलें.textकॉलम में मौजूद सभी numbers के लिए भी यही करें.textकॉलम को tokens में बाँटें. आउटपुट कॉलम का नामwordsरखें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Import the necessary functions
from pyspark.sql.functions import ____
from pyspark.ml.feature import ____
# Remove punctuation (REGEX provided) and numbers
wrangled = sms.withColumn('text', ____(sms.text, '[_():;,.!?\\-]', ____))
wrangled = wrangled.withColumn(____, ____(____, ____, ____))
# Merge multiple spaces
wrangled = wrangled.withColumn('text', regexp_replace(wrangled.text, ' +', ' '))
# Split the text into words
wrangled = ____(inputCol='text', outputCol=____).____(wrangled)
wrangled.show(4, truncate=False)