Twitter 항공사 감성 데이터에 TfIdf 적용
이제 TfIdf 방법을 사용해 피처를 만들어 보겠습니다. 계속해서 tweets 데이터셋을 사용해요.
이번 연습에서는 이전 레슨에서 배운 내용을 활용해 불용어를 제거하고, 토큰 패턴을 적용하며, n-gram을 지정합니다.
최종 출력은 TfidfVectorizer()로 생성한 열을 가진 DataFrame입니다. 이렇게 만든 DataFrame은 지도 학습 모델에 바로 전달할 수 있으며, 이는 다음 장에서 다룰 내용입니다.
이 연습은 강의의 일부입니다
Python으로 배우는 Sentiment Analysis
연습 안내
- TfidfVectorizer와
ENGLISH_STOP_WORDS를 만들기 위해 필요한 패키지를 임포트하세요. tweets데이터셋의text열로부터 TfIdf 벡터라이저를 구축하세요. n-gram은 uni-gram과 bi-gram을 사용하고, 제공된 토큰 패턴으로 영숫자 문자만 포함하는 토큰을 사용하며, 불용어는ENGLISH_STOP_WORDS를 지정하세요.- 학습에 사용한 것과 같은 열을 지정하여 벡터라이저를 변환하세요.
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())