簡體   English   中英

Python wordcloud 中 generate_from_frequencies 方法所需的元組數組

[英]Array of tuples necessary for generate_from_frequencies method in Python wordcloud

我正在嘗試根據字符串的重要性及其在 Excel 文檔中的相應數據值在 Python 中制作一個詞雲。 generate_from_frequencies 方法采用頻率參數,文檔說該參數應該采用元組數組。

來自wordcloud源代碼的部分代碼

def generate_from_frequencies(self, frequencies):
    """Create a word_cloud from words and frequencies.
    Parameters
    ----------
    frequencies : array of tuples
        A tuple contains the word and its frequency.
    Returns
    -------
    self
    """
    # make sure frequencies are sorted and normalized
    frequencies = sorted(frequencies, key=item1, reverse=True)
    frequencies = frequencies[:self.max_words]
    # largest entry will be 1
    max_frequency = float(frequencies[0][1])

    frequencies = [(word, freq / max_frequency) for word, freq in frequencies]

我嘗試使用常規列表,然后嘗試使用 numpy 中的 ndarray,但 PyCharm 顯示參數類型應為 array.py 的警告,我讀取的參數類型僅應采用字符、整數和浮點數( array.py 文檔):

該模塊定義了一個 object 類型,它可以緊湊地表示一組基本值:字符、整數、浮點數。

我的測試代碼:

import os
import numpy
import wordcloud

d = os.path.dirname(__file__)
cloud = wordcloud.WordCloud()
array = numpy.array([("hi", 6), ("seven"), 17])
cloud.generate_from_frequencies(array)  # <= what should go in the parentheses

如果我不顧 PyCharm 警告運行上面的代碼,我會收到以下錯誤,我想這是告訴我它不能接受 ndarray 類型的另一種方式:

  File "C:/Users/Caitlin/Documents/BioDataSorter/tag_cloud_test.py", line 8, in <module>
cloud.generate_from_frequencies(array)  # <= what should go in the parentheses
  File "C:\Python34\lib\site-packages\wordcloud\wordcloud.py", line 263, in generate_from_frequencies
frequencies = sorted(frequencies, key=item1, reverse=True)
TypeError: 'int' object is not subscriptable

另一個潛在的問題可能是 wordcloud 是用 Python 2 編寫的,但我使用的是 Python 3.4,這可能導致某些代碼無法使用。 我應該通過什么類型的方法?

從您的測試代碼... # <=這個括號中應該包含什么

我相信你應該有一個元組(("hi", float(6/(6+17)),("seven", float(17/(6+17))))

感謝J Herron和selva回答使用元組而不是列表對象 - 我最終得到了這個:

cloud.generate_from_frequencies((("hi", 3),("seven", 7)))

生成的詞雲

它仍然在我的IDE中出現錯誤,這是一種誤導,但它按照預期的方式工作。

基於 CCCodes 的回答,這里是所提供方式的新版本,權重映射到字典中的單詞:

cloud.generate_from_frequencies({"hi": 3,"seven": 7})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM