Python Forum
Image data cannot be converted to float
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Image data cannot be converted to float
#1
Write a function in the cell below that iterates through the words in file_contents, removes punctuation, and counts the frequency of each word. Oh, and be sure to make it ignore word case, words that do not contain all alphabets and boring words like "and" or "the". Then use it in the generate_from_frequencies function to generate your very own word cloud!

my code:

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()



myimage = calculate_frequencies(file_contents)
plt.imshow(np.real(myimage), interpolation = 'nearest')
plt.axis('off')
plt.show()
expected a wordcloud
Reply
#2
Post full traceback you get in error tags. Also show your full code - at the moment we are missing all imports

Note that I think there is problem with indentation on lines 15-22. Do you really want to return d and at that level?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: only size-1 arrays can be converted to Python scalars RainbowTrish1 1 4,475 Oct-29-2020, 06:16 PM
Last Post: DeaD_EyE
  parameter in function being involuntarily converted to str?? juliabrushett 8 5,993 Jul-03-2018, 04:23 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020