2016-07-07 3 views
1

문자열의 중요성과 해당 데이터 값을 Excel 문서에서 파이썬으로 작성하려고합니다. generate_from_frequencies 메소드는 문서가 튜플 배열을 취한다고 가정하는 주파수 매개 변수를 사용합니다.파이썬 wordcloud에서 generate_from_frequencies 메서드에 필요한 튜플 배열

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] 

, 그럼 내가 NumPy와에서 ndarray을 시도했지만 PyCharm은 매개 변수 유형은 내가 아니라 읽을 수있는 array.py해야한다는 경고를 보여줍니다

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

내 테스트 코드 :

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 
문자, 정수 및 부동 소수점 숫자 ( array.py docs) 걸릴 예정

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 

또 다른 잠재적 인 문제가 있었다 : 나는 PyCharm 경고에도 불구하고 위의 코드를 실행하면 6,

, 나는 나는 그것이 ndarray 유형을 받아 들일 수 있다는 것을 말해 또 다른 방법은 가정 다음과 같은 오류를 얻을 wordcloud는 Python 2로 작성되었지만 Python 3.4를 사용하고 있는데, 일부 코드는 사용할 수 없게 될 수도 있습니다. 이 메서드는 어떤 형식으로 전달해야합니까? 테스트 코드 에서

답변

0

J 헤론에 감사 셀바을한다고 생각

괄호 나는 이것으로 끝났다 object-- 목록 :

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

Word cloud generated

여전히 내 IDE에서 오해의 소지가있는 오류로 나타 났지만 예상했던대로 작동했습니다.

0

은 ... # <= 어떤이에 가야하는 것은 내가 대답이 대신 튜플을 사용하는 당신은 튜플에게 (("hi", float(6/(6+17)),("seven", float(17/(6+17))))

+0

답변 해 주셔서 감사합니다. 나는 그 행을 다음과 같이 변경했다 :'cloud.generate_from_frequencies (((hi), float (6/(6 + 17)), ("7", float (17/(6 + 17))))' TypeError : Float 객체는 subscriptable이 아니므로 float를 int로 변경하면 효과가있었습니다. – CCCodes