<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
<title type="text">Alex Salo Dev Notes</title>
<generator uri="https://github.com/mojombo/jekyll">Jekyll</generator>
<link rel="self" type="application/atom+xml" href="//alexsalo.github.io/feed.xml" />
<link rel="alternate" type="text/html" href="//alexsalo.github.io" />
<updated>2025-08-11T17:49:25-04:00</updated>
<id>//alexsalo.github.io/</id>
<author>
  <name>Alex Salo</name>
  <uri>//alexsalo.github.io/</uri>
  <email>alexsalodev@gmail.com</email>
</author>


<entry>
  <title type="html"><![CDATA[How to Download Multiple Instagram Photos at Once?]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/how-to-download-multiple-instagram-pics/" />
  <id>//alexsalo.github.io/how-to-download-multiple-instagram-pics</id>
  <published>2016-12-29T00:00:00-05:00</published>
  <updated>2016-12-29T00:00:00-05:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;You want to print your Insta photos or just keep them save and archived? Out of luck - there is no obvious way to download them, unless you manually click save on every one of them. Yes, there are a few services that claim they can download it for you, but none of them worked sufficiently well in my case. And so I set about writing a bash script. Here is how to use it:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Go to &lt;a href=&quot;https://www.instagram.com/username/&quot;&gt;https://www.instagram.com/username/&lt;/a&gt;, hit “load more” and scroll down until you are satisfied.&lt;/li&gt;
  &lt;li&gt;Right click -&amp;gt; inspect element -&amp;gt; click once at the top “html” parent node and hit ctrl+c to copy the &lt;strong&gt;generated&lt;/strong&gt; html into the clipboard. Instagram is foxy and with its implementation on React it does not leak you with all the urls easily. That’s why we could not just wget the page to begin with.&lt;/li&gt;
  &lt;li&gt;In any text editor, paste the html with ctrl-v and save the file into &lt;strong&gt;&lt;em&gt;username.txt&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;Paste the script into &lt;strong&gt;&lt;em&gt;getit.sh&lt;/em&gt;&lt;/strong&gt;, make it executable by &lt;strong&gt;&lt;em&gt;chmod +x getit.sh&lt;/em&gt;&lt;/strong&gt; and finally run it with &lt;strong&gt;&lt;em&gt;./getit.sh&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;c&quot;&gt;# split into new line with every new src tag&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$username&lt;/span&gt;.txt | &lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;s/src=\&quot;/\&apos;$&apos;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;/g&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; lines.txt

&lt;span class=&quot;c&quot;&gt;# collect the urls using regex&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;lines.txt | &lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;^https.*n\.jpg&apos;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; urls.txt

&lt;span class=&quot;c&quot;&gt;# replace the default resolution to the max resolution&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;urls.txt | &lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;s/s640x640/sMax/g&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; urls_max.txt

&lt;span class=&quot;c&quot;&gt;# loop over the urls and curl them down using the digit-name from the url&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;while &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;read &lt;/span&gt;p&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do
  &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$p&lt;/span&gt; | egrep &lt;span class=&quot;s1&quot;&gt;&apos;[0-9]{8}_[0-9]+_[0-9]+&apos;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;echo &lt;/span&gt;downloading &lt;span class=&quot;nv&quot;&gt;$name&lt;/span&gt; ...
  curl &lt;span class=&quot;nv&quot;&gt;$p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$name&lt;/span&gt;.jpg
&lt;span class=&quot;k&quot;&gt;done&lt;/span&gt; &amp;lt; &lt;span class=&quot;s1&quot;&gt;&apos;urls_max.txt&apos;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Profit!
&lt;img src=&quot;/images/yosemite.jpg&quot; alt=&quot;Yosemite&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/how-to-download-multiple-instagram-pics/&quot;&gt;How to Download Multiple Instagram Photos at Once?&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on December 29, 2016.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[Polymer and Vulcanization]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/polymer-and-vulcanization/" />
  <id>//alexsalo.github.io/polymer-and-vulcanization</id>
  <published>2016-08-08T00:00:00-04:00</published>
  <updated>2016-08-08T00:00:00-04:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;The revelation of the day: Polymer and Vulcanization for web dev.&lt;/p&gt;

&lt;p&gt;Polymer is a new library from Google that aims to extend basic HTML components with very neat and universal, yet blazingly fast and easy DOM level components. The idea is that you can use nice and beautiful components made by best designers just in any framework you like. Paper cards are great - supporting material design. And yes, they are responsive - all the nasty screen size adaptation is taken care of.&lt;/p&gt;

&lt;p&gt;The second one is the concept of vulcanization, which is just an idea of combining all the html together in one file, and all the js in the other, so that the client would only need to do a minimal number of http requests, which dramatically reduces the load time.&lt;/p&gt;

    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/polymer-and-vulcanization/&quot;&gt;Polymer and Vulcanization&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on August 08, 2016.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[Jeff Dean of Google]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/jeff-dean-google/" />
  <id>//alexsalo.github.io/jeff-dean-google</id>
  <published>2016-08-06T00:00:00-04:00</published>
  <updated>2016-08-06T00:00:00-04:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;I have never heard of Jeff before, today I’ve heard he is like a star, and now I can see why. Below is the list of things known to be true about Jeff (copied from Evan Putra Limanto answer on &lt;a href=&quot;https://www.quora.com/What-are-all-the-Jeff-Dean-facts&quot;&gt;Quora&lt;/a&gt;)&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Jeff Dean proved that P=NP when he solved all NP problems in polynomial time on a whiteboard.&lt;/li&gt;
  &lt;li&gt;Jeff Dean’s PIN is the last 4 digits of pi.&lt;/li&gt;
  &lt;li&gt;When Jeff gives a seminar at Stanford, it’s so crowded Don Knuth has to sit on the floor. (TRUE)&lt;/li&gt;
  &lt;li&gt;Jeff Dean once bit a spider, the spider got super powers and C readability&lt;/li&gt;
  &lt;li&gt;Once, in early 2002, when the index servers went down, Jeff Dean answered user queries manually for two hours. Evals showed a quality improvement of 5 points.&lt;/li&gt;
&lt;/ul&gt;

&lt;!--cut--&gt;
&lt;ul&gt;
  &lt;li&gt;Jeff Dean got promoted to level 11 in a system where max level is 10. (True.)&lt;/li&gt;
  &lt;li&gt;Google Search was Jeff Dean’s Noogler Project&lt;/li&gt;
  &lt;li&gt;Jeff Dean has punch card readability.&lt;/li&gt;
  &lt;li&gt;Jeff Dean puts his pants on one leg at a time, but if he had more than two legs, you would see that his approach is actually O(log n)&lt;/li&gt;
  &lt;li&gt;Jeff Dean acquired Sawzall readability after writing 58 lines of Sawzall code. As part of his readability review, he pointed out a flaw in the style guide which was promptly corrected by the reviewer.&lt;/li&gt;
  &lt;li&gt;Sanjay once asked Jeff Dean if he could keep the entire web in his memory. Due to the noise from his keyboard cooling fan, Jeff Dean misheard slightly and wrote Mustang instead of simply answering “Yes”.&lt;/li&gt;
  &lt;li&gt;Jeff Dean compiles and runs his code before submitting, but only to check for compiler and CPU bugs.&lt;/li&gt;
  &lt;li&gt;Unsatisfied with constant time, Jeff Dean created the world’s first O(1/n) algorithm.&lt;/li&gt;
  &lt;li&gt;Jeff Dean has binary readability.&lt;/li&gt;
  &lt;li&gt;Jeff Dean has binary writability.&lt;/li&gt;
  &lt;li&gt;When Jeff Dean goes on vacation, production services across Google mysteriously stop working within a few days. This is actually true.&lt;/li&gt;
  &lt;li&gt;Jeff Dean once shifted a bit so hard it ended up on another computer.&lt;/li&gt;
  &lt;li&gt;During his own Google interview, Jeff Dean was asked the implications if P=NP were true. He said “P = 0 or N = 1.” Then, before the interviewer had even finished laughing, Jeff examined Google’s public certificate and wrote the private key on the whiteboard.&lt;/li&gt;
  &lt;li&gt;You use 10% of your brain. The other 90% is running one of Jeff’s mapreduce jobs.&lt;/li&gt;
  &lt;li&gt;Jeff Dean’s resume lists the things he hasn’t done; it’s shorter that way.&lt;/li&gt;
  &lt;li&gt;To Jeff Dean, “NP” means “No Problemo”.&lt;/li&gt;
  &lt;li&gt;Jeff Dean wrote an O(n^2) algorithm once. It was for the Traveling Salesman Problem.&lt;/li&gt;
  &lt;li&gt;You don’t explain your work to Jeff Dean. Jeff Dean explains your work to you.&lt;/li&gt;
  &lt;li&gt;Jeff Dean’s resume has so many accomplishments, it has a table of contents.&lt;/li&gt;
  &lt;li&gt;Jeff Dean was forced to invent asynchronous APIs one day when he optimized a function so that it returned before it was invoked.&lt;/li&gt;
  &lt;li&gt;The rate at which Jeff Dean produces code jumped by a factor of 40 in late 2000 when he upgraded his keyboard to USB2.0.&lt;/li&gt;
  &lt;li&gt;When Jeff Dean designs software, he first codes the binary and then writes the source as documentation.&lt;/li&gt;
  &lt;li&gt;Jeff Dean’s Peer Review is what got Larry promoted to CEO.&lt;/li&gt;
  &lt;li&gt;When God said: “Let there be light!”, Jeff Dean was there to do the code review.&lt;/li&gt;
  &lt;li&gt;When Graham Bell invented the telephone, he saw a missed call from Jeff Dean&lt;/li&gt;
  &lt;li&gt;Compilers don’t warn Jeff Dean. Jeff Dean warns compilers.&lt;/li&gt;
  &lt;li&gt;Jeff Dean doesn’t exist, he’s actually an advanced AI created by Jeff Dean.&lt;/li&gt;
  &lt;li&gt;Jeff Dean’s IDE doesn’t do code analysis, it does code appreciation.&lt;/li&gt;
  &lt;li&gt;Jeff Dean’s keyboard doesn’t have a Ctrl key because Jeff Dean is always in control.&lt;/li&gt;
  &lt;li&gt;Jeff Dean doesn’t use ECC memory. He anticipates cosmic rays and uses them to improve performance.&lt;/li&gt;
  &lt;li&gt;Jeff Dean once failed a Turing test when he correctly identified the 203rd Fibonacci number in less than a second.&lt;/li&gt;
  &lt;li&gt;Jeff Dean invented Bigtable so that he would have a place to send his weekly snippets.&lt;/li&gt;
  &lt;li&gt;On the zeroth day, Jeff Dean created God.&lt;/li&gt;
  &lt;li&gt;Jeff Dean once implemented a web server in a single printf() call. Other engineers added thousands of lines of explanatory comments but still don’t understand exactly how it works. Today that program is known as GWS.&lt;/li&gt;
  &lt;li&gt;When Jeff has an ergonomic evaluation, it is for the protection of his keyboard.&lt;/li&gt;
  &lt;li&gt;Jeff Dean can beat you at connect four. In three moves.&lt;/li&gt;
  &lt;li&gt;Jefff Dean invented BigTable because his resume was too big to fit anywhere else.&lt;/li&gt;
  &lt;li&gt;Jeff Dean took the bite out of Apple’s logo.&lt;/li&gt;
  &lt;li&gt;Chuck Norris can kill you. Jeff Dean can kill -9 you.&lt;/li&gt;
  &lt;li&gt;Jeff Dean can parse HTML with a regular expression…correctly.&lt;/li&gt;
  &lt;li&gt;When Jeff has trouble sleeping, he Mapreduces sheep.&lt;/li&gt;
  &lt;li&gt;When Jeff Dean fires up the profiler, loops unroll themselves in fear.&lt;/li&gt;
  &lt;li&gt;When your code has undefined behavior, you get a seg fault and corrupted data. When Jeff Dean’s code has undefined behavior, a unicorn rides in on a rainbow and gives everybody free ice cream.&lt;/li&gt;
  &lt;li&gt;Jeff doesn’t sleep, he just sends SIGSUSPEND to the universe.&lt;/li&gt;
  &lt;li&gt;Jeff got Java readability with only 8 lines of code&lt;/li&gt;
  &lt;li&gt;Jeff Dean can instantiate abstract classes.&lt;/li&gt;
  &lt;li&gt;gcc -O4 sends your code to Jeff Dean for a complete rewrite.&lt;/li&gt;
  &lt;li&gt;Jeff can recite 20,000 digits of pi in 5 hours. He doesn’t remember them; he just recomputes them on the fly using only O(log n) space.&lt;/li&gt;
  &lt;li&gt;Jeff Dean remembers only one password. For each site, he concatenates it with the site name, computes its SHA-256 hash, and then types the result.&lt;/li&gt;
  &lt;li&gt;Jeff Dean is still waiting for mathematicians to discover the joke he hid in the digits of PI.&lt;/li&gt;
  &lt;li&gt;There is no ‘Ctrl’ key on Jeff Dean’s keyboard. Jeff Dean is always in control.&lt;/li&gt;
  &lt;li&gt;Jeff Dean was born on December 31, 1969 at 11:48 PM. It took him twelve minutes to implement his first time counter.&lt;/li&gt;
  &lt;li&gt;When Jeff Dean says “Hello World”, the world says “Hello Jeff”.&lt;/li&gt;
  &lt;li&gt;Jeff Dean can get 1s out of /dev/zero.&lt;/li&gt;
  &lt;li&gt;Jeff Dean simply walks into Mordor.&lt;/li&gt;
  &lt;li&gt;Jeff Dean spent some 20% time on an AI project. That produced Urs Hoelzle.&lt;/li&gt;
  &lt;li&gt;Google once had to move out of a datacenter after Jeff Dean accidentally compressed the index so densely that a black hole was formed.&lt;/li&gt;
  &lt;li&gt;Jeff starts his programming sessions with “cat &amp;gt; /dev/mem”.&lt;/li&gt;
  &lt;li&gt;The speed of light in a vacuum used to be about 35 mph. Then Jeff Dean spent a weekend optimizing physics.&lt;/li&gt;
  &lt;li&gt;When Jeff Dean sends you a code review, it’s because he thinks there’s something in it you should learn.&lt;/li&gt;
  &lt;li&gt;Jeff Dean does not sleep(), he wait()s.&lt;/li&gt;
  &lt;li&gt;Jeff Dean invented MapReduce so he could sort his fan mail.&lt;/li&gt;
  &lt;li&gt;Jeff Dean’s keyboard has two keys: 1 and 0.&lt;/li&gt;
  &lt;li&gt;When Jeff Dean listens to mp3s, he just cats them to /dev/dsp and does the decoding in his head.&lt;/li&gt;
  &lt;li&gt;Once Jeff Dean ordered a list, and the list obeyed him.&lt;/li&gt;
  &lt;li&gt;Chuck Norris is Jeff Dean’s 20% project.&lt;/li&gt;
  &lt;li&gt;When your code is killed by SIGJEFF, it never runs again.&lt;/li&gt;
  &lt;li&gt;Jeff Dean’s calendar goes straight from March 31st to April 2nd; no one fools Jeff Dean.&lt;/li&gt;
  &lt;li&gt;Jeff Dean never has the wrong number; you have the wrong phone.&lt;/li&gt;
  &lt;li&gt;Jeff Dean has exactly two keys on his keyboard: “0” and “1.”&lt;/li&gt;
  &lt;li&gt;Errors treat Jeff Dean as a warning.&lt;/li&gt;
  &lt;li&gt;Cricket matches used to take 5 days, until Jeff optimized them.&lt;/li&gt;
  &lt;li&gt;Jeff Dean’s watch displays seconds since January 1st, 1970. He is never late.&lt;/li&gt;
  &lt;li&gt;Jeff’s code is so fast the assembly code needs three HALT opcodes to stop it.&lt;/li&gt;
  &lt;li&gt;Emacs’ preferred editor is Jeff Dean.&lt;/li&gt;
  &lt;li&gt;Google: it’s basically a Jeff Dean’s side project.&lt;/li&gt;
  &lt;li&gt;Jeff Dean has to unoptimize his code so that reviewers believe it was written by a human.&lt;/li&gt;
  &lt;li&gt;Websearch is just a large unittest Jeff wrote for his real app.&lt;/li&gt;
  &lt;li&gt;Jeff Dean doesn’t need speakers or headphones. He just types “cat *.mp3”, glances at the screen, and his brain decodes the music in the background while he works.&lt;/li&gt;
  &lt;li&gt;Jeff Dean has Perl Readability. (TRUE)&lt;/li&gt;
  &lt;li&gt;Jeff Dean quicksorts his laundry.&lt;/li&gt;
  &lt;li&gt;The OR ELSE construct had to be removed from ISO C after Jeff Dean used it in Mustang and kernels started panicking in terror.&lt;/li&gt;
  &lt;li&gt;Jeff Dean is not afraid of evil constructors. They are afraid of him.&lt;/li&gt;
  &lt;li&gt;Jeff Dean doesn’t write bugs, just features you are unable to understand.&lt;/li&gt;
&lt;/ul&gt;

    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/jeff-dean-google/&quot;&gt;Jeff Dean of Google&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on August 06, 2016.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[Yosemite Budget Camping without Reservation]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/yosemite-budget-guide/" />
  <id>//alexsalo.github.io/yosemite-budget-guide</id>
  <published>2016-06-09T00:00:00-04:00</published>
  <updated>2016-06-09T00:00:00-04:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;Yosemite is awesome! The best time to go is Spring: you would have plenty of flow for the waterfalls, and it is much nicer to hike without extensive sweating.&lt;/p&gt;

&lt;p&gt;How to go Yosemite in the cost efficient manner and without six-months-in-advance-reservation? Here is a short guide.
&lt;a href=&quot;//alexsalo.github.io/images/travel/camp4.jpg&quot;&gt;&lt;img src=&quot;//alexsalo.github.io/images/travel/camp4.jpg&quot; alt=&quot;Camp 4&quot; /&gt;&lt;/a&gt;
&lt;!--cut--&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Camp 4 is first come first served and fits about 200 people. It’s only $6 per person. The problem is to get a spot. Your best chance is during “unpopular” time: not Friday or Saturday, “cold” weather, early in the morning (some people line up starting at 5 am). Your chances are actually pretty good if you come on Sunday at 10 am when the forecast predicts 32F for the night.&lt;/li&gt;
  &lt;li&gt;If that fails, you can try to get one of the canceled reservations. People don’t tell you about this, but it is actually a legit way to get a spot. Generally, 10-20 people would cancel their stay in the Lower and Upper Pines to get a refund. Those spots would be given aways at 3 pm to those who signed up at the “campground reservations” in the parking lot of the Pines. The sooner you signing up - the better the chances. We’ve tried two times, both times successfully. Keep in mind that many people get into the list but then don’t show up at 3 pm. So the strategy is to try get a spot at Camp4, but if nothing is available - sing up for waiting list in Pines.&lt;/li&gt;
  &lt;li&gt;If both fails, you can either camp at the Tamarack flats (hardly accessible by RVs and no utilities makes it unpopular, luckily for us) or go into the wild and camp in one of the many available spots - just talk to the ranger for a permit and bear can.&lt;/li&gt;
  &lt;li&gt;After a day hike you want a shower. For some reason this is not advertised, but super nice showers are available at the housekeeping camp for $5, which includes a towel and shampoos. Water is hot. Time is 45 mins. You can not ask for a better shower.&lt;/li&gt;
&lt;/ol&gt;

    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/yosemite-budget-guide/&quot;&gt;Yosemite Budget Camping without Reservation&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on June 09, 2016.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[Locally Weighted Linear Regression in NoSQL world MapReduce, Spark, Hive and Spark SQL implementations over Hadoop HDFS]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/nosql-locally-weighted-regression-mapreduce-spark-hive/" />
  <id>//alexsalo.github.io/nosql-locally-weighted-regression-mapreduce-spark-hive</id>
  <published>2016-04-27T00:00:00-04:00</published>
  <updated>2016-04-27T00:00:00-04:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;In this document we provide a brief discussion on the LWR algorithm and somewhat detailed description on how to run our code using each tool on the provided VirtualBox VM (code is available on &lt;a href=&quot;https://github.com/alexsalo/nosql-locally-weighted-regression&quot;&gt;project’s github&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Document structure:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;#lwr&quot;&gt;Locally Weighted Linear Regression&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#lwr-discussion&quot;&gt;Algorithm Discussion&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#lwr-performance&quot;&gt;Performance Analysis and Parallelization Options&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#hadoop-overview&quot;&gt;Hadoop Ecosystem Overview&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#os-setup&quot;&gt;Setting Up OS Env&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#nosql&quot;&gt;NoSQL implementations:&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#nosql-mapreduce&quot;&gt;MapReduce&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#nosql-spark&quot;&gt;Spark&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#nosql-hive&quot;&gt;Hive&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#nosql-sparksql&quot;&gt;SparkSQL&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;-1-locally-weighted-linear-regression&quot;&gt;&lt;a id=&quot;lwr&quot;&gt;&lt;/a&gt; 1. Locally Weighted Linear Regression&lt;/h2&gt;
&lt;p&gt;Often, when working with real data, we realize that our data doesn’t really lie on the straight line - in other words linear regression fails to capture a lot of variation. When that happens we can use a higher degree polynomial of our features to fit data perfectly. The problem with such parametric approach is two-fold: first, we need to manually choose the “right” degree of polynomials which leads to either underfitting or overfitting; second, we don’t take advantage of the new data that will flow in - once the model is set it is not going to change.&lt;/p&gt;

&lt;p&gt;To address these issues we want to implement Locally Weighted [Linear] Regression (LWR), which, assuming sufficient size of the training set, makes the choice of features much less critical. Additionally, LWR is a lazy learning approach and allows us learn on the newly obtained data. Given the goal of Big Data analytics, we definitely want to make use of all the data, yet we are concerned with the computational efficiency. Thus we would like to use NoSQL tools to implement LWR. Now let us briefly discuss the math behind the LWR algorithm, evaluate its time complexity  and find possibilities for optimization via parallelization.&lt;/p&gt;

&lt;!--cut--&gt;

&lt;h3 id=&quot;-algorithm-discussion&quot;&gt;&lt;a id=&quot;lwr-discussion&quot;&gt;&lt;/a&gt; Algorithm Discussion&lt;/h3&gt;
&lt;p&gt;To understand Locally Weighted linear Regression (LWR) let us consider a simple linear regression (LR) first. As an input we have N-dimensional dataset X with M examples (each example has N features) and 1-dimensional vector Y with targets. The goal of LR is to fit the line that would map X -&amp;gt; Y at the minimized OLS sum. Such line would allows us to predict y-value for any x-input.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;m  | X &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;features&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;    | Y &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;targets&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;  
&lt;span class=&quot;nt&quot;&gt;--------------------------------&lt;/span&gt;
1: |1 x0 x1 x2 ... xn| y
2: |1 x0 x1 x2 ... xn| y
.: |1 ...      ...   | y
M: |1 x0 x1 x2 ... xn| y&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Note that we appended X to a columns of ones - resulting matrix is called a &lt;em&gt;design matrix&lt;/em&gt; - it allows us to find the intercept of the line. To construct that line (or, more generally, a hyperplane in N-dimensional space) we find the best hypothesis &lt;strong&gt;theta&lt;/strong&gt; (which is a vector [size N] of coefficients for each dimension of X) by solving the following normal equation:&lt;/p&gt;
&lt;center&gt;X&apos;X&amp;theta;=X&apos;Y&lt;/center&gt;

&lt;p&gt;The closed form solution then:&lt;/p&gt;
&lt;center&gt;&amp;theta;=inv(X&apos;X)X&apos;Y&lt;/center&gt;

&lt;p&gt;This solution gives the intercept and slopes (hypothesis θ) for the line in each X’s dimension. The LWR goes a step further and locally adjusts the line according to the proximity to other data points - the closer the point the more weight it has.&lt;/p&gt;

&lt;p&gt;That is a good time to introduce one simple yet not very intuitive feature of LWR. In LR, all we need to do in order to construct a line (hyperplane) is to solve &lt;em&gt;one normal equation&lt;/em&gt;, which gives the hypothesis θ, using which &lt;em&gt;we can predict y-value for any input x-value&lt;/em&gt; by reusing the hypothesis and simply calculating the product x’θ (here and later capital X or Y represents a set of data examples while small x or y represents one particular example).&lt;/p&gt;

&lt;p&gt;In the LWR, since our predictions are no longer linear, but adjusted by the neighboring points, &lt;em&gt;for each x-point we want to predict (we would call it query point), we need to calculate its own hypothesis θ&lt;/em&gt;. For each such query point we consider all other points available, but their influence is weighted by the distance to our query point. That is a LWR in a nutshell. Let us now see how it translates into linear algebra.&lt;/p&gt;

&lt;p&gt;In fact, all we need to change for LWR is to add a weight matrix W (diagonal matrix of size m x m), and solve the analogous normal equation as:&lt;/p&gt;
&lt;center&gt;&amp;theta;=inv(X&apos;WX)X&apos;WY&lt;/center&gt;

&lt;p&gt;Finally, note that we use a Gaussian Kernel as our distance function. We use bandwidth parameter &lt;em&gt;C=0.5&lt;/em&gt; throughout this project. The choice of the bandwidth could be optimized and found automatically, but that does not change anything in our area of interest.&lt;/p&gt;

&lt;h3 id=&quot;-performance-analysis-and-parallelization-options&quot;&gt;&lt;a id=&quot;lwr-performance&quot;&gt;&lt;/a&gt; Performance Analysis and Parallelization Options&lt;/h3&gt;
&lt;p&gt;Just how fast LWR works? Should we even care to parallelize? The answer is yes - we want to calculate the distance to every other data point after all. In particular, we assume that there are ever growing loads of examples to learn on available (hundreds of thousands or more), while the number of features in each example is relatively small (on the order of tens or hundreds). In other word we explicitly assume &lt;strong&gt;M » N&lt;/strong&gt;. Also we have &lt;em&gt;P&lt;/em&gt; cores (or commodity computers in the cluster) available.&lt;/p&gt;

&lt;p&gt;One obvious way to parallelize LWR is when we want to calculate the prediction for several query points. In that case we simply split the set of Q query points between the P cored and we are done ~Q/P times faster. Implementing this trivial solution is rather straightforward so we would not consider it. Instead we would be concerned in how can we &lt;em&gt;parallelize the computations for a single query point&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Recall that in order to find a hypothesis θ for a single query point over the set of training examples we need to solve the following normal equation:&lt;/p&gt;
&lt;center&gt;A&amp;theta;=B&lt;/center&gt;
&lt;p&gt;Where:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;A &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; X&lt;span class=&quot;s1&quot;&gt;&apos;WX has dimensions: X&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;n x m&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; W&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;m x m&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; X&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;m x n&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; A &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;n x n&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
B &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; X&lt;span class=&quot;s1&quot;&gt;&apos;WY has dimensions: X&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;n x m&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; W&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;m x m&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; Y&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;m x 1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; B &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;n x 1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
theta &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; inv&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;B has dimensions: A &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;n x n&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; b &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;n x 1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; theta &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;n x 1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Note that we can perform exactly the same operations over an individual training examples:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;c&quot;&gt;# given ith example x = X[i]&lt;/span&gt;
a &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; x&lt;span class=&quot;s1&quot;&gt;&apos;wx has dimensions: x&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;n x 1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; w&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;1 x 1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; x&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;1 x n&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;n x n&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
b &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; x&lt;span class=&quot;s1&quot;&gt;&apos;wy has dimensions: x&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;n x 1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; w&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;1 x 1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; y&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;1 x 1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; b &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;n x 1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And they produce the matricies a and b of exactly the same size as A and B. Thus we notice the additivity property in the LWR calculation, and that is a good sign of the parallelization eligibility.&lt;/p&gt;

&lt;p&gt;In fact, all we need to do is to split all the training examples into the P equal parts and feed them to P cores to deal with. Each core will compute partial sums of matricies A and B. Then we simply sum those intermediate matricies produced by each core into resulting A and B, and then compute the hypothesis as θ=inv(A)*B&lt;/p&gt;

&lt;p&gt;The time complexity of a single core LWR is given by O(mn&lt;sup&gt;2&lt;/sup&gt;) to compute x’x, m times, plus O(n&lt;sup&gt;3&lt;/sup&gt;) to invert the matrix, totalling to O(mn&lt;sup&gt;2&lt;/sup&gt; + n&lt;sup&gt;3&lt;/sup&gt;).&lt;/p&gt;

&lt;p&gt;On our LWR (no parallelization of the matrix inverse), P cores gives us total of O(mn&lt;sup&gt;2&lt;/sup&gt;/P + n&lt;sup&gt;3&lt;/sup&gt;), plus some communication cost. Given our assumption m»n we can drop the n term and estimate the time complexity as O(m/P), and expect that, in theory, LWR should perform linearly faster with the increase in the number of cores.&lt;/p&gt;

&lt;h2 id=&quot;-2-hadoop-ecosystem-overview&quot;&gt;&lt;a id=&quot;hadoop-overview&quot;&gt;&lt;/a&gt; 2. Hadoop Ecosystem Overview&lt;/h2&gt;
&lt;p&gt;How do we set about to implement LWR in NoSQL world? We want something robust, open source and easy to work with. Luckily, there is just such a thing:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The Apache Hadoop software library is a framework that allows for the distributed processing of large data sets across clusters of computers using simple programming models.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hadoop consists of two main things: a &lt;em&gt;Distributed File System (HDFS)&lt;/em&gt; and computational framework &lt;em&gt;MapReduce&lt;/em&gt;. HDFS provides us with fault tolerant distributed file storage system that provides high-throughput access to application data. MapReduce allows easy parallel processing of large data sets. There are also a &lt;em&gt;Hadoop Common&lt;/em&gt; utilities used by other tools within Hadoop Ecosystem, and YARN - framework for job scheduling and cluster resource management, which is not going to play a critical role in our implementations.&lt;/p&gt;

&lt;p&gt;Thus our initial attempt of implementing LWR would be in native MapReduce over HDFS. To use MR one can either implement abstract map and reduce classes in Java or take advantage of Hadoop Streaming API, which is a utility allowing to &lt;em&gt;run any executable as Mapper/Reducer&lt;/em&gt;. Essentially, Hadoop Streaming facilitates passing data between our Map and Reduce phases via STDIN and STDOUT. Thus, since we want to use Python, to read &amp;lt;key, value&amp;gt; pair we simply read from sys.stdin; to emit &amp;lt;key, value&amp;gt; pair we write (print) to sys.stdout. Hadoop Streaming takes care of the rest.&lt;/p&gt;

&lt;p&gt;On the next step we will try Apache Spark, which works on top of HDFS. Moreover, Spark can run on top of YARN and/or MapReduce. Spark revolves around the concept of a resilient distributed dataset (RDD), which is a fault-tolerant collection of elements that can be operated on in parallel. Once created, the RDD can be operated on in parallel. Besides advantageous RDDs, Spark allows developer to write code in Java, Scala or Python.&lt;/p&gt;

&lt;p&gt;Another approach is to leverage distributed capabilities of HDFS and reuse MapReduce. That’s where HBase and Hive come into play.&lt;/p&gt;

&lt;p&gt;The main problem with HDFS, that as any FS, it &lt;em&gt;lacks random read and write access&lt;/em&gt;. That’s where HBase comes into play to provide read/write capabilities. HBase is a distributed, scalable, big data store build on blueprints of Google’s BigTable. In other words, HBase is a database (not as FS). HBase stores data as key/value pairs, which makes it look like a hashmap, or an index table: if you know the key - you get answer fast.&lt;/p&gt;

&lt;p&gt;Hive approaches a problem from the different angle. It provides data warehousing facilities on top of an existing Hadoop cluster. We can create tables in Hive and store data there. Hive also provide HQL, similar to SQL in syntax, to query our data in an arbitrary style.&lt;/p&gt;

&lt;p&gt;These are the descriptions from the official web sites:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ul&gt;
    &lt;li&gt;HBase™: A scalable, distributed database that supports structured data storage for large tables.&lt;/li&gt;
    &lt;li&gt;Hive™: A data warehouse infrastructure that provides data summarization and ad-hoc querying.&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;We are interested exactly in this &lt;em&gt;ad-hoc querying&lt;/em&gt;. For example, we want to run locally weighted regression on some subset of the columns, maybe even restricting rows with some &lt;em&gt;WHERE&lt;/em&gt; clause. Thus we probably want to choose Hive. To rephrase our choice I’d cite a StackOverflow answer:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;HBase is like a Map. If you know the key, you can instantly get the value. But if you want to know how many integer keys in Hbase are between 1000000 and 2000000 that is not suitable for Hbase alone. If you have data to be aggregated, rolled up, analyzed across rows then consider Hive.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thus we select Hive as our database-like approach to tackle LWR.&lt;/p&gt;

&lt;p&gt;Finally, we can run SparkSQL on top of everything: over HDFS files or Hive tables, over Spark RDDs or/and using MapRecude jobs. To quote SparkSQL web site:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;Seamlessly mix SQL queries with Spark programs. Spark SQL lets you query structured data inside Spark programs, using either SQL or a familiar DataFrame API. Usable in Java, Scala, Python and R.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;DataFrames and SQL provide a common way to access a variety of data sources, including Hive, Avro, Parquet, ORC, JSON, and JDBC. You can even join data across these sources.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Spark SQL reuses the Hive frontend and metastore, giving you full compatibility with existing Hive data, queries, and UDFs. Simply install it alongside Hive.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, with SparkSQL we can combine all the benefits of either HDFS, MapRecude, HIVE and Spark under unified workspace and using desirable Python.&lt;/p&gt;

&lt;h2 id=&quot;-3-setting-up-os-env&quot;&gt;&lt;a id=&quot;os-setup&quot;&gt;&lt;/a&gt; 3. Setting Up OS Env&lt;/h2&gt;
&lt;p&gt;You can skip this section if you are simply executing files on provided VM (come back if you see any suspicious exceptions). Here we highlight the basic OS environment setup that will ease our work with NoSQL tools. All the tools, in the latest stable version as of 25 April 2016, were downloaded from official Apache servers and unpacked into */nosql/&lt;toolname&gt;* folder. Along with that, */nosql/* folder contains an *input/* folder with our input data. Below is a short description of the project folder structure:&lt;/toolname&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;su    &lt;span class=&quot;c&quot;&gt;# password: bigdata&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /nosql
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;tree &lt;span class=&quot;nt&quot;&gt;--filelimit&lt;/span&gt; 8  &lt;span class=&quot;c&quot;&gt;# show folder structure&lt;/span&gt;
├── hadoop
│   ├── aux                             &lt;span class=&quot;c&quot;&gt;# Sources and auxiliary scripts that were left off&lt;/span&gt;
│   ├── hadoop-2.7.2                    &lt;span class=&quot;c&quot;&gt;# Hadoop Home&lt;/span&gt;
│   ├── lwr_mapreduce_find_theta.py     &lt;span class=&quot;c&quot;&gt;# Script to find hypothesis using MR output files&lt;/span&gt;
│   ├── lwr_mapreduce_mapper.py         &lt;span class=&quot;c&quot;&gt;# MapReduce mapper&lt;/span&gt;
│   ├── lwr_mapreduce_reducer.py        &lt;span class=&quot;c&quot;&gt;# MapReduce reducer&lt;/span&gt;
│   ├── lwr_plot_x_query.py             &lt;span class=&quot;c&quot;&gt;# Demo script to visualize LWR prediction in 2D&lt;/span&gt;
│   ├── np_matrix_helpers.py            &lt;span class=&quot;c&quot;&gt;# Helper functions to communicate Numpy matricies&lt;/span&gt;
│   └── README.md         
├── hive   
│   ├── apache-hive-2.0.0-bin           &lt;span class=&quot;c&quot;&gt;# Hive Home&lt;/span&gt;
│   ├── aux   
│   ├── lwr_hive_find_theta.py   
│   ├── lwr_hive_mapper.py   
│   └── README.md   
├── input                               &lt;span class=&quot;c&quot;&gt;# Input data (txt) files, separator=&apos;,&apos;&lt;/span&gt;
│   ├── x_data.txt x_data2.txt ...   
├── lwr_demo.png                        &lt;span class=&quot;c&quot;&gt;# Demo of the LWR prediction&lt;/span&gt;
├── README.md                           &lt;span class=&quot;c&quot;&gt;# This document&lt;/span&gt;
├── spark                            
│   ├── aux
│   ├── lwr_spark.py
│   ├── README.md
│   └── spark-1.6.1-bin-without-hadoop  &lt;span class=&quot;c&quot;&gt;# Spark Home&lt;/span&gt;
└── sparksql
    ├── csv-to-json.py
    ├── lwr_sparksql.py
    └── README.md&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;strong&gt;Important note&lt;/strong&gt;: if you change something, make sure all your &lt;em&gt;python executable files know the path to python interpreter&lt;/em&gt; and &lt;em&gt;have correct privileges&lt;/em&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; &amp;lt;python_script&amp;gt;.py
&lt;span class=&quot;c&quot;&gt;# #!/usr/bin/env python&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;chmod&lt;/span&gt; +x python_script.py&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To be able to &lt;em&gt;run all the tools from anywhere&lt;/em&gt; we need to update system &lt;em&gt;PATH&lt;/em&gt; variable. To make update permanent, we put append into &lt;em&gt;/root/.bash_rc&lt;/em&gt; the &lt;em&gt;/root/.bash_nosql_exports&lt;/em&gt; file with the following content:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;JAVA_HOME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/lib/jvm/java-7-openjdk-amd64
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HADOOP_HOME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/nosql/hadoop/hadoop-2.7.2
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;SPARK_HOME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/nosql/spark/spark-1.6.1-bin-without-hadoop
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HIVE_HOME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/nosql/hive/apache-hive-2.0.0-bin

&lt;span class=&quot;c&quot;&gt;# Append PATH&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$HIVE_HOME&lt;/span&gt;/bin:&lt;span class=&quot;nv&quot;&gt;$HADOOP_HOME&lt;/span&gt;/bin:&lt;span class=&quot;nv&quot;&gt;$SPARK_HOME&lt;/span&gt;/bin:&lt;span class=&quot;nv&quot;&gt;$PATH&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Save that and open a new terminal to apply changes. Check you PATH:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$PATH&lt;/span&gt;
/nosql/hive/apache-hive-2.0.0-bin/bin:/nosql/hadoop/hadoop-2.7.2/bin:/nosql/spark/spark-1.6.1-bin-without-hadoop/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Despite these are permanent settings, when we run the any of the tools, they look into *&lt;tool&gt;-env.sh* conf file to apply any settings. You want to make sure you have the following:&lt;/tool&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;vi &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HADOOP_HOME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;/etc/hadoop/hadoop-env.sh
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;JAVA_HOME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/lib/jvm/java-7-openjdk-amd64
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;JAVA_HOME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;/bin:&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PATH&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# make sure you append tools.jar to an existing HADOOP_CLASSPATH&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HADOOP_CLASSPATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;JAVA_HOME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;/lib/tools.jar:&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HADOOP_CLASSPATH&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;

vi &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;SPARK_HOME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;/conf/spark-env.sh
&lt;span class=&quot;c&quot;&gt;# We reuse our Hadoop installation in Spark - specify the path&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;SPARK_DIST_CLASSPATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;/nosql/hadoop/hadoop-2.7.2/bin/hadoop classpath&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;JAVA_HOME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/lib/jvm/java-7-openjdk-amd64
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;SPARK_HOME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/nosql/spark/spark-1.6.1-bin-without-hadoop

vi &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HIVE_HOME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;/conf/spark-env.sh
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HADOOP_HOME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/nosql/hadoop/hadoop-2.7.2  &lt;span class=&quot;c&quot;&gt;# Hive uses Hadoop common libraries&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HIVE_HOME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/nosql/hive/apache-hive-2.0.0-bin
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$HIVE_HOME&lt;/span&gt;/bin:&lt;span class=&quot;nv&quot;&gt;$PATH&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Note that SparkSQL, despite being a separate tool, comes within the basic Spark distribution and thus everything is already set (almost - we would need to compile it with the specific jars to support, say, the interaction with Hive, but we won’t do that).&lt;/p&gt;

&lt;h2 id=&quot;-4-nosql-implementations&quot;&gt;&lt;a id=&quot;nosql&quot;&gt;&lt;/a&gt; 4. NoSQL implementations:&lt;/h2&gt;
&lt;p&gt;Time for the experiments! Please note that to make examples self contained we provide redundant code for each tool (along with simplified code listings in the descriptions). Also, recall that our goal is to calculate prediction (by y=θx) of &lt;strong&gt;single x_query point&lt;/strong&gt;. We embedded this x_query point in each executable script to simplify the execution, but, obviously, this could be interfaced out. Additionally, we will provide a visual demo when using our first tool MapReduce, but for the following tools we will be satisfied when we get the same correct hypothesis θ. Finally, note that you &lt;strong&gt;have to&lt;/strong&gt; go through MapRecude part first in order to start HDFS service, which all other tools rely on. OK, Let’s get started!&lt;/p&gt;

&lt;h3 id=&quot;-mapreduce-via-streaming-api&quot;&gt;&lt;a id=&quot;nosql-mapreduce&quot;&gt;&lt;/a&gt; MapReduce via Streaming API&lt;/h3&gt;
&lt;p&gt;Hadoop Streaming API allows to run any executable files as mappers and reducers in MapReduce framework. We are using Python to implement locally weighted linear regression algorithm - we provide a simplified code listing for illustrative purposes below. There are three ways to execute python scripts:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;#linux-pipes&quot;&gt;Linux pipes to imitate MapReduce for development/testing purposes&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#hadoop-single&quot;&gt;Hadoop single node without HDFS to make sure everything works&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pseudo-distributed&quot;&gt;Hadoop pseudo-distributed mode, which emulate the full working environment with HDFS running (but without YARN)&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Also you can jump straight to &lt;a href=&quot;#demo&quot;&gt;Visual Demo of LWR Prediction&lt;/a&gt;&lt;/p&gt;

&lt;h5 id=&quot;mapreduce-streaming-api-implementation&quot;&gt;MapReduce Streaming API Implementation&lt;/h5&gt;
&lt;p&gt;Let us briefly review our implementation (full code available at &lt;a href=&quot;https://github.com/alexsalo/nosql-locally-weighted-regression&quot;&gt;project’s github&lt;/a&gt;). Mapper:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gauss_kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_query&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_to&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;d_sq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_sq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;


&lt;span class=&quot;c1&quot;&gt;# read file line by line; calculate and emit normal equation matricies A and B
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# parse values assuming x1 x2 x3 ... y
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;,&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])])&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gauss_kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# accumulate A and B
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# emit partial resulting martricies A and B into STDOUT
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;%s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;%s&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mat2str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;%s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;%s&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;b&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mat2str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Reducer:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# sum A and b matricies received from mapper
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# parse &amp;lt;key, value&amp;gt; from STDIN
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;mat_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mat_str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str2mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mat_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mat_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
       &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mat&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mat_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;b&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
       &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mat&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# emit full matricies A and B to STDOUT (saved into HDFS)
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;%s&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mat2str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;%s&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mat2str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Time to execute! All the following commands are issued from the &lt;em&gt;/nosql/hadoop/&lt;/em&gt; on the virtual machine under root privileges:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;su            &lt;span class=&quot;c&quot;&gt;# password: bigdata&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /nosql/hadoop/  &lt;span class=&quot;c&quot;&gt;# which has hadoop installation hadoop-2.7.2/&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h4 id=&quot;-1-linux-pipes-for-developmenttesting&quot;&gt;&lt;a id=&quot;linux-pipes&quot;&gt;&lt;/a&gt; 1. Linux Pipes for Development/Testing&lt;/h4&gt;
&lt;p&gt;While each step could be separated, ultimately we want to chain &lt;em&gt;input -&amp;gt; mapper -&amp;gt; reducer -&amp;gt; find_theta:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;First, let’s run mapper and reducer on the input files:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /nosql/input/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; | /nosql/hadoop/lwr_mapreduce_mapper.py | /nosql/hadoop/lwr_mapreduce_reducer.py&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Which produces the following output:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;0.04429884,-0.25175942,0.40566047,-0.01703711&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.25175942,1.43521279,-2.30624365,0.09647099&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;0.40566047,-2.30624365,3.71557318,-0.15618137&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.01703711,0.09647099,-0.15618137,0.00681046
&lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.26564261&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;1.51042747&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-2&lt;/span&gt;.43520908&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;0.10292634&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;These are two lines, each containing numpy arrays encoded for IO via &lt;em&gt;np_matrix_helpers.py&lt;/em&gt;. First line is a matrix &lt;strong&gt;A (n x n)&lt;/strong&gt;; second line is a matrix &lt;strong&gt;B (n x 1)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;With Linux pipes we can directly feed these matricies to &lt;em&gt;find_theta.py&lt;/em&gt; script to make a hypothesis:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /nosql/input/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; | /nosql/hadoop/lwr_mapreduce_mapper.py |
/nosql/hadoop/lwr_mapreduce_reducer.py | /nosql/hadoop/lwr_mapreduce_find_theta.py&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Which produces the hypothesis that we were looking for:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; 26.89559418]
 &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.58025811]
 &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-3&lt;/span&gt;.96902175]
 &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.40520663]]&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h4 id=&quot;-2-hadoop-single-node-without-hdfs&quot;&gt;&lt;a id=&quot;hadoop-single&quot;&gt;&lt;/a&gt; 2. Hadoop single node without HDFS&lt;/h4&gt;
&lt;p&gt;&lt;em&gt;–You can skip this part if you are not developing new functionality–&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;First, make sure single node mode is activated. Edit the following two files and leave empty configuration tags:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;vi hadoop-2.7.2/etc/hadoop/core-site.xml hdfs-site.xml
&lt;span class=&quot;c&quot;&gt;# &amp;lt;configuration&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# &amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Then let’s remove an output folder from the previous run (otherwise you’ll see error):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; mapreduce_output&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now we can run MapReduce. Don’t forget to include streaming API jar:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hadoop jar hadoop-2.7.2/share/hadoop/tools/lib/hadoop-streaming-2.7.2.jar &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-mapper&lt;/span&gt; lwr_mapreduce_mapper.py &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-reducer&lt;/span&gt; lwr_mapreduce_reducer.py &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-input&lt;/span&gt; /nosql/input/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-output&lt;/span&gt; output&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;That produces two lines with matricies &lt;strong&gt;A&lt;/strong&gt; and &lt;strong&gt;B&lt;/strong&gt;, just as our Linux pipes example did (we will take a closer look at MapReduce log info in the next part). To make a hypothesis do:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;output/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; | /nosql/hadoop/lwr_mapreduce_find_theta.py&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Which again produces the hypothesis:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; 26.89559418]
 &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.58025811]
 &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-3&lt;/span&gt;.96902175]
 &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.40520663]]&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h4 id=&quot;-3-hadoop-in-pseudo-distributed-mode-with-hdfs-running&quot;&gt;&lt;a id=&quot;pseudo-distributed&quot;&gt;&lt;/a&gt; 3. Hadoop in Pseudo-Distributed Mode with HDFS Running&lt;/h4&gt;
&lt;p&gt;Now that the trials of dev and tests are finished, let’s run a full deal. First, make sure pseudo-distributed mode is activated. Edit the following two files and add the following configuration tags:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;vi hadoop-2.7.2/etc/hadoop/core-site.xml
&lt;span class=&quot;c&quot;&gt;# &amp;lt;configuration&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#     &amp;lt;property&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#         &amp;lt;name&amp;gt;fs.defaultFS&amp;lt;/name&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#         &amp;lt;value&amp;gt;hdfs://localhost:9000&amp;lt;/value&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#     &amp;lt;/property&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# &amp;lt;/configuration&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;vi hadoop-2.7.2/etc/hadoop/hdfs-site.xml
&lt;span class=&quot;c&quot;&gt;# &amp;lt;configuration&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#     &amp;lt;property&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#         &amp;lt;name&amp;gt;dfs.replication&amp;lt;/name&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#         &amp;lt;value&amp;gt;1&amp;lt;/value&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#     &amp;lt;/property&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# &amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Then format HDFS:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs namenode &lt;span class=&quot;nt&quot;&gt;-format&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Start NameNode and DataNode daemons:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hadoop-2.7.2/sbin/start-dfs.sh&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Check that HDFS has the following directories by browsing http://localhost:50070/. If not present - create them:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-mkdir&lt;/span&gt; /user
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-mkdir&lt;/span&gt; /user/root  &lt;span class=&quot;c&quot;&gt;# should be the same username as in your vm&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-mkdir&lt;/span&gt; /user/root/input&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Put input file(s) from local path into HDFS path. Note that (given our test data size is relatively small) in order to make more mappers we want to make more data splits, which is naturally achieved by dividing data file into several files. In our case we simply copy the same data file several times.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-put&lt;/span&gt; /nosql/input/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; /user/root/input/&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now that the preparation is done we can actually run our executable python scripts as a MapReduce jobs (make sure you removed the output folder if you run second time):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-rm&lt;/span&gt; /user/root/mapreduce_output
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hadoop jar hadoop-2.7.2/share/hadoop/tools/lib/hadoop-streaming-2.7.2.jar &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-mapper&lt;/span&gt; lwr_mapreduce_mapper.py &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-reducer&lt;/span&gt; lwr_mapreduce_reducer.py &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-input&lt;/span&gt; /user/root/input/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-output&lt;/span&gt; /user/root/mapreduce_output&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Note the following in the MapReduce log info:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;...
16/04/26 11:14:25 INFO mapred.FileInputFormat: Total input paths to process : 4
16/04/26 11:14:25 INFO mapreduce.JobSubmitter: number of splits:4
...
Map-Reduce Framework
		Map input &lt;span class=&quot;nv&quot;&gt;records&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;5488
		Map output &lt;span class=&quot;nv&quot;&gt;records&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;8
		Map output &lt;span class=&quot;nv&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;940
    ...
		Reduce input &lt;span class=&quot;nb&quot;&gt;groups&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;2
    ...
		Reduce input &lt;span class=&quot;nv&quot;&gt;records&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;8
		Reduce output &lt;span class=&quot;nv&quot;&gt;records&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;2&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;MapReduce framework decide number of splits over the input data based on the split_size, determined as &lt;em&gt;split_size = max(minimumSize, min(maximumSize, blockSize))&lt;/em&gt;. Additionally, it naturally “splits” the separate files. We took advantage of the latter to force 4 splits, which produced 8 map output records (4 of each partially summed matricies A and B). Then framework shuffled and sorted map output records and reduced by key into 2 output records (fully summed A and B). That is the output that we save on HDFS /mapreduce_output.&lt;/p&gt;

&lt;p&gt;Now we can cat resulted matricies in HDFS and calculate thetas what we were looking for:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-cat&lt;/span&gt; /user/root/mapreduce_output/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; | /nosql/hadoop/lwr_mapreduce_find_theta.py&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Which again produces the correct hypothesis θ:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; 26.89559418]
 &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.58025811]
 &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-3&lt;/span&gt;.96902175]
 &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.40520663]]&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h4 id=&quot;-visual-demo-of-lwr-prediction&quot;&gt;&lt;a id=&quot;demo&quot;&gt;&lt;/a&gt; Visual Demo of LWR Prediction&lt;/h4&gt;
&lt;p&gt;This tutorial comes with a demo. Run the following script to visually confirm LWR prediction, which takes into account the locality of the x_query (unlike simple linear regression):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-cat&lt;/span&gt; /user/root/mapreduce_output/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; | /nosql/hadoop/lwr_plot_x_query.py&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This should produce a picture like that:
&lt;a href=&quot;//alexsalo.github.io/images/lwr_demo.png&quot;&gt;&lt;img src=&quot;//alexsalo.github.io/images/lwr_demo.png&quot; alt=&quot;Demo&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To finish up - shut down the daemons:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hadoop-2.7.2/sbin/stop-dfs.sh&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h5 id=&quot;a-note-on-scalability&quot;&gt;A Note on Scalability&lt;/h5&gt;
&lt;p&gt;Provided algorithm scales efficiently as the data volume growth. With N nodes in the system, the HDFS distributes the large file over all these N nodes. When you start a job, there will be N mappers by default (in our case of small data split into four files there are four mappers by default). Hadoop makes sure the mapper on a machine will process the part of the data that is stored on this node, which is called Rack awareness.&lt;/p&gt;

&lt;h5 id=&quot;a-note-on-matrix-io&quot;&gt;A Note on Matrix IO&lt;/h5&gt;
&lt;p&gt;We implemented custom serializer/parser to communicate matricies between mapper and reducer. As it turned out, the float precision is of crucial importance for that matter. For a production environment one may want to increase precision of the floating point (currently 8 digits after the point) in &lt;em&gt;np_matric_helpers&lt;/em&gt;.&lt;/p&gt;

&lt;h3 id=&quot;-spark&quot;&gt;&lt;a id=&quot;nosql-spark&quot;&gt;&lt;/a&gt; Spark&lt;/h3&gt;
&lt;p&gt;While MapReduce provides a fine grain way to write our own mappers and reducers for the parallel run, the developer has to make a lot of strategic decisions on how exactly to separate logic load between mapper and reducer executable and how to communicate between them. Additionally, the output of the reducers is a set of &amp;lt;key, value&amp;gt; pairs that have to be evaluated in the separate program. It is neither good or bad, but definitely requires more effort and skill than when the developer uses our next tool - Spark.&lt;/p&gt;

&lt;p&gt;Spark revolves around the concept of a resilient distributed dataset (RDD), which is a fault-tolerant collection of elements that can be operated on in parallel. For this tutorial we need HDFS to be already set and running; HDFS should contain /user/root/input folder with data files. Please refer to the README in /nosql/hadoop folder on how to set it up.&lt;/p&gt;

&lt;p&gt;All the following commands are issued from the &lt;em&gt;spark&lt;/em&gt; working directory on the virtual machine under root privileges:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;su           &lt;span class=&quot;c&quot;&gt;# password: bigdata&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /nosql/spark/  &lt;span class=&quot;c&quot;&gt;# which has spark installation spark-1.6.1-bin-without-hadoop/&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Then start Spark cluster (then check http://localhost:8080/):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;/nosql/spark/spark-1.6.1-bin-without-hadoop/sbin/start-all.sh&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;With Spark, we can access files in both local FS and the HDFS:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;pyspark
&lt;span class=&quot;c&quot;&gt;# Open Local File in FS&lt;/span&gt;
localFile &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; sc.textFile&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;file:///nosql/input/x_data.txt&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
localFile.count&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 1372&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Open file in HDFS:&lt;/span&gt;
hdfsFile &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; sc.textFile&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;/user/root/input/*.txt&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
hdfsFile.count&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 5488&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The major benefit of Spark over plain MapReduce is that it provides an consolidated API in Java, Scala or Python. From the &lt;a href=&quot;http://spark.apache.org/docs/latest/programming-guide.html&quot;&gt;Spark Programming Guide&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;For example, map is a transformation that passes each dataset element through a function and returns a new RDD representing the results. On the other hand, reduce is an action that aggregates all the elements of the RDD using some function and returns the final result to the driver program.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That means we still use the same idea of mapper and reducer here. However the benefit is, to quote the official site again:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;All transformations in Spark are lazy, in that they do not compute their results right away. Instead, they just remember the transformations applied to some base dataset (e.g. a file). The transformations are only computed when an action requires a result to be returned to the driver program. This design enables Spark to run more efficiently – for example, we can realize that &lt;em&gt;a dataset created through map will be used in a reduce and return only the result of the reduce to the driver, rather than the larger mapped dataset&lt;/em&gt; (Italics are mine).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So what we have to change in our LWR scripts? Not much. Basically, we simply make a functions out of the mapper and reducer and pass into Spark to run. Below is a simplified implementation (full code available at &lt;a href=&quot;https://github.com/alexsalo/nosql-locally-weighted-regression&quot;&gt;project’s github&lt;/a&gt;):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;sc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SparkContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;local&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;spark app&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;textFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;/user/root/input/x_data*.txt&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mapAB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gauss_kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):...&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# parse values assuming x1 x2 x3 ... y
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;,&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])])&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gauss_kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;b&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Run LWR
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ab&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flatMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mapAb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# transformation, maps input into list of tuples (K, V)
&lt;/span&gt;         &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduceByKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# transformation, sorts and reduces the list
&lt;/span&gt;         &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# action, forces execution of all *lazy* transformations
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# first element of the list, second element in the tuple
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# second element of the list, second element in the tuple
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;I&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# we found our LWR hypothesis!
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Save result into HDFS
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parallelize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;saveAsTextFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;/user/root/spark_output&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Note that we didn’t even have to write a custom reducer since all it does is it sums the Numpy matricies - easily done with simple lambda expression. To execute the full script:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;spark-submit &lt;span class=&quot;nt&quot;&gt;--master&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;4] /nosql/spark/lwr_spark.py&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Let us check what we’ve got and compare with the MapReduce output:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;c&quot;&gt;# Show hypothesis produced by Hadoop MapReduce&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-cat&lt;/span&gt; /user/root/mapreduce_output/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; | /nosql/hadoop/lwr_mapreduce_find_theta.py
&lt;span class=&quot;c&quot;&gt;# [[ 26.89715476]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#  [ -0.57990451]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#  [ -3.96889352]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#  [ -0.40332286]]&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Show hypothesis produced by Spark&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-cat&lt;/span&gt; /user/root/spark_output/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# [[ 26.88083126]]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# [[-0.57996902]]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# [[-3.96718554]]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# [[-0.40413609]]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Thus we obtained the same results. Note, however, that in Spark we pass mapper and reducer as a function (or lambda expression) within single python script and thus able to find hypothesis theta right away, while in MapReduce we have to read the output of the reducers and apply our &lt;em&gt;find_theta&lt;/em&gt; routine.&lt;/p&gt;

&lt;p&gt;Another benefit (probably more important one) is in &lt;strong&gt;native support&lt;/strong&gt; of python in Spark, which means that we can operate over language objects rather than pure strings. In our case, in MapReduce we had to communicate our partial matricies via standard input/output by converting and parsing np.maprix class into the string, which incurred additional time consumed and created a space for to err. In Spark, however, we don’t have to do that anymore: we simply pass objects of np.maprix class from one RDD to another, and that is truly advantageous.&lt;/p&gt;

&lt;h5 id=&quot;spark-summary&quot;&gt;Spark Summary&lt;/h5&gt;
&lt;p&gt;Spark proven to be more flexible tool for executing functions on the distributed data. By handling the intermediate data management Spark is able to take advantage of various optimization techniques (lazy transformations, early projections etc), as well as to provide a more user friendly consolidated interface for programming in the language of choice.&lt;/p&gt;

&lt;h3 id=&quot;-hive&quot;&gt;&lt;a id=&quot;nosql-hive&quot;&gt;&lt;/a&gt; Hive&lt;/h3&gt;
&lt;p&gt;Recall that HIVE provides data warehousing facilities on top of an existing Hadoop cluster. We can create tables in Hive and store and data there. Hive also provide HQL, very familiar to SQL in syntax, to query our data.&lt;/p&gt;

&lt;p&gt;To begin with, we need to create some service directories with privileges in HDFS for Hive to use:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-mkdir&lt;/span&gt;     /tmp
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-chmod&lt;/span&gt; g+w /tmp
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-mkdir&lt;/span&gt;     /user/hive
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-mkdir&lt;/span&gt;     /user/hive/warehouse
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-chmod&lt;/span&gt; g+w /user/hive/warehouse  &lt;span class=&quot;c&quot;&gt;# give privileges to hive&apos;s warehouse directory&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As usual, all the following commands are issued from the &lt;em&gt;hive&lt;/em&gt; working directory on the virtual machine under root privileges:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;su           &lt;span class=&quot;c&quot;&gt;# password: bigdata&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /nosql/hive/   &lt;span class=&quot;c&quot;&gt;# which has spark installation apache-hive-2.0.0-bin/&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Hive stores its metadata in a RDBMS of choice (derby&lt;/td&gt;
      &lt;td&gt;mysql&lt;/td&gt;
      &lt;td&gt;postgres&lt;/td&gt;
      &lt;td&gt;oracle). Thus we need to remove previous (meta) data store and init a new one (with provided ‘derby’ engine):&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;apache-hive-2.0.0-bin/
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-Rf&lt;/span&gt; metastore_db
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;schematool &lt;span class=&quot;nt&quot;&gt;-initSchema&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-dbType&lt;/span&gt; derby

&lt;span class=&quot;c&quot;&gt;# (Optional) in case of abnormal exit (i.e. Ctrl+Z) you will see a bunch of exceptions,&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# so you can try simply removing the lock.&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;rm &lt;/span&gt;metastore_db/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;.lck  &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now we can create a table (stored in HDFS) in Hive that will suit our data_x.txt file. Note however, that the default field terminator in Hive is &lt;strong&gt;\t&lt;/strong&gt;, but our file is &lt;strong&gt;’,’&lt;/strong&gt; delimited. Thus we need to explicitly put statement to use a different field separator:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;beeline &lt;span class=&quot;nt&quot;&gt;-u&lt;/span&gt; jdbc:hive2://  &lt;span class=&quot;c&quot;&gt;# connect to Hive&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# 0: jdbc:hive2://&amp;gt;&lt;/span&gt;
CREATE TABLE lwr_data &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;x1 DOUBLE, x2 DOUBLE, x3 DOUBLE, x4 DOUBLE&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  ROW FORMAT DELIMITED FIELDS TERMINATED BY &lt;span class=&quot;s1&quot;&gt;&apos;,&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
SHOW TABLES&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# +-----------+--+&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# | tab_name  |&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# +-----------+--+&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# | lwr_data  |&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# +-----------+--+&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now we can load the data into the table from either local store or from HDFS file. Let us do HDFS. As noted in the documentation:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;Note that loading data from HDFS will result in &lt;strong&gt;moving the file/directory&lt;/strong&gt;. As a result, the operation is &lt;em&gt;almost instantaneous&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thus to load, we first need to create a new redundant copies of input files:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;c&quot;&gt;# In a new terminal:&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;su
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-mkdir&lt;/span&gt; /user/hive/input  &lt;span class=&quot;c&quot;&gt;# make input dir for hive&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# copy files from local file system&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-put&lt;/span&gt; /nosql/input/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; /user/hive/input  &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And then load them (or, rather, move and apply schema) into the DBMS (note that we can apply basic filter to select &lt;em&gt;all the files&lt;/em&gt; in the input directory):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;LOAD DATA INPATH &lt;span class=&quot;s1&quot;&gt;&apos;/user/hive/input/*&apos;&lt;/span&gt; INTO TABLE lwr_data&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Try some SQL-like query&lt;/span&gt;
SELECT &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; FROM lwr_data WHERE x1 &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 4 AND x2 &amp;lt; &lt;span class=&quot;nt&quot;&gt;-3&lt;/span&gt; LIMIT 3&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# +--------------+--------------+--------------+--------------+--+&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# | lwr_data.x1  | lwr_data.x2  | lwr_data.x3  | lwr_data.x4  |&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# +--------------+--------------+--------------+--------------+--+&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# | 4.6765       | -3.3895      | 3.4896       | 1.4771       |&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# | 4.8906       | -3.3584      | 3.4202       | 1.0905       |&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# | 4.3239       | -4.8835      | 3.4356       | -0.5776      |&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# +--------------+--------------+--------------+--------------+--+&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 3 rows selected (0.19 seconds)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;After LOAD command you can confirm that the files are all gone from /user/hive/input directory. We can also confirm that all data rows are stored by running HQL:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;SELECT COUNT&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; from lwr_data&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# +-------+--+&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# |  c0   |&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# +-------+--+&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# | 5488  |&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# +-------+--+&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Interestingly enough, initially Hive was supposed to run all the selection queries as a MapReduce jobs under the hood. Thus when do &lt;em&gt;SELECT COUNT(*)&lt;/em&gt; we see the MapReduce output information, along with the following warning:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. tez, spark) or using Hive 1.X releases.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Since running on Spark execution engine should not affect out implementation, let us create an HQL user defined function (UDF), which will be analogous to our mapper to produce intermediate results (matricies A and B). First, and this already feels like ad-hoc, we probably need to create an intermediate table to store the mapper’s output:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;CREATE TABLE ab_mat &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;a STRING, b STRING&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now let us take a look at the simplified code of the Hive mapper (full code available at &lt;a href=&quot;https://github.com/alexsalo/nosql-locally-weighted-regression&quot;&gt;project’s github&lt;/a&gt;):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gauss_kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mat2str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# parse values assuming x1 x2 x3 ... y     
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# (!) change to internal Hive&apos;s delimiter
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])])&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gauss_kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# emit partial resulting martricies A and B into STDOUT
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;%s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;%s&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mat2str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mat2str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Note that it basically the same as a mapper for MapReduce (converts rows into A and B matricies), except it has different delimiter for fields.&lt;/p&gt;

&lt;p&gt;Now add that python mapper into Hive context:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;add FILE /nosql/hive/lwr_hive_mapper.py&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# replaces old version&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Added resources: [/nosql/hive/hive_mapper.py]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Then, we can query our original data (with any WHERE or other clauses we want - but we won’t do that so we could compare the results), transform it using our mapper, and load into newly create intermediate table:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;INSERT OVERWRITE TABLE ab_mat
  SELECT TRANSFORM &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;x1, x2, x3, x4&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  USING &lt;span class=&quot;s1&quot;&gt;&apos;python lwr_hive_mapper.py&apos;&lt;/span&gt;
  AS &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;a, b&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  FROM lwr_data&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Finally, we can select all A and B matricies and pass them to another mapper (lwr_hive_find_theta.py) that will compute hypothesis theta.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;add FILE /nosql/hive/lwr_hive_find_theta.py&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
SELECT
  TRANSFORM &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;a, b&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  USING &lt;span class=&quot;s1&quot;&gt;&apos;python lwr_hive_find_theta.py&apos;&lt;/span&gt;
  AS &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;h1, h2, h3, h4&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
FROM ab_mat&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# +--------------+------------------+-----------------+------------------+--+&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# |      h1      |        h2        |       h3        |        h4        |&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# +--------------+------------------+-----------------+------------------+--+&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# | 26.87052487  | -0.579815330225  | -3.96594330195  | -0.403622196259  |&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# +--------------+------------------+-----------------+------------------+--+&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 1 row selected (1.32 seconds)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Thus we arrived at the same results as when using MapReduce or Spark, but this time using HQL as a tool for processing data stored in Hive warehouse on top of HDFS.&lt;/p&gt;

&lt;p&gt;Last thing: quit Hive2 (beeline) correctly to avoid problems with the metastore:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;quit&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h5 id=&quot;hive-summary&quot;&gt;Hive Summary&lt;/h5&gt;
&lt;p&gt;Hive proven to be easy-to-use and SQL-like tool for dealing with distributed data on HDFS. It is probably advantageous when the users are very confident SQL masters but poor programmers. However, for problems like LWR in Hive one needs a &lt;em&gt;custom (user defined) queries (UDF)&lt;/em&gt;. But in general, since we assume our users are not good programmers, the benefit of Hive become less apparent.&lt;/p&gt;

&lt;p&gt;To summarize, HIVE is a good tool with limited applications. On the one hand, we need to design and create tables to store the data, then intermediate tables to fit intermediate results - extra work; on the other hand, we can easily add arbitrary WHERE (or other) clauses when querying such data. We can conclude, that HIVE is indeed a data warehouse - if you care about the data a lot and want to accumulate it in structured manner (yet reaping the benefits of the distributed fault-tolerant HDFS) - HIVE maybe a good choice, but if you want to get a single answer over all the different unstructured data - HIVE would be an overkill.&lt;/p&gt;

&lt;h3 id=&quot;-sparksql&quot;&gt;&lt;a id=&quot;nosql-sparksql&quot;&gt;&lt;/a&gt; SparkSQL&lt;/h3&gt;
&lt;p&gt;When working with HIVE we liked the universality of HQL, which allowed us familiar querying with different clauses, but we didn’t like the fact that we had to take infrastructural care of the intermediate steps during LWR computation. To solve that issue we now turn to Spark SQL. To quote their web site:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;Seamlessly mix SQL queries with Spark programs. Spark SQL lets you query structured data inside Spark programs, using either SQL or a familiar DataFrame API. Usable in Java, Scala, Python and R.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;DataFrames and SQL provide a common way to access a variety of data sources, including Hive, Avro, Parquet, ORC, JSON, and JDBC. You can even join data across these sources.
Spark SQL reuses the Hive frontend and metastore, giving you full compatibility with existing Hive data, queries, and UDFs. Simply install it alongside Hive.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Thus we can use Spark SQL framework to use and &lt;strong&gt;query either plain files on HDFS or Hive warehouse&lt;/strong&gt; using familiar DataFrame API, and &lt;strong&gt;do it in Python&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Working with HIVE is rather straightforward, but requires to build Spark with related flags (Hive support leads to increase in the number of external dependencies). Thus let’s test yet another feature of SparkSQL: inferring JSON format by reflection and automatic conversion of JSON to an SQL-like table. Then we can query that table and calculate LWR. That sounds like a  reasonable idea - suppose we receive data from our business client in JSON format every night.&lt;/p&gt;

&lt;p&gt;As usual, all the following commands are issued from the &lt;em&gt;sparksql&lt;/em&gt; working directory on the virtual machine under root privileges:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;su               &lt;span class=&quot;c&quot;&gt;# password: bigdata&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /nosql/sparksql/   &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;First, let’s generate some JSON data. Just feed data_x.txt to our conversion script:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /nosql/input/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; | /nosql/sparksql/csv-to-json.py&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now put generated JSON into HDFS:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-put&lt;/span&gt; /nosql/sparksql/data_x.json /user/root/&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We can now register that file as if a table to query as we like:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;pyspark
lwr_data &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; sqlContext.read.json&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;/user/root/data_x.json&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
lwr_data.registerTempTable&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;lwr_data&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

sqlContext.sql&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;SELECT * FROM lwr_data WHERE x1 &amp;gt; 4 AND x2 &amp;lt; -3 LIMIT 3&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;.show&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
+------+-------+-------+-------+
|    x0|     x1|     x2|     x3|
+------+-------+-------+-------+
|3.4566| 9.5228|-4.0112|-3.5944|
|4.3684| 9.6718|-3.9606|-3.1625|
|5.2423|11.0272| &lt;span class=&quot;nt&quot;&gt;-4&lt;/span&gt;.353|-4.1013|
+------+-------+-------+-------+&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Thus we get the same functionality as with HIVE yet we don’t have to describe the structure - it is inferred automatically! Better yet, the result of any query is, in fact, RDD, which supports all the normal RDD operations. Thus we can seamlessly combine the syntax of the two:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nb&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; sqlContext.sql&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;SELECT * FROM lwr_data WHERE x1 &amp;gt; 4 AND x2 &amp;lt; -3&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
df.count&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 528&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Try arbitrary map and reduce over our DataFrame obtained from temp table over JSON:&lt;/span&gt;
df.map&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;lambda row: abs&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;row.x1 + row.x2 - row.x3&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;.reduce&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;lambda a, b: a + b&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 4646.904679999999&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This is very elegant and powerful. Also note that we can use up-down keystokes to repeat recent operations (which you can’t do in HIVE).&lt;/p&gt;

&lt;p&gt;Implementation of our LWR is rather straightforward - all we need to do is to adjust our MapAB function to work with rows rather than the string line. Below is simplified code (full code available at &lt;a href=&quot;https://github.com/alexsalo/nosql-locally-weighted-regression&quot;&gt;project’s github&lt;/a&gt;):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;sc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SparkContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;local&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;spark sql app&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sqlContext&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SQLContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sqlContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;/user/root/data_x.json&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;registerTempTable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;lwr_data&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mapAB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gauss_kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):...&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# parse values from the row assuming x1 x2 x3 ... y
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;x%s&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;xrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;x%s&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]])&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gauss_kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;b&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Run LWR
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ab&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flatMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mapAb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# transformation, maps input into list of tuples (K, V)
&lt;/span&gt;         &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduceByKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# transformation, sorts and reduces the list
&lt;/span&gt;         &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# action, forces execution of all *lazy* transformations
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# first element of the list, second element in the tuple
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# second element of the list, second element in the tuple
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;I&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# we found our LWR hypothesis!
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Save result into HDFS
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parallelize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;saveAsTextFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;/user/root/sparksql_output&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To execute the full script:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;spark-submit &lt;span class=&quot;nt&quot;&gt;--master&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;4] /nosql/sparksql/lwr_sparksql.py&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Check the correctness of the hypothesis produced by SparkSQL:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hdfs dfs &lt;span class=&quot;nt&quot;&gt;-cat&lt;/span&gt; /user/root/sparksql_output/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# [[ 26.88083126]]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# [[-0.57996902]]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# [[-3.96718554]]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# [[-0.40413609]]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h5 id=&quot;sparksql-summary&quot;&gt;SparkSQL Summary&lt;/h5&gt;
&lt;p&gt;While JSON reflection is super useful it doesn’t appear to be robust yet (I failed to parse my Google takeout data). In spite of that, SparkSQL proven to be an extremely nice and easy to use tool. While calculating LWR in SQL worlds appears to be a bad idea (since the whole point of SQL is to allow to run ‘mainstream’ queries on data, while LWR requires us to write a custom script) SparkSQL allows us to soften the edge here by fusing the SQL like functionality with robust RDD features and convenient DataFrame API.&lt;/p&gt;

&lt;h3 id=&quot;bibliography&quot;&gt;Bibliography&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;[1] Chu, Cheng, Sang Kyun Kim, Yi-An Lin, YuanYuan Yu, Gary Bradski, Andrew Y. Ng, and Kunle Olukotun. “Map-reduce for machine learning on multicore.” Advances in neural information processing systems 19 (2007): 281.&lt;/li&gt;
  &lt;li&gt;[2] &lt;a href=&quot;http://cs229.stanford.edu/notes/cs229-notes1.pdf&quot;&gt;Andrew Ng Machine Learning Class Notes at Stanford&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;[3] Atkeson, Christopher G., Andrew W. Moore, and Stefan Schaal. “Locally weighted learning for control.” In Lazy learning, pp. 75-113. Springer Netherlands, 1997.&lt;/li&gt;
&lt;/ul&gt;

    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/nosql-locally-weighted-regression-mapreduce-spark-hive/&quot;&gt;Locally Weighted Linear Regression in NoSQL world MapReduce, Spark, Hive and Spark SQL implementations over Hadoop HDFS&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on April 27, 2016.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[Data Interpretation and Learning on Heterogeneous Behavioral Data Sets]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/master-project-writeup/" />
  <id>//alexsalo.github.io/master-project-writeup</id>
  <published>2016-04-13T00:00:00-04:00</published>
  <updated>2016-04-13T00:00:00-04:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;Below is my first real paper in English: a master’s project write-up at ECS, Baylor University. Acedemic writing, surprisingly, proved to be easier (for non-native) than publicistic style: we want to use short, precise and concise words (which are easier to know since they mostly universal due to the Latin roots) with simple sentence structure, and generally avoid very long complicated sentences like this one, which is so prone to ample mistakes. Anyways, here is an abstract for the PDF attached (click on the image):&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/papers/master_project_writeup.pdf&quot;&gt;
&lt;img border=&quot;0&quot; alt=&quot;Masters Project Writeup&quot; src=&quot;/papers/preview/master-project-1.png&quot; width=&quot;260&quot; align=&quot;right&quot; /&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The Monkey Alcohol and Tissue Research Resource (MATRR) is a rich data repository containing heterogeneous experimental data derived from well-documented non-human primate alcohol self-administration studies. This macaque model has demonstrated categorical drinking norms reflective of human drinking populations. By thorough analysis of the available data we are able to better understand the causative agents, progression and features of alcoholism.&lt;/p&gt;

  &lt;p&gt;This project presents investigation results separated into two sections: the first describes the rapid discovery and testing of new hypothesis; the second describes the detailed study of predicting future levels of alcoholism based on early (induction) drinking and behavioral data using machine learning techniques. Each study takes advantage of a different set of tools and traces their effectiveness, validity and limitations when describing data in meaningful visual form. We also explore the assignment and cross-examination of data from different sources, and how they aid in generating hypothesis and observing gender and drinking category differences.&lt;/p&gt;
&lt;/blockquote&gt;


    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/master-project-writeup/&quot;&gt;Data Interpretation and Learning on Heterogeneous Behavioral Data Sets&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on April 13, 2016.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[How To Print Everything From Django Query via Pandas?]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/django-pandas-print-all/" />
  <id>//alexsalo.github.io/django-pandas-print-all</id>
  <published>2016-03-23T00:00:00-04:00</published>
  <updated>2016-03-23T00:00:00-04:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;Most often use case when working with the data via Django ORM is simly run some query and print out everything from the output. Doing so directly renders unreadable one-liner:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MonkeyBEC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objects&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;monkey_id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# [(25256, 10138, None, 62691, datetime.datetime(2011, 3, 29, 0, 0), 
# None, None, None, datetime.time(5, 36), None, None, 51.3, 0.28, 
# 0.3, 74.0, None, 0.933333333333333, False), 
# (25259, 10139, None, 62694, datetime.datetime(2011, 3, 29, 0, 0), 
# None, None, None, datetime.time(5, 51), None, None, 22.8, 0.14, 
# 0.2, 0.0, None, 0.7, False), &apos;...(remaining elements truncated)...&apos;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To make this readable, we can simply pass it to the Pandas engine, wrapping query output into list:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt; 
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MonkeyBEC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objects&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;monkey_id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#         0      1     2      3          4     5     6     7         8     9   \
# 0    25256  10138  None  62691 2011-03-29  None  None  None  05:36:00  None   
# 1    25259  10139  None  62694 2011-03-29  None  None  None  05:51:00  None   
# 2    25265  10136  None  62749 2011-04-06  None  None  None  05:47:00  None   
# 3    25271  10135  None  62783 2011-04-11  None  None  None  05:28:00  None   
# 4    25273  10139  None  62785 2011-04-11  None  None  None  05:36:00  None   
# 5    25277  10138  None  62810 2011-04-15  None  None  None  05:47:00  None   
# 6    25283  10141  None  62816 2011-04-15  None  None  None  05:54:00  None  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Cool! But what about the column names? 1,2,3… is not very informative. But hey, this is python, so we can fix it easily:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt; 
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MonkeyBEC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objects&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;monkey_id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()),&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;.&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MonkeyBEC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_meta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#      bec_id  monkey   dto    mtd bec_collect_date bec_run_date bec_exper  \
# 0     25256   10138  None  62691       2011-03-29         None      None   
# 1     25259   10139  None  62694       2011-03-29         None      None   
# 2     25265   10136  None  62749       2011-04-06         None      None   
# 3     25271   10135  None  62783       2011-04-11         None      None   
# 4     25273   10139  None  62785       2011-04-11         None      None   
# 5     25277   10138  None  62810       2011-04-15         None      None   
# 6     25283   10141  None  62816       2011-04-15         None      None   &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Much better! Each model has modelName._meta.fields which returns list of fields in the same order as values_list(). Those fields however are in app.model.field format, hence splitting by dot and getting the last item.&lt;/p&gt;

&lt;p&gt;And you can also just put a static method that prints the entire content without filtering:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt; 
&lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;staticmethod&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;content_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MonkeyBEC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objects&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()),&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;.&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MonkeyBEC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_meta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Now this does the job:
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MonkeyBEC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/django-pandas-print-all/&quot;&gt;How To Print Everything From Django Query via Pandas?&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on March 23, 2016.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[Everyday Design&#58; Microwave]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/everyday-design-microwave/" />
  <id>//alexsalo.github.io/everyday-design-microwave</id>
  <published>2016-03-23T00:00:00-04:00</published>
  <updated>2016-03-23T00:00:00-04:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;When I earn enough money I plan to embark on the journey of designing better things. It is mysteriously surprising why everyday things around us are designed, often, so poorly. Stave Jobs was obsessed with usability, simplicity and cleanness of the design in his products. Google has been producing top notch design for its products recently. Why then manufacturers of the everyday things do not seem to care about that at all? As an example, let’s consider the microwave from my apartment:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/images/design/microwave-bad-design.jpg&quot;&gt;&lt;img src=&quot;//alexsalo.github.io/images/design/microwave-bad-design.jpg&quot; alt=&quot;Bad Design Microwave&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looks typical? Sadly, yes. Here is why this design is sad:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Running line on the display is ridiculously annoying. In general, running lines should be avoided everywhere because they distract us, and it is hard to read the information anyways. You can always shorten the words or better not use them at all: make it self evident. In this case, microwave provides very valueable info: &lt;strong&gt;&lt;em&gt;touch start to…&lt;/em&gt;&lt;/strong&gt; In general, we always try to desing buttons in such a way that it would be obvious to anyone that they should be pressed or touched. In this case buttons look ok, so no need for redundant info, which by the way never goes away, it &lt;strong&gt;keeps running until the next use&lt;/strong&gt;, which distracts me even from my bed in another room if the door is not completely shut. Thus this microwave shows redundant info 90% of the time, rather than showing oh, well, time?&lt;/li&gt;
  &lt;li&gt;According to my observations, 95% of the time people don’t want from the microwave anything fancy. They just want to start it, say for 30 seconds. How to start? In my previous apartment that would happen when you press green colored “start” button. It took us almost a year to realize that this microwave has a &lt;strong&gt;&lt;em&gt;jet start&lt;/em&gt;&lt;/strong&gt; button which does exactly that. Why designating a separate button to something that could be done by another button?&lt;/li&gt;
  &lt;li&gt;Actually, this argument extends to &lt;strong&gt;all the buttons&lt;/strong&gt;. Why we need so many of them? What are the core functionality of the device? It’s &lt;strong&gt;&lt;em&gt;to heat up&lt;/em&gt;&lt;/strong&gt;. By far, most of the times we simply need to start/stop the microwave, but we have this abundance of buttons of &lt;strong&gt;equal size&lt;/strong&gt; which initate actions of different importance.&lt;/li&gt;
  &lt;li&gt;Nine buttons are used as a keypad for setting time of heating. How many of us need it very precies? Do we care if its 40 or 42 seconds? No. Thus keypad is a waste, simple control knob will do the job much better.&lt;/li&gt;
  &lt;li&gt;The last one is arguably the most annoying feature ever: once time is up, the microwave will start triple beep once in 30 seconds. It will continue to do it… forever, I kid you not! Nothing should go on forever, why not make a switch back to normal after several minutes?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Based on all the observations above I made a sketch of my microwave prototype. I’d call it ‘Simple’ or ‘Go’:
&lt;a href=&quot;//alexsalo.github.io/images/design/microwave_simple_go.png&quot;&gt;&lt;img src=&quot;//alexsalo.github.io/images/design/microwave_simple_go.png&quot; alt=&quot;Simple Design Microwave&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Several important details which are not clear from the picture:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Diplay always shows 4 digits in ‘mm:ss’ format.&lt;/li&gt;
  &lt;li&gt;Simple control knob is used to change the numbers; pressing it starts the device, if idle or stops, if active.&lt;/li&gt;
  &lt;li&gt;Secondary switches are small and located separately. Simple button + indicator (on/off) could be used to emulate physical switch.&lt;/li&gt;
  &lt;li&gt;Set time button temporarily (5 sec?) allows to use the control knob to adjust time.&lt;/li&gt;
  &lt;li&gt;Once inactive (1 min timout?) display starts displaying clock time instead of cooking time.&lt;/li&gt;
  &lt;li&gt;Cooking time is never reset, we just use control knob to add or substract time.&lt;/li&gt;
  &lt;li&gt;This control knob should be smart too: the faster and longer you spin - the more are the chunks of the time increase.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thus by determining and separating the core functionality of the microwave and by optimizing the control tools we created intuitive and easy-to-use device.&lt;/p&gt;


    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/everyday-design-microwave/&quot;&gt;Everyday Design&amp;#58; Microwave&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on March 23, 2016.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[Weak Pass And Poor Checkbox Use]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/passwords-and-checkboxes/" />
  <id>//alexsalo.github.io/passwords-and-checkboxes</id>
  <published>2016-03-14T00:00:00-04:00</published>
  <updated>2016-03-14T00:00:00-04:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;Making the first impression is very important for your service, and login/register form is typically the first thing user will see, which renders it important to invest and design it right.&lt;/p&gt;

&lt;p&gt;There are abundance of utterly horrible designs out there, but today,  I want to criticize a relatively good one:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/images/design/elo-bad-design.png&quot;&gt;&lt;img src=&quot;//alexsalo.github.io/images/design/elo-bad-design.png&quot; alt=&quot;Change Password Form Design&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First thing first: 10 chars is &lt;strong&gt;not enough&lt;/strong&gt; for the password if you &lt;strong&gt;really care&lt;/strong&gt;, and if you don’t - why bother with special chars? Oops, I misread the info: the length is actually 8 chars, and must contain at least one letter and one number. Here is the clue: it should be nearly impossible to misread your design - be clear and precise, test it on people! Now, 8 chars is not very secure at all - breakable by brute force. Yet they ask for at least one number (normally other services in USA ask for other garbage as well). As every school kid knows: adding a several char options into domain space do &lt;strong&gt;not increase the secureness&lt;/strong&gt; of the password - machine really doesn’t care which chars you use.&lt;/p&gt;

&lt;p&gt;So far we have a weak password with the number which makes it harder to remember (than secure long paraphrase).&lt;/p&gt;

&lt;p&gt;Do three checkbox requirements look like you could/should click on them? To me - absolutely. But they are not. Why would you do that to your valuable user?&lt;/p&gt;

&lt;p&gt;Now why you want me to confirm my new password? Since long we agreed that the chance of such event (you mistype your password) are relatively low, hence you should be trusted with one attempt only, and in case of unlikely event - you just rest the pass again - sure you can since you got here in the first place.&lt;/p&gt;

&lt;p&gt;Again, this design is very nice compare to what is typically out there, yet it could be improved. Please design nice things!&lt;/p&gt;

    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/passwords-and-checkboxes/&quot;&gt;Weak Pass And Poor Checkbox Use&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on March 14, 2016.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[Keeping Your Email Address Safe]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/keeping-your-email-address-safe/" />
  <id>//alexsalo.github.io/keeping-your-email-address-safe</id>
  <published>2016-02-24T00:00:00-05:00</published>
  <updated>2016-02-24T00:00:00-05:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;How do spammers learn our email addresses? Well, it’s possible to buy them (in huge bunches, of course) from various sellers. Seller could be not-so-honest-web-site, that just sells its customer base, or someone, who crawled a part of the web (in which you are present) and &lt;strong&gt;parsed&lt;/strong&gt; your email address.&lt;/p&gt;

&lt;p&gt;Anytime, we put our email address or cellphone number on the static web page - we just leave it out there to be parsed and sold to the spammers. Below are some more or less efficient ways to display yet not compromise your email address or cellphone number. Then, we’ll try to outsmart that protection.&lt;/p&gt;

&lt;p&gt;By the way, have you noticed that in AirBnB you can’t send you cell number to the potential host? Otherwise, you could have skipped the middleman (airbnb) and arranged the bed with the owner directly. AirBnB doesn’t want that and detects any of your attempts to send a number or email address via direct messaging (before you paid for the night). My ‘nine-zero-eight-…’ was not sophisticated enough to trick the system when were stuck in Paris…&lt;/p&gt;

&lt;h3 id=&quot;concealing-email-address-from-parsing&quot;&gt;Concealing Email Address from Parsing&lt;/h3&gt;
&lt;p&gt;Here are some common methods to conceal email address:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Using uppercase: &lt;strong&gt;name@mail.COM&lt;/strong&gt; - pretty inefficient and ugly looking&lt;/li&gt;
  &lt;li&gt;Insert some repeated symbol: &lt;strong&gt;a-l-e-x-@-m-a-i-l-.-c-o-m&lt;/strong&gt; - ugly&lt;/li&gt;
  &lt;li&gt;Playing with ‘@’ symbol:
    &lt;ul&gt;
      &lt;li&gt;substitute as at or (at): &lt;strong&gt;name(at)mail.com&lt;/strong&gt; - kind of easy, though looks ok&lt;/li&gt;
      &lt;li&gt;use Unicode sequence &lt;strong&gt;|&amp;amp; #x40 ;&lt;/strong&gt; which renders @ - efficient and cool&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Playing with ‘.’ symbol:
    &lt;ul&gt;
      &lt;li&gt;dot, dom, dt: &lt;strong&gt;name@mail dt com&lt;/strong&gt; - pretty lame though efficient if used not trivially&lt;/li&gt;
      &lt;li&gt;using other special char: &lt;strong&gt;name@ecs;stanford;com&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;simply space: &lt;strong&gt;name @ ecs mail com&lt;/strong&gt; - this is surprisingly tricky to parse, but easy to understand&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Use word construct: &lt;strong&gt;teresa (followed by “@stanford.edu”)&lt;/strong&gt; - hard to parse&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are the common tricks, out of which I’d recommend to go with simple spaces with changing @:
&lt;strong&gt;alex at cs baylor edu&lt;/strong&gt;&lt;/p&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
function obfuscate( domain, name ) { document.write(&apos;&lt;a href=&quot;mai&apos; +
&apos;lto:&apos; + name + &apos;@&apos; + domain + &apos;&quot;&gt;&apos; + name + &apos;@&apos; + domain + &apos;&lt;/&apos; + &apos;a&gt;&apos;); }
&lt;/script&gt;

&lt;p&gt;Now, all of the above still sort of parsable. To make it &lt;strong&gt;really&lt;/strong&gt; hard, we can write small js script to print the email address: obfuscate(name, domain) in html -&amp;gt; renders name@domain.com in the browser. Since parser works with html it would not have pieces together. Try wget this page and parse this address: &lt;script&gt; obfuscate(&apos;mail.edu&apos;,&apos;alex&apos;); &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Here is the script, which was copied from Dan Jurafsky Stanford’s web page:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;script&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;text/javascript&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obfuscate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;domain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;lt;a href=&quot;mai&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;lto:&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;domain&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&quot;&amp;gt;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;domain&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;a&amp;gt;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;sr&quot;&gt;/script&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obfuscate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;mail.edu&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;alex&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;sr&quot;&gt;/script&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Alternatively, one can simply use a picture with the email address, which is impossible to parse automatically (well it’s possible, but you don’t know which picture, and if you checked manually - you would not have needed the parser anyways).&lt;/p&gt;

&lt;h3 id=&quot;parsing-email-address&quot;&gt;Parsing Email Address&lt;/h3&gt;
&lt;p&gt;Now what if we interested to parse email addresses for some good reasons? There is surely endless room for improvement, but here is what I have (using python’s re):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;re&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# () capture group - will be returned in the tuple
&lt;/span&gt;           &lt;span class=&quot;c1&quot;&gt;# (?:) non capture group
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# login could be any chars, digit or dot; at least one symbol
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;login&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;([a-z0-9_\.]+)&apos;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# at could be written as a word &apos;at&apos;, symbol &apos;@&apos; or unicode symbol &apos;&amp;amp;#x40;&apos;
# and potentially surrounded by parethsesis or spaces
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;(?:\s?\(?(?:@|\sat\s|&amp;amp;#x40;|where)\)?\s?)&apos;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# dots in domain are usually masked
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dots&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;dot&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;dom&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;dt&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;\.&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;\s&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos; &apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# and potentially surrounded by spaces
# use badass python way to concat list elems with custom sep: sep.join(list)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;(?:\s?(?:&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;|&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dots&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;)\s?)&apos;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# domain is (name + dot) one or more times
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;domain&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;((?:[a-z0-9_]+&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;){1,3})&apos;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# zone
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zone&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;(edu|com)&apos;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;email_pattern&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;login&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;domain&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zone&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print_email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;email_line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# lowercase and remove obfuscating symbols
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;email_line&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;email_line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;-&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# match regexp
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;matches&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;re&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;findall&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;email_pattern&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;email_line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;re&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IGNORECASE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# python re returns list of tuples with the number of capture groups
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;login&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;domain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zone&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matches&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dots&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# convert each trick dot into real dot
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;domain&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;domain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;.&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# some spaces turned into extra dots, compress dots
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;domain&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;domain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;..&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;.&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;..&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;.&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# print email in proper way
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;email&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;%s@%s%s&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;login&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;domain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;email&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;print_email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;alex at ecs mail com&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# alex@ecs.mail.com&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;P.S. Inspired by NLP class &lt;a href=&quot;https://class.coursera.org/nlp/lecture/]&quot;&gt;https://class.coursera.org/nlp/lecture/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since class has ended, homeworks are no longer available. However, you can get them in here: &lt;a href=&quot;http://www.mohamedaly.info/teaching/cmp-462-spring-2013#TOC-Assignments&quot;&gt;http://www.mohamedaly.info/teaching/cmp-462-spring-2013&lt;/a&gt;&lt;/p&gt;

    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/keeping-your-email-address-safe/&quot;&gt;Keeping Your Email Address Safe&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on February 24, 2016.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[Pretty Boxplots in Matplotlib, Pandas and Seaborn]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/pretty-boxplots-python-matplotlib-pandas-seaborn/" />
  <id>//alexsalo.github.io/pretty-boxplots-python-matplotlib-pandas-seaborn</id>
  <published>2016-01-22T00:00:00-05:00</published>
  <updated>2016-01-22T00:00:00-05:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;Python’s Matplotlib offers great flexibility of plotting. Pandas adds convenience of working with data and plotting it. There is also a &lt;a href=&quot;https://stanford.edu/~mwaskom/software/seaborn/&quot;&gt;Seaborn&lt;/a&gt; that makes the output plots look pretty. Let’s see an example.&lt;/p&gt;

&lt;h4 id=&quot;basic-pandas-boxplot&quot;&gt;Basic Pandas Boxplot&lt;/h4&gt;

&lt;p&gt;I have a Pandas dataframe containing rows of data points describing monkey’s drinking. Each row has a few quantitative attributes of interest and experiment-specific attributes (factors), such as ‘Sex’ (Gender) and ‘Period’ (arbitrary stage of the experiment). Our goal is to see, whether the quantitative attributes are dependent on those factors.&lt;/p&gt;

&lt;p&gt;In particular, it is of interest, for each quantitative attribute, to see the boxplot for each combination (Sex, Period). To do just that is very simple with pandas:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;boxplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;column&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;Sex&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;Period&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/images/matrr/bad_boxplot_double_groupby_sex_period.png&quot;&gt;&lt;img src=&quot;//alexsalo.github.io/images/matrr/bad_boxplot_double_groupby_sex_period.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And if, instead, we want to see M and F close together, by Period, then the change is tiny:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;boxplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;column&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;Period&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;Sex&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/images/matrr/bad_boxplot_double_groupby_period_sex.png&quot;&gt;&lt;img src=&quot;//alexsalo.github.io/images/matrr/bad_boxplot_double_groupby_period_sex.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;dealing-with-two-factors&quot;&gt;Dealing With Two Factors&lt;/h4&gt;
&lt;p&gt;OK, well - that’s quite ugly: though works, it’s very hard to read the plot - viewer have to pay a close attention to the labels. How can we do better? First thing - is split factors somehow. Luckily, Pandas offers an easy way to do just that - we first groupby by one factor, and the boxplot with ‘by’ by another factor:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;groupby&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;Sex&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;boxplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;column&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;Period&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/images/matrr/boxplot_combined_drink_to_bout_ratio_LD_BD.png&quot;&gt;&lt;img src=&quot;//alexsalo.github.io/images/matrr/boxplot_combined_drink_to_bout_ratio_LD_BD.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s something - definitely looks better. The problem arises for the other grouping, however:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;groupby&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;Period&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;boxplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;column&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;Sex&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/images/matrr/bad_boxplot_groupby.png&quot;&gt;&lt;img src=&quot;//alexsalo.github.io/images/matrr/bad_boxplot_groupby.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&quot;factorplot-in-seaborn&quot;&gt;Factorplot in Seaborn&lt;/h4&gt;
&lt;p&gt;Luckily, we can both make the plots look better and cope with bad subplots arrangement by using the Seaborn library. It doesn’t allow for multiple groupby in it’s boxplot implementation, however it offers a special &lt;strong&gt;&lt;em&gt;factorplot&lt;/em&gt;&lt;/strong&gt; which does exactly what we want:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;seaborn&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sns&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;factorplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;box&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# Boxplot
&lt;/span&gt;               &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;# Y-axis - values for boxplot
&lt;/span&gt;               &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;Period&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# X-axis - first factor
&lt;/span&gt;               &lt;span class=&quot;n&quot;&gt;hue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;Sex&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;# Second factor denoted by color
&lt;/span&gt;               &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_dc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# Dataframe 
&lt;/span&gt;               &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;# Figure size (x100px)      
&lt;/span&gt;               &lt;span class=&quot;n&quot;&gt;aspect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# Width = size * aspect 
&lt;/span&gt;               &lt;span class=&quot;n&quot;&gt;legend_out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Make legend inside the plot&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/images/matrr/acomb_sex_factor_drink_to_bout_ratio_LD_BD.png&quot;&gt;&lt;img src=&quot;//alexsalo.github.io/images/matrr/acomb_sex_factor_drink_to_bout_ratio_LD_BD.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And surely that works for the alternative arrangement of the factors:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;sns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;factorplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;box&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;Sex&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;Period&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_dc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aspect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;legend_out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/images/matrr/acomb_period_factor_drink_to_bout_ratio_LD_BD.png&quot;&gt;&lt;img src=&quot;//alexsalo.github.io/images/matrr/acomb_period_factor_drink_to_bout_ratio_LD_BD.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With that, I hope all people, who produce plots, would care about their viewers and make a nice readable plots and will never ever ever create a piechart…&lt;/p&gt;

    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/pretty-boxplots-python-matplotlib-pandas-seaborn/&quot;&gt;Pretty Boxplots in Matplotlib, Pandas and Seaborn&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on January 22, 2016.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[Time Series Investigations]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/time-series-investigations/" />
  <id>//alexsalo.github.io/time-series-investigations</id>
  <published>2016-01-19T00:00:00-05:00</published>
  <updated>2016-01-19T00:00:00-05:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;I’ve taken Time Series analysis class to figure out if analyzing financial data and predicting stock markets (my first association with time series) were something I would be interested in doing. Additionally, there are many practical scientific and business applications - starting with electrocardiology analysis and ending with airline tickets sales.&lt;/p&gt;

&lt;p&gt;What I’ve learned, shortly:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Don’t play on the stock markets - the best guess is (almost) always the current value (in other words, the chance of being any good in prediction is basically zero).&lt;/li&gt;
  &lt;li&gt;Use TS to gain insights about your business data.&lt;/li&gt;
  &lt;li&gt;Scientific use is very complicated most of the times nonetheless interesting.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;what-is-time-series-analysis-anyways&quot;&gt;What is Time Series Analysis anyways?&lt;/h3&gt;
&lt;p&gt;In regular stats we have a population (set of all data points), from which we have a random (i.e. representative - hopefully) sample (of size N) of available observations. We calculate &lt;strong&gt;sample&lt;/strong&gt; statistics and &lt;em&gt;infer it to be true&lt;/em&gt; about the &lt;strong&gt;general&lt;/strong&gt; population as well. The bigger the sample size N the more we are statistically sure.&lt;/p&gt;

&lt;p&gt;Now, Time Series Analysis aims to perform similar trick (analyzing sample and inferring about general) but for the &lt;strong&gt;&lt;em&gt;process&lt;/em&gt;&lt;/strong&gt; rather than for the population. Usually, then, we have only &lt;strong&gt;one&lt;/strong&gt; observation/realization (Apple stock price history) of the process , but this process could have generated a different realization - in fact infinitely many of them! That is the key difference of the TS: we trying to describe the process that generated different realizations using available observations (usually one) and then predict what would happen to our realization in the future. I hope by now you get the idea of how sketchy and speculative all that sound - in case of the stock markets, especially, it’s pretty much a gamble.&lt;/p&gt;

&lt;p&gt;To describe the observation (time series) people came up with a model ARIMA, which stands for auto regressive integrated moving average. Sounds scary, but in reality it’s extremely simple:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;AR part is just a linear combination (summation with coefficients) of previous TS values equal white noise.&lt;/li&gt;
  &lt;li&gt;MA part is again just a linear combination, this time of random noise values equals to the current TS value.&lt;/li&gt;
  &lt;li&gt;Integrated simply means that de-trend the time series by linearly subtracting previous value from the current.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Given the realization, we try to fit the coefficients for those linear combinations and then predict the future values.&lt;/p&gt;

&lt;p&gt;That is, shortly, what is Time Series about. Again, importantly, the length of the TS doesn’t give us the statistical power, as in regular statistics - we only gain power if we have &lt;strong&gt;more realizations&lt;/strong&gt;, when, for instance, we track the heart beat process for the 24 hours straights, which yields a lot of observations of that cyclic process.&lt;/p&gt;

&lt;h3 id=&quot;investigations-pdf-papers&quot;&gt;Investigations (pdf papers)&lt;/h3&gt;

&lt;h4 id=&quot;estimating-arma-coefficients-and-predicting-future&quot;&gt;Estimating ARMA Coefficients and Predicting Future&lt;/h4&gt;
&lt;p&gt;&lt;a href=&quot;/papers/investigations/time-series/estimating-coefficients-and-predicting-ARMA-process.pdf&quot;&gt;
&lt;img border=&quot;0&quot; src=&quot;/papers/investigations/time-series/preview/estimating-coefficients-and-predicting-ARMA-process.png&quot; width=&quot;260&quot; align=&quot;right&quot; /&gt;
&lt;/a&gt;
In this take home final exam for TS class we created a definitive ARMA process (by specifying the true coefficients) to generate 500 realizations. Then using them (all or partially) tried to fit the coefficients using the standard tool set, while judging which metrics work better for our case. Finally, there is example of predicting TS - even for the simple case it works remarkably bad, in my opinion.&lt;/p&gt;

&lt;h4 id=&quot;non-consistent-estimators-and-the-effect-of-differencing&quot;&gt;(Non) Consistent Estimators and the Effect of Differencing&lt;/h4&gt;
&lt;p&gt;&lt;a href=&quot;/papers/investigations/time-series/realizations-and-estimators-consistency.pdf&quot;&gt;
&lt;img border=&quot;0&quot; src=&quot;/papers/investigations/time-series/preview/realizations-and-estimators-consistency.png&quot; width=&quot;260&quot; align=&quot;right&quot; /&gt;
&lt;/a&gt;
Consistent estimator is an estimator which only gets closer to the true parameter given more realizations. In this paper we learn that while sample autocorrelation is consistent estimator, periodogram is not.&lt;/p&gt;

&lt;p&gt;(First order) differencing is just: for each x_(i) in TS do: x_(i) = x_(i) - x_(i-1). By doing so we can remove linear trend. Paper discusses how to do that in R and how it affects sample correlogram and spectrum.&lt;/p&gt;

&lt;h4 id=&quot;harmonic-process-and-how-to-convert-arma-into-ma&quot;&gt;Harmonic Process and How to Convert ARMA into MA&lt;/h4&gt;
&lt;p&gt;&lt;a href=&quot;/papers/investigations/time-series/harmonic-and-AR-MA-identifyability.pdf&quot;&gt;
&lt;img border=&quot;0&quot; src=&quot;/papers/investigations/time-series/preview/harmonic-and-AR-MA-identifyability.png&quot; width=&quot;260&quot; align=&quot;right&quot; /&gt;
&lt;/a&gt;
Harmonic process is simply sum of sinusoids, we show it to be second-order stationary. Also it doesn’t have a good spectrum function because it’s not absolutely continuous; the cumulative spectrum is alright though.&lt;/p&gt;

&lt;p&gt;Then paper shows how to convert ARMA into MA(inf) representation by long division - which only works if the AR roots are outside of unit circle.&lt;/p&gt;

&lt;p&gt;Paper also shows the effect of taking the 12th differencing on spectrum and shows how to check whether or not the AR coefficients are valid (possible).&lt;/p&gt;

&lt;h4 id=&quot;time-series-models-summary&quot;&gt;Time Series Models Summary&lt;/h4&gt;
&lt;p&gt;&lt;a href=&quot;/papers/investigations/time-series/time-series-models-summary.pdf&quot;&gt;
&lt;img border=&quot;0&quot; src=&quot;/papers/investigations/time-series/preview/time-series-models-summary.png&quot; width=&quot;260&quot; align=&quot;right&quot; /&gt;
&lt;/a&gt;
A dry summary on TS models: White Noise, Random Walk, Harmonic, AR, MA, ARMA and their properties: ACF, partial ACF and spectrum.&lt;/p&gt;

    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/time-series-investigations/&quot;&gt;Time Series Investigations&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on January 19, 2016.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[Visualizations in Python via Pandas and Matplotlib]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/python-matplotlib-pandas-visualizations/" />
  <id>//alexsalo.github.io/python-matplotlib-pandas-visualizations</id>
  <published>2016-01-16T00:00:00-05:00</published>
  <updated>2016-01-16T00:00:00-05:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;I’ve grown to be quite a fan of Python tools for plotting - namely, Matplolib library, and other packages that uses it, i.e. Pandas. Community is also great, all the questions has long been answered on StackOverflow.&lt;/p&gt;

&lt;p&gt;In this post I just want to share and preserve those useful methods and approaches that I came up with while working on my bio data analysis. Below is the result plot, which contains a lot of raw and aggregated information:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/images/matrr/22hr_bec_more2stdev_12combinedpanels.png&quot;&gt;&lt;img src=&quot;//alexsalo.github.io/images/matrr/22hr_bec_more2stdev_12combinedpanels.png&quot; alt=&quot;Regression (BD)&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And here goes the code that makes it done, along with explanatory comments:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;plot_panels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df_group_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df_group_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;label1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;font_sz&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Create figure and axes
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subplots&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nrows&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ncols&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;figsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;facecolor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;w&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;edgecolor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;k&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Makes axes a flat list
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ravel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# We would need a secondary Y-axis
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;axs_hist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;twiny&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Const some plot props
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;xlim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ylim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.3&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Iterate through drinking categories (DC) - rows in the plot
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Enumerate allows to have index i
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dc&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;LD&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;BD&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;HD&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;VHD&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]):&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# Filter data frames by DC
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;df_dc_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df_group_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_group_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;df_dc_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df_group_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_group_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;df_dc_all&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df_all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# Plot each group
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax_offset&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;etoh_previos_day&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;etoh_at_bec_sample_time&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;s&quot;&gt;&apos;etoh_next_day&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]):&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;# Condition A
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;df_dc_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;scatter&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;bec&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;g&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                         &lt;span class=&quot;n&quot;&gt;xlim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ylim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ylim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;# Condition B
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;df_dc_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;scatter&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;bec&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;orange&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                         &lt;span class=&quot;n&quot;&gt;xlim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ylim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ylim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;# Add histogram for all data points
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;df_dc_all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;b&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;orientation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;horizontal&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                               &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axs_hist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                               &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# transparency
&lt;/span&gt;                               &lt;span class=&quot;n&quot;&gt;bins&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;# number of bins to distribute
&lt;/span&gt;                               &lt;span class=&quot;n&quot;&gt;normed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# normalize to be comparable across 
&lt;/span&gt;                                              &lt;span class=&quot;c1&quot;&gt;# different sample sizes
&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;# Add regressions
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;_regression_and_corr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_dc_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;_regression_and_corr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_dc_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;# Calculate ANCOVA p-value (H0: slopes are equal)
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;_plot_ancova_regression_pvalue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Fine tune plot look&apos;n&apos;feel
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tight_layout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Hide spaces between subplots
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subplots_adjust&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hspace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subplots_adjust&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wspace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Hide all ticks
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_xticklabels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;visible&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_yticklabels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;visible&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Show first columns and last row ticks since they are shared
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_xticklabels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;visible&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_yticklabels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;visible&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Label rows Y-label
&lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_ylabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_ylabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;LD&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fontsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font_sz&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_ylabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;BD&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fontsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font_sz&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_ylabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;HD&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fontsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font_sz&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_ylabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;VHD&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fontsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font_sz&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Dummy scatter for legend
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;dot_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;g&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;marker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;o&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dot_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;orange&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;marker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;o&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;legend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;handles&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dot_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                  &lt;span class=&quot;n&quot;&gt;bbox_to_anchor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                  &lt;span class=&quot;n&quot;&gt;loc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;lower left&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;n&quot;&gt;scatterpoints&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Set figure&apos;s title
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;BEC correlation: EtOH the day before, day of and day after&apos;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.94&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ha&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;center&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fontsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font_sz&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.005&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;BEC&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;va&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;center&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rotation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;vertical&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subplots_adjust&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;top&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.90&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Label groups
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.19&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;EtOH Day Before&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ha&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;center&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fontsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font_sz&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;EtOH Day of BEC Sample&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ha&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;center&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fontsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font_sz&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.83&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;EtOH Day After&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ha&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;center&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fontsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font_sz&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subplots_adjust&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bottom&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Where regression lines are made with Numpy’s polyfit:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_regression_and_corr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Get x and y
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;bec&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# Fit straight line (polynomial of the first degree)
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;polyfit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linecol&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Annotate
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;props&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;boxstyle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;round&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;facecolor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;wheat&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;group_label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos; Corr: %s&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.95&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text_y_adj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transAxes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fontsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;verticalalignment&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;top&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bbox&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The reason why histogram appeared on the plot is because we spotted some unexpected behavior - histogram clarified that it was just a perception problem - too many data points. As an alternative, we can do hexbin plot with a slight twitch of code:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;c1&quot;&gt;# Using 24 subplots instead
# And Pandas&apos;s hexbin method:
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hexbin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_dc_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;etoh_previos_day&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_dc_group_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
			   &lt;span class=&quot;n&quot;&gt;cmap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Greens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Color scheme
&lt;/span&gt;			   &lt;span class=&quot;n&quot;&gt;gridsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;# Fairly important parameter&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/images/matrr/22hr_bec_more2stdev_hexbins.png&quot;&gt;&lt;img src=&quot;//alexsalo.github.io/images/matrr/22hr_bec_more2stdev_hexbins.png&quot; alt=&quot;Regression (BD)&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hexbin plot allows us to see another dimension for the case of too many data points by histograming those points into hexagonal bins.&lt;/p&gt;

    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/python-matplotlib-pandas-visualizations/&quot;&gt;Visualizations in Python via Pandas and Matplotlib&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on January 16, 2016.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[ANCOVA, Compare Regression Slopes in Python]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/ancova-comparing-regression-slopes/" />
  <id>//alexsalo.github.io/ancova-comparing-regression-slopes</id>
  <published>2016-01-15T00:00:00-05:00</published>
  <updated>2016-01-15T00:00:00-05:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;How to compare two regressions in Python? I didn’t find a good simple answer - hence this post. Seems like we need to compare regressions by slope, but how to test the significance? ANCOVA is the answer!&lt;/p&gt;

&lt;p&gt;Imagine you have a dataset, in which you want to investigate the dependence of one variable of another, in our case - how BEC (Blood Ethanol Concentration) depends on EtOH. Additionally, we have the categorical variable - condition, in our case condition is either ‘Within’ or ‘Over’.&lt;/p&gt;

&lt;p&gt;First step in the analysis is to scatterplot the data and calculate the correlation, for each condition:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/images/matrr/bec_etoh_corr_regression_BD.png&quot;&gt;&lt;img src=&quot;//alexsalo.github.io/images/matrr/bec_etoh_corr_regression_BD.png&quot; alt=&quot;Regression (BD)&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/images/matrr/bec_etoh_corr_regression_HD.png&quot;&gt;&lt;img src=&quot;//alexsalo.github.io/images/matrr/bec_etoh_corr_regression_HD.png&quot; alt=&quot;Regression (HD)&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can clearly see, that while for ‘BD’ two regression lines differ in slopes a lot, for ‘HD’ they &lt;strong&gt;look&lt;/strong&gt; almost parallel. Now the question is how to make a statistically sound statement?&lt;/p&gt;

&lt;p&gt;Well, there is an ANCOVA (Analysis of Covariance) model. Why and how you can &lt;a href=&quot;https://www.csub.edu/~psmith3/Teaching/310-12.pdf&quot;&gt;read intro here.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, how to do that in Python? Apperantly, you can use a standard OLS model from statsmodel with just a tweak of a formula (that is there is no big difference in the tool set between ANOVA and ANCOVA). Assume the following Pandas Data Frame:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;DC&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;EtOH&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;BEC&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;Condition&lt;/span&gt;  
&lt;span class=&quot;o&quot;&gt;-------------------------&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;BD&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;2.60&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;59.0&lt;/span&gt;  &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;   
&lt;span class=&quot;n&quot;&gt;HD&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;2.20&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;77.0&lt;/span&gt;  &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;   
&lt;span class=&quot;n&quot;&gt;HD&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;2.22&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;60.0&lt;/span&gt;  &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;   &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;   &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Then by doing:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;statsmodels.formula.api&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ols&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# formula = &apos;BEC ~ EtOH&apos;               # Simple regression
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;formula&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;BEC ~ EtOH * C(Condition)&apos;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# ANCOVA formula
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;formula&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dataframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DC&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;HD&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We get the p-value of how significant the effect of the Condition is on our regression slopes:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;                            &lt;span class=&quot;n&quot;&gt;OLS&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Regression&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Results&lt;/span&gt;                            
&lt;span class=&quot;o&quot;&gt;==============================================================================&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Dep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                    &lt;span class=&quot;n&quot;&gt;bec&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;squared&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                       &lt;span class=&quot;mf&quot;&gt;0.172&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                            &lt;span class=&quot;n&quot;&gt;OLS&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;Adj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;squared&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                  &lt;span class=&quot;mf&quot;&gt;0.170&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;Least&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Squares&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;statistic&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                     &lt;span class=&quot;mf&quot;&gt;62.67&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;Thu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;14&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Jan&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2016&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;Prob&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;statistic&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;           &lt;span class=&quot;mf&quot;&gt;8.32e-37&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                        &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;31&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;Log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Likelihood&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;4877.4&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;No&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Observations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                 &lt;span class=&quot;mi&quot;&gt;906&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;AIC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                             &lt;span class=&quot;mf&quot;&gt;9763.&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Df&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Residuals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                     &lt;span class=&quot;mi&quot;&gt;902&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;BIC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                             &lt;span class=&quot;mf&quot;&gt;9782.&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Df&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                           &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;                                         
&lt;span class=&quot;n&quot;&gt;Covariance&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;nonrobust&lt;/span&gt;                                         
&lt;span class=&quot;o&quot;&gt;==============================================================================&lt;/span&gt;
                                    &lt;span class=&quot;n&quot;&gt;coef&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;          &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     
&lt;span class=&quot;o&quot;&gt;------------------------------------------------------------------------------&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Intercept&lt;/span&gt;                        &lt;span class=&quot;mf&quot;&gt;16.8906&lt;/span&gt;      &lt;span class=&quot;mf&quot;&gt;6.233&lt;/span&gt;      &lt;span class=&quot;mf&quot;&gt;2.710&lt;/span&gt;      &lt;span class=&quot;mf&quot;&gt;0.007&lt;/span&gt;     
&lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Condition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;             &lt;span class=&quot;mf&quot;&gt;37.2833&lt;/span&gt;     &lt;span class=&quot;mf&quot;&gt;26.816&lt;/span&gt;      &lt;span class=&quot;mf&quot;&gt;1.390&lt;/span&gt;      &lt;span class=&quot;mf&quot;&gt;0.165&lt;/span&gt;     
&lt;span class=&quot;n&quot;&gt;EtOH&lt;/span&gt;                             &lt;span class=&quot;mf&quot;&gt;26.4234&lt;/span&gt;      &lt;span class=&quot;mf&quot;&gt;2.160&lt;/span&gt;     &lt;span class=&quot;mf&quot;&gt;12.233&lt;/span&gt;      &lt;span class=&quot;mf&quot;&gt;0.000&lt;/span&gt;     
&lt;span class=&quot;n&quot;&gt;EtOH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Condition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;         &lt;span class=&quot;mf&quot;&gt;2.2716&lt;/span&gt;      &lt;span class=&quot;mf&quot;&gt;8.933&lt;/span&gt;      &lt;span class=&quot;mf&quot;&gt;0.254&lt;/span&gt;      &lt;span class=&quot;mf&quot;&gt;0.799&lt;/span&gt;     
&lt;span class=&quot;o&quot;&gt;==============================================================================&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Omnibus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                       &lt;span class=&quot;mf&quot;&gt;18.672&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;Durbin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Watson&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                   &lt;span class=&quot;mf&quot;&gt;1.143&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Omnibus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;                  &lt;span class=&quot;mf&quot;&gt;0.000&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;Jarque&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Bera&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;JB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;               &lt;span class=&quot;mf&quot;&gt;21.270&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Skew&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                           &lt;span class=&quot;mf&quot;&gt;0.284&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;Prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;JB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;                     &lt;span class=&quot;mf&quot;&gt;2.41e-05&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Kurtosis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;                       &lt;span class=&quot;mf&quot;&gt;3.490&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;Cond&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;No&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;                         &lt;span class=&quot;mf&quot;&gt;49.0&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;==============================================================================&lt;/span&gt;  &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We are interested in the line:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;                                    &lt;span class=&quot;n&quot;&gt;coef&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;          &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;     
&lt;span class=&quot;o&quot;&gt;------------------------------------------------------------------------------&lt;/span&gt;   
&lt;span class=&quot;n&quot;&gt;EtOH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Condition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;         &lt;span class=&quot;mf&quot;&gt;2.2716&lt;/span&gt;      &lt;span class=&quot;mf&quot;&gt;8.933&lt;/span&gt;      &lt;span class=&quot;mf&quot;&gt;0.254&lt;/span&gt;      &lt;span class=&quot;mf&quot;&gt;0.799&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Meaning p-value = 0.799, which suggests us not to reject the H0 (initial hypothesis) that two slopes are equal. That means that slopes in case of ‘HD’ are statistically same.&lt;/p&gt;

&lt;p&gt;No surprise - test for ‘BD’:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;                                    &lt;span class=&quot;n&quot;&gt;coef&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;          &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;-------------------------------------------------------------------------&lt;/span&gt;   
&lt;span class=&quot;n&quot;&gt;EtOH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Condition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;        &lt;span class=&quot;mf&quot;&gt;42.6065&lt;/span&gt;     &lt;span class=&quot;mf&quot;&gt;13.232&lt;/span&gt;      &lt;span class=&quot;mf&quot;&gt;3.220&lt;/span&gt;      &lt;span class=&quot;mf&quot;&gt;0.001&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;P-value of 0.001 and we happily reject the equal slopes hypothesis.&lt;/p&gt;

    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/ancova-comparing-regression-slopes/&quot;&gt;ANCOVA, Compare Regression Slopes in Python&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on January 15, 2016.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[Static Methods in Django Models and Neat Model's Content Printout]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/django-static-methods-and-neat-printout/" />
  <id>//alexsalo.github.io/django-static-methods-and-neat-printout</id>
  <published>2016-01-14T00:00:00-05:00</published>
  <updated>2016-01-14T00:00:00-05:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;Recently I figured a neat way to print the content of the model for your own testing of for data shipment. Most of the times you want to use Pandas DataFrame, but you need to pass along the fields that you want to select from the DB. More often than not though the fields that you want to display stay same for the model. That’s why I started to add one extra field to the model - columns for display. For example, in MonkeyEphys:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MonkeyEphys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;models&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;   
  &lt;span class=&quot;c1&quot;&gt;# bunch of fields: some useful, some technical
&lt;/span&gt;  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;monkey&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;models&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ForeignKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Monkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;related_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;mep_records&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;mep_frequency&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;models&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FloatField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Frequency (hz)&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;mep_tissue_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;models&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ForeignKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TissueType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;related_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;ephys_records&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;  

  &lt;span class=&quot;c1&quot;&gt;# Tuple: (list of db attributes, list of column names to display)
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;monkey__mky_id&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;mep_tissue_type__tst_tissue_name&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;s&quot;&gt;&apos;mep_ephys_type&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;mep_frequency&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
             &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;mky_id&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;tissue_name&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;ephys_type&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;frequency&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now you can use this tuple in Pandas:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt; 
  &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MonkeyEphys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objects&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;\
         &lt;span class=&quot;n&quot;&gt;values_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MonkeyEphys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])),&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# *kwargs - fields to be selected
&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MonkeyEphys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;# displayable column names
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now, it gets better than this. Have you ever dreamed of static methods for the model? Well, your dreams are true - you can delegate repeatable some code specifically to the model, yey! Let’s continue with the Ephys example, say we don’t want to type the selection code every time - well, just append a static method for it:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt; 
&lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;staticmethod&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;content_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MonkeyEphys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objects&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Now this does the job:
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MonkeyEphys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Pretty neat, hah? Too bad I was completely ignorant of that before.&lt;/p&gt;

    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/django-static-methods-and-neat-printout/&quot;&gt;Static Methods in Django Models and Neat Model&apos;s Content Printout&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on January 14, 2016.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[Drinking Makes You Fat!]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/drinking-makes-you-fat/" />
  <id>//alexsalo.github.io/drinking-makes-you-fat</id>
  <published>2015-12-16T00:00:00-05:00</published>
  <updated>2015-12-16T00:00:00-05:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;I’ve been working on &lt;a href=&quot;http://matrr.com/&quot;&gt;matrr.com&lt;/a&gt; project for the last year. Different labs across the country offer monkeys an alcoholic beverages (4% EtOH mixed with sugar in water) and investigate the consequesnces. We collect the data for the reuse by others, while also aggregating and doing various analysis. 
In particular, while making the weight change plots, I figured that &lt;strong&gt;drinking make you fat!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Well, there is no surprise - I’ve noticed that drinkers often fat (if they are not poor) a long time ago among humans. But let’s see that effect precisly on Rhesus Macaques:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/images/matrr/10215_weight_change.png&quot;&gt;&lt;img src=&quot;//alexsalo.github.io/images/matrr/10215_weight_change.png&quot; alt=&quot;Animal Weight Change&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how weight increases steadily over the alcohol exposure periods (Induction and Open Access). Then something interesting happens: after withdrawal (they take them away from alcohol), animal loses 20% of its body weight during one month! Then, alcohol restored, it gains weight again; cycle repeats with the new withdrawals.&lt;/p&gt;

&lt;p&gt;Lesson to be learned? &lt;strong&gt;Don’t drink heavily, but if you do, don’t eat too much, at least :)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;On the separate note, check out how easy it is to work with python\django\matplotlib\pandas - you type and it does it. Here is how to create this plot:&lt;/em&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;monkey_weight_plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;monkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    Create mky weight plot
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;weight_plot_makeup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
         Adjusting plot appearance
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# set font size
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;matplotlib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rcParams&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;font.size&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# get legend: handles (lines) and labels (captions)
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;handles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_legend_handles_labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# add animal&apos;s info
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;monkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mky_drinking_category&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;monkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mky_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos; Control&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;monkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mky_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos; &apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;monkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mky_drinking_category&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# plug labels back
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;legend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;handles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;upper left&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# reverse to keep order consistent
&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# set labels and titles
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_ylabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;Monkey Weight&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;Animal Weight Change&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# make tight layout to avoid space waste
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;pyplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tight_layout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;make_event_annotations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        Annotate the axis with cohort&apos;s events
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# retrieve events for cohort from DB
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;cevs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CohortEvent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objects&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cohort&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;monkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cohort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# put into data frame
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;evts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cevs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;event__evt_name&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;cev_date&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;event&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;date&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;# get values
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# filter by regex and negate
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;evts_begin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;evts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;evts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;.*(Before|Necropsy|Pre|Endocrine|(H2O)|(Ethanol.*End.*))&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# plot ablines
&lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axvline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;r&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;linestyle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;--&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;evts_begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# get ylim of axis
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;ymin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ymax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_ylim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# plot event at date while iteration tuples
&lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ymin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;verticalalignment&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;bottom&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;horizontalalignment&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;right&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;rotation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;vertical&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;evts_begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iterrows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()]&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# adjust left xlim to fit text annotaiton
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;xmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xmax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_xlim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_xlim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xmin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# retrieve data from DB into ORM&apos;s (Django) class objects
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;mtds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MonkeyToDrinkingExperiment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objects&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;monkey&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;monkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;\
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mtd_weight__isnull&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order_by&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;drinking_experiment__dex_date&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# if have data (no need to write count() &amp;gt; 0)
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mtds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# create pandas dataframe
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mtds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;drinking_experiment__dex_date&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;mtd_weight&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
                          &lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;Date&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;weight&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# get new figure with desired size and DPI
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pyplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;figure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;figsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEFAULT_FIG_SIZE_ALEX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dpi&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEFAULT_DPI&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# add subplot to a figure
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_subplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;111&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# line plot the weight change via pandas df&apos;s method
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;Date&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;weight&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DRINKING_CATEGORIES_COLORS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;monkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mky_drinking_category&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# adjust appearance
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;weight_plot_makeup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# make event annotations
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;make_event_annotations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/drinking-makes-you-fat/&quot;&gt;Drinking Makes You Fat!&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on December 16, 2015.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[Investigations on Machine Learning]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/machine-learning-investigations/" />
  <id>//alexsalo.github.io/machine-learning-investigations</id>
  <published>2015-12-10T00:00:00-05:00</published>
  <updated>2015-12-10T00:00:00-05:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;During the Spring 2015 I’ve taken the Machine Learning class which was a blast: by applying rather simple math apparatus in a fancy fashion we can &lt;em&gt;learn&lt;/em&gt; on the data we have.&lt;/p&gt;

&lt;h3 id=&quot;what-is-machine-learning-anyways&quot;&gt;What is Machine Learning anyways?&lt;/h3&gt;
&lt;p&gt;There is a slim edge between stats, data mining and ML - they all interconnected, sure, but the key difference is the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Stats: we try to describe the data and estimate significance based on the samples we have; most of the times we want sample to be drawn out of “normal” population (meaning, in stats, all the possible data points).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Data Mining: we try to &lt;em&gt;mine&lt;/em&gt; some associations and connections between the items in the dataset; no assumptions on the data.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Machine Learning: given the dataset (sample mapping) of X-&amp;gt;Y, we want to *learn to mimic” the algorithm that converts the X into Y, so we can use it later on new X’. In general, there are no assumptions on the distribution (although some methods do assume things), and what’s cool, that we don’t really care what the algorithm looks like, as long as we can successfully mimic it (what a bummer for all the stats people who are like “wat? I want to know the true process!” right now..)&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Anyways, during the class we had several home assignments to investigate different aspects of various methods which I present below, along with the short summary of what is learned.&lt;/p&gt;

&lt;h3 id=&quot;investigations-pdf-papers&quot;&gt;Investigations (pdf papers)&lt;/h3&gt;

&lt;h4 id=&quot;linear-and-logistic-regression-gradient-descent-and-newtons-method&quot;&gt;Linear and Logistic regression: Gradient Descent and Newton’s method&lt;/h4&gt;
&lt;p&gt;&lt;a href=&quot;/papers/investigations/machine-learning/mle_wlr_gradient-descent_newthon.pdf&quot;&gt;
&lt;img border=&quot;0&quot; alt=&quot;W3Schools&quot; src=&quot;/papers/investigations/machine-learning/preview/mle_wlr_gradient-descent_newthon.png&quot; width=&quot;260&quot; align=&quot;right&quot; /&gt;
&lt;/a&gt;
The most basic ML’s tool is the Linear Regression: we try to fit the line into the data points (i.e. price of the apartment based on size and distance to public transport; then we can predict how much would the new apt cost with given size and distance).&lt;/p&gt;

&lt;p&gt;Then we can do a “sliding window” for achieving locally-weighted linear regression - we can make the line flexible. Playing with the weights we trade off bias and variance.&lt;/p&gt;

&lt;p&gt;Instead of “float” regression, we can do “logistic” (dichotomy) regression, i.e. answering “yes” or “no” for a given data.&lt;/p&gt;

&lt;p&gt;To solve this we use the one of the following:&lt;/p&gt;

&lt;p&gt;Linear Regression &amp;amp; Logistic Regression:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Gradient Descent - fast and efficient, has closed form solution; could get stuck in local optima; could be randomized.&lt;/li&gt;
  &lt;li&gt;Newthon’s method - even faster - finishes in just a few iteration; could have issues on some data since needs Hessian matrix (second partial derivative).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;k-means-clustering&quot;&gt;K-Means Clustering&lt;/h4&gt;
&lt;p&gt;&lt;a href=&quot;/papers/investigations/machine-learning/kmeans_generalized-em.pdf&quot;&gt;
&lt;img border=&quot;0&quot; alt=&quot;W3Schools&quot; src=&quot;/papers/investigations/machine-learning/preview/kmeans_generalized-em.png&quot; width=&quot;260&quot; align=&quot;right&quot; /&gt;
&lt;/a&gt;
K-means is a work horse in data analysis - it is so simple, fast and efficient, that most of the times we end up using it either directly, or within more complex procedure.&lt;/p&gt;

&lt;p&gt;The paper investigates how to choose optimal &lt;strong&gt;k&lt;/strong&gt; (try several, check the error, take the &lt;strong&gt;k&lt;/strong&gt; with min error) and the importance of the initialization - with bad luck K-means can get stuck in local optima; but we are computer scientists, we don’t like to be dependent on luck - run K-means many many times and take the best results - the chances of getting stuck in local optima decrease polynomially.&lt;/p&gt;

&lt;p&gt;K-means is a particular case of Expectation-Minimization algorithms family. We could use median or whatever else to achieve the result - paper shows the generalized approach.&lt;/p&gt;

&lt;h4 id=&quot;gda-naive-bayes-and-ridge-regression&quot;&gt;GDA, Naive Bayes and Ridge Regression&lt;/h4&gt;
&lt;p&gt;&lt;a href=&quot;/papers/investigations/machine-learning/gda_ridge-regression_oprimal-margin_naive-bayes_svm.pdf&quot;&gt;
&lt;img border=&quot;0&quot; alt=&quot;W3Schools&quot; src=&quot;/papers/investigations/machine-learning/preview/gda_ridge-regression_oprimal-margin_naive-bayes_svm.png&quot; width=&quot;260&quot; align=&quot;right&quot; /&gt;
&lt;/a&gt;
In Gradient Descent we have some problems with the collinearity. Ridge regression technique allows to penalize high theta (hypothesis of algorithm that we are trying to learn) coefficients, that appeared due to the collinearity in the data, while with moderate values of parameter λ it would not significantly decrease predictive power (in other words would not increase cost function). By doing that we avoid blown-up theta values that potentially lead to calculus errors.&lt;/p&gt;

&lt;p&gt;Paper also compares the Naive Bayes vs SVM performance on spam filter example, how to do La-Place smoothing to avoid missing data errors, and also shows how the two-class Gaussian Discriminant Analysis, with common covariance Σ, is exactly a logistic sigmoid function.&lt;/p&gt;

&lt;h4 id=&quot;mistake-bounds-and-uniform-convergence&quot;&gt;Mistake Bounds and Uniform Convergence&lt;/h4&gt;
&lt;p&gt;&lt;a href=&quot;/papers/investigations/machine-learning/mistake-bounds_uniform-convergence_soft-margins-svm_vc-dimensions.pdf&quot;&gt;
&lt;img border=&quot;0&quot; alt=&quot;W3Schools&quot; src=&quot;/papers/investigations/machine-learning/preview/mistake-bounds_uniform-convergence_soft-margins-svm_vc-dimensions.png&quot; width=&quot;260&quot; align=&quot;right&quot; /&gt;
&lt;/a&gt;
The cool thing is - we can put a bound on how big our mistakes could be. This paper goes rather deep in how by making more hypothesis we reduce the chance of the mistakes. Also discuses the usage of L-2 soft margins for SVM.&lt;/p&gt;

    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/machine-learning-investigations/&quot;&gt;Investigations on Machine Learning&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on December 10, 2015.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[Cash Rewards and Sign Up Bonus]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/cash-rewards-sign-up-bonus/" />
  <id>//alexsalo.github.io/cash-rewards-sign-up-bonus</id>
  <published>2015-12-09T00:00:00-05:00</published>
  <updated>2015-12-09T00:00:00-05:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;Being a &lt;a href=&quot;http://www.mrmoneymustache.com/&quot;&gt;Mustashian&lt;/a&gt;, as I’ve learned only recently (many Russians are anyway), I like to see precisely how my money flow in and out. It’s been a year since I started to play the Credit Game, and now when my score was high enough to get some classic credit cards I’m ready to do some evaluation.&lt;/p&gt;

&lt;p&gt;Firstly, primary reasons for credit cards are:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Protection against fraud&lt;/li&gt;
  &lt;li&gt;Risk diversification - use credit cards in seedy places&lt;/li&gt;
  &lt;li&gt;Cash Back Rewards&lt;/li&gt;
  &lt;li&gt;Some other perks like free rental insurance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I decided to start with BoF Cash Rewards which promised a $100 sing-up bonus after I spent $500 in first three months. Since I can pay my electricity and at&amp;amp;t bills with the credit cards without any fees, along with the groceries of course, I decided $500 would not take longer then a couple of months anyway.&lt;/p&gt;

&lt;p&gt;Here is what I’ve got:&lt;br /&gt;
&lt;img src=&quot;/images/bof-cach-back-rewards.png&quot; alt=&quot;bof-cach-back-rewards&quot; style=&quot;width: 670px;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Surprisingly, I’ve gotten rewards on almost all the purchases (I did not plan individual purchases) since card makes 3% (instead of base 1%) on gas and groceries. Additionally, as promised, I received a $100 signing bonus.&lt;/p&gt;

&lt;p&gt;(Important caveat: I know that for many people it’s very hard to control what they buy when the see buzz words “free”, “reward” etc; I tend to believe I’m almost free of such impulses and all the purchases I would have made regardless of the whether or not I get a cash back bonus)&lt;/p&gt;

&lt;p&gt;Now the coolest part is that nobody forces me to spent this cash right away (although they try to advertise various stores and deals). I simply chosen to redeem as a direct deposit to my checking account. I was surprised that for doing so I actually receive additional %10 loyalty bonus:
&lt;img src=&quot;/images/bof-customer-bonus.png&quot; alt=&quot;bof-cach-back-rewards&quot; style=&quot;width: 670px;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Additionally, I actually used Chase Freedom credit card. I have had to flight for onsite interviews and had to spend reimbursable money - I figured free cash back would be nice. There were “spend $300 in three months” deal too so I’ve gotten additional $100 bonus.&lt;/p&gt;

&lt;p&gt;To compare cards: Chase’s freedom gives much better CL, but their online banking is garbage. They use points instead of actual currency which hinders analysis and that I don’t like.&lt;/p&gt;

&lt;p&gt;PS this post inspired by &lt;a href=&quot;http://frominsidethebox.com/view?key=5669283816275968&quot;&gt;Brandon’s blog&lt;/a&gt; which is a nice story to read. Reading, writing and thinking about things allows be to better understand nature, and that, in turn, makes me a happier primate.&lt;/p&gt;

&lt;p&gt;PSS ugly $100 for at&amp;amp;t is due to the new user on my account which covers two month of the usage. Normally I pay only $30, which still a whole lot for the little of simple service I need.&lt;/p&gt;

    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/cash-rewards-sign-up-bonus/&quot;&gt;Cash Rewards and Sign Up Bonus&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on December 09, 2015.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[The Beauty of Randomness]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/the-beauty-of-randomness/" />
  <id>//alexsalo.github.io/the-beauty-of-randomness</id>
  <published>2015-10-05T00:00:00-04:00</published>
  <updated>2015-10-05T00:00:00-04:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;I’m often fascinated by the randomness and its patterns around us. It’s gorgeous. Just look at this picture, generated by percolation simulation:
&lt;img src=&quot;/images/percolation500.png&quot; alt=&quot;percolation&quot; style=&quot;width: 500px;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Simulation starts with all the cells closed, then iteratively opens one cell at the time at random. If cell connected to the top - it percolates with water. Simulation stops whenever water reaches the bottom. On average, it takes ~0.59% of cells to be open for the system to percolate. As it percolates, some cells still stay closed.&lt;/p&gt;

&lt;p&gt;This picture reminds of the galaxies - why some happened to be suitable for water and atmosphere and some to be so barren? The answer is, probably, - due to the chance.&lt;/p&gt;

&lt;p&gt;Another picture, taken from wiki storage, shows one of the few possible chess board traversal by the knight without visiting the same cell twice.&lt;br /&gt;
&lt;img src=&quot;/images/knights_tour.png&quot; alt=&quot;knights tour&quot; style=&quot;width: 500px;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Interestingly, you can easily see the patterns, yet you can’t easily comprehend the complex structure of the puzzle.&lt;/p&gt;

    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/the-beauty-of-randomness/&quot;&gt;The Beauty of Randomness&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on October 05, 2015.&lt;/p&gt;
  </content>
</entry>


<entry>
  <title type="html"><![CDATA[Mobile Apps That Don't Get Job Done, or Web Anti-Patterns]]></title>
 <link rel="alternate" type="text/html" href="//alexsalo.github.io/apps-as-bad-website-substitute/" />
  <id>//alexsalo.github.io/apps-as-bad-website-substitute</id>
  <published>2015-09-21T00:00:00-04:00</published>
  <updated>2015-09-21T00:00:00-04:00</updated>
  <author>
    <name>Alex Salo</name>
    <uri>//alexsalo.github.io</uri>
    <email>alexsalodev@gmail.com</email>
  </author>
  <content type="html">
    &lt;p&gt;So much more often than not users are being solicited with the “Install our new super awesome mobile app”, which does not fulfil the goals for which they tried to access the web resource in the first place. Annoying pop up often prevents from getting further, but even if you manage to close the pop up - you’ll most likely get a mobile version of the web site.&lt;/p&gt;

&lt;p&gt;While apps and mobile versions of the sites are great and have some significant advantages, they often just don’t provide with the full functionality of the original resource, and rightly so. Well, the thing is, sometimes you just absolutely need to access the full version to get what you want. Alternatively, you don’t want to spend your space on the device (especially in today’s world of ridiculous &amp;gt;50 MB apps for something really unimportant), or you want to spare the internet connection - the list goes on and on.&lt;/p&gt;

&lt;p&gt;So the rule of thumbs for developers is: allow &lt;strong&gt;easy, obvious&lt;/strong&gt; access to the full version of the web site on the mobile devices.&lt;/p&gt;

&lt;p&gt;Another anti-pattern is to unilaterally turn on the sound on the mobile device. That’s just horrible, really. Never, never play sound on the mobile unless user asks for it. Facebook and Instagram recently realized that (obvious) idea which is great. Other devs should follow too.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/app-popup.png&quot; alt=&quot;Pop Up&quot; style=&quot;width: 250px;&quot; /&gt; &lt;img src=&quot;/images/app-sound.png&quot; alt=&quot;Sound&quot; style=&quot;width: 250px;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;P.S. As usual, screenshot doesn’t capture video frames. In reality, video &lt;strong&gt;is playing&lt;/strong&gt; without me starting it.&lt;/p&gt;

    &lt;p&gt;&lt;a href=&quot;//alexsalo.github.io/apps-as-bad-website-substitute/&quot;&gt;Mobile Apps That Don&apos;t Get Job Done, or Web Anti-Patterns&lt;/a&gt; was originally published by Alex Salo at &lt;a href=&quot;//alexsalo.github.io&quot;&gt;Alex Salo Dev Notes&lt;/a&gt; on September 21, 2015.&lt;/p&gt;
  </content>
</entry>

</feed>
