시작하기무료로 시작하기

텍스트 벡터 살펴보기, 2부

이전 연습 문제에서 작성한 return_weights() 함수를 사용해, 이제 텍스트 벡터의 각 문서에서 상위 단어들을 추출하고 해당 단어 인덱스 목록을 반환한 뒤, 그 목록을 이용해 텍스트 벡터를 상위 단어들만 남도록 필터링해 보세요.

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

Python으로 배우는 Machine Learning 전처리

강의 보기

연습 안내

  • 해당 문서에 대해 가중치가 높은 상위 단어들을 반환하도록 return_weights()를 호출하세요.
  • 반환된 filter_list에서 중복 값을 제거하기 위해 set()을 호출하세요.
  • words_to_filter를 호출하고, 매개변수로는 vocabvocab 파라미터에, tfidf_vec.vocabulary_original_vocab 파라미터에, text_tfidfvector 파라미터에 전달하고, 각 문서에서 가중치 상위 3개 단어를 가져오도록 top_n에는 3을 전달하세요.
  • 마지막으로, 그 filtered_words 집합을 리스트로 변환해 텍스트 벡터를 필터링하는 데 사용하세요.

실습형 인터랙티브 연습

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

def words_to_filter(vocab, original_vocab, vector, top_n):
    filter_list = []
    for i in range(0, vector.shape[0]):
    
        # Call the return_weights function and extend filter_list
        filtered = ____(vocab, original_vocab, vector, i, top_n)
        filter_list.extend(filtered)
        
    # Return the list in a set, so we don't get duplicate word indices
    return ____(filter_list)

# Call the function to get the list of word indices
filtered_words = ____(____, ____, ____, ____)

# Filter the columns in text_tfidf to only those in filtered_words
filtered_text = text_tfidf[:, list(____)]
코드 편집 및 실행