【问题标题】:Debugging a code to print the wordcount: type error: Image data cannot be converted to float调试代码以打印字数:typeerror:图像数据无法转换为浮点数
【发布时间】:2020-10-18 01:49:31
【问题描述】:

在这里,我想打印一个词云。我有一个不同的解决方案,但我想知道我的错误是什么。有人可以检查一下吗?我收到类型错误:图像数据无法转换为浮点数。

def calculate_frequencies(file_contents):
    # Here is a list of punctuations and uninteresting words you can use to process your text
    punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
    uninteresting_words = ["the", "a", "to", "if", "is", "it", "of", "and", "or", "an", "as", "i", "me", "my", \
    "we", "our", "ours", "you", "your", "yours", "he", "she", "him", "his", "her", "hers", "its", "they", "them", \
    "their", "what", "which", "who", "whom", "this", "that", "am", "are", "was", "were", "be", "been", "being", \
    "have", "has", "had", "do", "does", "did", "but", "at", "by", "with", "from", "here", "when", "where", "how", \
    "all", "any", "both", "each", "few", "more", "some", "such", "no", "nor", "too", "very", "can", "will", "just"]
    
    new_list= ""
    new_list2= ""
    my_dict= {}

    
    file_contents1=file_contents.split()
    for letters in file_contents:
        if letters.isalpha():
            letters+=new_list2

    
    
    for letters in new_list2:
        if letters not in uninteresting_words or  letters not in punctuations:
            letters+=new_list
    
    file_content = new_list.lower()
    for word in file_content:
        if word in my_dict:
            my_dict[word] += 1
        else:
            my_dict[word] = 1
    return my_dict
    print(new_list2)

    
            
    #wordcloud
    cloud = wordcloud.WordCloud()
    cloud.generate_from_frequencies(my_dict)
    return cloud.to_array()

myimage = calculate_frequencies(file_contents)
plt.imshow(myimage, interpolation = 'nearest')
plt.axis('off')
plt.show()

【问题讨论】:

    标签: python dictionary debugging word-count


    【解决方案1】:

    首先,这个 Python3 wordcloud 是第一个 Python 证书速成课程的最后一次评估,它是 Google IT 自动化的一部分,在 coursera 上有 Python 专业证书,我通过了它。其次,如果您真的开发了不同的代码,那么您应该很容易开发另一个代码。但是为了相互帮助,错误是:您没有正确地从 file_contents 生成字典。使用任何随机文本的 word_count_frequency 函数生成字典。专注于那部分,其余的很容易实现。祝你好运。

    【讨论】:

    • excise 是一种很常见的python,在网上很容易找到。我检查了,会再试一次。
    • 没问题。您只需要练习使用 word_count_frequency 函数生成字典。它是您正在搜索的内容的骨架。
    猜你喜欢
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 2023-03-30
    • 2017-11-12
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    相关资源
    最近更新 更多