<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Thanh-Nam blog</title>
        <description>Just for fun</description>
        <link>http://tndoan.github.io/</link>
        <atom:link href="http://tndoan.github.io/feed.xml" rel="self" type="application/rss+xml" />
        <pubDate>Tue, 07 Mar 2023 02:49:06 +0000</pubDate>
        <lastBuildDate>Tue, 07 Mar 2023 02:49:06 +0000</lastBuildDate>
        <generator>Jekyll v3.9.3</generator>
        
        <item>
            <title>Contour visualization with Plots.jl</title>
            <description>&lt;p&gt;I found that the official tutorial of &lt;a href=&quot;https://docs.juliaplots.org/latest/generated/gr/#gr-ref22&quot;&gt;contours plot&lt;/a&gt; on Plots.jl website is not enough for us to generate good visualization. So below is my attempt to have a nice plot.&lt;/p&gt;

&lt;h2 id=&quot;prerequisite&quot;&gt;Prerequisite&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Julia 1.7.1&lt;/li&gt;
  &lt;li&gt;Plots.jl 1.25.7&lt;/li&gt;
  &lt;li&gt;Backend: GR&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We use &lt;a href=&quot;https://en.wikipedia.org/wiki/Himmelblau%27s_function&quot;&gt;Himmelblau’s function&lt;/a&gt; to illustrate our visualization. The function is straightforward to define in Julia:&lt;/p&gt;

&lt;div class=&quot;language-julia highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Plots&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;x&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;mi&quot;&gt;2&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;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&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;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;x&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;y&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;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-julia highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;range_x&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;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&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;n&quot;&gt;range_y&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;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;the-first-attempt&quot;&gt;The first attempt&lt;/h2&gt;

&lt;div class=&quot;language-julia highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;contour&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;range_x&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;range_y&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xlabel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ylabel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;y&quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The code snippet above means that we need to draw the contour of function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; with range of its parameter &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; defined by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;range_x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;range_y&lt;/code&gt; respectively. Here is the result&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/2022-02-22-contour/contour_1.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The result is quite terrible since it does not allow us to recognize local minima. Can we do better?&lt;/p&gt;

&lt;h2 id=&quot;the-second-attempt&quot;&gt;The second attempt&lt;/h2&gt;

&lt;p&gt;The answer is yes since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;contour&lt;/code&gt; provides several parameters and we try to explore in order to make better plot.&lt;/p&gt;

&lt;p&gt;The most important parameter we will explore is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;levels&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-julia highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;contour&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;range_x&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;range_y&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xlabel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ylabel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;y&quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;levels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Basically, it will plot 200 contour lines in this visualization and here is the output&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/2022-02-22-contour/contour_2.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The figure above is a little bit better than the one of the first attempt. From the figure, we can somehow recognize that there are 4 local minima. However, since the number of contour lines is too large, the gap between 2 consecutive contour lines near the edge of figure is so small. Sequentially, it makes some illusion that there are more than 4 local minima. Hmm, it is not clear enough for us.&lt;/p&gt;

&lt;h2 id=&quot;the-third-attempt&quot;&gt;The third attempt&lt;/h2&gt;

&lt;p&gt;In this attempt, we try to make the number of contour lines small enough for clear visualization but at the same time, the lines must be representative enough. How can we achieve these two goals at the same time? We observe that Himmelblau’s function is always positive since it is the sum of two squares. Hence, we want to have contour lines when function has values 0. We also choose some other values to put contour lines. Now, we have a list of values of function that we want to draw contour lines and the list can be set by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;levels&lt;/code&gt; parameter.&lt;/p&gt;

&lt;div class=&quot;language-julia highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;contour&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;range_x&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;range_y&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xlabel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ylabel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;y&quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;levels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;120&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/assets/2022-02-22-contour/contour_3.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now, the figure is awesome, we now can see clearly the 4 local minima.&lt;/p&gt;

&lt;h2 id=&quot;the-final-attempt&quot;&gt;The final attempt&lt;/h2&gt;

&lt;p&gt;Black and white figure is boring. We want to have some color to make it more exciting.&lt;/p&gt;

&lt;div class=&quot;language-julia highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;contour&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;range_x&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;range_y&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xlabel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ylabel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;y&quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;levels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;120&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/assets/2022-02-22-contour/contour_4.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Finally, the contour plot is 90% on par with the one on Wikipedia page.&lt;/p&gt;

&lt;h2 id=&quot;closing-thoughts&quot;&gt;Closing thoughts&lt;/h2&gt;

&lt;p&gt;For me, to have good contour plot, there are two points we need to consider&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;First, we need to find the good value range of variable. Specifically, within this range, the function should have local maxima and minima.&lt;/li&gt;
  &lt;li&gt;Second, we should somehow predict the min/max value of function so that we can put these values for parameter &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;levels&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
            <pubDate>Tue, 22 Feb 2022 00:00:00 +0000</pubDate>
            <link>http://tndoan.github.io/2022/02/22/contour/</link>
            <guid isPermaLink="true">http://tndoan.github.io/2022/02/22/contour/</guid>
            
            
        </item>
        
        <item>
            <title>Some thoughts after Converting ISLR book to Julialang</title>
            <description>&lt;p&gt;&lt;a href=&quot;https://statlearning.com/&quot;&gt;An Introduction to Statistical Learning with Applications in R&lt;/a&gt; (ISLR) is an excellent book for introducing statistical learning to beginners. Its authors are well-known researchers in this field but this book is very intuitive and easy to follow. As a &lt;a href=&quot;https://julialang.org/&quot;&gt;Julialang&lt;/a&gt; advocate, I really want to see the Julia-version of this book. For this reason, I have translated all its materials to Julia with some inspiration from the Python project - &lt;a href=&quot;https://github.com/JWarmenhoven/ISLR-python&quot;&gt;ISLR-python&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;My project is named &lt;a href=&quot;https://github.com/tndoan/ISLR.jl&quot;&gt;ISLR.jl&lt;/a&gt;. It is a long project since I got the idea 3 years ago but I only seriously devote my time to this project last month. After finishing (nearly) all, I proudly shared it in &lt;a href=&quot;https://www.reddit.com/r/Julia/comments/kdrlz2/i_convert_an_introduction_to_statistical_learning/&quot;&gt;/r/julia&lt;/a&gt; and surprisingly, my post is well received by other advocates. Right now, it is my most starred repo in Github.&lt;/p&gt;

