0
0
Data Analysis Pythondata~20 mins

Word frequency analysis in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Word Frequency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of word frequency count using Counter
What is the output of this Python code that counts word frequencies in a list?
Data Analysis Python
from collections import Counter
words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
freq = Counter(words)
print(freq)
ACounter({'apple': 3, 'banana': 2, 'orange': 1})
B{'apple': 3, 'banana': 2, 'orange': 1}
CCounter({'apple': 2, 'banana': 2, 'orange': 2})
D{'apple': 2, 'banana': 2, 'orange': 1}
Attempts:
2 left
💡 Hint
Remember that Counter counts how many times each word appears.
data_output
intermediate
1:30remaining
Number of unique words in a text
Given this list of words, how many unique words are there?
Data Analysis Python
words = ['data', 'science', 'data', 'analysis', 'science', 'python', 'python', 'python']
unique_words = len(set(words))
print(unique_words)
A8
B3
C5
D4
Attempts:
2 left
💡 Hint
Use set to find unique items in a list.
visualization
advanced
2:30remaining
Bar chart of word frequencies
Which option shows the correct bar chart for the word frequencies in this data?
Data Analysis Python
import matplotlib.pyplot as plt
from collections import Counter
words = ['cat', 'dog', 'cat', 'bird', 'dog', 'dog']
counter = Counter(words)
plt.bar(counter.keys(), counter.values())
plt.show()
ABar chart with bars: cat=3, dog=2, bird=1
BBar chart with bars: cat=2, dog=3, bird=1
CBar chart with bars: cat=1, dog=2, bird=3
DBar chart with bars: cat=2, dog=2, bird=2
Attempts:
2 left
💡 Hint
Count how many times each word appears before plotting.
🔧 Debug
advanced
2:00remaining
Identify the error in word frequency code
What error does this code raise when counting word frequencies?
Data Analysis Python
words = ['red', 'blue', 'red', 'green']
freq = {}
for word in words:
    freq[word] += 1
print(freq)
ASyntaxError
BTypeError
CKeyError
DNo error, prints correct counts
Attempts:
2 left
💡 Hint
Check what happens when you try to add to a dictionary key that does not exist yet.
🚀 Application
expert
3:00remaining
Find the most frequent word in a large text
Given a large text split into words, which code snippet correctly finds the most frequent word?
Data Analysis Python
text = 'data science is fun and data science is powerful and data is everywhere'
words = text.split()
A
from collections import Counter
counter = Counter(words)
most_common = counter.most_common(1)[0][0]
print(most_common)
B
freq = {}
for w in words:
    freq[w] = freq.get(w, 0) + 1
most_common = max(freq)
print(most_common)
C
from collections import Counter
counter = Counter(words)
most_common = max(counter)
print(most_common)
D
freq = {}
for w in words:
    freq[w] = freq.get(w, 0) + 1
most_common = max(freq.values())
print(most_common)
Attempts:
2 left
💡 Hint
Use Counter's most_common method to get the word with highest count.