<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>alykhantejani.comalykhantejani.com</title>
	<atom:link href="http://www.alykhantejani.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alykhantejani.com</link>
	<description>~ Just Another Blog</description>
	<lastBuildDate>Sun, 03 Mar 2013 00:31:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>An Introduction To Random Forests: The Theory</title>
		<link>http://www.alykhantejani.com/an-introduction-to-random-forests-the-theory/</link>
		<comments>http://www.alykhantejani.com/an-introduction-to-random-forests-the-theory/#comments</comments>
		<pubDate>Sat, 02 Mar 2013 23:20:08 +0000</pubDate>
		<dc:creator>alykhantejani</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.alykhantejani.com/?p=134</guid>
		<description><![CDATA[It&#8217;s been a while since I&#8217;ve blogged, especially about anything computer science related, so here goes. This is going to be a two part introduction to the machine learning technique/framework called Random Forests. Random forests the have gained much popularity in recent years owing to it&#8217;s ability to handle extremely large training data efficiently as [...]]]></description>
				<content:encoded><![CDATA[<p>It&#8217;s been a while since I&#8217;ve blogged, especially about anything computer science related, so here goes.</p>
<p>This is going to be a two part introduction to the machine learning technique/framework called <a href="http://en.wikipedia.org/wiki/Random_forest">Random Forests</a>. Random forests the have gained much popularity in recent years owing to it&#8217;s ability to handle extremely large training data efficiently as well as provide  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_7ba55e7c64a9405a0b39a1107e90ca94.gif' style='vertical-align: middle; border: none; ' class='tex' alt="O(n)" /></span><script type='math/tex'>O(n)</script>  test time efficiency.</p>
<p>Before we can talk about random forests we must understand decision trees, and decision tree learning. I feel that it&#8217;s much easier to learn through example, so I will go through a simple one from the book: <a href="http://www.amazon.co.uk/MACHINE-LEARNING-Mcgraw-Hill-International-Edit/dp/0071154671/ref=la_B000APT5O2_1_1?ie=UTF8&#038;qid=1361650143&#038;sr=1-1">Machine Learning by Tom Mitchell</a>.</p>
<p>The goal is to be able to predict whether or not to play tennis on a Saturday morning given the following attributes  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_344cc2acc14e02a9a7f33ab4085aaf20.gif' style='vertical-align: middle; border: none; ' class='tex' alt="\{weather outlook, temperature, humidity, wind\}" /></span><script type='math/tex'>\{weather outlook, temperature, humidity, wind\}</script> . In this example we will be performing supervised learning, which is where we are given labelled training data. Going back to our example, the labelled training would look something like this:</p>
<p>Note:The labels are the playTennis column (yes,no).</p>
<table style="border:1px solid">
<tr>
<td><b>Data Id</b></td>
<td><b>Weather Condition</b></td>
<td><b>Temperature</b></td>
<td><b>Humidity</b></td>
<td><b>Wind</b></td>
<td><b>PlayTennis</b></td>
</tr>
<tr>
<td>d1</td>
<td>Sunny</td>
<td>Hot</td>
<td>High</td>
<td>Weak</td>
<td>No</td>
</tr>
<tr>
<td>d2</td>
<td>Sunny</td>
<td>Hot</td>
<td>Normal</td>
<td>Strong</td>
<td>Yes</td>
</tr>
<tr>
<td>d3</td>
<td>Rain</td>
<td>Mild</td>
<td>Normal</td>
<td>Weak</td>
<td>No</td>
</tr>
</table>
<p>From this data we wish to build a decision tree which, such that for an input  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_33605fbf0fd52f418bc5277c8524404d.gif' style='vertical-align: middle; border: none; ' class='tex' alt="x=\{weather outlook, temperature, humidity, wind\}" /></span><script type='math/tex'>x=\{weather outlook, temperature, humidity, wind\}</script>  we can follow a path to a leaf node which contains the answer  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_707715cfcc1ec5bc92e5e9e700ea7ec5.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="{yes, no}" /></span><script type='math/tex'>{yes, no}</script> .</p>
<p>In order for us to reach the correct leaf node, at each non-leaf node (called <i>split nodes</i>), a function  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_e84fec1e074026d6fa8e3155482c35c3.gif' style='vertical-align: middle; border: none; ' class='tex' alt="g(x)" /></span><script type='math/tex'>g(x)</script>  is evaluated on the input and based on the result we branch either right or left until we reach a <em>leaf node</em> which will contain the result (to play tennis or not).</p>
<p>The question now is, what qualities should the functions  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_e84fec1e074026d6fa8e3155482c35c3.gif' style='vertical-align: middle; border: none; ' class='tex' alt="g(x)" /></span><script type='math/tex'>g(x)</script>  have and how do we generate a good function,  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_e84fec1e074026d6fa8e3155482c35c3.gif' style='vertical-align: middle; border: none; ' class='tex' alt="g(x)" /></span><script type='math/tex'>g(x)</script> , using the training data?</p>
<p>Well, Ideally we would just have one split node which would contain a test function that managed to split the training data into two sets,  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_a33b2f08929e874ab3629de72b6d0d55.gif' style='vertical-align: middle; border: none; ' class='tex' alt="S_i and S_j" /></span><script type='math/tex'>S_i and S_j</script> , such that all members  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_02da2a736a512660a6018cc00f4993e1.gif' style='vertical-align: middle; border: none; ' class='tex' alt="x_i \in S_i" /></span><script type='math/tex'>x_i \in S_i</script>  have attribute  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_581d62c89f9ffdd60395b2fbe09f3398.gif' style='vertical-align: middle; border: none; ' class='tex' alt="playTennis=No" /></span><script type='math/tex'>playTennis=No</script>  and  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_98bdaf9ec647dd64af2709c3401aaf85.gif' style='vertical-align: middle; border: none; ' class='tex' alt="x_j \in S_j" /></span><script type='math/tex'>x_j \in S_j</script>  have attribute  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_5fbe40445318fb160b8a4155c48a4c00.gif' style='vertical-align: middle; border: none; ' class='tex' alt="playTennis=Yes" /></span><script type='math/tex'>playTennis=Yes</script> . </p>
<p>This situation may arise in very simple problems, or training data that doesn&#8217;t fully represent the problem. For example, look at the graph below, it shows that the data points can easily be split into two classes by the simple function  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_c2da869d670b0a7081a1b80494d95261.gif' style='vertical-align: middle; border: none; ' class='tex' alt="f(x) = x>5" /></span><script type='math/tex'>f(x) = x>5</script> . However, in real life situations data is seldom separated this easily, in fact real data is rarely linearly separable and can possibly only be split by a high dimensional hyperplane represented by a very complex function. It is, in fact, the combination of the simple split node tests, that together form a highly non-linear separation of the data in to the desired classes.</p>
<div id="attachment_174" class="wp-caption aligncenter" style="width: 650px"><a href="http://www.alykhantejani.com/wp-content/uploads/2013/02/clusters.png"><img src="http://www.alykhantejani.com/wp-content/uploads/2013/02/clusters.png" alt="An example of linearly separable data" width="640" height="387" class="size-full wp-image-174" /></a><p class="wp-caption-text">An example of linearly separable data</p></div>
<p>Even though it is unlikely that just one split function will be able to partition the data perfectly, I hope it has become clear that we would like a split function that <i>maximizes</i> this separation. In fact, we wish to maximize the <i>information gain</i> at each split node. The information gain of the function at a split node is a measure of much the function decreases the uncertainty of the class label in the data. For example, given data  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_e3e99b700faa7944bf75c5115abf378f.gif' style='vertical-align: middle; border: none; ' class='tex' alt="\{a,a,a,a,b,b,b\}" /></span><script type='math/tex'>\{a,a,a,a,b,b,b\}</script> , a split function that splits the data into  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_d425374b263a482a7492f632e3f6c162.gif' style='vertical-align: middle; border: none; ' class='tex' alt="\{b, b,b\} \& \{a,a,a,a\}" /></span><script type='math/tex'>\{b, b,b\} \& \{a,a,a,a\}</script>  would have a higher information gain than one that splits it into  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_42264bae74e5fd3bc249249596449010.gif' style='vertical-align: middle; border: none; ' class='tex' alt="\{a,a,b,b\} \& \{a,a,b\}" /></span><script type='math/tex'>\{a,a,b,b\} \& \{a,a,b\}</script>  as clearly the uncertainty of class label is lower in the sets produced by the first split than the second.</p>
<p>More formally, information gain is defined as the change in entropy ( <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_c1d9f50f86825a1a2302ec2449c17196.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="H" /></span><script type='math/tex'>H</script> ) from a prior state (the training data at the node,  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_804f14414dab2297b600211a82c39fa8.gif' style='vertical-align: middle; border: none; ' class='tex' alt="S_i" /></span><script type='math/tex'>S_i</script> ) to a new state (the subsets generated  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_57322f88a90b4fc9a5da1f5d01a8f6ea.gif' style='vertical-align: middle; border: none; ' class='tex' alt="S_{i}" /></span><script type='math/tex'>S_{i}</script> ). This is denoted by the following formula (in this case we are using the <a href="http://en.wikipedia.org/wiki/Entropy_(information_theory)">Shannon Entropy</a> as our measure):</p>
<p><b>note</b>: <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_0d61f8370cad1d412f80b84d143e1257.gif' style='vertical-align: middle; border: none; ' class='tex' alt="C" /></span><script type='math/tex'>C</script>  is the set of all classes, in our example this is  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_830e9d06154ad978554be32a465543f2.gif' style='vertical-align: middle; border: none; ' class='tex' alt="\{yes,no\}" /></span><script type='math/tex'>\{yes,no\}</script> <br />
<center><br />
 <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_91cb46007ec8b996bf058e3bb75fc316.gif' style='vertical-align: middle; border: none; ' class='tex' alt="{IG(S) = H(S) -\sum\limits_{i}w_i \times H(S_i)}\\where:\\H(S) = \sum\limits_{c \in C}{P(c|S) \times log_2(P(c|S))}\\w_i = \frac{|S_i|}{|S|}" /></span><script type='math/tex'>{IG(S) = H(S) -\sum\limits_{i}w_i \times H(S_i)}\\where:\\H(S) = \sum\limits_{c \in C}{P(c|S) \times log_2(P(c|S))}\\w_i = \frac{|S_i|}{|S|}</script> <br />
</center></p>
<p>So, at each node we wish to choose the function that maximizes the information gain. Now, returning to decision tree learning, the following algorithm, called C4.5, is used to grow our tree:</p>
<ol>
<li>If we should terminate, create leaf node. Else</li>
<li>For each attribute a</li>
<li>Find the normalized information gain from splitting on a</li>
<li>Let <i>a_best</i> be the attribute with the highest information gain</li>
<li>Create a decision node that splits on <i>a_best</i></li>
<li>Recurse on the subsets obtained by splitting on <i>a_best</i>, and add those nodes as children of the current node</li>
</ol>
<p>Returning to our example, for our terminating criterion we will stop if all data samples at the node have the same class, which is all yes or all no in our case. So given the training data, we start at the root of the tree and check, for each attribute, the information gain. For example, given the 3 data examples at the top, using the attribute  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_7e25b972e192b01004b62346ee9975a5.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="wind" /></span><script type='math/tex'>wind</script> , we calculate the information gain as follows:</p>
<p>First, we calculate the entropy, <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_e1536e4b4a45add2709920424244e62f.gif' style='vertical-align: middle; border: none; ' class='tex' alt="H(S)" /></span><script type='math/tex'>H(S)</script> , of the whole set  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_857fd6c39171376e1b6f995bdae198bf.gif' style='vertical-align: middle; border: none; ' class='tex' alt="S = \{d1,d2,d3\}" /></span><script type='math/tex'>S = \{d1,d2,d3\}</script>  as follows:<br />
<center><br />
 <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_d3b31a198d522968a77f3ae8b4152d78.gif' style='vertical-align: middle; border: none; ' class='tex' alt="H(S) = \sum\limits_{c\in\{yes,no\}}P(c|S) \times log_2(P(c|S)" /></span><script type='math/tex'>H(S) = \sum\limits_{c\in\{yes,no\}}P(c|S) \times log_2(P(c|S)</script> </p>
<p> <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_dea532df205a28cbbee8a707f480a43c.gif' style='vertical-align: middle; border: none; ' class='tex' alt="H(S) = -(\frac{1}{3} \times log_2(\frac{1}{3}) + \frac{2}{3} \times log_2(\frac{2}{3}))" /></span><script type='math/tex'>H(S) = -(\frac{1}{3} \times log_2(\frac{1}{3}) + \frac{2}{3} \times log_2(\frac{2}{3}))</script> </p>
<p> <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_4dcd91dbbd911055b132ea7187e7ec41.gif' style='vertical-align: middle; border: none; ' class='tex' alt="H(S) = 0.91829583405" /></span><script type='math/tex'>H(S) = 0.91829583405</script> <br />
</center><br />
Split the data into two sets based on the possible values the attribute  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_7e25b972e192b01004b62346ee9975a5.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="wind" /></span><script type='math/tex'>wind</script>  can take ( <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_ce770e4d498273d6e378519a06bd57b6.gif' style='vertical-align: middle; border: none; ' class='tex' alt="\{weak, strong\}" /></span><script type='math/tex'>\{weak, strong\}</script> ). This produces the following sets  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_67727c8753981405fabb7f998fd49832.gif' style='vertical-align: middle; border: none; ' class='tex' alt="S_1 = \{d1, d3\} and S_2 = \{d2\}" /></span><script type='math/tex'>S_1 = \{d1, d3\} and S_2 = \{d2\}</script> , where  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_9f15cdedd8d76e4abb50732f5727065b.gif' style='vertical-align: middle; border: none; ' class='tex' alt="S_1" /></span><script type='math/tex'>S_1</script>  has  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_1b0e51f5a00f83a91736866157140911.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="wind=weak" /></span><script type='math/tex'>wind=weak</script>  and  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_a3de00c1597600a387128a7add5b354f.gif' style='vertical-align: middle; border: none; ' class='tex' alt="S_2" /></span><script type='math/tex'>S_2</script>  has  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_f79ef8115924a55566ec878e2979f552.gif' style='vertical-align: middle; border: none; ' class='tex' alt="wind=strong" /></span><script type='math/tex'>wind=strong</script> .<br />
<center><br />
 <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_5cce6f75b7464b259ac5357af93abfc2.gif' style='vertical-align: middle; border: none; ' class='tex' alt="H(S_1) = -(0 + \frac{2}{2} \times log_2(\frac{2}{2}) = 0" /></span><script type='math/tex'>H(S_1) = -(0 + \frac{2}{2} \times log_2(\frac{2}{2}) = 0</script> <br />
and  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_85ce197b02625bebb2d7bb9ce8891a59.gif' style='vertical-align: middle; border: none; ' class='tex' alt="w_1 = \frac{2}{3}" /></span><script type='math/tex'>w_1 = \frac{2}{3}</script>  as  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_9f15cdedd8d76e4abb50732f5727065b.gif' style='vertical-align: middle; border: none; ' class='tex' alt="S_1" /></span><script type='math/tex'>S_1</script>  has two of the three training data.</p>
<p> <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_d3d305c310a457a09cb8a84dda84f374.gif' style='vertical-align: middle; border: none; ' class='tex' alt="H(S_2) = -(\frac{1}{1} \times log_2(\frac{1}{1}) = 0 " /></span><script type='math/tex'>H(S_2) = -(\frac{1}{1} \times log_2(\frac{1}{1}) = 0 </script> <br />
and  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_26f9a4aae8c0bf23ca23518287a90070.gif' style='vertical-align: middle; border: none; ' class='tex' alt="w_2 = \frac{1}{3}" /></span><script type='math/tex'>w_2 = \frac{1}{3}</script> <br />
</center><br />
So the information gain is:<br />
<center></p>
<p> <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_c60e5dc528ec24b490f90c3a6a9db1a9.gif' style='vertical-align: middle; border: none; ' class='tex' alt="IG(S) = {H(S) -\sum\limits_{i=\{1,2\}}w_i \times H(S_i)}" /></span><script type='math/tex'>IG(S) = {H(S) -\sum\limits_{i=\{1,2\}}w_i \times H(S_i)}</script> </p>
<p> <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_0868411b24276e1b42f82614cbc40c89.gif' style='vertical-align: middle; border: none; ' class='tex' alt="IG(S) = 0.91829583405  (0 + 0) = 0.91829583405" /></span><script type='math/tex'>IG(S) = 0.91829583405  (0 + 0) = 0.91829583405</script> <br />
</center><br />
Similarly, we check for the attributes  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_37022e4e85a2e435012b04c34d0de75d.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="weather condition" /></span><script type='math/tex'>weather condition</script> ,  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_eef438f7e448aa48c1f8db568d27b591.gif' style='vertical-align: middle; border: none; ' class='tex' alt="temperature" /></span><script type='math/tex'>temperature</script>  and  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_43aa11d087acb432f887266cf9128bc3.gif' style='vertical-align: middle; border: none; ' class='tex' alt="humidity" /></span><script type='math/tex'>humidity</script> , which give us information gains of -0.08170416595,-0.08170416595 and -0.08170416595 respectively. As we are trying to maximize information gain, we choose  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_7e25b972e192b01004b62346ee9975a5.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="wind" /></span><script type='math/tex'>wind</script>  as the best attribute and split the data on this attribute.</p>
<p>We now recurse on the two subsets produced ( <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_9f15cdedd8d76e4abb50732f5727065b.gif' style='vertical-align: middle; border: none; ' class='tex' alt="S_1" /></span><script type='math/tex'>S_1</script>  &#038;  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_a3de00c1597600a387128a7add5b354f.gif' style='vertical-align: middle; border: none; ' class='tex' alt="S_2" /></span><script type='math/tex'>S_2</script>  as above), which we can see both contain data from just one class which matches our termination criterion. So, we have now how two leaf nodes, and have built our tree. </p>
<p>Given our tree, given a new input  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_9dd4e461268c8034f5c8564e155c67a6.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="x" /></span><script type='math/tex'>x</script> , we check the  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_7e25b972e192b01004b62346ee9975a5.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="wind" /></span><script type='math/tex'>wind</script>  attribute and if it is  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_7ecc19e1a0be36ba2c6f05d06b5d3058.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="weak" /></span><script type='math/tex'>weak</script>  we answer  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_7fa3b767c460b54a2be4d49030b349c7.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="no" /></span><script type='math/tex'>no</script>  else if it&#8217;s  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_6f7f9432d35dea629c8384dab312259a.gif' style='vertical-align: middle; border: none; ' class='tex' alt="strong" /></span><script type='math/tex'>strong</script>  we answer  <span class='MathJax_Preview'><img src='http://www.alykhantejani.com/wp-content/plugins/latex/cache/tex_a6105c0a611b41b08f1209506350279e.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="yes" /></span><script type='math/tex'>yes</script> . Obviously this was highly simplified by the fact that we only used 3 training samples, which is hardly enough to capture the problem accurately. Nevertheless, I hope this example has made the algorithm clear.</p>
<p>In the second part of this post I will discuss the down side of using this approach and why we use random trees. Additionally (hopefully) I will have a walk through, with code, of a real application using random forests. The application will probably be some form of image classification, such as classifying images as either images of cats or dogs, or if I have the time perhaps I can try and tackle a where&#8217;s Wally solver.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alykhantejani.com/an-introduction-to-random-forests-the-theory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Back To The 70&#039;s</title>
		<link>http://www.alykhantejani.com/back-to-the-70s/</link>
		<comments>http://www.alykhantejani.com/back-to-the-70s/#comments</comments>
		<pubDate>Tue, 17 Jul 2012 17:55:36 +0000</pubDate>
		<dc:creator>alykhantejani</dc:creator>
				<category><![CDATA[Travel]]></category>

		<guid isPermaLink="false">http://www.alykhantejani.com/?p=118</guid>
		<description><![CDATA[I arrived in Manali and I couldn&#8217;t believe I was still in India. This is for two reasons. Firstly, there is a mass of unspoilt land overlooking the Himalayas which you just wont find anywhere else in India. Secondly, I have never seen so many hippies trying to discover themselves and I can only imagine [...]]]></description>
				<content:encoded><![CDATA[<p>I arrived in Manali and I couldn&#8217;t believe I was still in India. This is for two reasons. Firstly, there is a mass of unspoilt land overlooking the Himalayas which you just wont find anywhere else in India. Secondly, I have never seen so many hippies trying to discover themselves and I can only imagine this is what the 70&#8242;s were like. As with anywhere in India, there are cows walking around randomly and so lots of cow shit on the floor. The difference here though is that the hippie kids walk around bare foot, often with shit on their feet, trying to &#8220;feel the earth through every part of their body&#8221;. The reason these people are attracted here is because the weed <strong>literally</strong> grows on the side of the street. </p>
<div id="attachment_128" class="wp-caption aligncenter" style="width: 630px"><a href="http://www.alykhantejani.com/wp-content/uploads/2012/07/DSC06316.jpg"><img src="http://www.alykhantejani.com/wp-content/uploads/2012/07/DSC06316-1024x768.jpg" alt="" title="Marjuana Plant" width="620" height="465" class="size-large wp-image-128" /></a><p class="wp-caption-text">Roadside weeds, literally.</p></div>
<p>However this is confined to Old Manali, and with just a short walk to new Manali you can feel like you&#8217;re back in India. Walk a little bit further and you will hit the town of Vashisht, which is where many of the Indian tourists tend to stay. Vashisht is also the starting points to many treks into the Himalayas and therefore has many wonderful viewpoints.<br />
<div id="attachment_119" class="wp-caption aligncenter" style="width: 623px"><a href="http://www.alykhantejani.com/wp-content/uploads/2012/07/IMAG0432.jpg"><img src="http://www.alykhantejani.com/wp-content/uploads/2012/07/IMAG0432-613x1024.jpg" alt="Me at the Vashisht Waterfall" title="vashisht waterfall" width="613" height="1024" class="size-large wp-image-119" /></a><p class="wp-caption-text">Me at the Vashisht Waterfall holding a friends drum</p></div></p>
<p>As with any tourist town in India, you will find the snake charmers (here&#8217;s a <a href="http://www.youtube.com/watch?v=MB8Zi1m3Kpg&#038;feature=youtu.be" title="Snake Charmer" target="_blank">video</a> if you&#8217;ve never seen one). But what I particularly enjoyed was a street show by two tourists which attracted all of the locals:<br />
<center><br />
<iframe width="420" height="315" src="http://www.youtube.com/embed/Cr2c__ubkZQ" frameborder="0" allowfullscreen></iframe><br />
</center></p>
]]></content:encoded>
			<wfw:commentRss>http://www.alykhantejani.com/back-to-the-70s/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bhrigu Lake</title>
		<link>http://www.alykhantejani.com/bhrigu-lake/</link>
		<comments>http://www.alykhantejani.com/bhrigu-lake/#comments</comments>
		<pubDate>Wed, 04 Jul 2012 12:47:21 +0000</pubDate>
		<dc:creator>alykhantejani</dc:creator>
				<category><![CDATA[Travel]]></category>

		<guid isPermaLink="false">http://www.alykhantejani.com/?p=122</guid>
		<description><![CDATA[I&#8217;ve been in Manali for about a week and during this time took a 3 day trek in the Himalayas to Bhrigu Lake. A full post on Manali will follow, but for now the internet speeds are too slow to upload the video for that post. Bhrigu Lake is a holy lake where it is [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been in Manali for about a week and during this time took a 3 day trek in the Himalayas to Bhrigu Lake. A full post on Manali will follow, but for now the internet speeds are too slow to upload the video for that post.</p>
<p>Bhrigu Lake is a holy lake where it is said that Maharishi Bhrigu, one of the seven sages created by the God Brhama, meditated while he wrote the Bhrigu Samhita. It is located at an altitude of 4250m (~14,000 ft) and the trip from Manali (at 2000m) is done over 2 nights and 3 days. It was probably the most physically challenging thing I have ever done in my life.</p>
<p>Day one started at 9am and we ascended up to 3800m in about 4 hours, during which I realised I am not as fit as I thought I was and that altitude sickness is no myth! During the ascent we stumbled across a herd of mountain goats and a sheppard, who was kind enough to let me hold a baby goat <img src='http://www.alykhantejani.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . </p>
<div id="attachment_123" class="wp-caption aligncenter" style="width: 630px"><a href="http://www.alykhantejani.com/wp-content/uploads/2012/07/DSC06123.jpg"><img src="http://www.alykhantejani.com/wp-content/uploads/2012/07/DSC06123-1024x768.jpg" alt="Me and a baby goat" title="Me and a baby goat" width="620" height="465" class="size-large wp-image-123" /></a><p class="wp-caption-text">Me and a baby goat</p></div>
<p>Once we reached camp we were greeted with hot chai and food. The views over the Himalayas at this altitude are amazing and because we were camping make for possible the best toilet in the world.</p>
<div id="attachment_124" class="wp-caption aligncenter" style="width: 630px"><a href="http://www.alykhantejani.com/wp-content/uploads/2012/07/HIMALAYAS-INDIA-277.jpg"><img src="http://www.alykhantejani.com/wp-content/uploads/2012/07/HIMALAYAS-INDIA-277-1024x682.jpg" alt="If carlsberg made toilets..." title="The best toilet in the world" width="620" height="412" class="size-large wp-image-124" /></a><p class="wp-caption-text">The best toilet in the world</p></div>
<p>The next day we made the chilling ascent up to the lake. This involved snow trekking up the edge of the mountain which meant that the guide had to use his ice axe to carve out steps for us. It gave me a real adrenaline rush as if you slipped you could very well slide half way down the mountain. Due to this, I couldn&#8217;t find an opportune moment to take a picture of this. After about 4 hours of this we finally reached the lake and I must say it was worth it as it is one of the most beautiful things I&#8217;ve every seen.</p>
<div id="attachment_125" class="wp-caption aligncenter" style="width: 630px"><a href="http://www.alykhantejani.com/wp-content/uploads/2012/07/DSC06209.jpg"><img src="http://www.alykhantejani.com/wp-content/uploads/2012/07/DSC06209-1024x768.jpg" alt="Bhrigu Lake" title="Bhrigu Lake" width="620" height="465" class="size-large wp-image-125" /></a><p class="wp-caption-text">Bhrigu Lake</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.alykhantejani.com/bhrigu-lake/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Waggah Border Closing</title>
		<link>http://www.alykhantejani.com/waggah-border-closing/</link>
		<comments>http://www.alykhantejani.com/waggah-border-closing/#comments</comments>
		<pubDate>Tue, 26 Jun 2012 19:50:52 +0000</pubDate>
		<dc:creator>alykhantejani</dc:creator>
				<category><![CDATA[Travel]]></category>

		<guid isPermaLink="false">http://www.alykhantejani.com/?p=111</guid>
		<description><![CDATA[I&#8217;ve been told that no trip to Amritsar is complete without visiting the Waggah border to watch the closing ceremony, so of course I went. Waggah is village in Punjab and has been divided into a Pakistani and an Indian side. Waggah contains the only road crossing from India to Pakistan and connects Amritsar to [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been told that no trip to Amritsar is complete without visiting the Waggah border to watch the closing ceremony, so of course I went.<br />
Waggah is village in Punjab and has been divided into a Pakistani and an Indian side. Waggah contains the only road crossing from India to Pakistan and connects Amritsar to Lahore. \</p>
<p>Waggah is about 30km from central Amritsar, so I got the hotel to hire me a driver for the afternoon. The drive was fun, especially because the driver didn&#8217;t speak a word of English, luckily my Hindi was sufficient enough to have a reasonable conversation.</p>
<p>When I arrived I was shocked to see how many local people had attended at 4:30 on a Friday. Apparently this is a common occurrence and the viewing gallery gets filled every day. </p>
<p>Once I had cleared security (no bags allowed), I had no idea where to go. A few meters ahead I saw a few American chaps go through a gated area which was being guarded by a soldier. I decided to approach the gate and the soldier (not so) kindly told me that I cannot go through so I joined the crowd of Indian people who appeared to be standing around waiting for something.</p>
<p>Suddenly, another gate opened and the crowd swarmed towards the entrance carrying me with them like a strong tide. During this complete lack of order I saw once man get trampled and another loose his sandals. Luckily I am slightly bigger than the average in India and was not easily run over. Once I was through the gate it wasn&#8217;t so bad, people took their seats and waited for the show to start. This experience made me appreciate one English thing in particular, the orderly queue.</p>
<p>The ceremony consisted of patriotic chants from both sides of the border, some dancing from the crowd and some fancy marching by the soldiers. Granted I could only partially see into Pakistan, but from what I could see there were many more people on the Indian side of the border, and plenty of noise was made.</p>
<div id="attachment_113" class="wp-caption aligncenter" style="width: 630px"><a href="http://www.alykhantejani.com/wp-content/uploads/2012/06/DSC05708.jpg"><img src="http://www.alykhantejani.com/wp-content/uploads/2012/06/DSC05708-1024x768.jpg" alt="India&#039;s Viewing Gallery" title="India&#039;s Viewing Gallery" width="620" height="465" class="size-large wp-image-113" /></a><p class="wp-caption-text">India&#8217;s Viewing Gallery</p></div>
<div id="attachment_112" class="wp-caption aligncenter" style="width: 630px"><a href="http://www.alykhantejani.com/wp-content/uploads/2012/06/DSC05712.jpg"><img src="http://www.alykhantejani.com/wp-content/uploads/2012/06/DSC05712-1024x768.jpg" alt="Pakistan's Viewing Gallery" title="Pakistan's Viewing Gallery" width="620" height="465" class="size-large wp-image-112" /></a><p class="wp-caption-text">Pakistan&#8217;s Viewing Gallery</p></div>
<div id="attachment_114" class="wp-caption aligncenter" style="width: 630px"><a href="http://www.alykhantejani.com/wp-content/uploads/2012/06/DSC05728.jpg"><img src="http://www.alykhantejani.com/wp-content/uploads/2012/06/DSC05728-1024x768.jpg" alt="The crowd getting involved" title="The crowd getting involved" width="620" height="465" class="size-large wp-image-114" /></a><p class="wp-caption-text">The crowd getting involved</p></div>
<p>The next day I met another (American) tourist who told me that if I had shown my passport to the Soldier I could have gone through the mysterious gate, behind which exists an orderly queue for foreigners. So if any Indian, or Indian looking, tourists plan on visiting Waggah, I would recommend speaking no Hindi and just flash your passport for some VIP treatment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alykhantejani.com/waggah-border-closing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The Summer Of Aly</title>
		<link>http://www.alykhantejani.com/the-summer-of-aly/</link>
		<comments>http://www.alykhantejani.com/the-summer-of-aly/#comments</comments>
		<pubDate>Mon, 25 Jun 2012 20:59:16 +0000</pubDate>
		<dc:creator>alykhantejani</dc:creator>
				<category><![CDATA[Travel]]></category>

		<guid isPermaLink="false">http://www.alykhantejani.com/?p=93</guid>
		<description><![CDATA[I recently left my Software Engineer job to pursue a PhD in computer vision and have a few months before it begins, so I proclaim this The Summer of George Aly! I&#8217;ve decided to go traveling in India and am actually writing this post from a hotel in Manali overlooking the Himalayan mountains. The first [...]]]></description>
				<content:encoded><![CDATA[<p>I recently left my Software Engineer job to pursue a PhD in computer vision and have a few months before it begins, so I proclaim this <a href="http://www.youtube.com/watch?v=ZPS3qLtaFtw" title="The Summer of George" target="_blank">The Summer of <del datetime="2012-06-25T17:54:54+00:00">George</del> Aly</a>!</p>
<p>I&#8217;ve decided to go traveling in India and am actually writing this post from a hotel in Manali overlooking the Himalayan mountains.</p>
<div id="attachment_94" class="wp-caption aligncenter" style="width: 630px"><a href="http://www.alykhantejani.com/wp-content/uploads/2012/06/DSC05878.jpg"><img src="http://www.alykhantejani.com/wp-content/uploads/2012/06/DSC05878-1024x768.jpg" alt="View from hotel" title="Manali Mountains" width="620" height="465" class="size-large wp-image-94" /></a><p class="wp-caption-text">The view from my hotel balcony</p></div>
<p>The first stop was Amritsar, Punjab. Here I visited the Golden Temple, which is one of Sikhism&#8217;s most holy places as it houses the Adi Granth, which is the Sikh holy book. It is called Golden as the temple itself is covered in gold, which was done for protection rather than beauty both of which have been achieved.</p>
<div id="attachment_97" class="wp-caption aligncenter" style="width: 630px"><a href="http://www.alykhantejani.com/wp-content/uploads/2012/06/DSC05701.jpg"><img src="http://www.alykhantejani.com/wp-content/uploads/2012/06/DSC05701-1024x768.jpg" alt="The Golden Temple in the day" title="The Golden Temple in the day" width="620" height="465" class="size-large wp-image-97" /></a><p class="wp-caption-text">The Golden Temple in the day</p></div>
<div id="attachment_96" class="wp-caption aligncenter" style="width: 630px"><a href="http://www.alykhantejani.com/wp-content/uploads/2012/06/DSC05676.jpg"><img src="http://www.alykhantejani.com/wp-content/uploads/2012/06/DSC05676-e1340657905442-768x1024.jpg" alt="The Golden Temple at night" title="The Golden Temple at night" width="620" height="826" class="size-large wp-image-96" /></a><p class="wp-caption-text">The Golden Temple at night</p></div>
<p>I visited in the day and at night as the views are very different. At both times there were many pilgrims taking dips in the holy lake, however at night the temperature is more bearable and I queued to go inside the temple (no photography allowed). </p>
<div id="attachment_99" class="wp-caption aligncenter" style="width: 630px"><a href="http://www.alykhantejani.com/wp-content/uploads/2012/06/DSC05698.jpg"><img src="http://www.alykhantejani.com/wp-content/uploads/2012/06/DSC05698-1024x768.jpg" alt="A pilgrim taking a dip in the holy lake" title="Pilgrim at Golden Temple" width="620" height="465" class="size-large wp-image-99" /></a><p class="wp-caption-text">A pilgrim taking a dip in the holy lake</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.alykhantejani.com/the-summer-of-aly/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Movember Madness - Week 4</title>
		<link>http://www.alykhantejani.com/movember-madness-week-4/</link>
		<comments>http://www.alykhantejani.com/movember-madness-week-4/#comments</comments>
		<pubDate>Wed, 30 Nov 2011 22:24:15 +0000</pubDate>
		<dc:creator>alykhantejani</dc:creator>
				<category><![CDATA[Charity]]></category>

		<guid isPermaLink="false">http://www.alykhantejani.com/?p=78</guid>
		<description><![CDATA[Movember is over and all around us are men with cold upper lips. I’d like to say a big thank you to everyone who donated. Together we managed to raise £360 fine English Pounds for the prostate cancer charity! Now, as promised, the moment you all paid for, the Motos (Photos). Enjoy!]]></description>
				<content:encoded><![CDATA[<p>Movember is over and all around us are men with cold upper lips.</p>
<p>I’d like to say a big thank you to everyone who donated. Together we managed to raise £360 fine English Pounds for the prostate cancer charity!</p>
<p>Now, as promised, the moment you all paid for, <a href="http://www.flickr.com/photos/alykhantejani/sets/72157628324281205/" target="_blank">the Motos (Photos)</a>. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alykhantejani.com/movember-madness-week-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Movember Madness - Week 3</title>
		<link>http://www.alykhantejani.com/movember-madness-week-3/</link>
		<comments>http://www.alykhantejani.com/movember-madness-week-3/#comments</comments>
		<pubDate>Sun, 20 Nov 2011 22:22:41 +0000</pubDate>
		<dc:creator>alykhantejani</dc:creator>
				<category><![CDATA[Charity]]></category>

		<guid isPermaLink="false">http://www.alykhantejani.com/?p=75</guid>
		<description><![CDATA[We’re into week 3 (20 days to be precise) and the stash looks fabulous. Even though it itches uncontrollably, it does keep my upper lip warm from the cruel winter breeze. So, in short, ‘stash = cool, cancer = BAD!!! A big thanks to everyone who has donated, only 10 more days to go so [...]]]></description>
				<content:encoded><![CDATA[<p>We’re into week 3 (20 days to be precise) and the stash looks fabulous. Even though it itches uncontrollably, it does keep my upper lip warm from the cruel winter breeze. So, in short, ‘stash = cool, cancer = BAD!!! A big thanks to everyone who has donated, only 10 more days to go so please try and squeeze a donation in.<br />
<a href="http://www.alykhantejani.com/wp-content/uploads/2012/06/movember_week_3.jpg"><img src="http://www.alykhantejani.com/wp-content/uploads/2012/06/movember_week_3-225x300.jpg" alt="Movember After  3 weeks" title="movember_week_3" width="225" height="300" class="aligncenter size-medium wp-image-76" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.alykhantejani.com/movember-madness-week-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Double brace initialization</title>
		<link>http://www.alykhantejani.com/82/</link>
		<comments>http://www.alykhantejani.com/82/#comments</comments>
		<pubDate>Fri, 11 Nov 2011 22:26:52 +0000</pubDate>
		<dc:creator>alykhantejani</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.alykhantejani.com/?p=82</guid>
		<description><![CDATA[Double brace initialization is a shorthand way to initialize objects in Java, which is most commonly used when initializing collections like so: List&#60;String&#62; names = new LinkedList&#60;String&#62;(){{ add("Bob"); add("Pam"); add("Aly"); }}; Which is a nicer, slightly more concise way of writing List&#60;String&#62; names = new LinkedList&#60;String&#62;(); names.add("Bob"); names.add("Pam"); names.add("Aly"); So, what exactly does double brace [...]]]></description>
				<content:encoded><![CDATA[<p>Double brace initialization is a shorthand way to initialize objects in Java, which is most commonly used when initializing collections like so:</p>
<pre class="brush: java">
List&lt;String&gt; names = new LinkedList&lt;String&gt;(){{
    add("Bob");
    add("Pam");
    add("Aly");
}};
</pre>
<p>Which is a nicer, slightly more concise way of writing</p>
<pre class="brush: java">
List&lt;String&gt; names = new LinkedList&lt;String&gt;();
names.add("Bob");
names.add("Pam");
names.add("Aly");
</pre>
<p>So, what exactly does double brace initialization do under the covers? Well, the first set of braces creates an anonymous class that extends the class you are trying to instantiate. The second set creates an initialization block for this anonymous class. This means that the initialization block can refer to any public/protected fields and methods in the base type. So, this is not just a bit of syntactic sugar to populate collections on initialization, in fact, this technique can be used with any object.</p>
<p>There are however a few things to note before you use this technique:</p>
<p>If your object has a complicated equals method that depends on the type, class name or private fields then you probably want to avoid using double brace initialization. This is because the new type that is created will have a different equals method and may fail on .equals() checks.</p>
<p>The second gotcha is slightly more subtle and involves serialization. When classes implement the Serializable interface (as do most of the Collections in the Collections API) they have to provide a serialVersionUID and if they don&#8217;t the JVM will create one for them. The serialVersionUID is used as a version control mechanism for Java serialization. This is an important topic, and one for another blog post, but it is important to note that changing it can cause serialization on the client side to fail.</p>
<p>Now, when you use double brace initialization without explicitly adding the same serialVersionUID as the base type (which would defeat point of the nice shorthand syntax) the JVM will generate a serialVersionUID for the new anonymous type. The generated serialVersionUID can be based on the fields as well as the class name which is different for the anonymous type. This will lead to serialization conflicts, so if you are serializing you object, you will want to avoid this shorthand syntax.</p>
<p>—UPDATE-14-11-2011————————————–</p>
<p>A third gotcha, as pointed out by a friend of mine, Viral Shah, is that when the anonymous class is created, it implicitly gets a reference to the surrounding class which can be accessed with the Person.this syntax as shown below:</p>
<pre class="brush: java">
public class Person{
  public String name = "Aly";
  List&lt;String&gt; favouriteNames = new LinkedList(){{
    add(Person.this.name);
    add("Viral");
  }};
}
</pre>
<p>Whilst this implicit reference looks harmless, it can cause two subtle but relevant issues.</p>
<p>The first is that as long as a reference to the favouriteNames list is live, in any object, a reference to the Person object will be kept alive and thus consume heap space.</p>
<p>The second issue relates to (surprise, surprise) serialization. As the anonymous class has an implicit reference to the outer class, when the anonymous object is serialized it will (try to) serialize the Person object as well. Now, using the example above, Person does not implement the Serializable interface. So trying to serialize it, directly or via serializing the favouriteNames list, will throw a java.io.NotSerializableException exception. Additionally, this can not be avoided by using the transient keyword, as the reference is implicit and therefore we cannot mark it as transient.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alykhantejani.com/82/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Movember Madness - Week 2</title>
		<link>http://www.alykhantejani.com/movember-madness-week-2/</link>
		<comments>http://www.alykhantejani.com/movember-madness-week-2/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 22:14:28 +0000</pubDate>
		<dc:creator>alykhantejani</dc:creator>
				<category><![CDATA[Charity]]></category>

		<guid isPermaLink="false">http://www.alykhantejani.com/?p=70</guid>
		<description><![CDATA[It’s week two of my quest to aid cancer research and also to grow a stash like my boy Zehan (pictured below). That&#8217;s one helluva stash It itches like hell, but I can stroke it whilst reading and look more intelligent than I really am. As the saying goes, “pictures or it didn’t happen”, so [...]]]></description>
				<content:encoded><![CDATA[<p>It’s week two of my quest to aid cancer research and also to grow a stash like my boy Zehan (pictured below).<br />
<a href="http://www.alykhantejani.com/wp-content/uploads/2012/06/Zehan.jpg"><img src="http://www.alykhantejani.com/wp-content/uploads/2012/06/Zehan-300x225.jpg" alt="That&#039;s one helluva &#039;stash!" title="Zehan" width="300" height="225" class="aligncenter size-medium wp-image-71" /></a><br />
That&#8217;s one helluva stash</p>
<p>It itches like hell, but I can stroke it whilst reading and look more intelligent than I really am. As the saying goes, “pictures or it didn’t happen”,  so here it is:<br />
<a href="http://www.alykhantejani.com/wp-content/uploads/2012/06/movember_week_1.jpg"><img src="http://www.alykhantejani.com/wp-content/uploads/2012/06/movember_week_1-e1339798855757-225x300.jpg" alt="Movember after 1 week" title="movember_week_1" width="225" height="300" class="aligncenter size-medium wp-image-72" /></a><br />
If you found that funny and/or you want to help a good cause please donate to the cause here:  http://mobro.co/AlyTejani</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alykhantejani.com/movember-madness-week-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Initializing Instance Members With Non-Final Methods</title>
		<link>http://www.alykhantejani.com/initializing-instance-members-with-non-final-methods/</link>
		<comments>http://www.alykhantejani.com/initializing-instance-members-with-non-final-methods/#comments</comments>
		<pubDate>Wed, 09 Nov 2011 18:17:02 +0000</pubDate>
		<dc:creator>alykhantejani</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.alykhantejani.com/?p=53</guid>
		<description><![CDATA[I recently read the Oracle documentation on “Initializing Instance Members”. The article talks about the three most common ways of member initialization being assignment in the constructor, using initializer blocks and using final methods which are called from the constructor. Why just final methods? Well, as stated in the article: A final method cannot be [...]]]></description>
				<content:encoded><![CDATA[<p>I recently read the <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html" target="_blank">Oracle documentation on “Initializing Instance Members”</a>. The article talks about the three most common ways of member initialization being assignment in the constructor, using initializer blocks and using final methods which are called from the constructor.</p>
<p>Why just final methods? Well, as stated in the article:</p>
<p>A final method cannot be overridden in a subclass.</p>
<p>Ok, but why shouldn’t we override these initialization methods? After all I often see code like this:</p>
<pre class="brush: java">
public class Super{
 
    public Super(){
        //some code here
        init();
    }
 
    public void init(){
        //initialzation code here
    }
}
 
public class Sub extends Super{
 
    public Sub(){
        super();
    }
 
    @Override
    public void init(){
        //different initialization code here
    }
}
</pre>
<p>They then go on to say:</p>
<p>“Calling non-final methods during instance initialization can cause problems. Joshua Bloch describes this in more detail in Effective Java”</p>
<p>Now, I assume that like me, when people are reading this page, usually at work, they generally don’t have their copy of Effective Java with them. So, I’m going to try my best to explain this so that its available via a simple google search.</p>
<p>Let’s look at a simple example. We have a Duck and at design time we decide to make the assumption that when Ducks speak they all say “quack”. At some point in the future we decide to add a SqueakyDuck; which when speaking says “squeak”. The code for this is below:</p>
<p>Disclaimer: Now, I know this is a bad example and there are better ways to achieve this behaviour, such as the Strategy Pattern, but that is a post for another day. Nevertheless, I feel this general pattern occurs in practice quite a lot.</p>
<pre class="brush: java">
public class Duck{
 
    String sound = "quack";
    protected String speech;
    public Duck(){
        //some code here
        speech = initSpeech();
    }
 
    protected void initSpeech(){
        speech = "Hi, I'm a Duck and I say " + sound;
    }
 
    public void speak(){
        System.out.println(speech);
    }
 
    public String getSound(){
        return sound;
    }
}
 
public class SqueakyDuck extends Duck{
 
    String squeakSound = "squeak";
 
    public SqueakyDuck(){
        super();
        //some code here
    }
 
    @Override
    protected void initSpeech(){
        speech = "Hi, I'm a Squeaky Duck, and I say " + squeakSound;
    }

    @Override
    protected void getSound(){
        return squeakSound;
    }
}
</pre>
<p>The problem arises when we run the following code:</p>
<pre class="brush: java">
public static void main(String[] args){
    Duck squeaky = new SqueakyDuck();
    squeaky.speak();
    System.out.println(squeaky.getSound());
}
</pre>
<p>and we get the following output:</p>
<p>Hi, I’m a Squeaky Duck, and I say null<br />
squeak</p>
<p>Chapter 12.5 “Creation of New Class Instances” of the <a href="http://docs.oracle.com/javase/specs/#44670" target="_blank">Java Language Specification</a> helps to clarify why this occurs (relevant bits highlighted below):</p>
<p>Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:</p>
<ol>
<li>Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.</li>
<li>If this constructor begins with an explicit constructor invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
</li>
<p>	<b>
<li>This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
</li>
<li>Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5. (In some early implementations, the compiler incorrectly omitted the code to initialize a field if the field initializer expression was a constant expression whose value was equal to the default initialization value for its type.)
</li>
<p></b></p>
<li>Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.
</li>
</ol>
<p>This means that on execution of the SqueakyDuck constructor, it in turn calls super(), which in turn calls the overridden initSpeech() method, inside the SqueakyDuck class, before the SqueakyDuck instance members have been fully initialized. This means that the when initSpeech() is run the squeakySound field has the default value, which is null for all objects. However, once the SqueakyDuck object is fully initialized the squeakySound field is set to “squeak” which is why the call to getSound() returns “squeak”.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alykhantejani.com/initializing-instance-members-with-non-final-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
