Change language

Find the k most common words from a dataset in Python

| | |

The solution to this problem is already presented as counter a class from this module.

Examples:

 Input: "John is the son of John second. Second son of John second is William second." Output: [(’second’, 4), (’ John’, 3), (’son’, 2), (’ is’, 2)]  Explanation:  1. The string will converted into list like this: [’John’,’ is’, ’the’,’ son’, ’of’,’ John’, ’second’,’ Second’, ’son’,’ of’, ’John’, ’second’,’ is’, ’William’,’ second’] 2. Now ’most_common (4)’ will return four most frequent words and its count in tuple. Input: "geeks for geeks is for geeks. By geeks and for the geeks." Output: [(’geeks’, 5), (’ for’, 3)]  Explanation:  most_common (2) will return two most frequent words and their count. 

Suitable:

 
  1. Import Counter class from collections module.
  2. Split the string into list using split (), it will return the lists of words. 
  3. Now pass the list to the instance of Counter class
  4. The function ’most-common ()’ inside Counter will return the list of most frequent words from list and its count.

Below is the implementation of the above approach in Python:

# Python program to find the k most common words
# from dataset

from collections import Counter

  

data_set = "Welcome to the world of Geeks "

" This portal has been created to provide well written well "  

"thought and well explained solutions for selected questions"

"If you like Geeks for Geeks and would like to contribute"

"here is your chance You can write article and mail your article"

" to contribute at pythonengineering org See your article appearing on "

" the Geeks for Geeks main page and help thousands of other Geeks. "

  
# split () returns a list of all words in the string

split_it = data_set.split ()

 
# Pass the split_it list to the instance class Counter.

Counter = Counter ( split_it)

 
# most_common () creates k frequently encountered
# input values ​​and their corresponding values.

most_occur = Counter.most_common ( 4 )

 

print (most_occur)

Output:

 [(’Geeks’, 5), (’ to’, 4), (’and’, 4), (’ article’, 3 )] 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically