開始使用免費開始

使用停用字的航空公司情緒分析

給你一個名為 tweets 的資料集,包含顧客對航空公司的評論與情緒。它有兩個欄位:airline_sentimenttext,其中情緒可能是正向、負向或中立;text 則是推文的文字內容。

在這個練習中,你將建立一個 BOW 表示法,但同時考量停用字。記得停用字不具資訊量,你可能會想把它們移除。這樣會讓詞彙表更小,最終特徵也會更少。請記住,我們可以在預設的停用字清單之上,加入與我們情境相關的停用字,來加以擴充。

本練習屬於課程

Python 情感分析

檢視課程

練習說明

  • 匯入英語停用字的預設清單。
  • 使用給定的清單 ['airline', 'airlines', '@'] 擴充預設停用字,建立 my_stop_words
  • 在向量化器中指定停用字參數。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import the stop words
from sklearn.feature_extraction.text import CountVectorizer, ____

# Define the stop words
my_stop_words = ____.____(['airline', 'airlines', '@'])

# Build and fit the vectorizer
vect = CountVectorizer(____=my_stop_words)
vect.fit(tweets.text)

# Create the bow representation
X_review = vect.transform(tweets.text)
# Create the data frame
X_df = pd.DataFrame(X_review.toarray(), columns=vect.get_feature_names())
print(X_df.head())
編輯並執行程式碼