&lt;p&gt;ISLR is a must-read book for beginners of data science so re-implementing the book in Julia is kind-of a proxy for me to measure the readiness of Julia eco-system for data science. In this post, I will share my thoughts on this topic.&lt;/p&gt;

&lt;h2 id=&quot;what-do-data-scientist-need&quot;&gt;What do data scientist need?&lt;/h2&gt;

&lt;p&gt;In my opinion, a programming language and its eco-system are ready for data scientists when they can quickly apply their standard techniques e.g. PCA, linear regression to &lt;strong&gt;&lt;em&gt;torture&lt;/em&gt;&lt;/strong&gt; and plot their given data. For this reason, data scientists require&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Good library of statistic&lt;/li&gt;
  &lt;li&gt;Good visualization library&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;the-good-of-julia&quot;&gt;The good of Julia&lt;/h2&gt;

&lt;p&gt;Julia is getting more and more attention from researchers and developers around the world. Hence, its eco-system is becoming more and more mature. It means all basic need for data science is ready in Julia. For example, if we want to use support vector machine, we have &lt;a href=&quot;https://github.com/JuliaML/LIBSVM.jl&quot;&gt;libsvm.jl&lt;/a&gt;. For visualization, we have &lt;a href=&quot;https://github.com/JuliaPlots/PlotDocs.jl&quot;&gt;Plots.jl&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In case of not having ready library, we are able to borrow from other programming languages. For example, &lt;a href=&quot;https://github.com/JuliaPy/PyCall.jl&quot;&gt;PyCall.jl&lt;/a&gt; allows us to call Python within Julia or &lt;a href=&quot;https://github.com/JuliaPy/PyPlot.jl&quot;&gt;PyPlot&lt;/a&gt; triggers matplotlib.&lt;/p&gt;

&lt;h2 id=&quot;the-bad-of-julia&quot;&gt;The bad of Julia&lt;/h2&gt;

&lt;p&gt;There are several disadvantage of Julia. Here are my thoughts.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;The poor document&lt;/strong&gt;: Since Julia is quite &lt;em&gt;new&lt;/em&gt;, the maintainers of its libraries somehow do not care much about document. It is very hard when we encounter some issues during our coding process. For example, I have to dig into the source code of &lt;em&gt;Plots.jl&lt;/em&gt; to find out how to draw &lt;a href=&quot;https://github.com/JuliaPlots/Plots.jl/blob/3725a8d3875df2b45186bf248bdc7cda191969d5/src/shorthands.jl&quot;&gt;vertical/horizontal lines&lt;/a&gt;. An other example is that Generalized linear model is extremely popular in stat community but the &lt;a href=&quot;https://juliastats.org/GLM.jl/stable/&quot;&gt;document of GLM.jl&lt;/a&gt; is not that useful, I have to code and guess multiple times. Hopefully, the document becomes more reliable for users in future.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;The fragment of library&lt;/strong&gt;: In Python, &lt;a href=&quot;https://matplotlib.org/&quot;&gt;matplotlib&lt;/a&gt; is a de-facto library for visualization. However, I have found that besides &lt;em&gt;Plots.jl&lt;/em&gt;, I still need to use &lt;a href=&quot;https://github.com/JuliaPlots/StatsPlots.jl&quot;&gt;StatsPlots.jl&lt;/a&gt; for some plots related to statistic. Moreover, we do not have a standard library which is similar to &lt;em&gt;sklearn&lt;/em&gt; in Python. There are some efforts such as &lt;a href=&quot;https://github.com/alan-turing-institute/MLJ.jl&quot;&gt;MLJ.jl&lt;/a&gt; but it is not enough since these libraries are still in early state.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;The integrity of eco-system&lt;/strong&gt;: I believe Julia libraries should be implemented in Julia to use all of its benefits. However, there are still many libraries implemented in Julia but behind the scenes, they call other programming language. For example, &lt;a href=&quot;https://github.com/cstjean/ScikitLearn.jl&quot;&gt;ScikitLearn.jl&lt;/a&gt; totally calls scikit-learn and some parts of &lt;em&gt;MLJ.jl&lt;/em&gt; are used other Python libraries. I understand that this approach can reduce the cost of development but in long run, I think it is better if the entire is done via Julia.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Julia is a fantastic language and I believe it is the future of scientific computing. It cannot replace Python or R in near future but it gradually gets traction of the community. Hopefully, I will see more and more libraries and tools written in Julia.&lt;/p&gt;
</description>
            <pubDate>Fri, 25 Dec 2020 00:00:00 +0000</pubDate>
            <link>http://tndoan.github.io/2020/12/25/lessons-learned-from-islr/</link>
            <guid isPermaLink="true">http://tndoan.github.io/2020/12/25/lessons-learned-from-islr/</guid>
            
            
        </item>
        
        <item>
            <title>Learning neural network from real courses</title>
            <description>&lt;p&gt;Today, neural network and deep learning are more and more popular but what materials to learn will create a little headache due to the huge numbers of resources online. Different people have different tastes. Someones like online tutorials, some take Coursera courses. For me, I prefer learning neural networks and its application from courses of some big universities (e.g. Stanford, Berkeley, CMU). I like courses with slides, lecture notes, lecture record and homeworks. For this reason, I summarize these courses, categorize them according to their difficulty; I try to keep the post updated.&lt;/p&gt;

&lt;h2 id=&quot;beginner&quot;&gt;Beginner&lt;/h2&gt;

&lt;p&gt;In this section, I will list some courses that provide foundation for neural network learning.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://bcourses.berkeley.edu/courses/1478831&quot;&gt;Designing, Visualizing and Understanding Deep Neural Networks&lt;/a&gt; from UC Berkeley.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://web.stanford.edu/class/stats202/&quot;&gt;Stats 202: Data Mining and Analysis&lt;/a&gt; from Stanford. They also include some solutions for homework.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;intermediate&quot;&gt;Intermediate&lt;/h2&gt;

