Finding Patterns In Data Using NMF | by Himanshu Sharma | Towards Data Science

NLP-Natural Language Processing is one of the hottest topics in the field of Artificial Intelligence. It helps in building applications like chatbots, voice assistants, sentiment analysis, recommendation engines, etc. It is a budding field where most related companies are investing and researching to create next-gen voice assistants.
One of the most important algorithms of NLP is Topic modeling that helps in outlining the quantitative trend in text datasets and works on finding out the summary features of the dataset. In other words, it is a kind of feature engineering where we are finding out the most important feature in the dataset.
NMF is an unsupervised topic modeling technique where no training data or data labels are provided. It factorizes high dimension data into lower dimension representation.
Ecco is an open-source Python library that provides multiple features for exploring and explaining NLP models using interactive visualizations.
In this article, we will use Ecco to perform NMF and create an interactive visualization for it.
Let’s get started...
We will start by installing an Ecco using pip. The command given below will do that.
pip install ecco
In this step, we will import the required libraries and functions to create an NMF visualization. We will also load the pre-trained Bert Model from eeco.
import eccolm = ecco.from_pretrained('bert-base-uncased', activations=True)
In this step, we will tokenize the data that we will be using for NMF Visualization. You can use any text that you want, I am taking an article on Sachin Tendulkar.
text = ''' Sachin Tendulkar has been the most complete batsman of his time, the most prolific runmaker of all time, and arguably the biggest cricket icon the game has ever known. His batting was based on the purest principles: perfect balance, economy of movement, precision in stroke-making, and that intangible quality given only to geniuses - anticipation. If he didn't have a signature stroke - the upright, back-foot punch comes close - it's because he was equally proficient at each of the full range of orthodox shots (and plenty of improvised ones as well) and can pull them out at will.There were no apparent weaknesses in Tendulkar's game. He could score all around the wicket, off both front foot and back, could tune his technique to suit every condition, temper his game to suit every situation, and made runs in all parts of the world in all conditions.Some of his finest performances came against Australia, the overwhelmingly dominant team of his era. His century as a 19-year-old on a lightning-fast pitch at the WACA is considered one of the best innings ever to have been played in Australia. A few years later he received the ultimate compliment from the ultimate batsman: Don Bradman confided to his wife that Tendulkar reminded him of himself.Blessed with the keenest of cricket minds, and armed with a loathing for losing, Tendulkar set about doing what it took to become one of the best batsmen in the world. His greatness was established early: he was only 16 when he made his Test debut. He was hit on the mouth by Waqar Younis but continued to bat, in a blood-soaked shirt. His first Test hundred, a match-saving one at Old Trafford, came when he was 17, and he had 16 Test hundreds before he turned 25. In 2000 he became the first batsman to have scored 50 international hundreds, in 2008 he passed Brian Lara as the leading Test run-scorer, and in the years after, he went past 13,000 Test runs 30,000 international runs, and 50 Test hundreds. '''inputs = lm.tokenizer([text], return_tensors="pt")output = lm(inputs)
This is the final step where we will create the visualization for NMF, we will use two types of factorization i.e. Factorization of all the layers and Factorization of the First Layer.
#All layersnmf_1 = output.run_nmf(n_components=10)nmf_1.explore()
#First Layernmf_2 = output.run_nmf(n_components=8, from_layer=0, to_layer=1)nmf_2.explore()
Here we can clearly visualize the NMF visualization created using just a single line of code. These visualizations are interactive and highly explainable. We can clearly analyze the different topics and weights of NMF in the visualization.
Go ahead try this with different datasets and create beautiful visualization. In case you find any difficulty please let me know in the response section.
This article is in collaboration with Piyush Ingale.
Thanks for reading! If you want to get in touch with me, feel free to reach me on hmix13@gmail.com or my LinkedIn Profile. You can view my Github profile for different data science projects and packages tutorials. Also, feel free to explore my profile and read different articles I have written related to Data Science.