본문 바로가기

명사 美 비격식 (무리 중에서) 아주 뛰어난[눈에 띄는] 사람[것]

AI/LLM

wordcloud 만들때 데이터가 string형인가 dict형인가 generate() , generate_from_frequencies() - TypeError: unhashable type: 'dict' wordcloud.generate

TypeError: unhashable type: 'dict'

키를 찾고있는데 딕셔너리가 보여 에러가남.

^^^^^^^^^^^^
  File "D:\study\sk_playdata\study_ai\llm_workspace\day32_llm_nlp_analysis\test_konlpy_pjt\.venv\Lib\site-packages\wordcloud\wordcloud.py", line 586, in process_text
    words = re.findall(regexp, text, flags)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\playdata2\AppData\Local\Programs\Python\Python311\Lib\re\__init__.py", line 216, in findall
    return _compile(pattern, flags).findall(string)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected string or bytes-like object, got 'int'
(.venv) (base) PS D:\study\sk_playdata\study_ai\llm_workspace\day32_llm_nlp_analysis\test_konlpy_pjt> python test2.py
Traceback (most recent call last):
  File "D:\study\sk_playdata\study_ai\llm_workspace\day32_llm_nlp_analysis\test_konlpy_pjt\test2.py", line 56, in <module>
    wordcloud.generate(word_dic[word_dic])
                       ~~~~~~~~^^^^^^^^^^
TypeError: unhashable type: 'dict'

 

 

 

 

 

수정

generate() 는 하나의 긴 문자열로 생성하고 

generate_from_frequencies()는 dict 형태의 단어와 빈도가 담긴 딕셔너리로 생성한다. 

# wordcloud.generate()
wordcloud.generate_from_frequencies()

 

 

 

full code

from wordcloud import WordCloud
import matplotlib.pyplot as plt

wordcloud = WordCloud(
    font_path="C:/Windows/Fonts/malgun.ttf",
        width=1000,
        height=800,
        background_color="black",
        # colormap="hot"
        # colormap="hot"       # 검정→빨강→주황→노랑
        # colormap="inferno"   # 검정→보라→주황
        # colormap="plasma"    # 보라→핑크→노랑
        # colormap="viridis"   # 파랑→초록→노랑
        # colormap="magma"     # 검정→보라→주황
        colormap="turbo"     # 무지개 느낌
    )
# wordcloud.generate()
wordcloud.generate_from_frequencies(word_dic)

plt.figure(figsize=(10,10))
plt.imshow(wordcloud)
plt.axis("off")
plt.show()

 

 

해결.