Python wordcloud中generate_from_frequencies方法所需的元组数组 [英] Array of tuples necessary for generate_from_frequencies method in Python wordcloud

查看:52
本文介绍了Python wordcloud中generate_from_frequencies方法所需的元组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从Excel文档中字符串的含义及其对应的数据值中使Python中的词云变得模糊.generate_from_frequencies方法采用一个频率参数,文档认为该参数应采用一个元组数组.

来自

它仍然是我的IDE中的一个错误,虽然会引起误解,但它按照预期的方式运行.

I am trying to make a word cloud in Python from the significance of strings and their corresponding data values in an Excel document. The generate_from_frequencies method takes a frequencies parameter which the docs say is supposed to take an array of tuples.

Partial code from wordcloud source code:

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]

I tried using a regular list, then I tried a ndarray from numpy, but PyCharm shows warnings that the parameter type should be array.py, which I read is only supposed to take characters, integers, and floating point numbers (array.py docs):

This module defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers.

My test code:

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

If I run the code above despite the PyCharm warning, I get the following error, which I suppose is another way of telling me that it can't accept the ndarray type:

  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

Another potential problem could be that wordcloud was written in Python 2 but I am using Python 3.4, which may have rendered some of the code unusable. What type should I pass this method?

解决方案

Thanks to J Herron and selva for the answer to use tuples instead of a list object-- and I ended up with this:

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

It still came up as an error in my IDE, which was misleading, but it worked the way it was supposed to.

这篇关于Python wordcloud中generate_from_frequencies方法所需的元组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