[英]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.