TfIdf กับข้อมูล Twitter airline sentiment
ในแบบฝึกหัดนี้ จะได้สร้างฟีเจอร์โดยใช้วิธี TfIdf โดยยังคงทำงานกับชุดข้อมูล tweets ต่อไป
จะได้นำสิ่งที่เรียนรู้จากบทเรียนก่อนหน้ามาใช้ ได้แก่ การลบ stop words การกำหนด token pattern และการระบุ n-grams
ผลลัพธ์สุดท้ายจะเป็น DataFrame ที่มีคอลัมน์สร้างขึ้นจาก TfidfVectorizer() ซึ่ง DataFrame นี้สามารถนำไปใช้กับโมเดล supervised learning ได้โดยตรง อันเป็นสิ่งที่จะเรียนรู้ในบทถัดไป
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Sentiment Analysis ด้วย Python
คำแนะนำการฝึกหัด
- นำเข้าแพ็กเกจที่จำเป็นสำหรับสร้าง TfidfVectorizer และ
ENGLISH_STOP_WORDS - สร้าง TfIdf vectorizer จากคอลัมน์
textของชุดข้อมูลtweetsโดยกำหนด uni-gram และ bi-gram เป็นตัวเลือก n-grams ใช้ token pattern ที่กำหนดให้เพื่อให้รับเฉพาะอักขระที่เป็นตัวอักษรและตัวเลข และระบุ stop words ด้วยENGLISH_STOP_WORDS - Transform vectorizer โดยระบุคอลัมน์เดียวกับที่ใช้ fit
- ระบุชื่อคอลัมน์ในฟังก์ชัน
DataFrame()
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Import the required vectorizer package and stop words list
____
# Define the vectorizer and specify the arguments
my_pattern = r'\b[^\d\W][^\d\W]+\b'
vect = ____(____=(1, 2), max_features=100, ____=my_pattern, ____=ENGLISH_STOP_WORDS).fit(tweets.text)
# Transform the vectorizer
X_txt = vect.____(____.____)
# Transform to a data frame and specify the column names
X=pd.DataFrame(X_txt.toarray(), columns=____.____)
print('Top 5 rows of the DataFrame: ', X.head())