&lt;p&gt;After learning foundation about neural network, we move to some of its applications&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;http://web.stanford.edu/class/cs224n/&quot;&gt;CS224n: Natural Language Processing with Deep Learning&lt;/a&gt; from Stanford (Winter 2019). The note and materials are fantastic. Chris Manning is also a big name in the field. Must try course.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://vision.stanford.edu/teaching/cs231n/&quot;&gt;CS231n: Convolutional Neural Networks for Visual Recognition&lt;/a&gt; from Stanford (2016). An other masterpiece. It will help you understand the whole neural network application for computer vision.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;advanced&quot;&gt;Advanced&lt;/h2&gt;

&lt;p&gt;We move forward to more advanced technique of deep learning.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;http://rail.eecs.berkeley.edu/deeprlcourse/&quot;&gt;CS 285 Deep Reinforcement Learning&lt;/a&gt; from Berkeley. It helps us to understand deep RL. There is another course from &lt;a href=&quot;http://web.stanford.edu/class/cs234/index.html&quot;&gt;Stanford&lt;/a&gt; but I feel that the materials of Stanford are not that details to follow.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://lagunita.stanford.edu/courses/HumanitiesSciences/StatLearning/Winter2016/about&quot;&gt;Statistical Learning&lt;/a&gt; from Stanford. It is so advanced, be careful.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://see.stanford.edu/Course/EE364A&quot;&gt;Convex Optimization&lt;/a&gt;. The instructor is Stephen Boyd. He is a god in this field. Follow the &lt;a href=&quot;https://see.stanford.edu/Course/EE364B&quot;&gt;sequence&lt;/a&gt; if you enjoy the course.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;bonus&quot;&gt;Bonus&lt;/h2&gt;

&lt;p&gt;Some useful materials for neural network&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://nlp.seas.harvard.edu/2018/04/03/attention.html&quot;&gt;The Annotated Transformer&lt;/a&gt; from Harvard. It is for us to understand Transformer - an attention neural network.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://bair.berkeley.edu/blog/2017/08/31/saddle-efficiency/&quot;&gt;How to Escape Saddle Points Efficiently&lt;/a&gt; from Berkeley. I think the title says all, I dont need to explain :)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hopefully, these courses are useful for your study.&lt;/p&gt;
</description>
            <pubDate>Sat, 09 Nov 2019 00:00:00 +0000</pubDate>
            <link>http://tndoan.github.io/2019/11/09/deep-learning-resource/</link>
            <guid isPermaLink="true">http://tndoan.github.io/2019/11/09/deep-learning-resource/</guid>
            
            
        </item>
        
        <item>
            <title>Install Google Chrome inside Docker on Ubuntu 16.04 LTS</title>
            <description>&lt;p&gt;Google Chrome is a great browser with a huge number of plug-in. I think I dont need to advertise more for Chrome since it already has a huge popularity nowadays.&lt;/p&gt;

&lt;p&gt;Last week, one question raised from my head “How can I install and use Google Chrome browser via Docker?” and this question got stuck inside me for a quite time. Let me explain my scenario a little bit about the reason of this question.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;I want to have the second Chrome installed in my computer. Searching around does not give me any positive results.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;I want to have the safe browsing. Some examples are testing new interesting plug-ins of Chrome or checking some websites.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All of the requirements are satisfied by Docker but running GUI apps via Docker is quite tough task. After searching around, I have found the &lt;a href=&quot;https://blog.jessfraz.com/post/docker-containers-on-the-desktop/&quot;&gt;instruction of Jessie Frazelle&lt;/a&gt;. She created some Dockerfile for the easy of using Chrome via Docker. I follow her guide with some modification to be able to run in my computer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; Install image using her Dockerfile and run&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker run -it --net host --cpuset-cpus 0  -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY -v $HOME/Downloads:/root/Downloads --name chrome --cap-add=SYS_ADMIN jess/chrome
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Below are step by step guide and some explanation&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;Pull her Dockerfile and create and image in our machine &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker pull jess/chrome&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Run the docker run command above to open Chrome. Notice that I have changed some arguments from the original recommendation of Jessie.&lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;To overcome the error &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Failed to move to new namespace: PID namespaces supported, Network namespace supported, but failed: errno = Operation not permitted&lt;/code&gt;, we need to add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--cap-add=SYS_ADMIN&lt;/code&gt; to assign some admin capacities for docker containers to control the host machine. There are a &lt;a href=&quot;https://docs.docker.com/engine/reference/run/#pid-settings-pid&quot;&gt;little documents&lt;/a&gt; about this docker option. I am not sure if it makes any harms for the host machine but from &lt;a href=&quot;https://serverfault.com/questions/824809/chrome-under-docker-cap-sys-admin-vs-privileged&quot;&gt;the discussion here&lt;/a&gt;, we can trust this option over &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--privileged&lt;/code&gt; one.&lt;/li&gt;
      &lt;li&gt;I use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-e DISPLAY&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-e DISPLAY=unix$DISPLAY&lt;/code&gt; since docker is intelligent enough to automatically select our current display and make use. In her setting, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unix$DISPLAY&lt;/code&gt; is a little bit unique for her computer. To check the X display, you can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xauth list&lt;/code&gt; to see if it matches the default setting. If not match, we usually get the error&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  No protocol specified

  (google-chrome:1): Gtk-WARNING **: cannot open display: unix:0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That is all. Hura, now we can use the second Chrome in our computer within docker.&lt;/p&gt;
</description>
            <pubDate>Mon, 16 Oct 2017 00:00:00 +0000</pubDate>
            <link>http://tndoan.github.io/2017/10/16/Chrome-in-Docker/</link>
            <guid isPermaLink="true">http://tndoan.github.io/2017/10/16/Chrome-in-Docker/</guid>
            
            
        </item>
        
        <item>
            <title>Shapefile drawing using Python</title>
            <description>&lt;p&gt;Last week, I needed to draw some maps and display data on these maps also. I have spent some hours to discover how to complete this task. Thus, in this post, I will summarize step by step to accomplish the initial goal.&lt;/p&gt;

