<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mohak Sharda on Medium]]></title>
        <description><![CDATA[Stories by Mohak Sharda on Medium]]></description>
        <link>https://medium.com/@mohaksharda?source=rss-5c1ba9c116d4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*A-7fg520EJ_5iKZBvwkfTg.jpeg</url>
            <title>Stories by Mohak Sharda on Medium</title>
            <link>https://medium.com/@mohaksharda?source=rss-5c1ba9c116d4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 17:16:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mohaksharda/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Reality mine, reality yours]]></title>
            <link>https://mohaksharda.medium.com/reality-mine-reality-yours-b251110c1e69?source=rss-5c1ba9c116d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/b251110c1e69</guid>
            <dc:creator><![CDATA[Mohak Sharda]]></dc:creator>
            <pubDate>Sat, 31 Oct 2026 19:31:18 GMT</pubDate>
            <atom:updated>2026-11-01T05:02:58.699Z</atom:updated>
            <content:encoded><![CDATA[<p>Sitting</p><p>On the passenger seat, beside me.</p><p>White wires plugged in her ears</p><p>Rain beading on the car glass</p><p>she leans against</p><p>She looks deep into</p><p>The oblivion.</p><p>Sense</p><p>Unease I could in her eyes, yet</p><p>Unbothered</p><p>Unhinged</p><p>Unaware</p><p>She thought O thoughts deeper than the</p><p>S</p><p>E</p><p>A</p><p>S</p><p>“Tell me, tell me anything”</p><p>I say to her with my eyes.</p><p>She doesn’t reply, for her</p><p>Eyes are guarded by raven wings.</p><p>Oh and I would,</p><p>I would try again, for her thoughts had onto me unloaded thoughts my own bereft.</p><p>Afraid but</p><p>I am</p><p>That the driver would move faster through the traffic, before</p><p>Her twin ravens would lift their wings.</p><p>I could hear the song she is hearing</p><p>On the radio</p><p>Mine.</p><p>The melancholic chords</p><p>Sent as if by the jockey</p><p>To make her feel her thoughts.</p><p>To make me feel her thoughts.</p><p>You ask, Try again interrobang.</p><p>O Yes I would</p><p>But the traffic moves on</p><p>She moves on, shebang.</p><p>Stays back but is the</p><p>Melancholic chord</p><p>On the radio</p><p>Mine</p><p>Stays back but is the</p><p>Melancholic reality</p><p>Of her in</p><p>Mine</p><p>For we shared a reality together</p><p>Which she will never know</p><p>For she did not know</p><p>Me</p><p>And I now know</p><p>Her</p><p>Thought</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b251110c1e69" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A few seconds at the signal]]></title>
            <link>https://mohaksharda.medium.com/a-few-seconds-at-the-signal-1dbeea668b3e?source=rss-5c1ba9c116d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/1dbeea668b3e</guid>
            <dc:creator><![CDATA[Mohak Sharda]]></dc:creator>
            <pubDate>Sat, 03 Oct 2026 09:57:26 GMT</pubDate>
            <atom:updated>2026-10-03T17:23:58.170Z</atom:updated>
            <content:encoded><![CDATA[<p>From my car, I watched the signal turn green.</p><p>A father steered his bike out from the far left,</p><p>his two children leaning with him,</p><p>towards a man at the divider, palm held out.</p><p>I was glad someone had noticed him</p><p>before the road carried us all away.</p><p>He steadied the bike and reached into his pocket.</p><p>The older child leaned out to watch</p><p>the money changing hands.</p><p>Horns sounded around us.</p><p>I had somewhere to be,</p><p>but I could spare them these few seconds.</p><p>From my car, I watched the signal turn green.</p><p>At last. Then a bike cut diagonally left to right across my lane –</p><p>a father, two children, heading for the man</p><p>asking for money by the divider.</p><p>I braked hard enough to jerk against my belt.</p><p>The car behind stopped close.</p><p>All that red, and he had chosen now.</p><p>His hand searched his pocket;</p><p>mine pressed the horn.</p><p>There was room ahead, if he would only move.</p><p>I kept my foot on the brake</p><p>and watched our green run out in those few seconds.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1dbeea668b3e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dimensionality reduction is not clustering]]></title>
            <link>https://mohaksharda.medium.com/dimensionality-reduction-is-not-clustering-b5226569382a?source=rss-5c1ba9c116d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/b5226569382a</guid>
            <category><![CDATA[dimensionality-reduction]]></category>
            <category><![CDATA[principal-component]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[clustering]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Mohak Sharda]]></dc:creator>
            <pubDate>Fri, 12 May 2023 10:21:05 GMT</pubDate>
            <atom:updated>2023-05-13T09:46:46.768Z</atom:updated>
            <content:encoded><![CDATA[<p>Too obvious? Most people don’t think so.</p><p>Dimensionality reduction and clustering are both unsupervised learning techniques in machine learning. Both of these techniques can help find the hidden structure in the data, when no guidance is available. I will explain this with an example. But before that I just want to explicitly mention what this post is about. Most data science enthusiasts, especially beginners, do not appreciate the fact that the “<strong><em>clusters</em></strong>” that dimensionality reduction methods return are not the same as the <strong><em>clusters</em></strong> that clustering returns. Both the techniques have different agendas behind them and the interpretation of results should be made keeping that in mind. Now let’s get to the meat of the post.</p><h3><strong>What is unsupervised learning?</strong></h3><p>Broadly speaking, there are two ways in which we have learnt things in school (there are more ways including reinforcement learning, but let’s stick to two in this post).</p><p>Imagine a teacher comes in the class and shows you a picture of a cat and tells you that the picture they are showing you is of a cat. They also show you a picture of a dog and tell you that the picture is of a dog. Next, they ask you to identify the differences between the two. This kind of learning is where you are <strong><em>supervised</em></strong> beforehand that the two pictures shown are of differently named animals and you learn to then identify what makes them different.</p><p>The other kind of learning is where the teacher shows you a lot of pictures of cats and dogs mixed up in a pile and doesn’t tell you which picture belongs to which group (cat or dog). Instead they ask you to segregate the pictures into piles based on their similarity into two or more piles. This kind of learning is <strong><em>unsupervised</em></strong> where you use certain criteria to segregate the pictures into two or more piles (remember ideally the piles you create should be two, but because you are not supervised as to how many kinds of animals the pile contains, it is an exercise that you need to do on your own. Therefore, you might end up with more than two piles).</p><p>Obviously, the first technique is much robust (given the labelling is done correctly in the first place). But unfortunately, most of the data we have in the world is unsupervised. And the two techniques we are going to discuss in this post help us learn patterns in the data in this fashion.</p><p><strong><em>Side point to ponder:</em></strong><em> Is supervised learning always desired? Could there be times when you have the data labels and you would still prefer using unsupervised learning to understand your data?</em></p><p>What are these two unsupervised learning techniques? Why do we need more than one technique to learn? Are they replaceable or do they generate the same kind of understanding of the data? These are some of the question we will try and understand in this post. And we will understand this using a toy dataset and carry out the analysis and visualization in python.</p><h3>What is dimensionality reduction?</h3><p>Let us continue our example from above. Imagine you have a pile of pictures of different mammals (and you do not know what the labels of each picture are). You can look at all pictures and come up with criteria to differentiate between each picture. For example, you can segregate the pictures on the basis of colors, number of hind legs, number of fore legs, presence of whiskers, presence of fur, picture taken during the day or night, picture is a cartoon representation of the animal or a real camera photograph and so on. Some of the features will be highly correlated. For example, most mammals that have two eyes will also have two external pinna or external ears (except whales and platypus that do not have external pinna). Some features will be noise like a feature like “picture taken during the day or night” or “picture is a cartoon representation of the animal or a real camera photograph” will not be helpful in segregating mammals. Dimensionality reduction techniques like Principal Component analysis (PCA) will try to retain only the most important criteria that could best distinguish all mammal pictures. Because you are reducing the criteria or dimensions to a small number, these techniques are called dimensionality reduction techniques.</p><p>To get a more detailed understanding of PCA and dimensionality reduction, you can read my previous post on this topic. <a href="https://medium.com/@mohaksharda/coding-principal-component-analysis-pca-as-a-python-class-968af5e0d9">This is the link.</a></p><h3>What is clustering?</h3><p>Now imagine you want to explicitly segregate the mammal pictures into two clusters (imagine there are only dogs and cats images). You would want to take all the features and calculate a similarity score. (There are different ways to measure similarity, we are not going to get into that in this post.) Based on the similarity score, you would assign each mammal picture to one of the two clusters (here, we have predetermined how many clusters we want i.e. n =2. In reality, number of clusters expected is not one-size-fits-all exercise or trivial. But that is a discussion of another post). Hopefully, if the features that we are using to calculate similarity scores would result in segregating all dog and dog-like pictures into one group and all cat and cat-like pictures into another.</p><p>A very important point to note here is that the similarity scores are a function of the features used to calculate it. Obviously, noisy features might make the similarity scores more promiscuous. In real world analyses, most pipelines usually carry out dimensionality reduction on features first, to obtain relatively more informative features first and then use those features to carry out clustering.</p><h3>Applying PCA and K-means clustering to wine dataset</h3><p>So far, we discussed the concept behind what dimensionality reduction and clustering does. But what is the entire hullabaloo about? Why do people usually mistake results of dimensionality reduction with that of clustering and sometimes consider them replaceable. It’s a little more nuanced to understand just through words. Therefore, we are going to understand the subtle difference between the two techniques through coding.</p><p>At this point, you should keep in mind that, unlike clustering, there is no similarity score based explicit grouping of mammal pictures in dimensionality reduction. So even though dimensionality reduction gives some idea of variation among the mammal pictures, its motivation is not to segregate them into clusters but only to represent the pictures with the least amount of necessary information that is just sufficient enough to explain the differences among the pictures.</p><p>Still not clear? Do not worry, read ahead and code along. I suggest you to come back and read the paragraph above once you have reached the end of this section. I assure you, it will make sense in retrospect.</p><p>The code below does the following:</p><ol><li>Import all the important libraries necessary to run the code.</li><li>We load an in-built dataset on wine classification in the sklearn library and save it to a variable called wine. There are three kinds of wines (class_0, class_1 and class_2. Don’t worry we won’t use these labels while learning.) These wines are described by thirteen chemical features like phenol content, alcohol content and so on.</li><li>Next we carry out K-means clustering on the thirteen features of wines. Note we are explicitly mentioning three clusters. But you could change it to two or four or more. (For brevity, we are setting n=3 since we know there are three kinds of wines.)</li><li>In the next line of code, we independently also carry out Principal Component analysis, a kind of dimensionality reduction techniques. And we are explicitly mentioning that we want the thirteen features to be collapsed to just two features that could still do a decent job in describing the wine samples (note how the exercise is not saying to segregate but to describe the wine samples in the best way that the original thirteen features were doing. Read this again.)</li><li>In the next line, let’s plot the results. K-means clustering returns three clusters based on similarity score calculated from thirteen features (we haev not used the wine labels so far). Let’s plot them over the PCA generated plot with x-axis being principal component 1 and y-axis being principal component 2. In essence, we are plotting the information of thirteen features describing wines using just two features in lower dimension (which are a linear combination of original features). Let’s color cluster 1 as red, cluster 2 as blue and cluster 3 as green. (it might ot might not put all the original wine class labels into independent clusters that it returned. Let’s see later.)</li><li>Next, let us color the same PCA plot with original wine labels (instead of clusters returned by K-means clustering). We color class_0 wine as red, class_1 wine as blue and class_2 wine as green.</li></ol><p>In the end, we obtain two versions of PCA plot with different coloring schemes. The plot on the left is color coded on the basis of the cluster to which each wine sample was assigned to (cluster 1, 2 or 3). The plot on the right is color coded on the basis of the original wine class label that each wine sample was from.</p><p>Let us interpret the two plots in the next section.</p><pre>import matplotlib.pyplot as plt<br>from sklearn.cluster import KMeans<br>from sklearn.decomposition import PCA<br>from sklearn.datasets import load_wine<br><br># Load the winedataset<br>wine = load_wine()<br><br># Perform k-means clustering on the wine dataset<br>kmeans = KMeans(n_clusters=3, random_state=0).fit(wine.data)<br><br># Perform PCA on the wine dataset<br>pca = PCA(n_components=2).fit_transform(wine.data)<br><br># Plot the results of k-means clustering and PCA<br>fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))<br><br># Plot the k-means clusters<br>colors = np.array([&#39;red&#39;, &#39;blue&#39;, &#39;green&#39;])<br>ax1.scatter(pca[:, 0], pca[:, 1], c=colors[kmeans.labels_])<br>ax1.set_title(&#39;K-means clustering&#39;)<br><br># Plot the PCA results<br>ax2.scatter(pca[:, 0], pca[:, 1], c=colors[wine.target])<br>ax2.set_title(&#39;PCA&#39;)<br><br>plt.show()</pre><h3>Dimensionality reduction is not clustering</h3><p>We can already see the difference between the two plots (figure below).</p><p>If we look at the plot on the right, we see that wine class_1 (blue) and class_2 (green) are not well segregated based on the information contained in the thirteen features of wine chemistry. class_0 (red) is relatively well separated from the other two wine classes.</p><p>However, the plot on the left shows that K-means has returned three distinct clusters based on the chemistry of the wine samples, agnostic of what the real wine class was. As you can appreciate, the clusters are distinct based on the similarity score, but they re-assign the three kinds of wine classes into three clusters. The labels have lost their meaning. Let’s interpret the left plot to understand this. So the green cluster (or the cluster 3 returned by K-means) is a mixture of class_1 and class_2 wines (original labels can be referenced from the plot on the right - class_0 is red, class_1 is blue and class_2 is green). Similarly the red cluster (or the cluster 1 returned by K-means) is a mixture of class_0, class_1 and class_2 wines (original labels). Finally, the blue cluster (or the cluster 2 returned by K-means) is largely class_0 (original label red in right plot), with one wine sample from class_1 included as well (original label blue in right plot).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/618/1*AjNt-HbBd8M0SrOrlCUwRQ.png" /><figcaption><strong><em>Left panel:</em></strong> K-means clusters (n=3) assigned to wine dataset based on overall similarity scores superimposed on the PCA plot (Cluster 1: Red, Cluster 2: Blue and Cluster 3: Green). <strong><em>Right panel:</em></strong> Original wine labels superimposed on the PCA plot (Class_0 wine: Red, Class_1: Blue, Class_2: Green).</figcaption></figure><p>What does it tell us?</p><p>Dimensionality reduction results using PCA just reveal how best the wine samples are related to each other based on the information encoded in the thirteen features describing their chemistry. It shows how class_0 is distinct from class_1 and class_2, whereas class_1 and class_2 are not well segregated based on the overall variance explained by the features. Clustering on the other hand tells us that if overall similarity scores are to be calculated from the thirteen features and thereafter used to segregate the wine samples into three clusters, original labels might not be a good representation of the wine samples after all. One possibility could be that the class labels could have been assigned in the first place just by the mere fact that they were collected from three different geographical locations in Italy (for more information about the dataset read the documentation of this dataset from sklearn’s official page). So clustering reveals that because there is so much variability in the wine samples even from the same geographical location, a better way of clustering them could be based on the similarity scores obtained from thirteen features. Recall, PCA did not have any agenda to do so in the first place.</p><p>Let the plots simmer a bit longer. Re-read the last section, keeping the plots in front of you again. You will get an epiphany eventually, the longer you stare at the plots (if you haven’t got any yet).</p><h3>Conclusion</h3><p>This post was motivated by my observation across literature where people use dimensionality reduction techniques as a replacement of clustering. That is not correct. The two techniques have different reasons to exist and they reveal different aspects about the data. Clustering returns clusters based on the similarity among the samples calculated from the features that describe the sample. Dimensionality reduction techniques simply preserve the relationship among the samples in lower dimensions i.e. there is no explicit agenda to carry out any clustering. To put simply, if data was telling some story, dimensionality reduction is the ability to describe the story in less words making it less verbose without affecting the overall essence or message of the story, as much as possible. Clustering on the other hand is the ability to identify what part of the story would be appropriate to be called a premise, what part of the story would qualify as the build-up, what part would qualify as the conflict, what part would qualify as escalation and finally what part would qualify as resolution. You see both tell different things about the data. And one should not be replaced by the other. In fact, in practice people sometimes carry out dimensionality reduction first followed by clustering.</p><p>What I have not covered in this post is what could happen when clustering is carried out with redundancy in the data? But I will leave you with this- data with and without redundancy (correlated features) could yield very different results. Something I could cover in the next posts.</p><h3>Bonus coding section</h3><p>Here’s the code that I used above in the style of object oriented programming for advanced coders:</p><pre>import numpy as np<br>import matplotlib.pyplot as plt<br>from sklearn.cluster import KMeans<br>from sklearn.decomposition import PCA<br>from sklearn.datasets import load_wine<br><br>class WineVisualizer:<br>    def __init__(self):<br>        self.wine = load_wine()<br>        self.kmeans = None<br>        self.pca = None<br><br>    def perform_clustering(self, n_clusters):<br>        self.kmeans = KMeans(n_clusters=n_clusters, random_state=0).fit(self.wine.data)<br><br>    def perform_pca(self, n_components):<br>        self.pca = PCA(n_components=n_components).fit_transform(self.wine.data)<br><br><br>    def plot_original_labels_and_clusters(self):<br>        <br>        fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))<br><br>        # Plot the k-means clusters<br>        colors = np.array([&#39;red&#39;, &#39;blue&#39;, &#39;green&#39;])<br>        ax1.scatter(self.pca[:, 0], self.pca[:, 1], c=colors[self.kmeans.labels_])<br>        ax1.set_title(&#39;K-means clusters overlayed on PCA plot&#39;)<br><br>        # Plot the PCA results<br>        ax2.scatter(self.pca[:, 0], self.pca[:, 1], c=colors[self.wine.target])<br>        ax2.set_title(&#39;Original wine class labels overlayed on PCA plot&#39;)<br><br>        plt.show()<br><br># Create an instance of WineVisualizer<br>visualizer = WineVisualizer()<br><br># Perform k-means clustering with 3 clusters<br>visualizer.perform_clustering(n_clusters=3)<br><br># Perform PCA with 2 components<br>visualizer.perform_pca(n_components=2)<br><br># Plot the k-means clusters<br>visualizer.plot_original_labels_and_clusters()</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b5226569382a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coding Principal Component Analysis (PCA) as a python class | by Mohak Sharda]]></title>
            <link>https://mohaksharda.medium.com/coding-principal-component-analysis-pca-as-a-python-class-968af5e0d9?source=rss-5c1ba9c116d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/968af5e0d9</guid>
            <category><![CDATA[math]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Mohak Sharda]]></dc:creator>
            <pubDate>Mon, 03 Apr 2023 12:55:26 GMT</pubDate>
            <atom:updated>2023-04-03T14:14:43.549Z</atom:updated>
            <content:encoded><![CDATA[<p>Finding hidden patterns in the data from a coder’s perspective</p><p>Principal Component Analysis (PCA) is one of the most powerful unsupervised learning techniques. It helps you find the hidden structure in the data with a lot of variables (also called dimensions).</p><p>Let’s take an example to understand this.</p><p>Imagine you have a box of hundreds of books and you want to select and gift some books to your friend such that each book you choose is unique in its own way. The idea here is that if there are two books that have the same genre, same kind of ending and same kind of character arc, you would want to just choose one of them to gift to your friend and not both.</p><p>Also, imagine if you could catalogue hundreds of characteristics for these books. Some characteristics would always coexist in books. For example, there is one characteristic called “<em>if the book is a murder mystery</em>”. Another characteristic called “<em>whether atleast one person dies in the book</em>”. As you can notice, whenever a book would be catalogued under a murder mystery, the second characteristic will always have to be true. We would say both these characteristics are linearly correlated or the information encoded is redundant.</p><p>PCA helps us deal with such situations. It helps in reducing variables (or dimensions) such that it still best describes the data. Therefore, it is called as a <strong><em>dimensionality reduction technique</em></strong>.</p><p>Before we proceed with coding PCA, there are two more terms that are important to keep in mind. PCA is a <strong><em>linear</em></strong> technique that helps preserve the <strong><em>global structure</em></strong> of the data in lower dimensions. What does that mean?</p><p>Let us first understand what global means. You could segregate books based on many characteristics, some more nuanced and some less nuanced. Obviously as more nuanced the characteristics get, you would start clubbing books into smaller clumps, even if they fall under the same genre. For example, if the character that dies in a murder mystery is always a girl who is below twenty years of age. This is more nuanced. PCA will not deal with these. More nuanced characteristics will give rise to many <em>local</em> structures. What PCA does is to reduce the dimensions such that it maximizes the variance of the overall data. On a lighter note, on a scale of “don’t judge a book by its cover” to “judge a book by its cover”, PCA leans towards the latter.</p><p>Second, PCA looks at only linear relationships among variables. This can be confusing to get. People often confuse linear relationship with correlation. Yes, a perfectly linear relationship is correlated, but it is not the only condition for correlation. There could be non-linear relationships among variables that could be correlated. For example, number of words in a book are linearly correlated with number of pages in a book. PCA can deal with such variables and reduce the dimensions after detecting them. However, there could be relations that are non-linearly correlated. For example, the popularity of a book could increase with the complexity of the plot, but after a certain point, if the plot becomes overly complex, the popularity could start decreasing. PCA does not consider such relationships.</p><p>Mathematically speaking, PCA uses the magic of linear algebra. That is what we are going to code.</p><p>We will make use of the library <em>numpy</em> in python. We will define a class PCA with a constructor __init__.</p><p>Notice in code block I below, we have passed an argument <em>desired_principal_components </em>to our constructor that we wish the user to specify.</p><p>Then we define three variables for our class — <em>desired_principal_components, extracted_eigenvectors and feature_mean. </em>We initialise desired_principal_components with user input (compulsory argument) and the rest two as None.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18668c9836156c45417e5d3b0bd33078/href">https://medium.com/media/18668c9836156c45417e5d3b0bd33078/href</a></iframe><p>Next, we define a function called fit(). It takes as an argument, the feature table from the user with each row as one data point and columns as features describing the attributes of that data point.</p><p>We will do the following:</p><p>a) Center each variable around the mean. Why are we doing it? Well, to prevent features with larger means to dominate (and hence falsely appearing to contribute to a given principal component). By centering each feature around their mean, we are making the calculation of covariance in the next step easier and comparable.</p><p>b) Calculate a covariance matrix to describe how a given feature covaries with other features. Since we are using numpy, it requires the features to be rows and each sample or data point to be a column. Therefore we transpose the input matrix using .T functionality from numpy.</p><p>c) Next, we calculate eigenvectors and eigenvalues from the covariance matrix that we calculated in step b. Eigenvectors will give us the orthogonal directions (uncorrelated) among the data points that would maximize the variance (coming from the covariance matrix) in a given direction. And Eigenvalues will give us the magnitude of that variance. Hold on to these; later we will project our original points onto a straight line/plane/hyperplane that passes through the points in the direction given by the eigenvectors.</p><p>d) We want to choose the number of principal components as given by the user through the variable <em>desired_principal_components </em>such that we report the top principal components (PCs) arranged in a decreasing order of their ability to maximize the variance among the data points based on the input feature set. Of course, these principal components will contain non-redundant information. But what does a principal component represent? I will clarify that in a moment. Read along.</p><p>e) Since we need to fetch PCs in a decreasing fashion, we need to sort our eigenvalues. We will use the argsort() method from numpy. Since we want the eigenvalues sorted in a decreasing order we use the slicing functionality [ : :-1] i.e reversing the order that argsort() returns by default. This will return the indices of eigenvalues arranged from the highest to lowest.</p><p>For example, the vector for eigenvalues was as follows: [13, 5, 25, 4]. Here the index of the highest eigenvalue is 2 i.e 25, next highest is 0 i.e 13 and so on. So the indices vector that argsort()[ : : -1] will return would be [2,0,1,3].</p><p>f) Next we need to fetch the eigenvectors corresponding to the sorted eigenvalues based on these indices. Since we are using numpy, we need to first transpose our variable <strong><em>eigenvectors</em></strong> that contain all eigenvectors. Finally, we assign the desired number of eigenvectors (arranged in descreasing order based on their corresponding eigenvalues) to our variable extracted_eigenvectors that we had initialised to <strong><em>None</em></strong> in our constructor.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a25925ee7cba46746363c1d519941379/href">https://medium.com/media/a25925ee7cba46746363c1d519941379/href</a></iframe><p>What does a principal component mean though? Well, the answer isn’t that straightforward. But let us understand it intuitively. Let us say we had only two dimensions in our book example. One dimension was <strong><em>number of pages</em></strong> and another dimension was <strong><em>number of words</em></strong>. Since both are linearly correlated, PCA would merge both of them into principal component 1 (with maximum variance) and that new reduced dimension that is a linear combination of both the original dimensions could represent a new feature (that does the best job describing the books as one variable instead of two original variables) called <strong><em>book content. </em></strong>Remember, we are naming the PC1 as book content here because that is what intuitively <strong><em>number of pages</em></strong> and <strong><em>number of words</em></strong> together represent. Sometimes PCs could be a linear combination of many variables that are correlated linearly but might not have an intuitive single phrase name to describe them. Therefore, one can think of PCs as an abstract version of the original variables. One can though look at the individual contribution of different original variables that goes into making up a given principal component. These are called as <strong><em>factor loadings</em></strong> in PCA lingo.</p><p>Now, getting back to our code. So far we have used the covariance structure of the data to extract the eigenvectors and eigenvalues ( i.e. the direction and magnitude) from our data such that we pick the topmost PCs maximizing the variance in the data later on. All we are now left to do is project our original data onto the line/plane/hyperplane given by the eigenvectors. These are what we will call principal components.</p><p>f) For this, we will make another function called transform(). It will take the original data matrix as an input. We would center the data again, since the eigenvectors that we get were on the centered data. So the projections should also be between centered inputs (original matrix and eigenvectors). Projecting the datapoints in the principal component space is nothing but taking a dot product between the centered original data matrix and matrix of eigenvectors that we sorted and extracted using the fit function. Again, we need to first transpose the <strong><em>extracted_eigenvectors</em></strong> variable. And VOILA! We have the principal components!!!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0b6dc31a8697c65f378897c71a9c67f/href">https://medium.com/media/a0b6dc31a8697c65f378897c71a9c67f/href</a></iframe><p>Now let us test our class PCA() on a dataset.</p><p>We are going to use already built-in datasets in <strong><em>sklearn</em></strong> library for classification. Remember, we are not going to do a supervised learning. But once we visualize our data using PCA on a two dimensional graph, we will overlay the class labels of whatever dataset we use to see how best PCA was able to segregate the data, without explicitly trying to segregate the classes.</p><p>Let us take an example of sklearn’s wine dataset with different features, collected from three different geographical locations in Italy i.e. three different kinds of wines. Let us see with the features for each kind of wine, can PCA segregate the three types of wines or not.</p><p>Full documentation can be found at <a href="https://scikit-learn.org/stable/datasets/toy_dataset.html#wine-dataset"><strong><em>https://scikit-learn.org/stable/datasets/toy_dataset.html#wine-dataset</em></strong></a></p><p>The code for plotting principal components based on the class we create above is shown below and is self explanatory (with comments for each line of code).</p><p>Before we dive into the code, let’s see what the data comprises of. There are 13 numeric features describing the various wine components. These are:</p><ol><li>alcohol content,</li><li>malic acid content,</li><li>magnesium content,</li><li>total phenols present,</li><li>flavanoids,</li><li>nonflavanoid phenols,</li><li>color intensity,</li><li>proanthocyanins</li><li>hue</li><li>OD280/OD315 of diluted wines</li><li>proline</li><li>Ash</li><li>Alkalinity of ash</li></ol><p>There are three classes of wines:</p><p>a. class_0 (59 samples),</p><p>b. class_1 (71 samples) and,</p><p>c. class_2 (48 samples).</p><p>Back to the code now.</p><pre>##importing necessary libraries<br><br>import numpy as np<br>import matplotlib.pyplot as plt<br><br>#the class PCA() that we made above is saved as a pca.py<br>from pca import PCA <br><br>#importing datasets from sklearn that has the wine dataset<br>from sklearn import datasets<br><br>##loading the wine dataset() from sklearn<br>wine_data = datasets.load_wine()<br><br>##extracting the features table and labels<br><br>#pca will be carried on the following input matrix<br>input_features_matrix = data.data<br><br>#for coloring our datapoints later based on these labels describing the three kinds of wines<br>target_labels = data.target<br><br>##carrying out PCA based on our code<br><br>#this will initialise the variables we defined in the constructor of class PCA()<br>pca_object = PCA(2)<br><br>#let&#39;s extract the eigenvectors<br>pca_object.fit(input_features_matrix)<br><br>#let&#39;s project the data onto the eigenvectors to get PCs<br>principal_components = pca.transform(input_features_matrix)<br><br>#let&#39;s plot the first two principal components<br><br>PC1=principal_components[:,0]<br>PC2=principal_components[:,1]<br><br>plt.scatter(PC1,PC2,c=target_labels,edgecolor=&#39;none&#39;,alpha=0.8,cmap=plt.cm.get_cmap(&#39;viridis&#39;,3))<br><br>plt.xlabel(&#39;Principal Component 1&#39;)<br>plt.ylabel(&#39;Principal Component 2&#39;)<br>plt.colorbar()<br>plt.show()<br></pre><p>The script above will result in the following plot:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/381/1*B86AG18BT6nMikb1ynB8CQ.png" /></figure><p>Remember, we are just overlaying the class type on the plot in the end. What this tells us is that based on the global and linear structure that is hidden among the 13 variables that describe the three kinds of wines, <em>class 0 </em>wine is well segregated from <em>class 1</em> and <em>class 2</em> wine. However, there is huge variability among different samples of <em>class 0</em> wine, on the first principal component. It also tells us that wines represented by <em>class 1</em> and <em>class 2</em> are very similar to each other and not well segregated based on the 13 features using PCA. (Hmm… It could be that wines <em>class 1</em> and <em>class 2</em> are grown in geographically and climatically similar conditions?)</p><p>What does this leave us with? There are many possibilities that could explain the observations and dictate the next steps in our analysis. I am listing four here:</p><ol><li>One would need more features that could segregate even <em>class 1</em> and <em>class 2</em> using PCA.</li><li>Maybe there are more nuanced local structures in the data that PCA can not take care of, to better segregate <em>class 1</em> and <em>class 2</em>?</li><li>Maybe there are non-linear relationships that exist between <em>class 1</em> and <em>class 2 </em>wines that again PCA can not handle.</li><li>Maybe <em>class 1</em> and <em>class 2</em> wines are indeed the same wine and labelled differently because of different sellers trying to increase their business.</li></ol><p>Could you think of other reasons? Maybe there is mislabeling? Inaccurate measurements? Other reasons?</p><p>Remember PCA is just one unsupervised technique. There are other unsupervised techniques one could try putting this dataset through. For example, t-SNE or UMAP or PACMAP. All these can preserve the local and/or the global structure of the data better than PCA and are worth trying to explore more nuanced structures ( Oops! I use nuanced A LOT, I just realised).</p><p>Finally, since we have the information of the class labels for each of our samples, one could use classification supervised learning techniques like logistic regression, Support Vector Machines, Tree-based models and so on. It is worth trying now, since PCA does reveal that the features at hand are not completely garbage and this data exploration suggests that it would be worth spending more time using the more robust supervised learning methods.</p><p>What I have not covered in this post is to understand what features (out of the 13) contribute most to the principal component 1 and what is the magnitude of individual contribution to that PC. That is important and interesting to know since they will help us understand what exactly makes these wines so different from each other. Could you think of a way to code these factor loadings, as I mentioned earlier, in the PCA class that we made?</p><p>Maybe I could do another post for it next time!</p><p>For now I leave you with the code that we built so far. I encourage you to try other datasets provided by sklearn, other than the wine dataset. For example, you could try the breast cancer dataset with two classes — cancer versus normal. The features (30 features) are differences in characteristics of cancer and normal cells as seen under the microscope. The link for the breast cancer dataset is : <a href="https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_breast_cancer.html#sklearn.datasets.load_breast_cancer">https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_breast_cancer.html#sklearn.datasets.load_breast_cancer</a></p><p>Happy PCAing! *pours a glass of wine* (I choose <strong><em>class 0 </em></strong>wine)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=968af5e0d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sins and Sinners]]></title>
            <link>https://mohaksharda.medium.com/sins-and-sinners-f62abdfb2561?source=rss-5c1ba9c116d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/f62abdfb2561</guid>
            <category><![CDATA[writing]]></category>
            <category><![CDATA[philosophy]]></category>
            <category><![CDATA[poem]]></category>
            <category><![CDATA[poetry]]></category>
            <category><![CDATA[reading]]></category>
            <dc:creator><![CDATA[Mohak Sharda]]></dc:creator>
            <pubDate>Mon, 07 Jun 2021 08:51:18 GMT</pubDate>
            <atom:updated>2021-06-07T16:08:54.450Z</atom:updated>
            <content:encoded><![CDATA[<p><em>When does one qualify to be a sinner?</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/467/1*l0ETJIdff7gVZPeVU66LBw.jpeg" /></figure><p>The sky seemed to have hosted a battle long awaited,<br>for the blood that spilled, painted the field.<br>It was at this hour of the day that the vultures all gathered,<br>they had waited a fortnight for the big feast.</p><p>“Justice will be served, yes!”, said one.<br>Others repeated these words, as it be the only known truth.<br>Murderer, Man-slayer, Machiavellian, shouted the just people;<br>their dictionaries seemed biased for words but other.</p><p>“Do you wish anything before your last breath, traitor?”,<br>asked the hangman as he prepared the noose.<br>The traitor looked up at the sky, the mob, the horizon;<br>the wishes he couldn’t count outnumbered the ones he could.</p><p>He knew he wasn’t getting rid of the disdain that filled the air,<br>the filth that corrupted the place washed sincerely the sincere souls.<br>The cutthroat had longed for fame, for recognition, his whole life,<br>finally which he got disguised as infamy, for death was on his side.</p><p>“Yes, I have one wish. But I fear if that’s feasible to be fulfilled.”<br>“Go on. Vomit it out! Be it any. The mob will decide; unless it’s to be freed.”<br>“Last night, I conversed with a nine year old. I wish to let these people know that, uninterrupted.”<br>“I speak on behalf of the mob you <em>mook</em>. Words have never been stopped unless otherwise interpreted.”</p><p>I was all sordid and glum.<br>Like a maniac I ate and drank rum.<br>People around me couldn’t help but chastise,<br>From among them came a boy half their size.</p><p>He wiped my tears and rubbed my shoulder.<br>And let me sense how much I had grown pale and colder.<br>He sat beside me without any condescension,<br>and asked me when I was going to die and if I had any apprehensions.</p><p>“I see death each day, dawn and dusk.”<br>declared the boy while he tried to tie his shoe lace.<br>“What do you know about death?”<br>I couldn’t help but reply, as I felt my feet go numb.</p><p>“I have seen it in the eyes of Lucy.<br>A room is where she stays in the daytime.<br>For in the night, she is occupied by different men, sometimes women, each hour.<br>I don’t know why, but she screams a lot, and there’s nobody to help her.”</p><p>“I have felt it in the trembles of a monk.<br>He lives right next to the building where Lucy stays.<br>I didn’t know until I saw him coming out of the room where she stays.<br>My friend Paul says that it’s an emotion of guilt.”</p><p>“I have sensed it in the eyes of Lucy’s mother.<br>She stays a block away from where Lucy stays.<br>But in the night she fetches people with manners my mother asks I should learn.<br>And she jots down their names, and sends them to make Lucy scream.”</p><p>“I have tried to forget it when my mother cried,<br>when I tried to help Lucy get out of that room.<br>With every slap she gave me, her cries became louder.<br>When I exclaimed that I wanted to help, she hugged me.”</p><p>“I have heard it from my father’s mouth.<br>He is the hangman who will hang you from the noose tomorrow.<br>He says its a way of salvation for all those people who have committed sins,<br>to hang them and let the world be free of sinners.”</p><p>“Sir, I guess although I hate my father,<br>but I believe it’s better to die once<br>than to die everyday, for the sins will never end<br>and the sinners will never cease to exist.”</p><p>I was shocked by the irony he brought through his words.<br>And couldn’t help but ask, “Why do you hate your father?<br>He helps people get salvation.”<br>The boy replied, “My father owns the building where Lucy stays.<br>And he does nothing to stop her screams.”</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f62abdfb2561" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Putting ABC in Phylogenetic Comparative Methods]]></title>
            <link>https://mohaksharda.medium.com/putting-abc-in-phylogenetic-comparative-methods-6d93a8e63a91?source=rss-5c1ba9c116d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/6d93a8e63a91</guid>
            <category><![CDATA[computational-biology]]></category>
            <category><![CDATA[evolutionary-biology]]></category>
            <category><![CDATA[bioinformatics]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[bayesian-statistics]]></category>
            <dc:creator><![CDATA[Mohak Sharda]]></dc:creator>
            <pubDate>Sat, 29 May 2021 06:41:39 GMT</pubDate>
            <atom:updated>2021-05-29T08:17:17.191Z</atom:updated>
            <content:encoded><![CDATA[<p>Employing Approximate Bayesian Computation approach in Phylogenetic Comparative Methods</p><h3>Phylogenetic Comparative Methods (PCMs)</h3><p>When did humans develop the ability to walk on two feet, as compared to their ancestors? Did this happen because of a change in lifestyle from arboreal to terrestrial? If so, did this transition universally change all terrestrial animals? Did certain mechanisms (more than others) help humans develop bipedal-ism?</p><p>Phylogenetic comparative methods (PCMs) let us ask such questions.</p><p>What are PCMs? Let’s break it down.</p><p>In the context of evolutionary biology, the origin of all the extant and extinct species can be traced back to one common ancestor, billions of years ago. This historical relationship throughout evolution can be represented in the form of, what is called as,<em> The Tree of Life </em>(Figure 1)<em>. </em>The study of how the organisms are related to each other in this tree of life is called <strong>phylogenetics</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*cKTSX42kniiXbq7vEu4nCw.png" /><figcaption>Figure 1: The tree of life</figcaption></figure><p><strong>Phylogenetic comparative methods</strong>(PCMs) are a set of tools — at the intersection of mathematics, biology and computer science — that extend from the field of phylogenetics. They allow us to compare different sets of species and populations throughout evolution and ask questions like — 1) what is the extent of similarity and/or dissimilarity between two or more groups of species?, 2) is an evolutionary process, contributing to the formation of new species, specific to certain organisms or can it be generalised? More specifically, one can ask questions like — did the ability to fly evolve among organisms once or multiple times throughout the history (figure 1)?</p><h3>Conventional methods in PCMs</h3><p>Using PCMs we try to infer the past given the information in hand. To do this we build models that allow us to test across hypotheses. These models, based on assumptions, are used to estimate evolutionary parameters that can best describe a given phenomenon.</p><p>One of the most common models that is employed for continuous traits — traits that can take on real values like brain size — is the single rate Brownian motion (BM) model.</p><p>Traits evolving under Brownian motion change randomly, in distance and direction, over a given time interval. Therefore, BM model assumes the change in trait to follow a Gaussian distribution with a mean zero and variance proportional to the length of time and evolutionary rate. <em>Note how it models change in trait and </em><strong><em>not</em></strong><em> the trait value per se.</em> <strong>Briefly, imagine a starting population with a mean trait value x. Any new species emerging from this ancestral population will have deviations from x, say x’, as a function of time and rate of evolution. In other words, the lesser the time since the new species diverged from the ancestor, the closer will be their trait values.</strong></p><p>Based on the variance-covariance relationship among species one can calculate the unknown parameters like evolutionary rate for the BM model.</p><p>Owing to the inconsistent nature of evolution, it is common to observe certain traits evolving at a different rate in a group of species (group A) when compared to a distantly related group of species (group B). In such cases, a better alternative would be to employ a multiple rate BM model — a more complex model than the one with single rate. As the evolutionary processes get more and more complex, with an increasing number of unknown parameters, the conventional methods used in PCMs start becoming inappropriate to model. This particularly poses a problem where most of the <strong>parameters lack an analytical solution</strong>.</p><p>One way to work around this is to use a simulation based approach called <strong><em>The Approximate Bayesian Computation or ABC</em></strong>.</p><h3>Approximate Bayesian Computation (ABC)</h3><p>Before we get to the <em>how</em> of it, let’s understand ABC first.</p><p>Given a set of observed trait data <strong>T</strong> for species involved in the study and an assumed evolutionary model <strong>E</strong> with, let’s say, <strong>N</strong> parameters, the approximate Bayesian computation approach consists of the following steps (Figure 2):</p><ol><li>Infer the prior probability distributions of all the <strong>N</strong> parameters for <strong>E</strong>.</li><li>For each simulation, generate a random set of values for <strong>N</strong> parameters from their respective prior distributions. Let these random set of values be denoted by <strong>R</strong>.</li><li>Using <strong>R</strong>, simulate trait data points <strong>S</strong>.</li><li>If <strong>S</strong> is similar to <strong>T</strong>, accept <strong>R</strong>.</li><li>Repeat these simulations till a large number of <strong>R</strong> values have been accepted.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hC6GcFI7mQ03RQN0km8UiQ.png" /><figcaption>Figure 2: Using ABC in PCMs</figcaption></figure><p>Let’s revisit step 4 — if S is similar to T, accept R.</p><p>Wait what? If S is similar to T? What does it mean to be “similar”?</p><p>Also, what are the chances that you would get an exact replica of trait values in a simulation <strong>for all the species included in the analysis</strong> that are identical (or even similar) to the original data set?</p><p>What I mean to say is this — Imagine you have five mammals — East African Lion, Plains Zebra, Reticulated Giraffe, Hippopotamus and Humans (that being the star cast of the movie <em>Madagascar</em>, except the last mammal on the list). Let’s say we have the data for their brain sizes (denoted by <strong>T</strong>) as measured by biologists . Now we want to test models with associated parameters that can help us understand how brain size evolved in these animals. Let’s assume the analytical solution of the unknown parameters (let’s say <strong>N=3</strong>) is not possible to calculate. Now according to the ABC approach, we have to get to a set of parameters (<strong>R</strong>, chosen at random from their prior probability distributions) that after simulation, give us brain size values (denoted as <strong>S</strong>) which are very similar to <strong>T</strong>. Isn’t that too abstract to decide if S is similar to T?</p><p>Therefore, to be clear about what the similarity is, in step 4 is crucial.</p><p>One approach is to use a summary statistics like mean, variance, likelihood etc. to compare S (simulated trait data) and T (observed trait data). Comparing one value versus a large number of values is way easy.</p><p>(imagine including and comparing brain size S-T pair values for all the mammals in the analysis,</p><p>…and birds,</p><p>…and insects,</p><p>… and Archean and bacteria… okay, scratch the last two, they don’t have brains.</p><p>I mean, you get the point!).</p><p>But again, <em>how similar is similar enough</em>?</p><p>In other words, how much deviation between S and T can one <strong>tolerate</strong>?</p><p>One can introduce a measure called <strong><em>tolerance</em></strong> (<strong>t</strong>) such that the difference between S and T (measured by some <strong><em>distance index d</em></strong>) has to always be less than <strong>t </strong>in order to accept R.</p><p>There are a number of algorithms that can be used to describe the criterion for similarity. What we just saw above is called <strong><em>rejection sampling</em></strong>.</p><p>Here’s a food for thought before I end this section — How does one decide on a summary statistics appropriate for one’s analysis? The “<em>Is there a better alternative?</em>” issue. Go figure!</p><h3>Tying it all together</h3><p>So here we are with a set of R (parameter estimates) values accepted over simulations. Now what?</p><p>Remember we used ABC to <strong><em>estimate unknown parameters</em></strong> for trait evolution. We started with prior distributions, our expectations, our beliefs of what those parameters could be. And after getting a set of R values, what we get are the <strong>posterior distributions</strong> for the parameters. One of the most common measures calculated from the posteriors is the <em>Maximum a posteriori probability</em> estimate or <strong>MAP</strong>. It is the mode of the distribution that is used as the most likely estimate of the parameter. I would like you to revisit the last sentence and appreciate how easy it becomes to infer measures like “the most likely estimate”. This is possible only because Bayesian statistics let’s us deal directly in terms of probabilities. Furthermore, we also get the uncertainty associated with the parameter estimates.</p><p>So the recipe is — you have your belief (i.e prior), you are presented with some evidence (i.e trait values) and you update your belief (i.e posterior) after taking that evidence into account (i.e similarity between the observed and the simulated trait values) and you find the most probable estimate of the parameter. <strong>That is how we are in real life as well!</strong></p><p>Okay, what else can we do with the posterior distributions?</p><p>One can use the posterior distributions to see how much they deviate from a value that one expects; let’s say a value under a null hypothesis. Not just that, one can even get posterior distributions for all parameters across different evolutionary models and compare them to find which model(s) best describe(s) the data.</p><p>To wrap this section up, we saw how parameters without analytical solutions can be approximated using ABC. There are other instances that ABC might help us with. For example, it is not possible to capture the entire species belonging to a group (just like how this post is not exhaustive for the topic it presents) or to have the observed trait data for all species one wishes to study. This is a problem known to reduce the accuracy with which the parameters are estimated. ABC comes in handy in this situation since it depends on comparing the summary statistics of the observed and simulated trait data. And as long as the sampling is representative enough, the power to test across models remain uncompromised as well.</p><h3>Looking ahead</h3><p>In all, I feel the employment of ABC in PCMs is relatively rare, with an untapped potential waiting to be explored. For example, using this approach one can actually focus on studying different stages of an evolutionary process. This is different from what PCMs are usually used for — to study just the patterns of trait evolution. In this article, my intention was to give a connecting link between ABC and PCM, and introduce tid-bits to ponder upon later. In case anyone of you reading this article finds their interest in a specific topic, I could expand on it in my next posts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6d93a8e63a91" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding the structure of an object of class Phylo]]></title>
            <link>https://mohaksharda.medium.com/understanding-the-structure-of-an-object-of-class-phylo-92102cf357a6?source=rss-5c1ba9c116d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/92102cf357a6</guid>
            <category><![CDATA[computational-biology]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[bioinformatics]]></category>
            <category><![CDATA[biology]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Mohak Sharda]]></dc:creator>
            <pubDate>Thu, 25 Jun 2020 05:44:14 GMT</pubDate>
            <atom:updated>2020-06-25T05:44:14.357Z</atom:updated>
            <content:encoded><![CDATA[<p>The R package <em>ape </em>is widely used for any analysis related to phylogenetics and comparative methods used to study evolution across organisms. The first thing required to carry out any such study is to store all the features that define a phylogenetic tree. <em>ape</em> does this by creating a special structure called ‘Phylo’.</p><p>What are these features? What is this structure? How are they stored? How do they look when visualised?</p><p>Let’s try and understand these things.</p><p>We will start with a top-down approach. Let’s look at a phylogenetic tree consisting of five individuals. For the sake of keeping things simple, let’s name the individuals as A,B,C,D and E.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*9gKAEkA-m6YUHBFIjPxz8w.png" /><figcaption>Figure 1. A phylogenetic tree depicting the relationship between five individuals.</figcaption></figure><p>Let’s try and get a structure to build this tree.</p><p>We are going to code this in R.</p><p>Let’s define an empty variable called <em>phylogenetic_tree</em> of the type list().</p><pre>phylogenetic_tree&lt;- list()</pre><p>Next, we are going to assign a class name ‘phylo’ to our variable. This will allow us to use the functions available in <em>ape, </em>that are applicable only on objects belonging to ‘phylo’.</p><pre>class(phylogenetic_tree) &lt;- “phylo”</pre><p>You can imagine that our class ‘phylo’ is like a rulebook that will dictate all the features that any tree should have. Our object, in this example called <em>phylogenetic_tree</em>, will have those features by the virtue of it belonging to ‘phylo’ (as assigned above).</p><p>Let’s give our tree some structure now. We want to make a tree as depicted in figure 1. There are five individuals, also called as leaves of the tree. There are three internal nodes labelled 7, 8 and 9. There is one root for the entire tree i.e node number 6. It is the Most Recent Common Ancestor (MRCA) for the five individuals in our example. We are going to make a fully bifurcating rooted tree. Therefore, the root of the tree is at the internal node 6. It bifurcates into two children or descendent nodes 7 and 9. The internal node 7 bifurcates further into individual A and internal node 8.</p><p>Why are the direct descendents of the root not 7 and 8 instead? The answer lies in the way the traversal happens while reading a tree.</p><p>Stand at the root of the tree (level 1) facing the leaves and start moving along the path. After the path bifurcates, the traversal would dictate you to move to the right node (internal or tip) first and then to the left node. In our case, we first move from the internal node 6 to the internal node 7. Now, the latter (level 2) bifurcates again. Since on the right is a leaf i.e individual A, it terminates. It then moves to the left side which is an internal node. Therefore, in sequence this would be internal node 8 (level 3). This further bifurcates into two individual leaves B and C. There are no further levels left. It comes back to level 2. There is another internal node bifurcating from 6 on the left side that we will name as internal node 9. Finally, this terminates into two leaves E and D.</p><p>(If you read the previous paragraph without visualising each line on the tree as depicted in Figure 1, I would urge you to do that before proceeding ahead.)</p><p>Let’s code this in R, exactly the same way we traversed.</p><pre>phylogenetic_tree$<strong>edge</strong> &lt;- matrix(c(6,7,<br> 7,1,<br> 7,8,<br> 8,2,<br> 8,3,<br> 6,9,<br> 9,4,<br> 9,5),8,2,byrow=TRUE)</pre><p>The above command will result in a matrix with 8 rows and 2 columns. The rows represent the number of steps (formally called as edges) it takes to cover the entire tree from one node to another. The columns keep a track of the two nodes participating in each step. Remember, we are storing this as a feature of our variable <em>phylogenetic_tree</em>, an object of the class ‘phylo’. Specifically, we would call this feature using the syntax: <em>phylogenetic_tree$</em><strong><em>edge</em></strong>.</p><p>Let us give our object some more properties. Let’s add a feature <strong><em>Nnode</em></strong> storing the number of internal nodes our tree would have. Let us label our internal nodes and tips(or leaves) as well.</p><pre>phylogenetic_tree$<strong>Nnode</strong> &lt;- 4<br>phylogenetic_tree$<strong>node.label</strong> &lt;- c(6,7,8,9) #as per our traversal<br>phylogenetic_tree$<strong>tip.label</strong> &lt;- c(“A”,“B”,“C”,“D”,“E”)</pre><p>Let’s also assign some values to the edges. Remember, till now, we have only worked on the topology of the tree. We are yet to scale the edges. The length of each edge would represent either time since it diverged from the root of the tree or genetic distance in terms of changes in the genome of the individual(s) with respect to the ancestral node(s). A tree without any edge lengths would mean that the individuals along the tree are evolving independent of each other i.e there is no effect of a shared ancestory. (We will talk more on this in the subsequent posts.) We will see how <em>ape</em> represents this sort of a tree at the end of this post.</p><p>In figure 1, individual B has diverged the most. We will cap it to the maximum edge length of 1. And all the other edge lengths will be scaled down relative to it.</p><p>Let’s code this. Again, the sequence of assignment would follow the traversal path that we have been following so far. In other words, the order of rows in our matrix <strong><em>edge </em></strong>will decide the sequence of occurrence of edge lengths in our vector<strong><em> edge.length.</em></strong></p><pre>phylogenetic_tree$<strong>edge.length </strong>&lt;- c(0.4,0.7,0.5,1,0.3,0.4,0.5,0.4)</pre><p>Great! We have given our object <em>phylogenetic_tree</em> some nice properties as defined for the class ‘phylo’<em>. </em>This would help us in analysing our tree using other functions available in <em>ape</em>. I would cover those in the subsequent posts.</p><p>Finally, to plot <em>phylogenetic_tree</em>, we are going to use a function available in <em>ape</em>. Let’s install and load the package first and then use it’s function plot.phylo() to plot our tree as depicted in figure 1.</p><pre>install.packages(“ape”)</pre><pre>library(ape)</pre><pre>plot.phylo(tree,show.node.label = TRUE)</pre><p>This function, by default, only shows the tip labels. The plot.phylo() option show.node.label has to be set to TRUE in order to label the internal nodes as well (as seen in Figure 1).</p><p>For more information on different functions and their related options in <em>ape, </em>you can check out it’s extensive documentation:</p><p><a href="https://www.rdocumentation.org/packages/ape/versions/5.3">https://www.rdocumentation.org/packages/ape/versions/5.3</a></p><p>Now coming back to how <em>ape</em> represents a tree without any edge length. Let’s write the code again, this time without making an <strong><em>edge.length</em></strong> vector:</p><pre>ptree &lt;- list()</pre><pre>class(ptree) &lt;- “phylo”</pre><pre>ptree$edge&lt;-matrix(c(6,7,<br> 7,1,<br> 7,8,<br> 8,2,<br> 8,3,<br> 6,9,<br> 9,4,<br> 9,5),8,2,byrow=TRUE)</pre><pre>ptree$Nnode &lt;- 4</pre><pre>ptree$node.label &lt;- c(6,7,8,9)</pre><pre>ptree$tip.label &lt;- c(“Species_A”,“Species_B”,“Species_C”,“Species_D”,“Species_E”)</pre><pre>library(ape)</pre><pre>plot.phylo(ptree,show.node.label = TRUE)</pre><p>For visualisation purposes, <em>ape</em> will depict it as shown in figure 2, even though we haven’t coded for any edge lengths. For example, for species E and species D, we can not say that both have equally diverged from their ancestor at node 9, even though it might seem like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*xPnDmoBARrC1P2aaLN6M0Q.png" /><figcaption>Figure 2: A phylogenetic tree representing a relationship of five species A- E not scaled according to their edge lengths.</figcaption></figure><p>That’s all for this post, folks. I will be covering other subtle concepts related to phylogenetics and phylogenetic comparative methods as a part of this series. If there is something you guys would specifically like to read/know, please let me know in the comments below. Happy reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=92102cf357a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>