<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rohan Sahai</title>
    <description>The latest articles on DEV Community by Rohan Sahai (@rohanjamin).</description>
    <link>https://dev.to/rohanjamin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2503%2F2532bb95b495dea6a56a4d51e3977ab9.jpeg</url>
      <title>DEV Community: Rohan Sahai</title>
      <link>https://dev.to/rohanjamin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rohanjamin"/>
    <language>en</language>
    <item>
      <title>Classifying Tweets with Amazon ML</title>
      <dc:creator>Rohan Sahai</dc:creator>
      <pubDate>Mon, 30 Jan 2017 01:32:37 +0000</pubDate>
      <link>https://dev.to/rohanjamin/classifying-tweets-with-amazon-ml</link>
      <guid>https://dev.to/rohanjamin/classifying-tweets-with-amazon-ml</guid>
      <description>&lt;h2&gt;
  
  
  Preface
&lt;/h2&gt;

&lt;p&gt;I recently watched an awesome &lt;a href="https://www.youtube.com/watch?v=GRRX9amYsyM"&gt;video overview/demo&lt;/a&gt; of one of AWSâ€™s newer servicesâ€Šâ€”â€ŠAmazon ML. If you have ~50 minutes on hand to watch the above video and follow along I highly suggest it, if not read the rest of my blog post for a much briefer summary.&lt;/p&gt;

&lt;p&gt;So what is Amazon ML?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Amazon Machine Learning is a service that makes it easy for developers of all skill levels to use machine learning technology. Amazon Machine Learning provides visualization tools and wizards that guide you through the process of creating machine learning (ML) models without having to learn complex ML algorithms and technology. Once your models are ready, Amazon Machine Learning makes it easy to obtain predictions for your application using simple APIs, without having to implement custom prediction generation code, or manage any infrastructure.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, itâ€™s &lt;strong&gt;machine learning as a service&lt;/strong&gt;. For those familiar with machine learning, Amazonâ€™s service doesnâ€™t yet provide any sort of deep learning models, only regression and classification models. Nonetheless, you can do some fairly powerful stuff with these &lt;em&gt;shallow learning&lt;/em&gt; models.&lt;/p&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;In the video linked above the speaker goes through an example in which he builds a model to &lt;strong&gt;automatically classify tweets directed at AWS customer support as actionable or not actionable&lt;/strong&gt;. You can probably imagine a few different use cases for a model like this, one being a great way to save your customer support agents time filtering through garbage social media data to find actual customer issues/problems. Iâ€™ll go through the same example with my own companyâ€™s (&lt;a href="https://www.weebly.com"&gt;Weebly&lt;/a&gt;) twitter data below.&lt;/p&gt;

&lt;p&gt;The entire process of creating our ML model can be boiled down to 4 simple steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Collect our Data&lt;/li&gt;
&lt;li&gt;Classify Our Data&lt;/li&gt;
&lt;li&gt;Feed our classified data to Amazonâ€™s ML engine&lt;/li&gt;
&lt;li&gt;Test it out!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Collecting ourÂ Data
&lt;/h2&gt;

&lt;p&gt;First we need some actual data to work with. Since weâ€™ll be using twitter data, we can take advantage of the Twitter API to get a large subset of tweets that meet our requirement (i.e. tweets directed @weebly). This means youâ€™ll have to register an &lt;a href="https://apps.twitter.com/"&gt;developer application&lt;/a&gt; on twitter to get your relevant API keys and tokens. Once you do that you can start fetching the data you need from twitter. Below is a little ruby script to get all the tweets we care about and save them to a CSV.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Twitter client gem - https://github.com/sferik/twitter
client = Twitter::REST::Client.new do |config|
  config.consumer_key        = "API_KEY"
  config.consumer_secret     = "SECRET"
  config.access_token        = "ACCESS_TOKEN"
  config.access_token_secret = "ACCESS_TOKEN_SECRET"
end

CSV.open("weebly_tweets", "wb") do |csv|
  csv &amp;lt;&amp;lt; ["username", "tweet"] # first row
  client.search("@weebly", lang: "en").collect do |tweet|
    csv &amp;lt;&amp;lt; [tweet.user.screen_name, tweet.text]
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Awesome, now we have a CSV with thousands of rows of tweets. Time for the classification portion.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Classification
&lt;/h2&gt;

&lt;p&gt;Now that we have all our tweets in a CSV we need to classify them as actionable or not. One way we could do this is to create a third column in our CSV and manually go through all the tweets and classify them ourselvesâ€¦ that sounds pretty awful though. Fortunately Amazon has another pretty handy service called &lt;a href="https://www.mturk.com/mturk/welcome"&gt;Amazon Mechanical Turk&lt;/a&gt;. Using this service we can hire people to classify these tweets for us at a price we determine (often as low as a couple of cents per tweet). You can choose how many different people should be hired to classify your tweets, at what cost, and various other configurations. Below are some screenshots from the Mechanical Turk interface:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m22S_JRi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AUYXyOinMjlnafVBlUTEvhQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m22S_JRi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AUYXyOinMjlnafVBlUTEvhQ.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;configure HIT (Human Intelligence Task)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cDXKAzBj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2A1V_XntCYc8vVEUhue2ku8Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cDXKAzBj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2A1V_XntCYc8vVEUhue2ku8Q.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;HIT instructions&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One important thing thatâ€™s easy to forget when creating machine learning models is making sure you have a good &lt;strong&gt;clean data set&lt;/strong&gt;. Iâ€™m fairly new to creating ML models and in pretty much every instance Iâ€™ve worked on a model I end up constantly going back to the original data set and re running the model because of some data cleaning I did after the fact. Some early filtering on your data could end up in big wins down the line with your models performance, and save you lots of time. One exampleâ€Šâ€”â€Šthe first time I fetched all this twitter data I noticed lots of tweets in Chinese. Unless our customer support agents speak Chinese this data will only pollute our model. Another example is that I noticed lots of tweets that were automatically generated by Weeblyâ€™s Blog Platformâ€™s â€œshareâ€ featureâ€Šâ€”â€Šthese could be easily filtered out of data set by a simple check for certain text (â€œblog post shared via @weeblyâ€). Our model could easily learn to classify these appropriately but we could save time calculating our model by filtering out these tweets beforehand, and more importantly we could save money if we are paying a third party classification service to classify our data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating OurÂ Model
&lt;/h2&gt;