&lt;h2 id=&quot;basic-drawing&quot;&gt;Basic drawing&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;First time first, we need to download the shapefile before doing anything. Luckily, &lt;a href=&quot;https://www.openstreetmap.org&quot;&gt;&lt;strong&gt;Open street map&lt;/strong&gt;&lt;/a&gt; is an open source data for us to download the shapefile of boundary of many countries. However, there are &lt;a href=&quot;http://www.gadm.org/country&quot;&gt;some services&lt;/a&gt; extracted the shapefile of each countries for us. The data is quite complete and ready to use. I also put the demo data of Singapore &lt;a href=&quot;/assets/2016_03_08/SGP_adm0.shp&quot;&gt;link1&lt;/a&gt; &lt;a href=&quot;/assets/2016_03_08/SGP_adm0.dbf&quot;&gt;link2&lt;/a&gt; &lt;a href=&quot;/assets/2016_03_08/SGP_adm0.shx&quot;&gt;link3&lt;/a&gt;, and they are used to illustrate latter steps (please download the whole three).&lt;/li&gt;
  &lt;li&gt;Secondly, we need to choose the library to draw. &lt;a href=&quot;http://matplotlib.org/basemap/&quot;&gt;BaseMap&lt;/a&gt; is a common lib for drawing shapefile but it is overkilled in my situation i.e. I do not need to use its fancy features. Thus, &lt;a href=&quot;https://pypi.python.org/pypi/pyshp&quot;&gt;pyshp&lt;/a&gt; is my selection. It is lightweight but still powerful enough to satisfy my requirement. Moreover, installation and usage are as easy as ABC :-) . Last but not least, I have used Python 2.7 and matplotlib 1.4.2 to draw but the conversion to Python 3.x is not hard.&lt;/li&gt;
  &lt;li&gt;Let us draw :-D&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# import libraries
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;shapefile&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shp&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;#load shapefile
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'SGP_adm0.shp'&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;figure&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;shape&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shapeRecords&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# end index of each components of map
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parts&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;len_l&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;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# how many parts of countries i.e. land and islands
&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;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;mi&quot;&gt;0&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;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;points&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:]]&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# list of latitude
&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;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;mi&quot;&gt;1&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;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;points&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:]]&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# list of longitude
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&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;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# ensure the closure of the last component
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&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;len_l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# draw each component of map.
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# l[k] to l[k + 1] is the range of points that make this component
&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;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;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&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;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&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;s&quot;&gt;'k-'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# display
&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;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The final result is not bad.
&lt;img src=&quot;/assets/2016_03_08/plain_Singapore.png&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;display-histogram&quot;&gt;Display histogram&lt;/h2&gt;

&lt;p&gt;Using the same setup as above, we will display the histogram in 2D map to illustrate the density. There are some tricks to complete the task so let us view the code first.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;shapefile&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shp&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mpl&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;mpl_toolkits.axes_grid1&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;make_axes_locatable&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;drange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&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;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&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;mf&quot;&gt;0.0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&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;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;mf&quot;&gt;1.0&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;nb&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&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;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&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;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&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;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;readLoc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fname&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;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'r'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readlines&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;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;close&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;nb&quot;&gt;list&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;nb&quot;&gt;list&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;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;comp&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;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&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;','&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;append&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;comp&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;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&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;comp&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;k&quot;&gt;return&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;c1&quot;&gt;#load shapefile
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'SGP_adm0.shp'&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;figure&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;shape&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shapeRecords&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parts&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;len_l&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;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# how many parts of countries i.e. land and
&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;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;mi&quot;&gt;0&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;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;points&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;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;mi&quot;&gt;1&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;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;points&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:]]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&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;x&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;k&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;len_l&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;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;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&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;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&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;s&quot;&gt;'k-'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;lower_x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;103.6&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;lower_y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.2&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;upper_x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;104.1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;upper_y&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;c1&quot;&gt;# plot the frame of city
&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;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lower_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;upper_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;upper_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lower_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;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower_y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;upper_y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;upper_y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lower_y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lower_y&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;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;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# load data
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fname&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'1.35_103.9_1.4_103.95'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;comp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fname&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;'_'&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;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;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;n&quot;&gt;comp&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;readLoc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'user_density.txt'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# plot the line
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;area_x&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;loc&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;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;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;loc&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;loc&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;area_y&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;loc&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;loc&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;n&quot;&gt;loc&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;n&quot;&gt;loc&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;loc&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;# plot a box in map
&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;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;area_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;area_y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'b--'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# histogram
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xedges&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;drange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;upper_x&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;c1&quot;&gt;# 0.01 is the size of bin
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;yedges&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;drange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower_y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;upper_y&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;n&quot;&gt;H&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xedges&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;yedges&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;histogram2d&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;bins&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;xedges&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;yedges&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;im&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;imshow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;H&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;n&quot;&gt;cmap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'OrRd'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;interpolation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'bilinear'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;origin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'low'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;extent&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;xedges&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;xedges&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;yedges&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;yedges&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;c1&quot;&gt;# add label to axis and do not scale unit of x, y axises
&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;ylabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Latitude'&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;20&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;xlabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Longitude'&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;20&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;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gca&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;ticklabel_format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;useOffset&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;divider&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;make_axes_locatable&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;# set size of color bar
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;divider&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append_axes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;right&quot;&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;s&quot;&gt;&quot;5%&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pad&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;c1&quot;&gt;# set size of color bar
&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;colorbar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;                                  &lt;span class=&quot;c1&quot;&gt;# set size of color bar 
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# finally, show the plot
&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;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_density.txt&lt;/code&gt; could be downloaded via &lt;a href=&quot;/assets/2016_03_08/user_density.txt&quot;&gt;this link&lt;/a&gt;. The data inside this file is generated randomly. Each line of file contains 2 information latitude and longitude of each point and both are separated via comma. Function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readLoc&lt;/code&gt; is used to load its data.&lt;/p&gt;

