繁体   English   中英

TypeError:图像数据无法在 wordcloud 项目上转换为浮点数

[英]TypeError: Image data cannot be converted to float on wordcloud project

所以我们有这个代码实践来创建一个词云,看起来很简单,因为它只是创建一个字典......但不知何故,他们提供的执行词云的代码给了我一个错误

"TypeError: Image data cannot be converted to float". 

我检查了我自己的计算机上使用的相同文本文件,只是为了生成字典,并证实这些值确实是整数,所以我被卡住了。 在下面发布此处提供的所有代码:

!pip install wordcloud
!pip install fileupload
!pip install ipywidgets
!jupyter nbextension install --py --user fileupload
!jupyter nbextension enable --py fileupload

import wordcloud
import numpy as np
from matplotlib import pyplot as plt
from IPython.display import display
import fileupload
import io
import sys

这是他们给我们的上传器小部件

def _upload():

    _upload_widget = fileupload.FileUploadWidget()

    def _cb(change):
        global file_contents
        decoded = io.StringIO(change['owner'].data.decode('utf-8'))
        filename = change['owner'].filename
        print('Uploaded `{}` ({:.2f} kB)'.format(
            filename, len(decoded.read()) / 2 **10))
         file_contents = decoded.getvalue()

    _upload_widget.observe(_cb, names='data')
    display(_upload_widget)

_upload()

我写的代码来了

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"]

# My Code
    frequencies = {}
    floatdic = {}
    case = file_contents.split()
    for word in case:
        words = word.strip(punctuations)
        lower = words.lower()
        if lower.isalpha() == False or lower in uninteresting_words:
            continue
        elif lower not in frequencies:
            frequencies[lower]= 0
        frequencies[lower]+=1
    return frequencies
    #Their Code Provided for wordcloud
    cloud = wordcloud.WordCloud()
    cloud.generate_from_frequencies(frequencies)
    cloud.to_file("myfile.jpg")
    return cloud.to_array()

然后他们给出这段代码来执行词云

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

它在“plt.imshow”行上崩溃。 如果有帮助,他们会在 Jupyter 笔记本上运行这些代码

我能够解决这个问题! 我意识到我有

return frequencies

在我用来测试字典的代码末尾,忘记为词云 function 删除它。 一旦完成,词云就完美地工作了!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM