<?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 Sujay Goswami on Medium]]></title>
        <description><![CDATA[Stories by Sujay Goswami on Medium]]></description>
        <link>https://medium.com/@sg23600?source=rss-5349b5e9a460------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ec51XoSM9VwhMA2l.jpg</url>
            <title>Stories by Sujay Goswami on Medium</title>
            <link>https://medium.com/@sg23600?source=rss-5349b5e9a460------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 13:28:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sg23600/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[Data Visualisation techniques commonly used in Machine Learning (Python)]]></title>
            <link>https://medium.com/gen-y/data-visualisation-techniques-commonly-used-in-machine-learning-python-27e58bb20e23?source=rss-5349b5e9a460------2</link>
            <guid isPermaLink="false">https://medium.com/p/27e58bb20e23</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Sujay Goswami]]></dc:creator>
            <pubDate>Sun, 26 Apr 2020 19:56:02 GMT</pubDate>
            <atom:updated>2020-05-01T08:29:17.813Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>Machine Learning, being a subset of AI gives us a wide range of possibilities. Possibilities which allow us to get close to accurate predictions, which are achieved by massive amounts of data. The visualisation of these huge datasets allows us to get an understanding of the statistics behind it. Hence, allowing us to use suitable models.</em></blockquote><p><em>Following are a few visualisation libraries —</em></p><ol><li>matplotlib (pyplot)</li><li>seaborn</li><li>ggplot</li></ol><p>S<strong>catter plot</strong></p><p>Simple cartesian points plotted with two variables on perpendicular axes.</p><pre>import matplotlib.pyplot as plt#random data used to plot scatter points<br>rent = [5, 5.6, 5.8, 7, 7.2, 8.5, 9.5, 13, 15.6]<br>area = [320, 410, 555, 654, 700, 920, 1020, 1090, 1500]<br>fig=plt.figure()<br>ax=fig.add_axes([0,0,1,1])<br>ax.scatter(area, rent, color=&#39;r&#39;)<br>ax.set_xlabel(&#39;AREA&#39;)<br>ax.set_ylabel(&#39;RENT&#39;)<br>ax.set_title(&#39;scatter plot&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/30/0*7FkOYKBpONNGhpKg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*-rQMcoAvYpjsUzzf.png" /></figure><p>H<strong>istograms</strong></p><p>These are bars plotted for frequenies of continuous data points.</p><pre>import numpy as np<br>import matplotlib.mlab as mlab<br>import matplotlib.pyplot as pltx =[1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3,4,4,4,5,5,5,6,6,6,8,8,8,9,9,9,9]<br>num_bins =9<br>n, bins, patches = plt.hist(x, num_bins, facecolor=&#39;blue&#39;, alpha=0.5)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/362/0*ATWcuWQTjmAvbPoK.png" /></figure><p>B<strong>ar plots</strong></p><p>These are for plotting frequencies of categorical data. We can draw many conclusions from this graph(such as number of modes, right/left skewed and amount of outliers).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/0*PKMvcmy2aL4NNWbv.png" /></figure><pre>import matplotlib.pyplot as plt<br>fig = plt.figure()<br>ax = fig.add_axes([0,0,1,1])<br>langs = [&#39;C&#39;, &#39;C++&#39;, &#39;Java&#39;, &#39;Python&#39;, &#39;PHP&#39;]<br>students = [23,17,35,29,12]<br>ax.bar(langs,students)<br>plt.show()</pre><p>B<strong>oxplots</strong></p><p>We can get the representation of median, first and third quartiles, maximum — minimum and outliers all in one single boxplot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/415/0*4ADN0OaevuNh9T34.png" /></figure><pre>import pandas as pd<br>import numpy as np<br>df = pd.DataFrame(np.random.rand(10, 5), columns=[&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;])<br>df.plot.box(grid=&#39;False&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/0*ac4HDgCEtAjboYCe.png" /></figure><p>V<strong>iolin Plots</strong></p><p>It is similar to box plots with an addition of added rotated kernel density plots placed along the sides.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/925/0*UoC5gERU_lwuug01.png" /></figure><p>F<strong>acet Grid</strong></p><p><strong>FacetGrid</strong> object takes a dataframe as input and the names of the variables that will form the row, column, or hue dimensions of the grid.</p><pre>import pandas as pd<br>import seaborn as sb<br>from matplotlib import pyplot as plt<br>df = sb.load_dataset(&#39;tips&#39;)<br>g = sb.FacetGrid(df, col = &quot;time&quot;)<br>g.map(plt.hist, &quot;tip&quot;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*9XuWh7Bu54olzNkE.jpg" /></figure><p>P<strong>air Grid</strong></p><p>We can draw a bunch of subplots for better visualisation. It forms a matrix of subplots</p><pre>import pandas as pd<br>import seaborn as sb<br>from matplotlib import pyplot as plt<br>df = sb.load_dataset(&#39;iris&#39;)<br>g = sb.PairGrid(df)<br>g.map(plt.scatter);<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*jUtnAQtER2gG0UPZ.jpg" /></figure><h4><strong>Multidimensional data with ggplot2</strong></h4><p>We can plot multi-dimensional data, when we have multiple classes which are represented with the help of different colours.</p><pre>(ggplot(mpg) + aes(x=&#39;displ&#39;, y=&#39;hwy&#39;, color=&#39;class&#39;) + geom_point() + labs(title=&#39;Engine Displacement vs. Highway Miles per Gallon&#39;, x=&#39;Engine Displacement, in Litres&#39;, y=&#39;Highway Miles per Gallon&#39;))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/0*1LES9wVzOpK4c2gS.png" /></figure><p>M<strong>arginal Plots</strong></p><p>These are a detailed display of relation between two variables. The central chart shows the correlation. It can be a scatterplot, hexplot, 2D histogram or 2D density plot. The graphs on the top and right sides represent the distributions of the variables.</p><pre><strong>import</strong> seaborn as sns<br>df <strong>=</strong> sns.load_dataset(&#39;iris&#39;)</pre><pre># Custom the inside plot: options are: “scatter” | “reg” | “resid” | “kde” | “hex”</pre><pre>sns.jointplot(x<strong>=</strong>df[&quot;sepal_length&quot;],y<strong>=</strong>df[&quot;sepal_width&quot;],kind<strong>=</strong>&#39;scatter&#39;)</pre><pre>sns.jointplot(x<strong>=</strong>df[&quot;sepal_length&quot;], y<strong>=</strong>df[&quot;sepal_width&quot;], kind<strong>=</strong>&#39;hex&#39;)</pre><pre>sns.jointplot(x<strong>=</strong>df[&quot;sepal_length&quot;], y<strong>=</strong>df[&quot;sepal_width&quot;], kind<strong>=</strong>&#39;kde&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*2FIqTfAfbMCtYp_L.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*0hbYgR77UHldW1He.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*PjB357QR6dQvJCJm.png" /></figure><p>C<strong>ontour Plot</strong></p><p>It plots a 2D graph of the kernel density estimate (which gives the probability density function) from 2 variables.</p><pre># library &amp; dataset<br><strong>import</strong> seaborn as sns<br>df <strong>=</strong> sns.load_dataset(&#39;iris&#39;)</pre><pre># Basic 2D density plotsns.set_style(&quot;white&quot;)<br>sns.kdeplot(df.sepal_width, df.sepal_length)#sns.plt.show()</pre><pre># Custom it with the same argument as 1D density plotsns.kdeplot(df.sepal_width, df.sepal_length, cmap<strong>=</strong>&quot;Reds&quot;, shade<strong>=</strong>True, bw<strong>=</strong>.15)</pre><pre># Some features are characteristic of 2D: color palette and wether or not color the lowest range<br>sns.kdeplot(df.sepal_width, df.sepal_length, cmap<strong>=</strong>&quot;Blues&quot;, shade<strong>=</strong>True, shade_lowest<strong>=</strong>True, )<br>sns.plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*V2pe1WU4TXmnOBAW.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*QC7KF-elbD8i41rC.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*e3lgjnvPCzB_fFXF.png" /></figure><p>R<strong>egplots</strong></p><p>We can establish a regressional relationship with the help of regplots.</p><pre>import seaborn as sns<br>sns.set_style(&#39;whitegrid&#39;)sns.lmplot(x <strong>=</strong>&#39;total_bill&#39;, y <strong>=</strong>&#39;tip&#39;, data <strong>=</strong> dataset,hue <strong>=</strong>&#39;sex&#39;, markers <strong>=</strong>[&#39;o&#39;, &#39;v&#39;])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/0*-Dfm5FnV-lzC-7tE.png" /></figure><p><em>These are a few interesting data visualisation techniques used in machine learning using python. There are many more graphs which help us understand what data we are dealing with.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=27e58bb20e23" width="1" height="1" alt=""><hr><p><a href="https://medium.com/gen-y/data-visualisation-techniques-commonly-used-in-machine-learning-python-27e58bb20e23">Data Visualisation techniques commonly used in Machine Learning (Python)</a> was originally published in <a href="https://medium.com/gen-y">Gen-Y</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[NumPy Library]]></title>
            <link>https://medium.com/gen-y/numpy-library-240865c74bda?source=rss-5349b5e9a460------2</link>
            <guid isPermaLink="false">https://medium.com/p/240865c74bda</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[numpy]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Sujay Goswami]]></dc:creator>
            <pubDate>Sat, 03 Aug 2019 09:10:58 GMT</pubDate>
            <atom:updated>2019-08-03T09:10:58.269Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>NumPy Library</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/290/1*fLuLfz7HPOZxb-2qTWP2zg.png" /></figure><p>NumPy is used for numerical and scientific calculation in Python(programming language). High performance array objects are provided by this library. It is a very important library on which almost every data science or machine learning Python packages such as SciPy (Scientific Python), Mat−plotlib (plotting library), Scikit-learn, etc depends on to a reasonable extent.</p><p>In NumPy, dimensions are called axes. The number of axes is called rank. NumPy’s array class is called <em>ndarray. </em>The representation of an array’s dimensions is cal</p><p>led its <em>shape.</em></p><p>Examples:</p><p>[1][2]<br>[2][3]<br>[5][6]</p><p>Here the shape is (3,2). Code snippet-</p><pre>import numpy as np<br><br>arr = np.array([[1,2],<br>               [2,3],<br>                [5,6]])<br><br>print(&quot;array is of type &quot;, type(arr))<br>print(&quot;no. of dimensions &quot;, arr.ndim)<br>print(&quot;shape of array &quot;, arr.shape)<br>print(&quot;size of array &quot;, arr.size)<br>print(&quot;array stores elements of type &quot;, arr.dtype)</pre><pre>Output-<br>array is of type &lt;class ‘numpy.ndarray’&gt;<br>no. of dimensions 2<br>shape of array (3, 2)<br>size of array 6<br>array stores elements of type int32</pre><pre>Process finished with exit code 0</pre><p><strong>Creating Arrays</strong></p><p>There are several ways to make a multi-dimensional array:</p><ol><li>Using Arange Function : We can create a one-dimensional array with the help of arange function(np.arange) such that the elements of it are are stored as whole numbers till the value we want. Code snippet-</li></ol><pre>import numpy as np<br>array = np.arange(20)<br>print(array)</pre><pre>Output-<br>[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]</pre><pre>Process finished with exit code 0</pre><p>The shape is mentioned within brackets of function mentioned above.</p><p>2. Zero/One Matrices: These matrices are initially containing only one element in all entries. Sometimes, when the size of the array is known but it’s elements are unknown we use the functions np.zeros and np.ones resptectively. Code snippet-</p><pre>import numpy as np<br>array0 = np.zeros(2)<br>arr0 = np.zeros((2, 2))<br><br>array1 = np.ones(2)<br>arr1 = np.ones((2, 2))<br><br><br>print(array0)<br>print(arr0)<br>print (array1)<br>print (arr1)</pre><pre>Output-<br>[0. 0.]<br>[[0. 0.]<br> [0. 0.]]<br>[1. 1.]<br>[[1. 1.]<br> [1. 1.]]</pre><pre>Process finished with exit code 0</pre><p>As we can see, the shape is specified within brackets of the respective functions mentioned above.</p><p>3. Empty function array</p><p>An array can also be initialised with random values with the help of np.empty. Code snippet-</p><pre>import numpy as np<br>array0 = np.empty(2)<br>arr0 = np.empty((2, 2))<br><br>print(array0)<br>print(arr0)</pre><pre>Output-<br>[2.66032208e-048 1.21828703e-304]<br>[[1.25972494e-311 1.25972494e-311]<br> [1.25972494e-311 1.25972494e-311]]</pre><pre>Process finished with exit code 0</pre><p>The random elements are actually generated depending on the state of the memory.</p><p>4. Linspace</p><p>This function allows us to enter values at equal intervals. It requires starting and ending points with the number of intervals in between. For example: start=0, end=100, intervals= 5. The array would come out to be as[0, 25, 50, 75, 100]. Code snippet-</p><pre>import numpy as np<br><br>array = np.linspace(0, 100, num=5)<br>print(array)</pre><pre>Output-<br>[  0.  25.  50.  75. 100.]</pre><pre>Process finished with exit code 0</pre><p><strong>Reshape Function</strong></p><p>This is used to covert a one dimension array into multidimensional arrays. The new array’s dimensions are factors of the number of elements from the 1D array (and when they are multiplied we get this number). Code snippet-</p><pre>import numpy as np<br><br>array = np.arange(20)<br>array = array.reshape(5, 4)<br>print(array)</pre><pre>Output-<br>[[ 0  1  2  3]<br> [ 4  5  6  7]<br> [ 8  9 10 11]<br> [12 13 14 15]<br> [16 17 18 19]]</pre><pre>Process finished with exit code 0</pre><p><strong>Flatten function-</strong></p><p>This function converts a multidimensional array into a one dimensional array. Code snippet-</p><pre>import numpy as np<br><br>x = np.array([[1, 2], [3, 4]])<br>x = x.flatten()<br>print(x)</pre><pre>Output-<br>[1 2 3 4]</pre><pre>Process finished with exit code 0</pre><p><strong>Array Indexing</strong></p><p>(1)<em>Slicing</em>- It is used to chop an array, specifying a start(included) point and an ending(excluded) point. Code snippet-</p><pre>import numpy as np<br><br>x = np.array([[1, 2, 3, 4, 5, 6],<br>             [7, 8, 9, 10, 11, 12]])<br>print(x[1:4])<br>print(x[1:])<br>print(x[:4])<br>print(x[:-1])<br>print(x[:2, ::2])</pre><pre>Output-<br>[[ 7  8  9 10 11 12]]<br>[[ 7  8  9 10 11 12]]<br>[[ 1  2  3  4  5  6]<br> [ 7  8  9 10 11 12]]<br>[[1 2 3 4 5 6]]<br>[[ 1  3  5]<br> [ 7  9 11]] #alternate 2</pre><pre>Process finished with exit code 0</pre><p>(2)<em>Integer array indexing- </em>In this, specific elements of arrays are displayed by mapping the required dimension. (one one mapping). Code snippet-</p><pre>import numpy as np<br><br>x = np.array([[1, 2, 3, 4, 5, 6],<br>             [7, 8, 9, 10, 11, 12]])<br><br>print(x[[1, 0], [2, 3]])</pre><pre>Output-<br>[9 4]</pre><pre>Process finished with exit code 0</pre><p><strong>Basic Operations</strong></p><p><em>Single array operations</em>: We can perform any arithmetic operation on an array and it will apply the same on all individual elements of it. Code snippet-</p><pre>import numpy as np<br><br>x = np.array([[1, 2, 3, 4, 5, 6],<br>             [7, 8, 9, 10, 11, 12]])<br><br>print(x+2, x-2, x*2, x**2)</pre><pre>Output-<br>[[ 3  4  5  6  7  8]<br> [ 9 10 11 12 13 14]] [[-1  0  1  2  3  4]<br> [ 5  6  7  8  9 10]] [[ 2  4  6  8 10 12]<br> [14 16 18 20 22 24]] [[  1   4   9  16  25  36]<br> [ 49  64  81 100 121 144]]</pre><pre>Process finished with exit code 0</pre><p><em>Unary Operators: </em>These are used when only the array is the only oppernd. Many unary operations are provided as a method of <strong>ndarray</strong> class. Some functions are min, max, sum… Code snippet-</p><pre>import numpy as np<br><br>x = np.array([[1, 2, 3, 4, 5, 6],<br>             [7, 8, 9, 10, 11, 12]])<br><br>print(&quot;max=&quot;, x.max(), &quot;row-wise max=&quot;, x.max(axis=1), &quot;columnwise max=&quot;, x.max(axis=0))<br>print(&quot;min=&quot;, x.min(), &quot;row-wise min=&quot;, x.min(axis=1), &quot;columnwise min=&quot;, x.min(axis=0))<br><br>print(&quot;sum=&quot;, x.sum(), &quot;row-wise sum=&quot;, x.sum(axis=1), &quot;columnwise sum=&quot;, x.sum(axis=0))</pre><pre>Output-<br>max= 12 row-wise max= [ 6 12] columnwise max= [ 7  8  9 10 11 12]<br>min= 1 row-wise min= [1 7] columnwise min= [1 2 3 4 5 6]<br>sum= 78 row-wise sum= [21 57] columnwise sum= [ 8 10 12 14 16 18]</pre><pre>Process finished with exit code 0</pre><p><em>Binary Operations- </em>These are used when two operands are present. Like sum, product of array and matrices. Code snippet-</p><pre>import numpy as np<br><br>x = np.array([[1, 0],<br>              [0, 1]])<br>y = np.array([[2, 1],<br>              [4, 1]])<br><br>print(&quot;x+y=\n&quot;, x+y)<br>print(&quot;x-y=\n&quot;, x-y)<br>print(&quot;x*y=\n&quot;, x*y)<br>print(&quot;x.y=\n&quot;, x.dot(y))</pre><pre>Output-<br>x+y=<br> [[3 1]<br> [4 2]]<br>x-y=<br> [[-1 -1]<br> [-4  0]]<br>x*y=<br> [[2 0]<br> [0 1]]<br>x.y=<br> [[2 1]<br> [4 1]]</pre><pre>Process finished with exit code 0</pre><p><strong>Universal functions:</strong></p><p>These are provided for some more mathematical functions. Trigonometric, rithmetic, compelex, statistical functions are covered by this. Numpy provides various such functions.</p><p><em>Trigonometric- </em>sin(sine), cos(cosine), tan(tangent), arcsin(sin inverse), arccos(cos inverse), arctan(tan inverse). hypot(hypotenuse of triangle), sinh(hyperbolic sin), cosh(hyperbolic cos), tanh(hyperbolic tan), deg2rad and rad2deg (for conversions from degrees to radians and vice versa). Code snippet-</p><pre>import numpy as np<br>#array of angles<br>ang = np.array([0, 30, 45, 60, 90])<br>val = np.array([0, 1])<br>print(&#39;Radian angle array=&#39;, np.deg2rad(ang))<br>print(&#39;sin of angle array=&#39;, np.sin(ang))<br>print(&#39;sin inverse array=&#39;, np.rad2deg(np.arcsin(val)))<br>print(&#39;hypotenuse of right triangle is &#39;, np.hypot(3, 4))</pre><pre>Output-<br>Radian angle array= [0.         0.52359878 0.78539816 1.04719755 1.57079633]<br>sin of angle array= [ 0.         -0.98803162  0.85090352 -0.30481062  0.89399666]<br>sin inverse array= [ 0. 90.]<br>hypotenuse of right triangle is  5.0</pre><pre>Process finished with exit code 0</pre><p><em>Statistical- </em>There are functions used to calculate mean, median, variance, etc…</p><pre><br>import numpy as np<br><br># construct a weight array <br>weight = np.array([50.7, 52.5, 50, 58, 55.63, 73.25, 49.5, 45])<br><br># minimum and maximum  <br>print(&#39;Minimum and maximum weight of the students: &#39;)<br>print(np.amin(weight), np.amax(weight))<br><br># range of weight i.e. max weight-min weight <br>print(&#39;Range of the weight of the students: &#39;)<br>print(np.ptp(weight))<br><br># percentile <br>print(&#39;Weight below which 70 % student fall: &#39;)<br>print(np.percentile(weight, 70))<br><br># mean  <br>print(&#39;Mean weight of the students: &#39;)<br>print(np.mean(weight))<br><br># median  <br>print(&#39;Median weight of the students: &#39;)<br>print(np.median(weight))<br><br># standard deviation  <br>print(&#39;Standard deviation of weight of the students: &#39;)<br>print(np.std(weight))<br><br># variance  <br>print(&#39;Variance of weight of the students: &#39;)<br>print(np.var(weight))<br><br># average  <br>print(&#39;Average weight of the students: &#39;)<br>print(np.average(weight))</pre><pre>Output-<br>Minimum and maximum weight of the students: <br>45.0 73.25<br>Range of the weight of the students: <br>28.25<br>Weight below which 70 % student fall: <br>55.317<br>Mean weight of the students: <br>54.3225<br>Median weight of the students: <br>51.6<br>Standard deviation of weight of the students: <br>8.052773978574091<br>Variance of weight of the students: <br>64.84716875<br>Average weight of the students: <br>54.3225</pre><pre>Process finished with exit code 0</pre><p><em>Bitwise functions — </em>These accept integer values as input and perform bitwise operations on binary representation of those elements.</p><pre>import numpy as np<br><br>even = np.array([0, 2, 4, 6, 8, 16, 32])<br>odd = np.array([1, 3, 5, 7, 9, 17, 33])<br><br># bitwise_and<br>print(&#39;bitwise_and of two arrays: &#39;)<br>print(np.bitwise_and(even, odd))<br><br># bitwise_or<br>print(&#39;bitwise_or of two arrays: &#39;)<br>print(np.bitwise_or(even, odd))<br><br># bitwise_xor<br>print(&#39;bitwise_xor of two arrays: &#39;)<br>print(np.bitwise_xor(even, odd))<br><br># invert or not<br>print(&#39;inversion of even no. array: &#39;)<br>print(np.invert(even))<br><br># left_shift<br>print(&#39;left_shift of even no. array: &#39;)<br>print(np.left_shift(even, 1))<br><br># right_shift<br>print(&#39;right_shift of even no. array: &#39;)<br>print(np.right_shift(even, 1))</pre><pre>Output-<br>bitwise_and of two arrays: <br>[ 0  2  4  6  8 16 32]<br>bitwise_or of two arrays: <br>[ 1  3  5  7  9 17 33]<br>bitwise_xor of two arrays: <br>[1 1 1 1 1 1 1]<br>inversion of even no. array: <br>[ -1  -3  -5  -7  -9 -17 -33]<br>left_shift of even no. array: <br>[ 0  4  8 12 16 32 64]<br>right_shift of even no. array: <br>[ 0  1  2  3  4  8 16]</pre><pre>Process finished with exit code 0</pre><p><strong>Broadcasting</strong></p><p>Usually operations between two arrays take place when both have the same shape. But this constraint can be overcome with the help of broadcasting or treatment of arrays with two different sizes. <strong>Rule- </strong>The size of the trailing axes for both arrays in operation must be either the same size or one of them must be one.</p><pre>import numpy as np<br><br>a = np.array([1.0, 2.0, 3.0])<br><br>b = 3<br>print(a * b)<br>c = [3, 3, 3]<br>print(a * c)<br><br>a1 = np.array([0.0, 10.0, 20.0, 30.0])<br>b1 = np.array([0.0, 1.0, 2.0])<br><br>print(a1[:, np.newaxis] + b1)</pre><pre>Output-<br>[3. 6. 9.]<br>[3. 6. 9.]<br>[[ 0.  1.  2.]<br> [10. 11. 12.]<br> [20. 21. 22.]<br> [30. 31. 32.]]</pre><pre>Process finished with exit code 0</pre><p>In the above example, a*b is a much better process as its faster because numpy doesn’t create copies of the single ineteger value 3. Hence, saving memory. (np.newaxis converts array into a row/column vector)</p><p><strong>Date/Time</strong></p><p>Numpy can input array data in the date time form. The data type is called ‘datetime64’. We can use arange function which does the exact same sorting with date entries. Even arithmetic operations are possible. Code snippet —</p><pre>import numpy as np<br><br># creating a date<br>today = np.datetime64(&#39;2019-08-03&#39;)<br>print(&quot;Date is:&quot;, today)<br>print(&quot;Year is:&quot;, np.datetime64(today, &#39;Y&#39;))<br><br># creating array of dates in a month<br>dates = np.arange(&#39;2019-08&#39;, &#39;2019-09&#39;, dtype=&#39;datetime64[D]&#39;)<br>print(&quot;\nDates of August, 2019:\n&quot;, dates)<br>print(&quot;Today is August:&quot;, today in dates)<br><br># arithmetic operation on dates<br>dur = np.datetime64(&#39;2017-05-22&#39;) - np.datetime64(&#39;2016-05-22&#39;)<br>print(&quot;\nNo. of days:&quot;, dur)<br>print(&quot;No. of weeks:&quot;, np.timedelta64(dur, &#39;W&#39;))</pre><pre>Output-<br> [&#39;2019-08-01&#39; &#39;2019-08-02&#39; &#39;2019-08-03&#39; &#39;2019-08-04&#39; &#39;2019-08-05&#39;<br> &#39;2019-08-06&#39; &#39;2019-08-07&#39; &#39;2019-08-08&#39; &#39;2019-08-09&#39; &#39;2019-08-10&#39;<br> &#39;2019-08-11&#39; &#39;2019-08-12&#39; &#39;2019-08-13&#39; &#39;2019-08-14&#39; &#39;2019-08-15&#39;<br> &#39;2019-08-16&#39; &#39;2019-08-17&#39; &#39;2019-08-18&#39; &#39;2019-08-19&#39; &#39;2019-08-20&#39;<br> &#39;2019-08-21&#39; &#39;2019-08-22&#39; &#39;2019-08-23&#39; &#39;2019-08-24&#39; &#39;2019-08-25&#39;<br> &#39;2019-08-26&#39; &#39;2019-08-27&#39; &#39;2019-08-28&#39; &#39;2019-08-29&#39; &#39;2019-08-30&#39;<br> &#39;2019-08-31&#39;]<br>Today is August: True</pre><pre>No. of days: 365 days<br>No. of weeks: 52 weeks</pre><pre>Dates in sorted order: [&#39;2016-10-13&#39; &#39;2017-02-12&#39; &#39;2019-05-22&#39;]</pre><pre>Process finished with exit code 0</pre><p><strong>Linear Algebra</strong></p><p>Numpy provides several functions of linear algebra. We can find rank, determinant, trace etc of an array; eigenvalues of matrices; matrix product and exponentation and solve linear/tensor equation, etc…Code snippet-</p><pre>import numpy as np<br><br>A = np.array([[6, 1, 1],<br>              [4, -2, 5],<br>              [2, 8, 7]])<br><br>print(&quot;Rank of A:&quot;, np.linalg.matrix_rank(A))<br><br>print(&quot;\nTrace of A:&quot;, np.trace(A))<br><br>print(&quot;\nDeterminant of A:&quot;, np.linalg.det(A))<br><br>print(&quot;\nInverse of A:\n&quot;, np.linalg.inv(A))<br><br>print(&quot;\nMatrix A raised to power 2:\n&quot;, np.linalg.matrix_power(A, 2))</pre><pre>Output-<br>Rank of A: 3</pre><pre>Trace of A: 11</pre><pre>Determinant of A: -306.0</pre><pre>Inverse of A:<br> [[ 0.17647059 -0.00326797 -0.02287582]<br> [ 0.05882353 -0.13071895  0.08496732]<br> [-0.11764706  0.1503268   0.05228758]]</pre><pre>Matrix A raised to power 2:<br> [[42 12 18]<br> [26 48 29]<br> [58 42 91]]</pre><pre>Process finished with exit code 0</pre><p>Equation solving-</p><pre>import numpy as np<br><br># coefficients<br>a = np.array([[1, 2], [3, 4]])<br># constants<br>b = np.array([8, 18])<br><br>print(&quot;Solution of linear equations:&quot;, np.linalg.solve(a, b))</pre><pre>Output-<br>Solution of linear equations: [2. 3.]</pre><pre>Process finished with exit code 0</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=240865c74bda" width="1" height="1" alt=""><hr><p><a href="https://medium.com/gen-y/numpy-library-240865c74bda">NumPy Library</a> was originally published in <a href="https://medium.com/gen-y">Gen-Y</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>