&lt;p&gt;There are 2 important notes for the above code&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;I do not use the normal order in numpy manual which is (y, x)&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;H&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xedges&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;yedges&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;histogram2d&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;bins&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;xedges&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;yedges&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;We use the transpose matrix of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;H.T&lt;/code&gt; when using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;imshow&lt;/code&gt;. Option &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bilinear&lt;/code&gt; is used to make it look like heatmap.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;im&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;imshow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;H&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;n&quot;&gt;cmap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'OrRd'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;interpolation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'bilinear'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;origin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'low'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;extent&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;xedges&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;xedges&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;yedges&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;yedges&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The final result is quite beautiful
&lt;img src=&quot;/assets/2016_03_08/hist_Singapore.png&quot; /&gt;&lt;/p&gt;
</description>
            <pubDate>Tue, 08 Mar 2016 00:00:00 +0000</pubDate>
            <link>http://tndoan.github.io/2016/03/08/python-shapefile/</link>
            <guid isPermaLink="true">http://tndoan.github.io/2016/03/08/python-shapefile/</guid>
            
            
        </item>
        
        <item>
            <title>Periodic Mobility Model</title>
            <description>&lt;h2 id=&quot;disclamer&quot;&gt;Disclamer&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;This post is giant wall of text and formula. I am not sure it is 100% correct so please read with your caution.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://cs.stanford.edu/people/jure/pubs/mobile-kdd11.pdf&quot;&gt;“Friendship and Mobility: User movement in location-based social networks”&lt;/a&gt; is an interesting paper which discusses about user movement. I think its intuition is quite clear and trivial but it is (maybe) the first paper which separates the location of users into two clusters named &lt;em&gt;Home&lt;/em&gt; and &lt;em&gt;Work&lt;/em&gt;. The data analysis of this paper is very nice. After all, the authors proposed a model name &lt;em&gt;Periodic Mobility Model&lt;/em&gt; aka &lt;em&gt;PMM&lt;/em&gt; to predict the &lt;em&gt;Home&lt;/em&gt; and &lt;em&gt;Work&lt;/em&gt; location of users. The whole process is follow&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Firstly, given the time of day, it will define which is the current cluster (i.e. &lt;em&gt;Home&lt;/em&gt; or &lt;em&gt;Work&lt;/em&gt;) of user.&lt;/li&gt;
  &lt;li&gt;Secondly, based on cluster, the location of user is drawn.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Before going to the formula, let me introduce the symbol system&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;\(c_i = k\) means the check-in \(i\) is in cluster \(k\). In the original paper, \(k = 1 ; 2\) because there are only two clusters.&lt;/li&gt;
  &lt;li&gt;\(l_i\) is the location (latitude and logitude) of check-in \(i\).&lt;/li&gt;
  &lt;li&gt;\(t_i\) is the time of check-in \(i\).&lt;/li&gt;
  &lt;li&gt;\(x_i\) represents the check-in \(i\). Formally, it is a pair of location and time of check-in \(x_i = (l_i, t_i) \).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;the-first-attempt&quot;&gt;The first attempt&lt;/h2&gt;

&lt;p&gt;The joint distribution of each check-in will be calculated by&lt;/p&gt;

\[\begin{align}
p(c\_i = k, l\_i, t\_i \| \theta) &amp;amp;= p(l\_i \| c\_i = k, t\_i) \; p(t\_i, c\_i = k) \\\\
&amp;amp;= p(l\_i \| c\_i = k) \; p(c\_i = k \| t\_i ) \; p(t\_i)
\end{align}\]

&lt;p&gt;Taking the &lt;em&gt;log&lt;/em&gt; becomes&lt;/p&gt;

\[\begin{align}
\log p(c\_i = k, l\_i, t\_i \| \theta) =&amp;amp; \log p(l\_i \| c\_i = k) + \log p(c\_i = k \| t\_i ) + \log p(t\_i) \\\\
=&amp;amp; \log \mathcal{N}(\mu\_k, \Sigma\_k) + \log N\_k(t\_i) - \log \sum\_{k' = 1}^K N\_{k'}(t\_i) + \log p(t\_i) \\\\
=&amp;amp; - \frac{\log \| \Sigma\_k \|}{2} - \frac{1}{2} (l\_i - \mu\_k)^T \Sigma\_k^{-1} (l\_i - \mu\_k)  \\\\
&amp;amp; +  \log P\_{c\_k} - \log \sigma\_k - (\frac{\pi}{12})^2 \frac{(t\_i - \tau\_k)^2 }{2 \sigma\_k^2} \\\\
&amp;amp; + \log \sum\_{k'=1}^K N\_{k'}(t\_i) + const
\end{align}\]

&lt;p&gt;Follow the EM-algorithm, we must calculate&lt;/p&gt;

