articles

Home / DeveloperSection / Articles / Sentiment Analysis Using Python in Tableau with TabPy

Sentiment Analysis Using Python in Tableau with TabPy

Sandeep Aspari1588 22-Jul-2019

Tableau is one of the powerful and fantastic tool for data visualization. TabPy makes it even better and stronger by permitting you to run Python Scripts. As of now, we have many use cases for Python in Tableau. In this article, we are going to discuss how can one conduct sentiment analysis, this only done by Tableau Certification professionals.

sentiment analysis

Sentiment analysis is contextual mining, and analysis of data, which extracts and identifies the individual data from the source material and assisting the business to realize their brand's social sentiment. It is a kind of Natural Language Process(NLP), it conveys the emotion in the form of text. Commonly, most of the sentiment analysis is done by comparing each word in the text to a sentiment vocabulary that includes the words with a predefined score of sentiment.

For example, we will go through the VADER Dictionary that is specially developed and adapted to expressed social media sentiments. Moreover, it makes it quite advantageous for analysing some other non-formal written text.

What is TabPy

TabPy is a new API that allows the evolution of Python code within a Tableau workbook. If you are using TabPy with Tableau then we can premeditate the fields in Python, thereby you can leverage the power of a large number of machine learning libraries right from visualizations. Python in Tableau allows powerful scripts. For example, if we take a few lines in Python code, then you get different ways of sentiment scores for a product. This can be done with the integration of Python in Tableau.  

How to use TabPy

If you have a Tableau server up and want to run, then you connect to Tableau with a required data set. For example, we will be made a Billboard for Prime numbers between 1-100. After denoting the data can start through TabPy. We can make it simple by writing the regular python code to Tableau code then connect the two calculated fields with the help of below syntax.

SCRIPT_INT("

import nltk

from nltk.sentiment import SentimentIntensityAnalyzer

number = _arg1

scores = []

sid = SentimentIntensityAnalyzer()

for word in number:

    ss = sid.polarity_scores(prime number)

    scores.append(ss['compound'])

return scores

 ",

 ATTR([prime number]))

The output will be like…

Results are computed along table(across)

Script_real(“

Import nltk 

From nltk.sentiment import SentimentIntensityAnalyser

numbers=_arg1

scores= []

sid= SentimentIntensityAnalyser()

for a word in numbers

    ss=sid.polarity_scores(word)

    scores.append(ss[‘compound’])

Return scores

“,

ATTR([prime numbers])

While in the script function, two modifications are there In regular python code to Tableau code.

If you have specified variable in “return” then you get data from the function. The Arguments to be like _arg.

If you like to use more arguments in connecting of Python to Tableau, specify the arguments in the following order _arg1, _arg2, _arg3 etc.

In Tableau, these arguments are mapped in descending order.

    Numbers = _arg1

    Prime Number = _arg2

    return Scores ”,

    (

    ATTR(Numbers),

    ATTR(Prime Numbers)

    )

In the above example, we have only one argument called Numbers that we mapped to the Prime numbers in the field of Tableau. The reason why we keep Numbers in ATTR is we want to examine one number at a time. ATTR function stands for attributes, and it permits you to select only one number from the Numbers column.

Limitations of TabPy

While we are using TabPy, one thing keeps in mind that is a Tableau table calculation. The limitations are as follows:

If you want to run the script, then you should put it in view.

We could not use Calculation for creating base and values.

While you are work with a large volume of data, then you should be waiting for more time for every time of script run we need to change the view

While creating Tableau extract, Python generated data won't be extract

Dashboard Creation

We can use table calculation just the same as the creation of a new view for tableau table calculation. you can create a Dashboard in two ways

Dashboard creation using static data on Tableau Public

Dashboard creation using python code

The final view of the dashboard will look as follows

Sentiment Analysis Using Python in Tableau with TabPy

Conclusion:  

In conclusion, we see how to apply python code in Tableau, a new aspect which provides the opportunity to execute advanced analytics in our data. An example of a prime number calculation is included in the above post, and we can observe a clear benefit through this approach.



Sandeep Aspari, Science graduate and been into a technical content contribution for Mindmajix from past one year. He is passionate about learning new things about the technological advancements that help mankind for a better living. His articles focus more about latest trends happening around the world. To know more about Sandeep, you can visit his LinkedIn.

Leave Comment

Comments

Liked By