&lt;p&gt;Once weâ€™ve got all our data classified and an additional column in our csv with the labeled data, we can begin to actually create our model. As of the date Iâ€™m writing this, Amazon ML models need to pull data from either S3 or RedShift. I created a new s3 bucket quickly and placed by &lt;code&gt;weebly_tweets.csv&lt;/code&gt; there.&lt;/p&gt;

&lt;p&gt;From the Amazon ML &lt;a href="https://console.aws.amazon.com/machinelearning"&gt;console&lt;/a&gt; you can select â€œCreate new Model and Datasourceâ€. From there we can follow the fairly straightforward steps in creating our model. Noteâ€Šâ€”â€Šwe can do all of the below via Amazonâ€™s API, but for clarity and easier visualization Iâ€™ll use the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CMpB2B7w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2A17VJJYQv4RJTC8DXIwG3tw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CMpB2B7w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2A17VJJYQv4RJTC8DXIwG3tw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this first step we simply enter the location of our data and name this new Datasource. The steps after this will just have you confirm your columns and their data types, as well as identify the target column (the output). After creating our Datasource weâ€™ll finally get to the point of actually being able to create our model.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9DBe0xYK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AZj4iytVUbcUw_e4Y_4ASaw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9DBe0xYK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AZj4iytVUbcUw_e4Y_4ASaw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The screenshot above is the initial screen youâ€™ll see when creating a model. Choosing the default training and evaluation settings basically means you let Amazon configure everything about your model. If you choose custom you can change things like your regularization parameter, the training data vs evaluation data split, whether to randomly or sequentially choose training data, and more. The first time around I would suggest choosing Default and letting Amazon configure things for youâ€Šâ€”â€Šif you arenâ€™t happy with your results then you can start trying to work on some customizations.&lt;/p&gt;

&lt;p&gt;Once you finish creating your model- &lt;strong&gt;boom!&lt;/strong&gt; Amazon will start putting itâ€™s servers to work on training your model. Depending on how much data you feed it, this step can take awhile. Running binary classification on a couple thousand tweets took about ~7 minutes or so. Once itâ€™s done we can check out how our model performed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Tsj-swhA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2Ab_jR6buuQYMw5EpRqq2Z5w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Tsj-swhA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2Ab_jR6buuQYMw5EpRqq2Z5w.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Amazon will give you a nice little chart explaining your models performance on the evaluation data set. As you can see from the screenshot above, our modeled classified tweets as actionable at an 88% correct rateâ€Šâ€”â€Šthatâ€™s pretty cool. From this model performance screen you can also modify your modelâ€™s threshold to give you more or less false positives/negatives. In the case of actionable tweets, I figured itâ€™s better to have more false positives (a tweet is marked actionable when itâ€™s not), rather than false negatives (a tweet is marked not actionable and it was), so I modified my model to err on the side of false positives.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing our Model/Where do we go fromÂ here?
&lt;/h2&gt;

&lt;p&gt;Cool weâ€™ve got a model that works wellâ€¦ now what? For starters we can try out real time predictions right in the Amazon console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ba7fBfna--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AfjYXhyiwKGioYLHCPs_dng.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ba7fBfna--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AfjYXhyiwKGioYLHCPs_dng.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this instance (above) I tested &lt;em&gt;â€œ@weebly how do I fix my website?â€&lt;/em&gt; and correctly got a prediction of actionable (1 is actionable, 0 is not). In the â€œpredictedScoresâ€ blob you can get a closer look at how the model actually scored your data. The closer to 1 our score was the more confident the model was that it was actionable, and same for zero.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SFqKr-eE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2A8BXTx00UUgtmj6MnPA1uMA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SFqKr-eE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2A8BXTx00UUgtmj6MnPA1uMA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see in this example (&lt;em&gt;â€œ@weebly I love dogs!â€&lt;/em&gt;) the model very confidently predicted that this is not an actionable tweet.&lt;/p&gt;

&lt;p&gt;We can also interact with this model via Amazons API (maybe will save this for a separate post) so itâ€™s actually useful in a production environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Amazon ML is extremely easy to use. Itâ€™s still in itâ€™s infancy so the amount of customization and insight into your model you have is still little compared to when using services like &lt;a href="https://www.tensorflow.org/"&gt;tensorflow&lt;/a&gt; or &lt;a href="http://scikit-learn.org/stable/"&gt;scikit-learn&lt;/a&gt;. Overall, itâ€™s a great way to easily compute basic ML models, and quickly integrate your models into application layer code.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>datascience</category>
      <category>amazonml</category>
      <category>aws</category>
    </item>
  </channel>
</rss>
