시작하기무료로 시작하기

텍스트 벡터 탐색, 1부

방금 학습한 텍스트 벡터 탐색 방법을 volunteer 데이터셋의 title tf/idf 벡터로 확장해 보겠습니다. 텍스트 벡터 탐색의 첫 번째 단계에서는 슬라이드에서 살펴본 함수를 확장할 거예요. 이 함수는 숫자 목록을 반환하게 됩니다. 다음 연습 문제에서는 모든 문서에서 상위 단어를 모으는 다른 함수를 작성하고, 그 단어들을 추출한 뒤, 해당 목록을 사용해 text_tfidf 벡터를 필터링해 보겠습니다.

이 연습은 강의의 일부입니다

Python으로 배우는 Machine Learning 전처리

강의 보기

연습 안내

  • tfidf_vec.vocabulary_original_vocabtop_n이라는 매개변수를 추가하세요.
  • zip으로 만든 딕셔너리에 pd.Series()를 호출하세요. 이렇게 하면 이후 연산이 쉬워집니다.
  • .sort_values() 함수를 사용해 시리즈를 정렬하고, 인덱스를 top_n 개 단어까지만 슬라이스하세요.
  • 함수를 호출할 때 original_vocab=tfidf_vec.vocabulary_로 설정하고, 9번째 행을 가져오기 위해 vector_index=8, 가중치 상위 3개 단어를 가져오기 위해 top_n=3으로 설정하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Add in the rest of the arguments
def return_weights(vocab, ____, vector, vector_index, ____):
    zipped = dict(zip(vector[vector_index].indices, vector[vector_index].data))
    
    # Transform that zipped dict into a series
    zipped_series = ____({vocab[i]:zipped[i] for i in vector[vector_index].indices})
    
    # Sort the series to pull out the top n weighted words
    zipped_index = zipped_series.____(ascending=False)[:____].index
    return [original_vocab[i] for i in zipped_index]

# Print out the weighted words
print(return_weights(vocab, ____, text_tfidf, ____, ____))
코드 편집 및 실행