<?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 Charles Patel on Medium]]></title>
        <description><![CDATA[Stories by Charles Patel on Medium]]></description>
        <link>https://medium.com/@charlespatel?source=rss-37ed4475be1c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_LudQGh5G1TIIJhRdkGeEg.jpeg</url>
            <title>Stories by Charles Patel on Medium</title>
            <link>https://medium.com/@charlespatel?source=rss-37ed4475be1c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 12:15:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@charlespatel/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[Advance Python Development]]></title>
            <link>https://medium.com/@charlespatel/advance-python-development-5e6914fbb4b2?source=rss-37ed4475be1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e6914fbb4b2</guid>
            <category><![CDATA[ci-cd-pipeline]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[mypy]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[advanced-programming]]></category>
            <dc:creator><![CDATA[Charles Patel]]></dc:creator>
            <pubDate>Sat, 11 May 2024 23:24:46 GMT</pubDate>
            <atom:updated>2024-05-11T23:24:46.091Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Cvtxz9sA_shg6pP4" /><figcaption>Photo by <a href="https://unsplash.com/@synkevych?utm_source=medium&amp;utm_medium=referral">Roman Synkevych</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I’ll be very brief in this article assuming you already know the basics. This article only points out things needed to advance your skills in writing Python applications and repositories and assumes you are smart enough to google.</p><h4><strong>Setting up python projects</strong></h4><p>Learn how to configure your project using setup.py or pyproject.toml or using other open-source tools. Explore the popular Python package repo for reference.</p><p>Over the last few years, there have been many changes in setting up Python projects. To setup Python projects, you need to know about <strong><em>setup.py</em></strong> which is soon to be deprecated as per my best knowledge and will be replaced by <strong><em>pyprojects.toml</em></strong> file. <a href="https://python-poetry.org/">Poetry</a> is also an awesome tool for dependency management and maintaining Python packages.</p><h4>Python tooling</h4><p>There are tons of open-source Python code tooling available out there, these are some of the most popular among the Python community,</p><ul><li>mypy</li><li>ruff</li><li>black</li><li>flake8</li><li>isort</li><li>bandit</li></ul><p>Understand each for what it does and how to configure in your repository. These are very important for clean and mature code writing and avoiding inconsistency across your repo.</p><h4><strong>Typehints</strong></h4><p>It&#39;s a controversial topic whether to use type hints in Python. I <strong><em>strongly</em></strong> believe in type-hinting. It is good to have in your Python code as it will reduce testing code lines to a large extent if you use it in combination with static analysis tools like mypy or Pyre (Meta).</p><p>The basic type hints, list[str], set[Enum], dict[str, int], tuple[str]. The next step is to understand advanced typing i.e type inference and forwarding,</p><ul><li>Learn to use Sequence, Iterable, Generator, Mapping, MutableMapping, etc.</li><li>Learn <a href="https://typing.readthedocs.io/en/latest/spec/protocol.html">Protocol</a></li><li>Learn <a href="https://mypy.readthedocs.io/en/stable/generics.html#generics">generic types</a>, Generic, TypeVar, ClassVar, Type. See this useful video about <a href="https://www.youtube.com/watch?v=7Chd5gPHlDg">generics</a></li></ul><h4><strong>Understand PEP</strong></h4><p>PEP means “<strong><em>Python Enhancements Proposal</em></strong>”, The advanced python developer aka <em>Pythonista</em> writes PEP to advance the Python language. It includes new features and their usage.</p><p>Read the latest PEPs to be aware of the language changes, and understand how it is written, developed, reviewed, and deployed.</p><p>See most popular <a href="https://peps.python.org/pep-0008/">PEP8</a></p><h4><strong>Pypi server</strong></h4><p>It’s a simple server that stores Python packages and makes them available to download for your use. Learn how to build and upload packages to pypi server.</p><h4><strong>Configuration files</strong></h4><ul><li><strong>MANIFEST.in : </strong>To include static files or extra files besides common Python package files in the package source distribution. e.g. images, non-python files (.js, .config, etc.) See <a href="https://setuptools.pypa.io/en/latest/userguide/miscellaneous.html">Controlling files in the distribution</a></li><li><strong>requirements.txt: </strong>For defining the libraries needed to run the projects</li><li><strong>setup.py / setup.cfg / pyproject.toml</strong> : To set up the project and add other libraries configs</li></ul><h4>CICD</h4><p>Learn how we can leverage CICD to run the test, build, and upload packages to either the package registry or pypi server.</p><p>There are many other things, However, I think the above are needed to understand Python development from writing a single line to release. Moreover, I feel there should be an in-depth article on the above all points which I’ll try to cover soon.</p><p>In the next article, I’ll post best practices whilst code review in day-to-day work.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e6914fbb4b2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Relationship aka Correlation between features]]></title>
            <link>https://medium.datadriveninvestor.com/relationship-aka-correlation-between-features-e1fbc244694e?source=rss-37ed4475be1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1fbc244694e</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[correlation]]></category>
            <category><![CDATA[statistics]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Charles Patel]]></dc:creator>
            <pubDate>Sun, 30 Aug 2020 05:34:08 GMT</pubDate>
            <atom:updated>2020-09-01T19:11:49.950Z</atom:updated>
            <content:encoded><![CDATA[<h3>Relationship aka Correlation between Data</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*afk3Etz90JE4zot8Decx7A.jpeg" /></figure><p>In machine learning and data science, data exploration or data analysis is the primary step in building an effective model. While doing data analysis, first we need to understand the data and its purpose then understand the nature and distribution of data, whether the data is Gaussian (normal) distributed or distribution-free, then analyze or think about what statistical methods can be used to extract useful information which can be used building the model. This helps us in making a better model. Additionally, by doing so, we can filter undesired features and only allow those features that provide useful information to the model in turn reducing the model complexity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S38S5ljBxT78iEFUGoNtwg.png" /></figure><p>Once we figure out the basic property mentioned above for a feature, then we can apply statistical methods accordingly. It is comparatively easy to extract and understand the statistic of a single feature. But what if we want to understand the statistical relationship between two or more features?</p><p>To find a relationship between features, we need to calculate the correlation coefficient and perform some tests to extract valuable information. To apply such metrics and tests, we should determine the compatibility between such methods and the types of data. I’ll cover all these details in this post.</p><p>The features in our dataset can be dependent or independent of each other for various reasons. In order to find out to what extent they are related to each other, we often use <em>covariance</em> and <em>correlation</em> metrics or some statistical methods to quantify the relationship among them.</p><h3>Covariance</h3><p>The relationship between the two features can be found by calculating the covariance. Here’s the formula to calculate the covariance between two features:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/1*_Tt1odsqpxDsvOd3whidjA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vznQWyawAkDK8vLplR4jPw.png" /></figure><p>Covariance alone does not give away any useful insights. So, often to strengthen the covariance method, we generally use different correlation methods as described below.</p><h3>Correlation</h3><p>The relationship or correlation between features represent as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FwIIPy1lahGkKBADps5_cg.png" /></figure><p>In this blog, we will cover all popular correlation methods and try to understand when to use these methods on different types of data.</p><p>There are two primary types of correlation methods:</p><ul><li><strong>Parametric correlation</strong></li><li><strong>Non-parametric correlation</strong></li></ul><p>While finding a correlation between features, if features have gaussian distribution or linear relationship we apply <em>parametric correlation</em> methods whereas if the feature is distribution-free or the distribution is unknown, we apply <em>non-parametric correlation</em> methods. A heatmap will help us better visualize the correlation between all features.</p><figure><img alt="Heatmap" src="https://cdn-images-1.medium.com/max/1024/1*qLrAxd6y_eVpgxXURWRGAg.png" /><figcaption>Heatmap</figcaption></figure><blockquote>As a general rule of thumb, when the features are nominal (categorical) or ordinal, then a <strong>non-parametric</strong> test should be selected and when the features are continuous, then a <strong>parametric</strong> test should be selected.</blockquote><p><strong>Parametric correlation: </strong>Parametric tests assume that the features you are working with are <em>normally distributed</em> or follow a<em> bell curve</em>. The most frequent parametric test to examine for the strength of association between two features is</p><ul><li>Pearson correlation</li></ul><p><strong>Non-parametric correlation: </strong>Non-parametric tests are referred to as <em>distribution-free</em> tests because they do not follow any assumption in regards to the distribution of the data. Some of them are listed below:</p><ul><li>Spearman’s Rank Correlation</li><li>Kendall’s rank correlation coefficient</li><li>Goodman and Kruskal’s Rank Correlation</li><li>Somers’ Rank Correlation</li></ul><p><a href="https://www.datadriveninvestor.com/2020/05/09/the-disparity-correlation-vs-causation/">The disparity - Correlation vs Causation | Data Driven Investor</a></p><h4><strong>Pearson correlation</strong></h4><p>A Pearson correlation is used when assessing the relationship between two continuous variables. Pearson’s correlation coefficient is calculated as the covariance of the two variables divided by the product of the standard deviation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/378/0*aeGTDm9BTlw1N03C.png" /><figcaption>Pearson’s Correlation</figcaption></figure><p>The coefficient returns a value between -1 and 1 which shows a negative correlation to positive correlation respectively, A coefficient value of 0 means no correlation. The coefficient &lt; -0.5 and &gt; 0.5 represent significant correlation between features. A perfect positive correlation is +1 and a perfect negative correlation is -1.</p><p>Correlation between two features that has a normal distribution (Gaussian) can be calculated as the Pearson correlation. But the features that do not follow a normal distribution (categorical) we generally use rank correlation. Let’s understand the Rank Correlation.</p><p><strong>What is the Rank correlation?</strong></p><p>Rank correlation measures the ordinal relationship between features or the relationship between the rankings of different ordinal variables.</p><p>Rank correlation can be calculated as ranking 1 to the biggest number in a column, 2 to the second biggest value, and so on and for equal scores, the rank will be mean (average) rank. Rank correlation coefficients can then be calculated in order to represent the association between the two ranked variables.</p><p>Rank correlation methods are referred to as non-parametric correlation or distribution-free correlation. To know more about how to calculate ranks, take a look <a href="https://geographyfieldwork.com/SpearmansRank.htm">here</a>.</p><h4><strong>Spearman’s Correlation</strong></h4><p>It measures the strength and direction of the association between two ranked variables. It is used when the assumptions of the Pearson correlation is violated. The equation for Spearman’s Correlation is shown below,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/0*hWD9HITF8x4sP24X.gif" /><figcaption><strong>Spearman’s Correlation</strong></figcaption></figure><p>where D, is the difference between rank and n, i is the number of pairs</p><p>It calculates the Pearson’s correlation but uses ranks of the feature. Here’s the <a href="https://statistics.laerd.com/statistical-guides/spearmans-rank-order-correlation-statistical-guide.php">example</a> to calculate Spearman’s Correlation coefficient.</p><p><strong>Kendall’s Rank Correlation</strong></p><p>It calculates a normalized score for the number of matching or concordant rankings between the two features.</p><blockquote>Kendall’s Tau = C - D / C + D</blockquote><p>where C is the number of <a href="https://www.statisticshowto.com/concordant-pairs-discordant-pairs/">concordant pairs</a> and D is the number of discordant pairs.</p><p>The concordant pairs are the number of larger ranks below a certain rank and discordant pairs are the number of smaller ranks are below a certain rank.</p><p>Intuitively, the Kendall correlation between two features will be high when observations have a similar rank between the two features and low when observations have a dissimilar rank between the two features. Here’s the <a href="https://www.statisticshowto.com/kendalls-tau/">example</a> to calculate Kendall’s rank correlation coefficient.</p><p><strong>Goodman and Kruskal’s Rank Correlation</strong></p><p>It measures the strength and direction of association that exists between two ordinal features. It is based on <a href="https://statistics.laerd.com/spss-tutorials/goodman-and-kruskals-gamma-using-spss-statistics.php">two assumptions</a>, one being two features should be measured on an ordinal scale and the other one states that the features should be in a monotonic relationship</p><p>It is useful when there are many ties between features. A tie is when comparing two features both values are the same.</p><p>I have avoided the formula and its explanation for simplicity. You can check out the <a href="https://www.statisticshowto.com/gamma-coefficient-goodman-kruskal/">example</a> to see how to calculate Goodman and Kruskal’s Rank Correlation coefficient.</p><p><strong>Somers’ Rank Correlation</strong></p><p>It is a measure of association between an ordinal dependent variable and an ordinal independent variable. Somers’ Rank Correlation<strong> </strong>is based on the assumption that one feature will be ordinal dependent and others will be ordinal independent</p><p>Somers’ Rank Correlation is appropriate when you want to distinguish between a dependent and independent variable, Goodman and Kruskal’s gamma does not make any distinction between the two ordinal variables.</p><p><em>Wrapping up, we covered different correlation coefficient for parametric and non-parametric correlation between two features. In subsequent blog posts, I will cover testing methods such as t-test, ANOVA, chi-square test, etc. for more than two features. In the meantime, if you are interested in learning more about machine learning you can check out my other </em><a href="https://medium.com/@charlespatel/underneath-numpy-array-python-list-42a30e62f693"><em>blogs</em></a><em>. Thanks :)</em></p><h3>Reference</h3><ul><li><a href="https://statistics.laerd.com/">SPSS Statistics Tutorials and Statistical Guides | Laerd Statistics</a></li><li><a href="https://www.statisticshowto.com/">Statistics How To: Elementary Statistics for the rest of us!</a></li></ul><p><strong>Gain Access to Expert View — </strong><a href="https://datadriveninvestor.com/ddi-intel"><strong>Subscribe to DDI Intel</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1fbc244694e" width="1" height="1" alt=""><hr><p><a href="https://medium.datadriveninvestor.com/relationship-aka-correlation-between-features-e1fbc244694e">Relationship aka Correlation between features</a> was originally published in <a href="https://medium.datadriveninvestor.com/">DataDrivenInvestor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Underneath Numpy array & Python list]]></title>
            <link>https://medium.datadriveninvestor.com/underneath-numpy-array-python-list-42a30e62f693?source=rss-37ed4475be1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/42a30e62f693</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[numpy]]></category>
            <dc:creator><![CDATA[Charles Patel]]></dc:creator>
            <pubDate>Thu, 16 Jul 2020 17:01:26 GMT</pubDate>
            <atom:updated>2020-09-01T01:28:35.204Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mrDFie-EHuicw6r2.jpeg" /></figure><p>Ihave been working with Numpy for a long time and, I always noticed that Numpy arrays are faster than python lists in terms of speed and space executing tasks. I want to share my findings regarding the in-depth compilation and execution processes of these two data structures. Let’s dig in!</p><p>There are many <a href="https://towardsdatascience.com/how-fast-numpy-really-is-e9111df44347">tutorials</a>/blogs out there to compare Python list and Numpy array, you can refer to those. In this tutorial, we are going to explore their inner workings, how they work internally, their advantages and disadvantages, and some benchmarking to get a better idea.</p><p>Before we dive further into the performance of both data structures, First, we need to understand their internal working (the time where it is compiled into C) as both library and their data structures implemented in C.</p><p>When the python software is installed on your machine, minimally, it has an interpreter and support library. The interpreter is nothing but software that runs our Python scripts. Interestingly, it can be implemented in any programming language! <a href="https://en.wikipedia.org/wiki/CPython">CPython</a> is the default interpreter for python which is written in C programming language.</p><p>What’s all this fuss about CPython, <a href="https://en.wikipedia.org/wiki/Cython">Cython</a>, <a href="https://www.jython.org/">Jython</a>, <a href="https://ironpython.net/">IronPython</a>? I will cover all of your doubts. Please hold yourself till last and get ready for little advance terms used in python. Let us first understand all of these in detail.</p><p><strong>CPython</strong></p><ul><li>CPython is the reference implementation of the python programming language. Written in C and Python. Developed by the same author as of python, Guido van Rossum in 1994</li><li>CPython compiles your python code into bytecode (transparently) and interprets that bytecode, and executing it as it goes. Python uses CPython as the base</li></ul><p><strong>Cython</strong></p><ul><li>CPython does not translate your Python code to C by itself. Instead, it runs an interpreter loop, So, Here Cython comes in play to translate Python code to C.</li><li>Cython adds a few extensions to the python language and lets you compile your code to C extensions, code that plugs into the CPython interpreter.</li></ul><p><strong>Jython</strong>, <strong>IronPython</strong>, and <strong>PyPy</strong> are the current “other” implementations of the python programming language; these are implemented in Java, C#, and RPython (a subset of Python), respectively. Jython compiles your Python code to Java bytecode, so your Python code can run on the JVM. IronPython lets you run Python on the Microsoft CLR. And PyPy, being implemented in (a subset of) Python, lets you run Python code faster than CPython</p><p>Now let’s dive into the internal implementation of python lists and Numpy arrays</p><h3>List</h3><p>A list is a most versatile datatype available in python, everyone knows what list can do and why we use the list. So, for instance, a list can be used as a stack, as a container to store values of different data types and the list has some basic functions like append, extend, pop, remove, etc.</p><p>But the question is how it is implemented?</p><p>CPython’s lists are variable-length arrays, not <a href="http://www.gigamonkeys.com/book/they-called-it-lisp-for-a-reason-list-processing.html">Lisp-style</a> lists (uses linked lists). The CPython&#39;s implementation of the list uses a contiguous array of references to other objects and keeps a pointer to this array and the array’s length in a list head structure. This makes indexing a list a[i] an operation whose cost is independent of the size of the list or the value of the index.</p><p>When items are appended or inserted, the array of references is resized. Some cleverness is applied to improve the performance of appending items repeatedly; when the array must be grown, some extra space is allocated so the next few times don’t require an actual resize.</p><p>Below shown a structure of list implemented in C. don’t worry about this C code, This is just for your knowledge, So if someone asks how the list works behind the scene then you can at least say something about list object structure in C and also, I believe details are always better to know.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o49qrfH4jvRMMNymg5bIKw.png" /></figure><p>Now let’s take a look at how numpy works.</p><h3>Numpy</h3><p>In this section, we will see what happens under the hood when using the NumPy array.</p><p>A NumPy array is a multidimensional array of objects of the same type and is basically described by metadata (notably the number of dimensions, the shape, and the data type) and the actual data.</p><p>The data is stored in a homogeneous and contiguous block of memory, at a particular address in system memory (Random Access Memory, or RAM). This block of memory is called the <strong>data buffer</strong>.</p><p>This is the main difference between an array and a pure python structure, such as a list, where the items are scattered across the system memory. This aspect is the critical feature that makes NumPy arrays so efficient.</p><p>Here’s a quick look at how a numpy array objects look like in C. The structure PyArrayObject has four elements (*data, nd, *dimensions, *strides) that are needed in order to access the array&#39;s data from C code, please ignore other fields for brevity:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c36tnSrqbVo2QzArE2_TGg.png" /><figcaption>Numpy arrays structure in C</figcaption></figure><h3>Why NumPy efficient?</h3><ul><li>Computations on arrays written very efficiently in a low-level language such as C (and a large part of NumPy is actually written in C). Knowing the address of the memory block and the data type, it is just simple arithmetic to loop over all items, for example. There would be a significant overhead to do that in python with a list.</li><li>Spatial locality in memory access patterns results in performance gains notably due to the CPU cache. Indeed, the cache loads bytes in chunks from RAM to the CPU registers. Adjacent items are then loaded very efficiently (<a href="https://en.wikipedia.org/wiki/Locality_of_reference">locality of reference</a>)</li><li>Finally, the fact that items are stored contiguously in memory allows NumPy to take advantage of vectorized instructions of modern CPUs, such as Intel’s SSE and AVX, AMD’s XOP, and so on. For example, multiple consecutive floating-point numbers can be loaded in 128, 256, or 512 bits registers for vectorized arithmetical computations implemented as CPU instructions.</li><li>Additionally, NumPy can be linked-to highly optimized linear algebra libraries such as <a href="http://www.netlib.org/blas/">BLAS</a> and <a href="http://www.netlib.org/lapack/">LAPACK</a> through <a href="http://math-atlas.sourceforge.net/faq.html#doc">ATLAS</a> or the Intel Math Kernel Library (MKL). A few specific matrix computations may also be multithreaded, taking advantage of the power of modern multicore processors.</li></ul><p>However, numpy has some disadvantages too. It must require continuous memory allocation. Insertion and deletion operations become costly as data is stored in contiguous memory locations</p><p>In conclusion, storing data in a contiguous block of memory ensures that the architecture of modern CPUs is used optimally, in terms of memory access patterns, CPU cache, and vectorized instructions which is not possible in a python list.</p><p>Python’s list support efficient insertion, deletion, appending, and concatenation, and Python’s list comprehensions make them easy to construct and manipulate.</p><p>However, python’s list has certain limitations:</p><ul><li>Python’s list does not support “vectorized” operations, like element-wise addition and multiplication</li><li>Python’s list can contain objects of differing types mean that python must store type information for every element and must execute type-dispatching code when operating on each element. which leads to checking types at each iteration.</li></ul><p>So, we came to know why numpy so fast and efficient as compared to python’s list. Now, let’s compare its performance at a very high level.</p><h3>Performance Comparison</h3><p>In this section, we are going to benchmark python’s list and Numpy array in terms of memory consumption and time taken to execute. First import, necessary modules, and compare the performance of array/list for different sizes. In this example, we are going to take the ratio between the results from time/memory taken by python list and numpy array and see which performs better. Also, the ratio will be taken as a <em>python list: numpy_array</em>, so the results show how much times numpy is better than the python list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a7OK49mbn3EcwXRCQZKNhQ.png" /></figure><p><strong>Memory Consumption</strong></p><p>Here’s the quick example to show memory consumption to store different sizes of arrays.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xT2lHPN-wlaYh9b1WcePhA.png" /></figure><p>Let’s plot this as a graph to visualize better by taking the ratio between two.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*_zfFKdrsaNh2-GHxf8XlQQ.png" /></figure><p>The above graph shows numpy outperforms for all different sizes. this is because of python stores type information for each element which certainly requires more space unlike numpy, which has all the elements of the same type.</p><p><strong>Time Comparison</strong></p><p>The below example is about computing squares of every element of list/array and measures the time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bKxpaC5_Opt95kFrlTMKZA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*165Vmc5PWJal2kb-uVvBng.png" /></figure><p>In the above graph, for size=1 the python list performs better and in all the other cases numpy is performs better. The reason is, although python list is fast (both are using C as backend), its just an overhead of checking dtype for each element. In numpy, with type checking it also has to check other header information that we see in its C structure i.e dimension, stride, etc. whereas in python only dtype is enough.</p><p>Second Example, a <strong>vectorized operation</strong>, this example adds the elements of two array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e1-5lJQo50UqPHWONSedPA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*O9npNvssJJsWM79gzSyy5A.png" /></figure><p>In the above graph, for all sizes, numpy is an out-performer, because of its ability to do operations element-wise, and due to highly optimized linear algebra libraries, it can also take advantage of computing within multiple threads. But there are a lot of factors at play here, including the underlying library used (<a href="http://www.netlib.org/blas/">BLAS</a>/<a href="http://www.netlib.org/lapack/">LAPACK</a>/<a href="http://math-atlas.sourceforge.net/faq.html#doc">Atlas</a>)</p><p>Finally, I hope I shed some light on the inner working of python’s dynamically allocated list and NumPy&#39;s array.</p><p>Thanks for reading. Peace.</p><h3>References</h3><ul><li><a href="https://ipython-books.github.io/">https://ipython-books.github.io/</a></li><li><a href="https://www.scipy.org/">https://www.scipy.org/</a></li><li><a href="https://stackoverflow.com/questions/3917574/how-is-pythons-list-implemented">https://stackoverflow.com/questions/3917574/how-is-pythons-list-implemented</a></li><li><a href="http://folk.uio.no/inf3330/scripting/doc/python/NumPy/Numeric/numpy-13.html">http://folk.uio.no/inf3330/scripting/doc/python/NumPy/Numeric/numpy-13.html</a></li><li><a href="https://stackoverflow.com/questions/17130975/python-vs-cpython">https://stackoverflow.com/questions/17130975/python-vs-cpython</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42a30e62f693" width="1" height="1" alt=""><hr><p><a href="https://medium.datadriveninvestor.com/underneath-numpy-array-python-list-42a30e62f693">Underneath Numpy array &amp; Python list</a> was originally published in <a href="https://medium.datadriveninvestor.com/">DataDrivenInvestor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>