\[\begin{align}
p(c\_i = k \| l\_i\; t\_i) &amp;amp;= \frac{p(l\_i \| c\_i = k) \; p(c\_i = k \| t\_i ) \; p(t\_i)}{\sum\_{k' = 1}^K p(l\_i \; t\_i; \; c\_i= k')} \\\\
&amp;amp;= \frac{ p(l\_i \| c\_i = k) \; p( c\_i = k \| t\_i) }{\sum\_{k'=1}^K p(l\_i \| c\_i = k') \; p(c\_i = k' \| t\_i) } \\\\
&amp;amp;= \gamma\_{ik}
\end{align}\]

&lt;p&gt;where&lt;/p&gt;

\[\begin{align}
p(l\_i \| c\_i = k) &amp;amp;\sim \mathcal{N}(\mu\_k, \Sigma\_k) \\\\
p( c\_i = k \| t\_i) &amp;amp;= \frac{N\_k}{\sum\_{k'=1}^K N\_{k'}} \\\\
N\_k &amp;amp;= \frac{P\_{c\_k}}{\sqrt{2\pi \sigma^2\_k}} \exp ( - \(\frac{\pi}{12}\)^2 \frac{(t\_i - \tau\_k)^2}{2\sigma^2\_k} ) 
\end{align}\]

\[\begin{align}
Q(\theta, \; \theta^{old}) &amp;amp;= \mathbb{E}\_{C \| X, \theta^{old}} [ L (\theta; X, C)] \\\\
&amp;amp;= \sum\_{i=1}^N \sum\_{k=1}^K p(c\_i = k \| x\_i, \theta^{old}) \; \log p(x\_i, \; c\_i = k \| \theta) \\\\
\end{align}\]

&lt;p&gt;Updating \( \mu_k \) and \(\Sigma_k\) are similar to Gaussian mixture model so we have&lt;/p&gt;

\[\begin{align}
\frac{\partial Q}{\partial \mu\_k} &amp;amp;= \sum\_{i=1}^N \gamma\_{ik}[ \Sigma\_k^{-1} (l\_i - \mu\_k)] = 0 \\\\
\mu\_k &amp;amp;= (\sum\_{i=1}^N \gamma\_{ik})^{-1} \; \sum\_{i=1}^N \gamma\_{ik} \; l\_i
\end{align}\]

\[\begin{align}
\frac{\partial Q}{\partial \Sigma\_k} &amp;amp;= \sum\_{i=1}^N \frac{\gamma\_{ik}}{2} [ -\Sigma\_k^{-1} + \Sigma\_k^{-1} (l\_i - \mu\_k)(l\_i - \mu\_k)^T   \Sigma\_k^{-1}] = 0 \\\\
 \Sigma\_k &amp;amp;=\frac{\sum\_{i=1}^N \gamma\_{ik} (l\_i - \mu\_k) \; (l\_i - \mu\_k)^T }{ \sum\_{i=1}^N \gamma\_{ik}} \\\\
\end{align}\]

&lt;p&gt;We take the differentiation of Q over other parameters&lt;/p&gt;

\[\begin{align}
\frac{\partial Q}{\partial P\_{c\_k}} &amp;amp;= \sum\_{i=1}^N \frac{\gamma\_{ik}}{P\_{c\_k}} [1 + \frac{N\_k(t\_i)}{\sum\_{k'=1}^K N\_k' (t\_i)}] \\\\
\end{align}\]

\[\begin{align}
\frac{\partial Q}{\partial \tau\_k} &amp;amp;=\frac{1}{\sigma\_k^2} (\frac{\pi}{12})^2 \sum\_{i=1}^N \gamma\_{ik}(t\_i - \tau\_k) [1 + \frac{N\_k(t\_i)}{\sum\_{k'=1}^K N\_k' (t\_i)}] \\\\
\end{align}\]

\[\begin{align}
\frac{\partial Q}{\partial \sigma\_k} &amp;amp;= \sum\_{i=1}^N \gamma\_{ik} [1 + \frac{N\_k(t\_i)}{\sum\_{k'=1}^K N\_k' (t\_i)}] [-\sigma\_k^{-1} + (\frac{\pi}{12})^2 \frac{(t\_i - \tau\_k)^2}{\sigma\_k^3} ] \\\\
\end{align}\]

&lt;p&gt;Setting them equal to 0 cannot give us a closed form solution because \(N_k(t_i)\) contains all other parameters inside. Moreover, there are some conditions that we need to follow. First of all, \(P_{c_k}\) is positive and the sum over &lt;em&gt;k&lt;/em&gt; must be equal to 1. In other word, it is belong to the simplex. Secondly, \(\tau_k\) is time so it must in range of 0 and 24. Finally, \(\sigma_k\) should be positive also.&lt;/p&gt;

&lt;p&gt;Optimizing Q with these constraints is the pain in the neck. Moreover, I could not find the closed form solution. It is quite weird because the authors claimed that they could derive the closed form solution.&lt;/p&gt;

&lt;h2 id=&quot;another-look-of-model&quot;&gt;Another look of model&lt;/h2&gt;

\[\begin{align}
p(c\_i = k, l\_i, t\_i \| \theta) &amp;amp;= p(l\_i \| c\_i = k, t\_i) \; p(t\_i, c\_i = k) \\\\
&amp;amp;= p(l\_i \| c\_i = k) \; p(t\_i \| c\_i = k) \; p(c\_i = k)
\end{align}\]

&lt;p&gt;where&lt;/p&gt;

\[\begin{align}
p(l\_i \| c\_i = k) &amp;amp;\sim \mathbb{N}(\mu\_k, \Sigma\_k) \\\\
p(t\_i \| c\_i = k) &amp;amp;=\frac{1}{\sqrt{2\pi \sigma^2\_k}} \exp ( - \(\frac{\pi}{12}\)^2 \frac{(t\_i - \tau\_k)^2}{2\sigma^2\_k} ) \\\\
p(c\_i = k) &amp;amp;= P\_{c\_k}
\end{align}\]

&lt;p&gt;This viewpoint does not follow the original intuition of model. In this modification, user will choose his/her cluster first (&lt;em&gt;Home&lt;/em&gt; or &lt;em&gt;Work&lt;/em&gt;) and from this cluster, he/she will choose time and location of checkin.&lt;/p&gt;

&lt;p&gt;To calculate for the first step of EM
\(\begin{align}
p(c\_i = k \| l\_i\; t\_i) &amp;amp;= \frac{ p(l\_i \; t\_i \| c\_i = k) \; p(c\_i = k)}{\sum\_{k' = 1}^K p(l\_i \; t\_i; \; c\_i= k')} \\\\
&amp;amp;= \frac{ p(l\_i \| c\_i = k) \; p(t\_i \| c\_i = k) \; p(c\_i = k)}{\sum\_{k'=1}^K p(l\_i \| c\_i = k') \; p(t\_i \| c\_i = k') \; p(c\_i = k')} \\\\
&amp;amp;= \gamma\_{ik}
\end{align}\)&lt;/p&gt;

\[\begin{align}
\log p(l\_i, \; t\_i, \; c\_i = k) &amp;amp;= \log p(l\_i \| c\_i = k) + \log p(t\_i \| c\_i = k) + \log P\_{c\_k} \\\\
&amp;amp;= -\frac{1}{2} \log \| \Sigma\_k\| -\frac{1}{2} (l\_i - \mu\_k)^T \Sigma\_k^{-1} (l\_i - \mu\_k) - \log \sigma\_k - (\frac{\pi}{12})^2 \frac{(t\_i - \tau\_k)^2}{2 \sigma^2\_k} + \log P\_{c\_k} + const
\end{align}\]

\[\begin{align}
Q(\theta, \; \theta^{old}) &amp;amp;= \mathbb{E}\_{C \| X, \theta^{old}} [ L (\theta; X, C)] \\\\
&amp;amp;= \sum\_{i=1}^N \sum\_{k=1}^K p(c\_i = k \| x\_i, \theta^{old}) \; \log p(x\_i, \; c\_i = k \| \theta) \\\\
&amp;amp;= \sum\_{i=1}^N \sum\_{k=1}^K \gamma\_{ik} [ -\frac{1}{2} \log \| \Sigma\_k\| -\frac{1}{2} (l\_i - \mu\_k)^T \Sigma\_k^{-1} (l\_i - \mu\_k) - \log \sigma\_k - (\frac{\pi}{12})^2 \frac{(t\_i - \tau\_k)^2}{2 \sigma^2\_k} + \log P\_{c\_k}]
\end{align}\]

&lt;p&gt;We have one constraint that \(\sum\_{k=1}^K P\_{c\_k} = 1\) so apply Lagrange multiplier we have 
\(Q(\theta, \; \theta^{old})= \sum\_{i=1}^N \sum\_{k=1}^K \gamma\_{ik} [ -\frac{1}{2} \log \| \Sigma\_k\| -\frac{1}{2} (l\_i - \mu\_k)^T \Sigma\_k^{-1} (l\_i - \mu\_k) - \log \sigma\_k - (\frac{\pi}{12})^2 \frac{(t\_i - \tau\_k)^2}{2 \sigma^2\_k} + \log P\_{c\_k}] + \lambda ( \sum\_{k=1}^K P\_{c\_k} - 1)\)&lt;/p&gt;

&lt;p&gt;To optimize \(P_{c_k}\) we take the derivative of Q and set it to 0. Moreover, taking advantage of the contraint to infer the value of \(\lambda\) and plug it back to find the final value of \(P_{c_k}\)&lt;/p&gt;

\[\begin{align}
\frac{\partial Q}{\partial P\_{c\_k}} &amp;amp;= \sum\_{i=1}^N \gamma\_{ik} \frac{1}{P\_{c\_k}} + \lambda = 0 \\\\
\lambda \; P\_{c\_k} &amp;amp;=- \sum\_{i=1}^N\gamma\_{ik} \\\\
\lambda &amp;amp;= - \sum\_{i=1}^N \sum\_{k=1}^K \gamma\_{ik} \\\\
P\_{c\_k} &amp;amp;= \frac{\sum\_{i=1}^N\gamma\_{ik}}{\sum\_{i=1}^N \sum\_{k=1}^K \gamma\_{ik} }
\end{align}\]

&lt;p&gt;We do the same to optimize \(\tau_k\)&lt;/p&gt;

\[\begin{align}
\frac{\partial Q}{\partial \tau\_k} &amp;amp;= \sum\_{i=1}^N \gamma\_{ik}[( \frac{\pi}{12})^2 2 \frac{t\_i - \tau\_k}{2 \sigma^2\_k} ] = 0 \\\\
\sum\_{i=1}^N \gamma\_{ik} \; t\_i &amp;amp;= \sum\_{i=1}^N \gamma\_{ik} \tau\_k \\\\
\tau\_k &amp;amp;= \frac{\sum\_{i=1}^N \gamma\_{ik} \; t\_i}{ \sum\_{i=1}^N \gamma\_{ik}}  \\\\
\end{align}\]

&lt;p&gt;Note that using this formula \(\tau_k\) is still in range 0 and 24.&lt;/p&gt;

&lt;p&gt;Below is the update rule for \(\sigma_k^2\)&lt;/p&gt;

\[\begin{align}
\frac{\partial Q}{\partial \sigma\_k} &amp;amp;= \sum\_{i=1}^N \gamma\_{ik}[-\frac{1}{\sigma\_k} +  \frac{\pi^2 \; (t\_i - \tau\_k)^2}{12^2\; \sigma^3\_k} ] = 0 \\\\
\sigma\_k^2 &amp;amp;= (\frac{\pi}{12})^2 \frac{ \sum\_{i=1}^N \gamma\_{ik} (t\_i - \tau\_k)^2 }{ \sum\_{i=1}^N \gamma\_{ik}  }
\end{align}\]

&lt;p&gt;Updating \( \mu_k \) follows the rule&lt;/p&gt;

\[\begin{align}
\frac{\partial Q}{\partial \mu\_k} &amp;amp;= \sum\_{i=1}^N \gamma\_{ik}[ \Sigma\_k^{-1} (l\_i - \mu\_k)] = 0 \\\\
\mu\_k &amp;amp;= (\sum\_{i=1}^N \gamma\_{ik})^{-1} \; \sum\_{i=1}^N \gamma\_{ik} \; l\_i
\end{align}\]

&lt;p&gt;Last but not least, \(\Sigma_k\)&lt;/p&gt;

\[\begin{align}
\frac{\partial Q}{\partial \Sigma\_k} &amp;amp;= \sum\_{i=1}^N \frac{\gamma\_{ik}}{2} [ -\Sigma\_k^{-1} + \Sigma\_k^{-1} (l\_i - \mu\_k)(l\_i - \mu\_k)^T   \Sigma\_k^{-1}] = 0 \\\\
 \Sigma\_k &amp;amp;=\frac{\sum\_{i=1}^N \gamma\_{ik} (l\_i - \mu\_k) \; (l\_i - \mu\_k)^T }{ \sum\_{i=1}^N \gamma\_{ik}} \\\\
\end{align}\]

</description>
            <pubDate>Thu, 06 Aug 2015 22:30:00 +0000</pubDate>
            <link>http://tndoan.github.io/2015/08/06/PMM/</link>
            <guid isPermaLink="true">http://tndoan.github.io/2015/08/06/PMM/</guid>
            
            
        </item>
        
        <item>
            <title>Stochastic Variance Reduced Gradient</title>
            <description>&lt;p&gt;Optimization has a long history but it is still developed by many researchers around the world. With the raise of machine learning and large dataset nowaday, optimization is now evoluating as the heart of parameter learning process. Researchers are trying to make it faster and more efficient.&lt;/p&gt;

&lt;p&gt;Among all algorithms, Stochastic Gradient Descent(SGD) is considered as a good choice. It does not require a huge memory and each of its step is &lt;em&gt;cheap&lt;/em&gt;. However, SGD is fast to converge to its local neighborhood rather than the optimal solution.  &lt;a href=&quot;http://stat.rutgers.edu/home/tzhang/papers/nips13-svrg.pdf&quot;&gt;Stochastic Variance Reduced Gradient(a.k.a SVRG)&lt;/a&gt; is a recent research work and could be viewed as a modified version of SGD. This algorithm will converge faster to optimal value by reducing the variance.&lt;/p&gt;

&lt;p&gt;The proposed algorithm SVRG is simple and elegent so I try to implement it and compare with SGD. Actually, we can download an implementation of SVRG from &lt;a href=&quot;http://riejohnson.com/svrg_download.html&quot;&gt;author website&lt;/a&gt; but it is written in C++ and not easy to match with the paper. For this reason, I try to &lt;em&gt;translate&lt;/em&gt; the algorithm from paper to program using &lt;a href=&quot;https://github.com/JuliaLang/julia&quot;&gt;&lt;strong&gt;Julia ver 0.3&lt;/strong&gt;&lt;/a&gt;. All of my implementation could be found in this &lt;a href=&quot;https://github.com/tndoan/Learning_Stuff/tree/master/StoGradDes&quot;&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;First of all, I create a training data of &lt;em&gt;n&lt;/em&gt; points:&lt;/p&gt;

\[w = [1; 2; 3; 4]\]

\[x = r + ra\]

\[y = w' * x\]

&lt;p&gt;where \(r \sim U(-50, 50)\) and \(ra \sim \mathcal{N}(0, 1)\). It means &lt;em&gt;r&lt;/em&gt; is drawn from uniform distribution from -50 to 50. This task is implemented in file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;genData.jl&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Secondly, I will minimize the squared loss of training data to get back vector &lt;em&gt;w&lt;/em&gt;. Formally, we have&lt;/p&gt;

\[P = \frac{1}{n} \sum\_{i=1}^n \phi\_i(w)\]

&lt;p&gt;where&lt;/p&gt;

\[\phi\_i = (w^T * x\_i - y\_i)^2\]

&lt;p&gt;Finally, \(w\_{opt} = argmin\_w P\). The algorithm is in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;svrg.jl&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In my running, &lt;em&gt;n&lt;/em&gt; is selected to be 100 000, update frequency \(m = 2n\), learning rate is 0.0001. To run the code, we could type to terminal&lt;/p&gt;

&lt;div class=&quot;language-julia highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;julia&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jl&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The output will appear&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;17373.341317718146
2845.1973361489718
3087.7982712672765
1459.1467179096912
102.26535395599187
16.986501037489642
3.284287345559172
1.8046052335362346
0.8103649634668636
0.15111556209992194
0.10221744358936354
0.03395696393082646
0.0030038768910633142
0.000374384037301745
1.9358523058164334e-5
6.338155215045428e-6
[0.9999890714244761,2.0000090291730728,3.00007147492654,3.99995174501473]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;All the lines are value of objective function per each iteration and the last line is the final \(w\). It is nearly similar to the groundtruth so my implementation is 95% correct :-).&lt;/p&gt;

&lt;h1 id=&quot;some-thought-of-svrg&quot;&gt;Some thought of SVRG&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;The procedure is easy to understand and implement.&lt;/li&gt;
  &lt;li&gt;The proof is not so hard to follow but we need to be familiar with SGD first.&lt;/li&gt;
  &lt;li&gt;The learning rate of algorithm is still a parameter to tune. In the paper, authors advises us to chose learning rate \(\eta &amp;lt; \frac{1}{L}\) with L is Lipschitz constant. An inappropriate \(\eta\) could create divergence to our program. For example, applying learning rate \(\eta = 0.01\) will make the objective function increase over time. This condition will help SVRG to converge but in practice, finding L is hard.&lt;/li&gt;
  &lt;li&gt;In &lt;em&gt;backtracking&lt;/em&gt; SGD, the learning rate must be decreased over time to ensure the convergence. However, SVRG will keep the &lt;em&gt;suitable&lt;/em&gt; value of learning rate until the convergence so it could achieve the better result by reducing the variance.&lt;/li&gt;
&lt;/ul&gt;
</description>
            <pubDate>Thu, 16 Jul 2015 21:30:00 +0000</pubDate>
            <link>http://tndoan.github.io/2015/07/16/sgd-svrg/</link>
            <guid isPermaLink="true">http://tndoan.github.io/2015/07/16/sgd-svrg/</guid>
            
            
        </item>
        
        <item>
            <title>Logistic Regression: does label matter?</title>
            <description>&lt;p&gt;Logistic Regression&lt;/p&gt;
</description>
            <pubDate>Sun, 04 Jan 2015 00:00:00 +0000</pubDate>
            <link>http://tndoan.github.io/2015/01/04/logistic-regression/</link>
            <guid isPermaLink="true">http://tndoan.github.io/2015/01/04/logistic-regression/</guid>
            
            
        </item>
        
        <item>
            <title>Changing to blue Dropbox icons</title>
            <description>&lt;p&gt;Dropbox in Linux has been upgraded to &lt;a href=&quot;https://www.dropboxforum.com/hc/communities/public/questions/201488805-Stable-Build-3-0-3&quot;&gt;version 3&lt;/a&gt; with a lot of changes. However, it also uses a black/while color for the indicator. These icons, for me, are very ugly. I love the blue icons of Dropbox indicator so much so I decide to switch back to the blue versions.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;First of all, we need to download an older version of Dropbox such as &lt;a href=&quot;https://dl.dropboxusercontent.com/u/17/dropbox-lnx.x86-2.10.52.tar.gz&quot;&gt;2.10.52&lt;/a&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Then extract it. After extract, go to&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;downloadPath/.dropbox-dist/dropbox-lnx.x86-2.10.52/images/hicolor/16x16/status/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and move all of these images to override your current Dropbox icons in&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;~/.dropbox-dist/dropbox-lnx.x86_64-3.0.3/images/hicolor/16x16/status
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: After extracting, it is the hidden folder so press &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-H&lt;/code&gt; to show or just use terminal to navigate.&lt;/p&gt;

&lt;p&gt;Finally, open terminal and restart Dropbox&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;dropbox stop

dropbox start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Done. Dropbox icons are blue again :-)&lt;/p&gt;

</description>
            <pubDate>Fri, 12 Dec 2014 00:00:00 +0000</pubDate>
            <link>http://tndoan.github.io/2014/12/12/Dropbox-icon-color/</link>
            <guid isPermaLink="true">http://tndoan.github.io/2014/12/12/Dropbox-icon-color/</guid>
            
            
        </item>
        
    </channel>
</rss>
