<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gerold Csendes on Medium]]></title>
        <description><![CDATA[Stories by Gerold Csendes on Medium]]></description>
        <link>https://medium.com/@gerold-csendes?source=rss-774e90ec9daa------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*uxjjGCxUidTL1RG3YjfDaQ.jpeg</url>
            <title>Stories by Gerold Csendes on Medium</title>
            <link>https://medium.com/@gerold-csendes?source=rss-774e90ec9daa------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 17:47:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gerold-csendes/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[15 common coding mistakes data scientist make in Python (and how to fix them)]]></title>
            <link>https://medium.com/data-science/15-common-coding-mistakes-data-scientist-make-in-python-and-how-to-fix-them-7760467498af?source=rss-774e90ec9daa------2</link>
            <guid isPermaLink="false">https://medium.com/p/7760467498af</guid>
            <category><![CDATA[getting-started]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[pandas]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Gerold Csendes]]></dc:creator>
            <pubDate>Tue, 01 Dec 2020 06:45:03 GMT</pubDate>
            <atom:updated>2020-12-15T21:30:40.888Z</atom:updated>
            <content:encoded><![CDATA[<h4><a href="https://towardsdatascience.com/tagged/getting-started">Getting Started</a></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*N7uFPEDEpbSTW-Vs" /><figcaption>Photo by <a href="https://unsplash.com/@goshua13?utm_source=medium&amp;utm_medium=referral">Joshua Aragon</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I gradually realized in my data science career that by applying software engineering best practices, you can deliver better quality projects. Better quality might be less bugs, reliable results and higher productivity in coding. This article is not intended to introduce you to these best practices in detail. Instead, it summarizes the most common mistakes I encountered (and made myself too) and offers methods, ideas and resources on how to best tackle them.</p><p>When reading my article, you might be tempted to think “Well, when I work on my own I don’t really need to follow this advice because I know my code”. Chances are that at least one other person will read your code: your future self. What you find self-evident at the moment will be total nonsense months later. Let’s make her life easier by avoiding the following mistakes.</p><h4>1. You don’t work in an isolated environment</h4><p>Okay this may not be a coding issue per se but I still consider isolated environments as an important feature for my code. Why would you consider using a dedicated environment for each of your project? You want to make your code reproducible: on your computer in the future, on your coworker’s machine and in production too. Ever faced the issue that your peer could not run your code? It is quite likely that she doesn’t have the same dependencies as you. (Or maybe after running hundreds of cells, you forgot to check whether your notebook breaks when using a clear kernel). If you have no idea what dependency management means then it is best to start with <a href="https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html">Anaconda Virtual Environment</a> or <a href="https://realpython.com/pipenv-guide/">Pipenv</a>. I personally use Anaconda and there is a great <a href="https://towardsdatascience.com/a-guide-to-conda-environments-bc6180fc533">tutorial </a>that you can access by clicking the link. If you want to go deeper then Docker is your go.</p><h4><strong>2. (Overuse of) Jupyter Notebooks</strong></h4><p>Notebooks are really good for educational purposes and to do some quick and dirty job but it fails to act as a good IDE. A good IDE is your real weapon when fighting data science tasks and can enhance your productivity immensely. There is lots of smart people shedding light on the shortcomings of notebooks. I consider Joel Grus’ <a href="https://www.youtube.com/watch?v=7jiPeIFXb6U">talk </a>to be the best and most hilarious.</p><p>Don’t get me wrong, notebooks are fine for experimentation and it is great that you can show your results to your peers with ease. However, they are really prone to errors and when it comes to doing longer-term, collaborative and deployable projects then you better look for a real IDE e.g. VScode, Pycharm, Spyder etc.. I do use notebooks every now and then but I made a mental model: I only use notebooks if the project doesn’t exceed one day.</p><h4>3. You don’t organize your code</h4><p>Data scientists have a track record of stockpiling all their project files in a single directory. It is a bad practice. Take a look at the figure below and imagine that you are to take over a project of your colleague. Which project structure would put you into existential crisis after hours of trying to figure out of what is going on? Of course, the structure on the left is your go. <a href="https://drivendata.github.io/cookiecutter-data-science/">Cookiecutter</a> is a brilliant initiative promoting standardized project structure for data science. Make sure to check this out.</p><figure><img alt="Good and bad project structure" src="https://cdn-images-1.medium.com/max/942/1*bWVPYC3KAd3zf7Ds4K_gmQ.png" /><figcaption>Good and bad project structure — Screenshot by Author</figcaption></figure><h4>4. Absolute instead of relative paths</h4><p>Ever faced a comment in code “pls fix your path”? Such a comment suggest for bad code design. Fixing this consist of 2 steps. 1) share the project structure with your peer (maybe the one suggested above) 2) set your IDE root/working directory to your project root that is usually the outmost directory in your project. The latter one is sometimes not that trivial to do but it is definitely worth the effort because your peer will be able to run your code without changing it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/458b2a54d92f910cded53baae30156c5/href">https://medium.com/media/458b2a54d92f910cded53baae30156c5/href</a></iframe><h4>5. Magic numbers</h4><p>Magic numbers are numerics without context in code. By using magic numbers, you may end up with really hard to track errors. The gist below clearly shows that by simply using an unassigned number in a multiplication, you lose context of why this is happening and if you later have to change this, it is rather stressful. It is thus desired to use named constants in capitals in Python. You don’t actually have to use capitalization, it is only a convention but it is a good idea to distinguish your “constants ” from your “regular” variables.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6e6ab77750cae6d21671887c4ae73db/href">https://medium.com/media/a6e6ab77750cae6d21671887c4ae73db/href</a></iframe><h4>6. Not dealing with warnings</h4><p>We have all been there when our code ran but generated weird warning messages. You are happy that you finally got your code running and received a meaningful output. So why dealing with the warning? Well, warnings themselves are not errors but they call attention to potential bugs or issues. They appear when there is something dubious in your code that though it ran successful but maybe not the way it was intended. The most common warnings I faced were Pandas’ SettingwithCopyWarning and DeprecationWarning. DataSchool explains in a <a href="https://www.youtube.com/watch?v=4R4WsDJ-KVc">neat way</a> how SettingwithCopyWarning is triggered. DeprecationWarning usually points out that Pandas deprecated some functionality and your code will break when using a later release. Of course, there are a handful of other warning types and my experience is that they arise when using a something in a way it was not designed. Understanding the source code of that functionality always helps. With that you can get rid of those warnings 99% of the time.</p><h4>7. You don’t use type annotation</h4><p>I need to admit, this is a practice that I picked up recently but I can already see its benefits. Type annotation (or type hint) is a method to assign types to your variables. You basically extend your code with hints which are really extensions to your code indicating the type of variables/parameters. This makes your code easier to read because the intentions of the coder are explicit. To demonstrate this, I have taken an example from Daniel Starner at <a href="https://dev.to/dstarner/using-pythons-type-annotations-4cfe">dev.to</a>. Without type hints <em>mystery_combine()</em> runs with both integer and string inputs and outputs either an integer or string. This might be ambiguous for a fellow developer. By using type annotation, you can be explicit with your intentions and make your peers life easier.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df0a64c0e1bb62392d03daa1696e1021/href">https://medium.com/media/df0a64c0e1bb62392d03daa1696e1021/href</a></iframe><p>Additionally, code with type annotation can be statically (without actually running the code) checked for bugs. The screenshot below shows that the first two arguments are not well specified. Statically checking your code is a nice and useful way for a pre-check before running it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pK2WgL-73DQ6JW1IjCzCpA.png" /><figcaption>Screenshot by Author</figcaption></figure><h4><strong>8. You don’t use (enough) list comprehensions</strong></h4><p>List comprehension is a really powerful feature of python. Many foor loops may be substituted with list comprehension that is more readable, pythonic and also faster. Below you can see an example code which intends to read csv file in a directory. You might say that using a for loop is not a sin in this case but try checking only for csv files (there may be other formatted files like jsons). You can sense that adding such a feature is easy to maintain when using list comprehension.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c95fc1765c7e6b33fc21a7adf813bdb/href">https://medium.com/media/4c95fc1765c7e6b33fc21a7adf813bdb/href</a></iframe><h4>9. Your pandas code is not readable</h4><p>Method chaining is a great feature in pandas but your code can get unreadable if you express everything in a single line. There is a trick that enables you to break the expression up. If you put your expression into parentheses then you are able to use a single line for each component of the expression. Isn’t that a lot cleaner?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/972a43e8c41111343e1fe06a975b301e/href">https://medium.com/media/972a43e8c41111343e1fe06a975b301e/href</a></iframe><h4>10. You are afraid to use dates</h4><p>Dates can be intimidating in Python. The syntax is weird and it is hard to wrap your head around it. A common mistake I see is that people handle dates like numerics. You can always do a workaround and hack code together but it is really error prone, hard to read and maintain. See an example below where the task is to list all months between two dates in a %Y%m format. You can see that your code becomes much more readable and maintainable if you follow the datetime implementation. In my case, dealing with dates still requires lots of googling but I have learnt not to be intimidated if I don’t find a solution on the first try.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef670af6c587c2d96253df2681fb7317/href">https://medium.com/media/ef670af6c587c2d96253df2681fb7317/href</a></iframe><h4>11. You don’t use good variable names</h4><p>Naming your dataframes df and i, j, k for your loop indexes are just non-descriptive and makes your code less readable. An effort for keeping your variable names too short is a guarantee for confusing the coders on your project. Don’t be afraid to use long(er) names for your variables. There is nothing stopping you from using more ‘_’-s. Make sure to check out Will Koehrsen’s great <a href="https://towardsdatascience.com/data-scientists-your-variable-names-are-awful-heres-how-to-fix-them-89053d2855be">article</a> on this topic to get further insights.</p><h4><strong>12. You don’t modularize your code</strong></h4><p>Modularization means breaking up long and complex code into simpler modules that perform smaller, specific tasks. Don’t just create a long script for your project. Defining your classes or functions at the top of your code is bad practice. It is hard to maintain and read. Instead create modules (packages) and structure them based on their functionality. Again, you can visit realpython.org <a href="https://realpython.com/courses/python-modules-packages/">Python Modules and Packages</a> tutorial for an in-depth introduction.</p><h4>13. You don’t follow PEP conventions</h4><p>When I started out with programming in Python, I ended up writing ugly, unreadable code and started making my own design rules on how to make my code look better. It took quite a lot of time to come up with them and I did break these rules often. Then, I found out about <a href="https://www.python.org/dev/peps/">PEP</a> which is the official styling guideline for Python. I am really fond of PEP because it makes collaboration easier by enabling you to standardize the appearance of your code. By the way, I do ignore some PEP rules but I would say I use them in 90% of my code.</p><p>Any good Python IDE can be extended with a linter. The picture below demonstrates how a linter works in practice. They point out code quality issues and if it is still vague for you, you can check out the specific PEP index which is indicated in parantheses. If you want to see what linters are available out there then, as always, <a href="https://realpython.com/python-code-quality/#linters">realpythong.org</a> is a good source for python stuff.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uWKK2aI3LuF6bkhcR8louQ.png" /><figcaption>Screenshot by Author</figcaption></figure><h4>14. You don’t use a coding assistant</h4><p>You want to have big productivity gains in coding? Start using a coding assistant which helps by clever autocomplete, opening up documentation and giving suggestions to improve your code. I like using pylance which is a new tool developed by Microsoft and is available in VScode. Kite is an alternative which is also really nice and available in a number of editors.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgfycat.com%2Fifr%2Fbaggynicelemur&amp;display_name=Gfycat&amp;url=https%3A%2F%2Fgfycat.com%2Fbaggynicelemur&amp;image=https%3A%2F%2Fthumbs.gfycat.com%2FBaggyNiceLemur-size_restricted.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=gfycat" width="1920" height="1020" frameborder="0" scrolling="no"><a href="https://medium.com/media/0844560c2c13474afd027f77aef964ed/href">https://medium.com/media/0844560c2c13474afd027f77aef964ed/href</a></iframe><h4>15. You don’t hide secrets in your code</h4><p>Pushing secrets (passwords, keys) to public github repositories is a widespread security flaw. If you want to get a sense of the seriousness of this issue, check out this <a href="https://qz.com/674520/companies-are-sharing-their-secret-access-codes-on-github-and-they-may-not-even-know-it/">qz </a>article. There are bots crawling the internet waiting for you to make such a mistake. As far as I am concerned, security is a topic that is hardly ever part of any data science curriculum. So, you need to fill in the gap yourself. I suggest you to first start with using OS environment variables. This <a href="https://dev.to/biplov/handling-passwords-and-secret-keys-using-environment-variables-2ei0">dev.to</a> article might be a good start.</p><p>Thanks for pulling through this long blogpost. You deserve a 🍪! Did I miss out any other common mistake? I would love to hear your feedback.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7760467498af" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/15-common-coding-mistakes-data-scientist-make-in-python-and-how-to-fix-them-7760467498af">15 common coding mistakes data scientist make in Python (and how to fix them)</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to detect tweet sentiment with Amazon Comprehend in R]]></title>
            <link>https://medium.com/analytics-vidhya/how-to-detect-tweet-sentiment-with-amazon-comprehend-in-r-526a3d496e0d?source=rss-774e90ec9daa------2</link>
            <guid isPermaLink="false">https://medium.com/p/526a3d496e0d</guid>
            <category><![CDATA[social-media]]></category>
            <category><![CDATA[r]]></category>
            <category><![CDATA[sentiment-analysis]]></category>
            <category><![CDATA[amazon]]></category>
            <category><![CDATA[nlp]]></category>
            <dc:creator><![CDATA[Gerold Csendes]]></dc:creator>
            <pubDate>Sat, 16 Nov 2019 09:06:11 GMT</pubDate>
            <atom:updated>2019-11-26T08:16:00.212Z</atom:updated>
            <content:encoded><![CDATA[<h3>Table of Contents</h3><ol><li><em>Introduction</em></li><li><em>Scraping twitter and using Amazon comprehend in R</em></li><li><em>Sentiment Analysis</em></li><li><em>Conclusions</em></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gIf7j8i3GZ4F6e6q" /><figcaption>Photo by <a href="https://unsplash.com/@lidyanada?utm_source=medium&amp;utm_medium=referral">Lidya Nada</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Introduction</h3><p>This article was written as part of a university project and since it is my first one on Medium, I would be flattered for all kinds of feedback. I also want to highlight the fact that what you are about to read is rather technical and the emphasis will not be on the analysis itself but rather on how you set up a framework to do a sentiment analysis easily. With all that being said, let’s come up with a use-case for which this framework can be applied.</p><p>Imagine that you are an owner of a renown football club in England and you want to find out what the fans think about the club manager. How can you do this on your <strong>own </strong>in a time- and cost-effective way? With some basic coding skills and an Amazon Web Services account with as little as 5$, you can get a nice overview about the trends of fan’s satisfaction towards the manager’s performance.</p><p>Without further ado, let’s dive into the tweet sentiment analyis! My code for this article can be found here: <a href="https://github.com/geroldcsendes/tweet_sentiment">github</a>.</p><h3><em>Scraping Twitter and using Amazon Comprehend in R</em></h3><p>In order to use Twitter’s API to scrape tweets, you need to have a Twitter account and to create a Twitter application <a href="https://developer.twitter.com/en/apps">here</a>. I took me about 40 minutes setting up the application because there are a bunch of forms to fill in but I ended up receiving the permission to my application in only a matter of minutes. By the way, this <a href="https://www.earthdatascience.org/courses/earth-analytics/get-data-using-apis/use-twitter-api-r/">article </a>provides a nice summary on how to do this.</p><p>Once you are done with this, you should see on your developer dashboard something similar (I just blurred my keys and tokens for security :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Yimq7XhD3oY7vGX_Pau29Q.png" /><figcaption>Twitter Developer Dashboard</figcaption></figure><p>One of the R packages you can use to use Twitter’s API is <strong>rtweet</strong>. You can create token in rtweet using your credentials with a code like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/999783fc3302e40d421ffcddf2438777/href">https://medium.com/media/999783fc3302e40d421ffcddf2438777/href</a></iframe><p>Okay, so now we can communicate with Twitter in R. The next step is set up to access Amazon Comprehend. You obviously need an AWS account for that, so if you don’t have it, it is time create one. It should not take more than 10 minutes. Once you have an account, an access key has to be generated on Amazon’s Identity and Access Management (IAM) Console for secure communication with the API. On the dashboard, you will probably see something very similar. Create an access key and save it to your R project’s location.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QupROHw_ICdiis9Jga4eBA.png" /><figcaption>Amazon IAM dashboard</figcaption></figure><p>R’s package to use Comprehend is <strong>aws.comprehend</strong>. To be able to use Comprehend from R, we need to provide the access keys to aws.comprehend. If you want to find out how it works in more detail, check <a href="https://github.com/cloudyr/aws.signature/">this </a>github page out.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd97faa732feeab4be3a7acded2a8165/href">https://medium.com/media/dd97faa732feeab4be3a7acded2a8165/href</a></iframe><p>Okay, we have set up everything that is needed to carry out that sentiment analysis on football tweets. Let’s get to work!</p><h3>Sentiment Analysis</h3><p>For my sentiment analysis, I am only going to analyze tweets in English, though Amazon Comprehend supported 5 other languages at the time of writing this article. I wanted to further restrict the tweets by geographical location on a country level because I think fans have more influence on clubs who live in the motherland. It turned out that although the Twitter API provides some geographical data, these are poorly filled. Thus, I decided to keep all tweets in English.</p><p>You should note that Twitter poses some restriction on requesting tweets. Currently, the supported tweet history is 7 days, so one should plan ahead if he want so analyze the tweets for a specific future event or act quickly when something unexpected happens. Twitter also limits the requests you can make but I haven’t found any information about this. What I know is that I was able to make 36.000 requests within 30 minutes, so twitter doesn’ really seem very restricting.</p><p>As a subject to my analysis, I chose Pep Guardiola, manager for Manchester City. City lost 3–1 against Liverpool securing Liverpool an 8 point advance in the Premier League. I wanted to find out how the football fans reacted to this loss. This game was 5 days before doing this analysis, so I was right on time.</p><h4>Getting and cleaning tweets</h4><p>To fetch tweets from the Twitter API, simply use rtweet’s<em> search_tweets()</em> function. You will see that twitter provides the data in a dataframe with 90 columns and you can also get additional ones. Check out rtweets <a href="https://rtweet.info/index.html">documentation </a>on further possibilities.</p><p>The text column contains the tweets from the users. Tweets usually contain URLs and emojies and I that by removing them, we can help Amazon Comprehend to make a better guess about a tweet’s sentiment. By the way, emotions may be easier to detect just by analyzing emojies than by text. I found no information indicating whether Comprehend could use emojis for its sentiment detection. It may be really exciting to conduct a sentiment analysis only on emojis and compare its results against Comprehend.</p><p>So let’s get rid of those emojis for our current analysis. I concluded that R’s base enc2native function works the best to convert tweets with emojies into UTF coding standards without losing any other information. I included this code snippet with the most relevant steps but you can find the full code on my github.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/758f695f95a72d7e6ee0daac044efe53/href">https://medium.com/media/758f695f95a72d7e6ee0daac044efe53/href</a></iframe><p>Actually rtweet has a function called <em>plain_tweets() </em>which should apply the same cleaning techniques I did but I found it worse performing than my methods.</p><p>We are now done with the data cleaning but before jumping into sending sentiment requests to Amazon, it would be nice to get a cost estimate. Amazon Comprehend is reasonably priced but we don’t want to end up paying more than our budget due to some error. For this purpose, I created a function which estimates the job’s cost.</p><p>Amazon charges 0.0001 dollars per 100 characters and a minimum of 0.0003 dollars per request (meaning that you pay the same for a 100 and a 300 character tweet). My dataset contains 17,997 observations and estimated 5.2 dollars for the whole dataset.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20b16b8751af8a790d63b97d32320f04/href">https://medium.com/media/20b16b8751af8a790d63b97d32320f04/href</a></iframe><p>It is worth to mention that though Amazon Comprehend doesn’t seem to be an expensive solution, it is still not optimally priced for tweet sentiment detection. Tweets -when removing emojis and URLs- consist of a lot less characters than 300. To illustrate this, I included a plot about character distribution for my dataset.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e4_NLftvHJA0vaIrZ4a3uA.png" /></figure><p>The average tweet length is 157 characters and only 0.3% of the tweets have more than 300 characters but about 24% are longer than 250 characters. We might worry whether this number of characters is enough to express feelings. Just to give you an idea how many is 157 characters, I added a tweet this long:</p><p><a href="http://twitter.com/VeskoTrajkovic">“@</a>&lt;blurred_user&gt; is a very good manager, no doubt, but he always had best team as his disposal. Klopp has made average teams become excellent teams.”</p><p>We don’t necessarily need longer tweets to detect sentiments it may be even that shorter ones like “Booo Guardiola” or “GTF Guardiola” tend to be either negative or positive and longer ones neutral. Anyways, I am no expert in NLP, just wanted to provide some context to my analysis.</p><blockquote>So while it may be not financially worth to request sentiments for shorter texts from Comprehend, it should still work.</blockquote><h4>Detecting sentiment</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39d07ea4adeb3dad9faf37a8050f6473/href">https://medium.com/media/39d07ea4adeb3dad9faf37a8050f6473/href</a></iframe><p>When using Amazon Comprehend, we get back as a result a probability score for the 4 sentiments and the most likely sentiment will be flagged in the ‘Sentiment’ column. With that in mind, we can define a function which extracts the tweet column, sends it to Amazon and merges the returned dataframe to the ‘base’ horizontally.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2dc1351a9d97eaa103007f8fa345e661/href">https://medium.com/media/2dc1351a9d97eaa103007f8fa345e661/href</a></iframe><p>And that’s it! We have now a dataframe with tweets and their sentiments. It is time to see what people think about Pep Guardiola after losing the big game.</p><p>So, first I want to take a look at the date range of my dataset. It turns out that the tweets fall between 12.11.2019 and 15.11.2019 (the time of writing this article). It is quite unlucky because I was hoping for tweets on the day of the game (10.11), since I was expecting the fans to be more harsh in the heat of the moment and then gradually become more understanding. I only came to realize after conducting my analysis, that you can pass in an ‘until’ argument for the Twitter API which returns tweets before a given day. You can find more information about this <a href="https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets">here</a>.</p><p>Anyway, as I already expressed, this article aims at only showing a framework and doesn’t necessarily go for an error-free analysis. So keeping that in mind, let’s take a look at the tweet sentiment relative frequencies. As you can see, the neutral sentiments dominate with 57% followed by the positives with about 23% and negatives with 18%. Tweets with mixed sentiments can be ignored.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a2R1P41pwcLjl96dkFrEhw.png" /></figure><p>As mentioned before, it may make sense to see how tweet sentiments evolve in time after the game. If we take a look at the chart below, we can see the pattern that neutral sentiments decrease which is caused by the increase in positive sentiments while negatives remain quite unchanged. So it may be as I suspected that fans become more and more understanding after losing a big game and this is why the positive tweets increase relative to the negatives.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*maAkB-xO-vpL6wulmqfVlg.png" /></figure><h3>Conclusions</h3><p>In this article, I demonstrated how Amazon’s Comprehend can be used in R to detect tweet sentiments. It is very easy to set up this framework and you can analyze what people think about a specific event by applying the same method. I consider this as a time- and cost-efficient way of doing a quick sentiment analysis. The questions remains whether Amazon Comprehend works well enough for our purposes. In the near future, I am planning to compare Amazon Comprehend sentiment predictions to emojis and see how these relate to each other and then preferrably write an article about it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=526a3d496e0d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/how-to-detect-tweet-sentiment-with-amazon-comprehend-in-r-526a3d496e0d">How to detect tweet sentiment with Amazon Comprehend in R</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>