word-cloud - TypeError:图像数据无法转换为浮点数,我的代码哪里出错了?
问题描述
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"]
# LEARNER CODE START HERE
dict1=[]
d ={}
for words in file_contents.split():
if words.isalpha() and words.lower() not in uninteresting_words:
dict1.append(words.lower())
for words in dict1:
if words not in d:
d[words] =0
d[words]+=file_contents.split().count(words)
return d
#wordcloud
cloud = WordCloud(width=900,height=500, max_words=1628,relative_scaling=1,normalize_plurals=False)
cloud.generate_from_frequencies(calculate_frequencies)
return cloud.to_array()`enter code here`)
解决方案
# LEARNER CODE START HERE
words = file_contents.split(" ")
words_list = []
frequency={}
file_contents=file_contents.split()
for word in words:
for uninteresting_word in uninteresting_words:
if word is not uninteresting_word:
words_list.append(word)
for word in words_list:
if not word.isalpha():
word =''.join([letter for letter in word if word.isalpha()])
words_dict = {}
for word in words_list:
if word not in words_dict.keys():
words_dict[word] = words_list.count(word)
#wordcloud
cloud = wordcloud.WordCloud()
cloud.generate_from_frequencies(words_dict)
return cloud.to_array()
推荐阅读
- r - 使用一个数据帧中的值作为在另一个数据帧上估计的模型的参数
- angular - SyntaxError: Unexpected token { - 运行量角器黄瓜测试时出错
- youtube-livestreaming-api - 如何查找 YouTube Live Chat Ban ID 以将其删除
- sql - 我想为每个选择查询获取数据“order by”,然后使用“Union All”将它们组合起来
- android - BottomSheetDialog 片段共享元素转换实现?
- python - 从oracle表中读取大量数据并提取到数据框中的最佳方法是什么
- ruby-on-rails - 如何从“date_select”中获取日期并进行验证?
- c# - 使用 TokenLookUpEdit 的 SelectedItems 填充 WPF Datagrid
- hive - 如何在 Dataproc 集群中为 Hive 2.1.0 启用 LLAP?
- java - 如何接受动态 json 响应并向该对象添加一个键值并从 Spring Boot 控制器返回相同的 JSONObject