<?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 Sohail on Medium]]></title>
        <description><![CDATA[Stories by Sohail on Medium]]></description>
        <link>https://medium.com/@syedar.sohail?source=rss-c20d54ec1d36------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1arw3Vp-8Zos2Yq5Xbk_TQ.jpeg</url>
            <title>Stories by Sohail on Medium</title>
            <link>https://medium.com/@syedar.sohail?source=rss-c20d54ec1d36------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 16:47:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@syedar.sohail/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[Docstring and why is it important ? — Python Classes, Modules and Functions]]></title>
            <link>https://medium.com/@syedar.sohail/docstring-and-why-is-it-important-python-classes-modules-and-functions-95fee5247ff5?source=rss-c20d54ec1d36------2</link>
            <guid isPermaLink="false">https://medium.com/p/95fee5247ff5</guid>
            <category><![CDATA[programmer]]></category>
            <category><![CDATA[programming-tips]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[docstring]]></category>
            <dc:creator><![CDATA[Sohail]]></dc:creator>
            <pubDate>Sun, 30 Oct 2022 18:12:26 GMT</pubDate>
            <atom:updated>2022-10-30T18:12:26.786Z</atom:updated>
            <content:encoded><![CDATA[<h3>Docstring and why is it important ? — Python Classes, Modules and Functions</h3><h4>“Code is more often read than written.”— <em>Guido van Rossum</em></h4><p>When the complexity of code increases, we are pushed to make our own classes that is okay but what if we wrote 1000 lines of code and we have given it to our colleague or shared the files privately or to a group in a organisation who wants to use our classes. Well, they may spend a lot of time figuring out what our class attributes might do, so to tackle this problem Python has something called ‘Docstrings’ to help us out. Well, what is a docstring first? A Docstring is a brief text that explains what something does.</p><p>Please do not confuse thinking comments and Docstrings are the same. well, they may highly look similar in the way they work but there is a lot of difference. Comments are written generally to show some unusual portions of code and for fixing the bugs. While Docstrings are the right tool for documenting the classes, functions, modules and packages. Yes, that’s right ! we can document all these. Let’s look at the practical example below.</p><p>Well, let us look at the two scenarios what happens when we use a docstring in our class and what happens if we don’t use it.</p><p>I have created a class named iphone_entry with two constructor methods with attributes name , size and color then I have created an instance of the class using id1 and this instance id1 is inheriting everything that iphone_entry has got and I have given some values and printed the values out.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*whwwKxd1qkzi4CyrbNfgmw.png" /></figure><p>Let’s say a coworker named Joe who is part of production team is testing the program and wants to suggest few changes but he doesn’t understand what this methods and attributes mean, now this may seem very silly but just for the sake of understanding and how docstrings works I am doing this.</p><p>Joe uses the “help” function to find out more and does this but the help function to his surprise shows not enough documentation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/1*rT6C04tbuFV522v5aJT9qg.png" /></figure><p>So, to help my coworkers I write the documentation using docstrings as shown below. The docstrings are just like normal strings but are multiline and have triple quotes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y9PiFPt9GHor34sCnUj-sQ.png" /></figure><p>Let’s re-run the scenario now and use the help function and see the difference in results.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Iw3z1PhQ_zAwdN8VVnicpA.png" /></figure><p><em>“It doesn’t matter how good your software is, because </em><strong><em>if the documentation is not good enough, people will not use it.”</em></strong><em>— Daniele Procida</em></p><p>This time the results are very helpful, Joe can now understand what the “check_id”, “signatures” and little description about values that could be in the class. In the above example the docstrings were used in both the class “iphone_entry”and the methods “check_id”, “signatures” inside the class.</p><p>Docstrings can also be used to create libraries and they can also be used to get information of python libraries (modules) like in the example below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/1*uzsXwb1orjbxc0s9afxJXg.png" /></figure><p><em>The importance here is not just about that we can write a python code the emphasis is also on how we code i.e., the style of writing it, the variable names we choose (mnemonic) and how we structure it. There are many ways to write a code but this difference is what separates a elegant and disciplined programmer from the rest. And this habit helps in the long term assuming a decent span of career.</em></p><p><strong>Conclusion:</strong></p><p>In the examples above, we have seen docstrings in action and how powerful they can be if used in the right way. This tiny good practises in the long run are really helpful and they distinguish us from the rest if you want to code in an elegant way. Hope you follow along with this practise and cultivate the new habit of using it.</p><p>Thank you so much if you have followed along till here.</p><p>I really hope this has been a great read and you must have learnt some new stuff.</p><p>Please <strong>Comment </strong>out below for suggestions and feedback. Just like you, I am still learning how to become a better Data Scientist and Engineer. Please help me improve so that I could help you better in my subsequent article releases.</p><p>Thank you and Happy coding :)</p><p><strong><em>Credits</em></strong> :</p><p><a href="https://www.geeksforgeeks.org/python-docstrings/">Python Docstrings — GeeksforGeeks</a></p><p><a href="https://www.datacamp.com/tutorial/docstrings-python">Python Docstrings Tutorial : Examples &amp; Format for Pydoc, Numpy, Sphinx Doc Strings | DataCamp</a></p><p><a href="https://realpython.com/documenting-python-code/">Documenting Python Code: A Complete Guide — Real Python</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=95fee5247ff5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is a Decision Tree in Machine Learning ?|Classification |Regression]]></title>
            <link>https://medium.com/@syedar.sohail/what-is-a-decision-tree-in-machine-learning-classification-regression-bc27d91d1631?source=rss-c20d54ec1d36------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc27d91d1631</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[regression]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[classification]]></category>
            <category><![CDATA[decision-tree]]></category>
            <dc:creator><![CDATA[Sohail]]></dc:creator>
            <pubDate>Wed, 24 Nov 2021 09:58:46 GMT</pubDate>
            <atom:updated>2021-11-24T09:58:46.584Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is a Decision Tree in Machine Learning ?|Classification |Regression</h3><p>In this article I want to walk you through about decision trees and then I will end up this article with some exercises. Okay let’s get started.</p><p>In its simplest form, a decision tree is a type of flowchart that shows a clear pathway to a decision. In terms of data analytics, it is a type of algorithm that includes conditional ‘control’ statements to classify data. A decision tree starts at a single point (or ‘node’) which then branches (or ‘splits’) in two or more directions. Each branch offers different possible outcomes, incorporating a variety of decisions and chance events until a final outcome is achieved. When shown visually, their appearance is tree-like…hence the name!</p><p>Decision trees are extremely useful for data analytics and machine learning because they break down complex data into more manageable parts. They’re often used in these fields for prediction analysis, data classification, and regression. Don’t worry if this all sounds a bit abstract — I have provided a example below to help clear things up. First though, let’s look at the different aspects that make up a decision tree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S10T4ah3_JqdQ-eY6Hau0Q.png" /><figcaption>Above figure depicting the decision tree with a simple example. credit : <a href="https://www.bing.com/images/search?view=detailV2&amp;ccid=S10T4ah3&amp;id=A111E8AF00FD0EF09FC0118B8F2DBEA3E145269E&amp;thid=OIP.S10T4ah3_JqdQ-eY6Hau0QHaEF&amp;mediaurl=https%3a%2f%2fth.bing.com%2fth%2fid%2fR.4b5d13e1a877fc9a9d43e798e876aed1%3frik%3dniZF4aO%252bLY%252bLEQ%26riu%3dhttp%253a%252f%252fdataaspirant.com%252fwp-content%252fuploads%252f2017%252f01%252fB03905_05_01-compressor.png%26ehk%3dVRe2XAnc9cpm%252fhU%252bCYqg6HZMoxCd8pMqcA3Rx7oBAAA%253d%26risl%3d%26pid%3dImgRaw%26r%3d0&amp;exph=1226&amp;expw=2220&amp;q=decision+tree&amp;simid=608019746961635973&amp;FORM=IRPRST&amp;ck=D8EB4952773E55960DFA844448DBE2B7&amp;selectedIndex=13&amp;ajaxhist=0&amp;ajaxserp=0">decision tree — Bing images</a></figcaption></figure><p>Basically a decision tree has 3 basic nodes, <strong>root node</strong> first where the beginning is a single entry point here it is salary at least $50,000 then two conditions of “yes” and “no” which are called <strong>decision nodes </strong>which will lead to branch nodes and the final outcome which is also a single output point that is called <strong>leaf node, </strong>leaf because as a real tree has leaves at the tips.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xWT4n0IN4e31mSTWYR0oGg.png" /><figcaption>These are the basic decision tree classifier default hyperparameter values.</figcaption></figure><p><strong>Bonus !!!</strong></p><p><strong>A collection of decision trees is called Random Forests.</strong></p><p>I will write about Random Forests some other day.</p><p>Decision Tree has lot of hyperparameters and it is always a good practice to use them. why should you use them ? because if you are not going to use the hyperparameters the decision tree will work as a <strong>non-parametric model</strong> meaning it will stick very close to training data and boom! the result is overfitted model.</p><p>lets code a simple decision tree classifier</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*Mz0hajeMk8iDgAZ7ZFYbZw.png" /><figcaption>Here we are training a decision classifier with 10 subsets i.e., cross validation sets and the random state hyper parameter set to zero try using max depth with values between 0 to 8 and see the results and let me know in the comments below what pattern did you observe.</figcaption></figure><p><strong>Do you know ?</strong></p><p><strong>Decision Tree classifier can also be used for regression ?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/885/1*uxXzlkSWUGItgmIpg9ZsLg.png" /><figcaption>An example code depicting its use for regression.</figcaption></figure><p>Exercises and others…</p><p><strong><em>1. If it takes one hour to train a Decision Tree on a training set containing 1 million instances, roughly how much time will it take to train another Decision Tree on a training set containing 10 million instances?</em></strong></p><p>Ans: The complexity of training a decision tree is given by formula</p><p><strong>k= O(n × m log(m)), where m is the number of instances</strong></p><p>we do know that k=1 hour for 1 million if we equate with help of simple math we will get k=11.4(approx.) hours.</p><p><strong><em>2.if Decision Tree is underfitting the training set, is it a good idea to try scaling the input features?</em></strong></p><p>Ans: Decision Trees don’t care whether or not the training data is scaled or centered; that’s one of the nice things about them.</p><p>3. Train and fine-tune a Decision Tree for any classification dataset.</p><p>a. Split it into a training set and a test set using train_test_split().</p><p>b. Use grid search with cross-validation (with the help of the GridSearchCV</p><p>class) to find good hyperparameter values for a DecisionTreeClassifier.</p><p>Hint: try various values for max_leaf_nodes.</p><p>c. Train it on the full training set using these hyperparameters, and measure</p><p>your model’s performance on the test set.</p><p><strong>I hope you got a better understanding about decision trees.</strong></p><p><strong>Thank you so much for reading it !!!</strong></p><p><strong>Happy reading!</strong></p><p><strong>Credits:</strong></p><ol><li>Hands on Machine Learning with Scikit-Learn, Keras &amp; Tensor Flow.</li><li><a href="https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html">sklearn.tree.DecisionTreeClassifier — scikit-learn 1.0.1 documentation</a></li><li><a href="https://careerfoundry.com/en/blog/data-analytics/what-is-a-decision-tree/">What Is a Decision Tree and How Is It Used? (careerfoundry.com)</a></li><li><a href="https://rachelchen0104.medium.com/6-decision-trees-hands-on-ml-f0822ee43219">Hands-On ML Chapter 6. Training and Visualizing a Decision… | by Sisi (Rachel) Chen | Medium</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc27d91d1631" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Outlier handling using Robust Scaler — A python tutorial]]></title>
            <link>https://medium.com/@syedar.sohail/outlier-handling-using-robust-scaler-a-python-tutorial-613d174b58eb?source=rss-c20d54ec1d36------2</link>
            <guid isPermaLink="false">https://medium.com/p/613d174b58eb</guid>
            <category><![CDATA[scaling]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[feature-engineering]]></category>
            <category><![CDATA[k-means-clustering]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Sohail]]></dc:creator>
            <pubDate>Sat, 31 Jul 2021 14:42:09 GMT</pubDate>
            <atom:updated>2021-08-01T04:42:11.259Z</atom:updated>
            <content:encoded><![CDATA[<h3>Feature Scaling in Machine Learning: Robust Scaler and MinMax Scaler with K-Means Clustering — A python tutorial</h3><p>comparison with outlier techniques <strong><em>Min-Max Scaler </em></strong>and <strong>Robust Scaler , K-Means Clustering </strong>and much more.</p><p>To understand this concept let’s start by working with a dataset, we will be using a demographics dataset of a state Telangana which is in south part of India.</p><p>you can download the dataset using the link <a href="https://data.telangana.gov.in/dataset/demographics">Demographics | Telangana Open Data Portal</a></p><p>Basically in this tutorial, our problem is to divide the state into four regions based on <strong>urban population (%) </strong>presence, so we will use a simple K-Means clustering technique but in the approach to solve it we will use some <strong>new methods</strong> and learn about handling outliers too.</p><p>let’s look into the dataset</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9DTk5ALGOaWsMpdKlD78Gw.png" /></figure><p>let’s look into the data types of the column attributes and look for null values in the Data Frame.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ioQBa5ds4lUTc2EcmmPVwA.png" /></figure><p>Now let’s look at some stats, we notice that describe function here is giving us the values of measures of central tendency and measures of dispersion.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nRg6aR0BbFEIYepxuPzzhw.png" /></figure><p>I assume we didn&#39;t really get much insights from the describe function, let’s do a plot and see will that help</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jZtM0lk8XXK0FpT1yLcIYg.png" /></figure><p>well this is quite better, we are seeing some correlation in positive and negative forms here, for example males vs females and females vs households and you can try to explore more trends.</p><p>Now let’s get started by converting the ‘Districts’ attributes column into numerical.</p><p>we can either select “<strong>ordinal</strong> <strong>encoder technique”</strong> or the <strong>“One Hot Encoding”.</strong></p><p>I will stick with One Hot Encoding as the number of district names are around 31, so the matrix will not be too big. If you have a long list of categorical values it is better to avoid One Hot Encoding as it builds up a huge matrix and it causes high computation cost, I will be writing in more detail about One Hot Encoder Vs Ordinal Encoder in future.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DoJaoN_u__ohNyJAFcfRyg.png" /><figcaption>we have got a 31 x 31 matrix</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UFhF1MP7DidJ9O1i73gndg.png" /><figcaption>dropping the Districts column as we have the location details in matrix of numbers</figcaption></figure><p>when ever you want to make a plot of data to look for outliers the best ways are either to go with scatter plot or histogram or box plots. They speak a lot about data than other plots. I will make us of box plot and make a plot first for the data frame remember we are working only with column named urban population (%).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VVH74b_G1iYfFZUtmp5g1Q.png" /></figure><p>That’s good we do have some outliers, when we train a model with outliers we often get compromised on many aspects specially the accuracy of the machine learning model.</p><p>one approach many people follow is to start scaling or normalizing the data so that they can bring down the numbers but this still doesn’t solve the problem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*vK-sqq2ZG37tsBqrqV6NmA.jpeg" /><figcaption><strong>image source : </strong><a href="https://th.bing.com/th/id/OIP.vK-sqq2ZG37tsBqrqV6NmAHaEO?pid=ImgDet&amp;rs=1">https://th.bing.com/th/id/OIP.vK-sqq2ZG37tsBqrqV6NmAHaEO?pid=ImgDet&amp;rs=1</a></figcaption></figure><p>I will not go much in detail on how to handle them but I will use a technique called Robust-Scaler which is very good in handling the outliers.</p><h3>Robust Scaler</h3><p>Robust Scaler algorithms scale features that are robust to outliers. The method it follows is almost similar to the MinMax Scaler but it uses the interquartile range (rather than the min-max used in MinMax Scaler). The median and scales of the data are removed by this scaling algorithm according to the quantile range.</p><p>It, thus, follows the following formula:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/171/0*TxvOdx4oRlp7ewAK.png" /></figure><p>Where Q1 is the 1st quartile, and Q3 is the third quartile.</p><h3>MinMax Scaler</h3><p>MinMax Scaler is one of the most popular scaling algorithms. It transforms features by scaling each feature to a given range, which is generally [0,1], or [-1,-1] in case of negative values.</p><p>For each feature, the MinMax Scaler follows the formula:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/197/0*Xxr8bx61DBmKn8B0.png" /></figure><p>It subtracts the mean of the column from each value and then divides by the range, i.e, max(x)-min(x).</p><p>This scaling algorithm works very well in cases where the standard deviation is very small, or in cases which don’t have Gaussian distribution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*odK_v2Nl60_gLDRqp_acQA.png" /><figcaption>try adding another technique “Standard Scaler” if you are following along and see the results</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o1x_0Ebup7cjo5OIbWpi7Q.png" /></figure><p>so what do we understand from the plot?</p><p>Min-Max scaler brought the outliers close to it in range of [0,1] where as Robust Scaler scaled the data down and has also maintained the distance proportion with outliers.</p><p>okay now let’s do the clustering and wrap this up</p><p>let’s combine the location data frame with the urban population(%) so we can apply K-Means clustering on it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TD5KWfqNgXMLlqNKfbMZ8g.png" /></figure><p>Now apply K-Means algorithm</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qZzodXj4Y8wO8eriF5sGBQ.png" /></figure><p>now let’s add the labels to the original data frame where the names of the districts are present, I will use a copy of data frame here and plot it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b-ZybYmD2bpa0hbFWkNsrg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X3emoV1dB9X5BHKO3K7zeQ.jpeg" /></figure><p><strong>Conclusion:</strong></p><p>we see that the urban population presence is more in Hyderabad and Medchal compared to the other districts, Hyderabad being the capital city of the state having more urban population is just normal but when we look into the map we see that Medchal is in very close proximity around 35km from Hyderabad it really does make sense that there is lot of influx and outflux from the district to the Hyderabad and K-Means clustering has done really a good pretty descent job in dividing the state. One point to remember is that K-Means works really well with small data sets.</p><p>Thank you so much if you have followed along till here.</p><p>I really hope this has been a great read and you must have learnt some new stuff.</p><p>Please <strong>Comment </strong>out below for suggestions and feedback. Just like you, I am still learning how to become a better Data Scientist and Engineer. Please help me improve so that I could help you better in my subsequent article releases.</p><p>Thank you and Happy coding :)</p><p><strong><em>Credits</em></strong> :</p><p><a href="https://machinelearninggeek.com/feature-scaling-minmax-standard-and-robust-scaler/">Feature Scaling: MinMax, Standard and Robust Scaler (machinelearninggeek.com)</a></p><p><a href="https://data.telangana.gov.in/dataset/demographics">Demographics | Telangana Open Data Portal</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=613d174b58eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>