Jekyll2022-01-27T13:54:41+00:00https://shreyateeza.github.io/feed.xmlShreya PrasadMy Portfolio and Blog
shreyateezaNumpy Notes2019-01-07T00:00:00+00:002019-01-07T00:00:00+00:00https://shreyateeza.github.io/2019/01/07/Numpy_Notes<p><strong>What is NumPy?</strong><br />
According to Wikipedia -<br />
NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.</p>
<h2><img src="https://thepracticaldev.s3.amazonaws.com/i/bhpsnl4f6zv9dz8ejicf.png" alt="" /></h2>
<p><strong>Install NumPy</strong>
a) If you already have Anaconda installed in your PC, then NumPy might be pre-installed in it or simply install it by running -<br />
<code class="language-plaintext highlighter-rouge">conda install numpy</code><br />
b) If you have pip installed in your PC, then run -<br />
<code class="language-plaintext highlighter-rouge">python -m pip install — user numpy</code></p>
<hr />
<p><strong>Import the Library</strong><br />
<code class="language-plaintext highlighter-rouge">import numpy as np</code><br />
You can also give other name instead of np.</p>
<hr />
<p><strong>Create Arrays and change elements of array</strong><br />
a) 1D Array<br />
<code class="language-plaintext highlighter-rouge">arr1 = np.array([1,2,3,4,5,6])</code><br />
b) Higher Dimension Array<br />
<code class="language-plaintext highlighter-rouge">arr2 = np.array([[1,2,3],[4,5,6]])</code><br />
c) Change an element of Array<br />
<code class="language-plaintext highlighter-rouge">arr1[1] = 22</code> # [1,22,3,4,5,6]<br />
<code class="language-plaintext highlighter-rouge">arr2[1][1] = 56</code> # [[1,2,3],[4,56,6]]<br />
d) Create a matrix/array with all its elements zero<br />
<code class="language-plaintext highlighter-rouge">arr3 = np.zeros(3)</code> # creates an array of 3 elements<br />
<code class="language-plaintext highlighter-rouge">arr4 = np.zeros((2,3))</code> # creates a matrix of 2 rows and 3 columns<br />
e) Create an Identity Matrix<br />
<code class="language-plaintext highlighter-rouge">arr5 = np.eye(4)</code> #creates a 4X4 identity matrix<br />
f) Creates a matrix/array with all its elements one<br />
<code class="language-plaintext highlighter-rouge">arr6 = np.ones((3,4))</code> # creates an array of 3 rows and 4 columns<br />
g) Creates a evenly spaced numbers over a specified interval<br />
<code class="language-plaintext highlighter-rouge">arr7 = np.linspace(start = 0, stop = 5, num = 10, endpoint = True)</code># creates an array of 10 elements with evenly spaced numbers from 0 to 5 (including 5).</p>
<hr />
<p><strong>Array of Random Numbers</strong><br />
<code class="language-plaintext highlighter-rouge">np.random.rand(5)</code> # creates an array of 5 random numbers from 0 to 1<br />
<code class="language-plaintext highlighter-rouge">np.random.randn(5,5)</code> # creates a 5X5 matrix of random numbers from -1 to # 1<br />
<code class="language-plaintext highlighter-rouge">np.random.randint(1,100)</code> # returns a number between 1 and 100<br />
<code class="language-plaintext highlighter-rouge">np.random.randint(1,100,10)</code> # returns an array of 10 numbers between 1 and 100<br />
<code class="language-plaintext highlighter-rouge">arr = np.arange(25)</code> # returns an array of 25 elements from 0 to 25 (exclusive)</p>
<hr />
<p><strong>Indexing</strong><br />
<code class="language-plaintext highlighter-rouge">arr[8]</code> # returns element at 8th index<br />
<code class="language-plaintext highlighter-rouge">arr[1:5]</code> # returns elements of 1st to 5th (excluding) index<br />
<code class="language-plaintext highlighter-rouge">arr2d[0][0]</code> # returns element at 0th row and 0th column<br />
<code class="language-plaintext highlighter-rouge">arr2d[:2,1:]</code></p>
<hr />
<p><strong>Operations</strong><br />
<code class="language-plaintext highlighter-rouge">arr-arr</code><br />
<code class="language-plaintext highlighter-rouge">arr+5</code><br />
<code class="language-plaintext highlighter-rouge">arr*arr</code><br />
<code class="language-plaintext highlighter-rouge">1/arr</code> # ensure that none of the elements has value zero<br />
<code class="language-plaintext highlighter-rouge">np.sqrt(arr)</code><br />
<code class="language-plaintext highlighter-rouge">np.exp(arr)</code><br />
<code class="language-plaintext highlighter-rouge">np.sin(arr)</code><br />
<code class="language-plaintext highlighter-rouge">np.log(arr)</code></p>
<hr />
<p><strong>Other functions</strong><br />
<code class="language-plaintext highlighter-rouge">arr.max()</code> # returns maximum element of array<br />
<code class="language-plaintext highlighter-rouge">arr.min()</code> # returns minimum element of array<br />
<code class="language-plaintext highlighter-rouge">arr.argmax()</code> # returns index of maximum element<br />
<code class="language-plaintext highlighter-rouge">arr.argmin()</code> # returns index of minimum element<br />
<code class="language-plaintext highlighter-rouge">arr.shape()</code> # returns the shape of array<br />
<code class="language-plaintext highlighter-rouge">arr.dtype()</code> # returns the type of array<br />
<code class="language-plaintext highlighter-rouge">arr1 = arr.copy()</code> # initialises a copy of arr array to arr1</p>shreyateezaWhat is NumPy? According to Wikipedia - NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays. Install NumPy a) If you already have Anaconda installed in your PC, then NumPy might be pre-installed in it or simply install it by running - conda install numpy b) If you have pip installed in your PC, then run - python -m pip install — user numpy Import the Library import numpy as np You can also give other name instead of np. Create Arrays and change elements of array a) 1D Array arr1 = np.array([1,2,3,4,5,6]) b) Higher Dimension Array arr2 = np.array([[1,2,3],[4,5,6]]) c) Change an element of Array arr1[1] = 22 # [1,22,3,4,5,6] arr2[1][1] = 56 # [[1,2,3],[4,56,6]] d) Create a matrix/array with all its elements zero arr3 = np.zeros(3) # creates an array of 3 elements arr4 = np.zeros((2,3)) # creates a matrix of 2 rows and 3 columns e) Create an Identity Matrix arr5 = np.eye(4) #creates a 4X4 identity matrix f) Creates a matrix/array with all its elements one arr6 = np.ones((3,4)) # creates an array of 3 rows and 4 columns g) Creates a evenly spaced numbers over a specified interval arr7 = np.linspace(start = 0, stop = 5, num = 10, endpoint = True)# creates an array of 10 elements with evenly spaced numbers from 0 to 5 (including 5). Array of Random Numbers np.random.rand(5) # creates an array of 5 random numbers from 0 to 1 np.random.randn(5,5) # creates a 5X5 matrix of random numbers from -1 to # 1 np.random.randint(1,100) # returns a number between 1 and 100 np.random.randint(1,100,10) # returns an array of 10 numbers between 1 and 100 arr = np.arange(25) # returns an array of 25 elements from 0 to 25 (exclusive) Indexing arr[8] # returns element at 8th index arr[1:5] # returns elements of 1st to 5th (excluding) index arr2d[0][0] # returns element at 0th row and 0th column arr2d[:2,1:] Operations arr-arr arr+5 arr*arr 1/arr # ensure that none of the elements has value zero np.sqrt(arr) np.exp(arr) np.sin(arr) np.log(arr) Other functions arr.max() # returns maximum element of array arr.min() # returns minimum element of array arr.argmax() # returns index of maximum element arr.argmin() # returns index of minimum element arr.shape() # returns the shape of array arr.dtype() # returns the type of array arr1 = arr.copy() # initialises a copy of arr array to arr1Tuples_n_lists2018-11-12T00:00:00+00:002018-11-12T00:00:00+00:00https://shreyateeza.github.io/2018/11/12/tuples_n_lists<h2 id="tuples-n-lists">Tuples n Lists</h2>
<ol>
<li>Tuples encloses the data items in round brackets and Lists encloses items in square brackets.</li>
<li>Tuples are immutable and hence cannot be copied whereas Lists are mutable and can be copied.</li>
<li>There’s a strong culture of tuples being for heterogeneous collection and lists for being homogeneous collection.</li>
<li>Performing operations on tuples is faster than that of list.</li>
</ol>
<p>Considering <em>tuples and lists</em> as two different sets of people. We see that Tuples are open to every type of people,
are strict to changes around them, they don’t wanna be imitated and are <strong>fast and furious</strong>. On the other hand,
Lists are not open to many, usually respond to stimuli around them and improve themselves, can be followed from down,
and are <strong>slow and steady</strong>.</p>
<p>On comparing we observe that some might like to be amongst the tuples and some like lists. But the harsh and supple
truth is that both type of people are required to exist and one must be like both of them. If one hand is Tuple and
other hand is list, then its amust to observe and accept wholeheartedly that both hands are required to clap because
one is incomplete without other.</p>shreyateezaTuples n Lists Tuples encloses the data items in round brackets and Lists encloses items in square brackets. Tuples are immutable and hence cannot be copied whereas Lists are mutable and can be copied. There’s a strong culture of tuples being for heterogeneous collection and lists for being homogeneous collection. Performing operations on tuples is faster than that of list. Considering tuples and lists as two different sets of people. We see that Tuples are open to every type of people, are strict to changes around them, they don’t wanna be imitated and are fast and furious. On the other hand, Lists are not open to many, usually respond to stimuli around them and improve themselves, can be followed from down, and are slow and steady. On comparing we observe that some might like to be amongst the tuples and some like lists. But the harsh and supple truth is that both type of people are required to exist and one must be like both of them. If one hand is Tuple and other hand is list, then its amust to observe and accept wholeheartedly that both hands are required to clap because one is incomplete without other.