From b27102693699349546e965c5ab76366eeca5920b Mon Sep 17 00:00:00 2001 From: Javali Date: Fri, 7 Oct 2022 00:45:48 +0530 Subject: [PATCH 1/4] Updated code --- Python/NumPy arrays/NumPy arrays.ipynb | 512 +++++++++++++++++++++++++ Python/NumPy arrays/NumPy arrays.md | 37 ++ 2 files changed, 549 insertions(+) create mode 100644 Python/NumPy arrays/NumPy arrays.ipynb create mode 100644 Python/NumPy arrays/NumPy arrays.md diff --git a/Python/NumPy arrays/NumPy arrays.ipynb b/Python/NumPy arrays/NumPy arrays.ipynb new file mode 100644 index 0000000..f9aa36d --- /dev/null +++ b/Python/NumPy arrays/NumPy arrays.ipynb @@ -0,0 +1,512 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "array A ndim: 1\n", + "array A shape: (6,)\n", + "array A size: 6\n", + "array B ndim: 2\n", + "array B shape: (3, 3)\n", + "array B size: 9\n" + ] + } + ], + "source": [ + "#To learn about array attributes, let us consider two arrays A and B of different dimensions.\n", + "#Each array has attributes ndim (the number of dimensions), shape (the size of each dimension), and size (the total size of the array).\n", + "import numpy as np\n", + "A=np.array([1,2,3,4,5,6]) #This is a one dimensional array\n", + "B=np.array([[1,2,3],\n", + " [4,5,6],\n", + " [7,8,9]]) #This is a two dimensional array\n", + "print(\"array A ndim: \", A.ndim)\n", + "print(\"array A shape:\", A.shape)\n", + "print(\"array A size: \", A.size)\n", + "print(\"array B ndim: \", B.ndim)\n", + "print(\"array B shape:\", B.shape)\n", + "print(\"array B size: \", B.size)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dtype: int32\n", + "itemsize: 4 bytes\n", + "nbytes: 36 bytes\n" + ] + } + ], + "source": [ + "#We also have the 'dtype','itemsize' and 'nbytes' attributes\n", + "print(\"dtype:\", B.dtype)\n", + "print(\"itemsize:\", B.itemsize, \"bytes\")\n", + "print(\"nbytes:\", B.nbytes, \"bytes\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#In a one-dimensional array, the ith value can be accessed by specifying the desired index in square brackets, just like lists in Python\n", + "A[2]" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#To index from the end of the array, negative indices can be used.\n", + "A[-1]" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#In a multi-dimensional array, items are accessed with comma-separated tuple of indices\n", + "B[2,1]" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "B[1,-1]" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([2, 4, 6])" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "A[1::2] # every other element, starting at index 1" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2, 3, 4, 5])" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Array Slicing\n", + "#We use the slice notation, marked by the colon (:) character. This syntax is the same as that of the Python list.\n", + "#array[start:stop:step] If these paramaters are unspecified, default will be start=0, stop=size of dimension, step=1.\n", + "A[:5] # first five elements" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 2],\n", + " [4, 5]])" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Multidimensional\n", + "B[:2, :2] # two rows, two columns" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[9, 8, 7],\n", + " [6, 5, 4],\n", + " [3, 2, 1]])" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "B[::-1, ::-1]" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 1 2 3]\n", + " [ 4 5 6]\n", + " [ 7 8 9]\n", + " [10 11 12]]\n" + ] + } + ], + "source": [ + "#Reshaping of arrays is done using 'reshape' method.\n", + "grid = np.arange(1, 13).reshape((4, 3))\n", + "print(grid)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1],\n", + " [2],\n", + " [3],\n", + " [4],\n", + " [5],\n", + " [6]])" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Also reshaping using the column vector via newaxis, also can be done with row\n", + "A[:, np.newaxis]" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2, 4, 5, 6, 8])" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Array concatenation/joining using 'concatenate' method.\n", + "\n", + "x = np.array([1, 2, 4])\n", + "y = np.array([5, 6, 8])\n", + "np.concatenate([x, y])" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2, 3, 4, 5, 6, 1, 2, 4])" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.concatenate([A,x])" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 2, 3],\n", + " [4, 5, 6],\n", + " [7, 8, 9],\n", + " [1, 2, 3],\n", + " [4, 5, 6],\n", + " [7, 8, 9]])" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#For multidimensional arrays\n", + "np.concatenate([B,B])" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Vertically,\n", + " [[7 8 9]\n", + " [1 2 3]\n", + " [4 5 6]\n", + " [7 8 9]]\n", + "Horizontally,\n", + " [[1 2 3 0]\n", + " [4 5 6 0]\n", + " [7 8 9 0]]\n" + ] + } + ], + "source": [ + "#Concatenation can also be done horizontally or vertically using np.vstack and np.hstack.\n", + "x = np.array([7, 8, 9])\n", + "y=np.array([[0],\n", + " [0],\n", + " [0]])\n", + "# vertically stack the arrays\n", + "print(\"Vertically,\\n\",np.vstack([x, B]))\n", + "print(\"Horizontally,\\n\",np.hstack([B,y]))" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3] [4 5] [6 7 8 9]\n" + ] + } + ], + "source": [ + "#Array splitting using 'np.split', 'np.hsplit', and 'np.vsplit'. \n", + "#We pass a list of indices giving the split points.\n", + "arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "x1, x2, x3 = np.split(arr, [3, 5])\n", + "print(x1, x2, x3)" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "First part:\n", + " [[1 2 3]\n", + " [4 5 6]]\n", + "Second part: [[7 8 9]]\n" + ] + } + ], + "source": [ + "upper, lower = np.vsplit(B, [2])\n", + "print(\"First part:\\n\",upper)\n", + "print(\"Second part:\",lower)" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "First part:\n", + " [[1 2]\n", + " [4 5]\n", + " [7 8]]\n", + "Second part:\n", + " [[3]\n", + " [6]\n", + " [9]]\n" + ] + } + ], + "source": [ + "left, right = np.hsplit(B, [2])\n", + "print(\"First part:\\n\",left)\n", + "print(\"Second part:\\n\",right)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 2]\n", + " [4 5]]\n", + "[[99 2]\n", + " [ 4 5]]\n", + "[[1 2 3]\n", + " [4 5 6]\n", + " [7 8 9]]\n" + ] + } + ], + "source": [ + "#Creating copies of an array. Done using the 'copy()' method.\n", + "B_copy = B[:2, :2].copy()\n", + "print(B_copy)\n", + "B_copy[0,0]=99\n", + "print(B_copy)\n", + "#Original array will not be modified.\n", + "print(B)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.9.12 ('base')", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.12" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "74ac7666c5f772f3962d8c6aaf586f39243e0025eb1ca792bb32d791e00cf10a" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Python/NumPy arrays/NumPy arrays.md b/Python/NumPy arrays/NumPy arrays.md new file mode 100644 index 0000000..94daada --- /dev/null +++ b/Python/NumPy arrays/NumPy arrays.md @@ -0,0 +1,37 @@ +# **NumPy Arrays** + +## About +Numpy is a core library used in Data Science, and an array is its most central data structure. This multidimensional array object has incredible speed, and the library also provides various tools to interact with these arrays. We will go over these tools in different Python programs. +## NumPy Array +Firstly, what is an array?
+Where an element is found, how to interpret an element and all this information about the raw data is found in an *array*, which is a grid of values. + +This grid can be indexed in different ways. The elements are all of the same type, referred to as the *array dtype*. +An N-dimensional array is simply an array with any number of dimensions. +## Code explanation +The Python programs describe the basic functions of the NumPy array through the following sub topics: + +1. Attributes of arrays:
+To determine the size, dimension, memory consumption, and data types of arrays. +2. Indexing of arrays:
To get and assign value of individual array elements. +3. Slicing of arrays:
To get and to insert or set smaller subarrays within a larger array. +4. Reshaping of arrays:
To change the shape of a given array. +5. Joining and splitting of arrays:
To combine multiple arrays into one, and to split one array into many. + +## NumPy array vs Python list +### Differences + +The use of an NumPy array saves memory and is simple. NumPy offers a means for specifying the data types and utilises significantly less RAM to store data. This enables even more code optimization. + +Elements of an array are stored contiguously in memory. For example, all rows of a two-dimensioned array must have the same number of columns. In a Python list, the elements do not have to be in contiguous memory. + +Another major difference is that unlike Python lists, NumPy arrays have a fixed type. For example, if a floating-point value is added to an integer array, the value will be silently truncated. + +### Advantages of NumPy arrays +1. Consumes lesser memory. +2. Fast as compared to the Python List. +3. Element-wise operation is possible in NumPy arrays unlike in Python lists. +3. Convenient to use. + + + From 039ac3e165e3c21ebdfa06283a9f67e73f96730c Mon Sep 17 00:00:00 2001 From: Javali Date: Fri, 7 Oct 2022 00:52:29 +0530 Subject: [PATCH 2/4] updated md --- Python/NumPy arrays/NumPy arrays.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Python/NumPy arrays/NumPy arrays.md b/Python/NumPy arrays/NumPy arrays.md index 94daada..b16eeca 100644 --- a/Python/NumPy arrays/NumPy arrays.md +++ b/Python/NumPy arrays/NumPy arrays.md @@ -1,11 +1,11 @@ # **NumPy Arrays** ## About -Numpy is a core library used in Data Science, and an array is its most central data structure. This multidimensional array object has incredible speed, and the library also provides various tools to interact with these arrays. We will go over these tools in different Python programs. +Numpy is a core library used in Data Science, and an array is its most central data structure. NumPy aims to provide an array object that is up to 50x faster than traditional Python lists. This multidimensional array object has incredible speed, and the library also provides various tools to interact with these arrays.
+We will go over these tools in different Python programs. ## NumPy Array Firstly, what is an array?
Where an element is found, how to interpret an element and all this information about the raw data is found in an *array*, which is a grid of values. - This grid can be indexed in different ways. The elements are all of the same type, referred to as the *array dtype*. An N-dimensional array is simply an array with any number of dimensions. ## Code explanation From bb4251d753b29446c9fdec4124e9fdc2356e14a4 Mon Sep 17 00:00:00 2001 From: Javali Date: Fri, 7 Oct 2022 00:59:08 +0530 Subject: [PATCH 3/4] updated --- Python/NumPy arrays/NumPy arrays.ipynb | 20 ++++++++++---------- Python/NumPy arrays/NumPy arrays.md | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Python/NumPy arrays/NumPy arrays.ipynb b/Python/NumPy arrays/NumPy arrays.ipynb index f9aa36d..d940dca 100644 --- a/Python/NumPy arrays/NumPy arrays.ipynb +++ b/Python/NumPy arrays/NumPy arrays.ipynb @@ -21,11 +21,12 @@ "source": [ "#To learn about array attributes, let us consider two arrays A and B of different dimensions.\n", "#Each array has attributes ndim (the number of dimensions), shape (the size of each dimension), and size (the total size of the array).\n", + "\n", "import numpy as np\n", "A=np.array([1,2,3,4,5,6]) #This is a one dimensional array\n", "B=np.array([[1,2,3],\n", " [4,5,6],\n", - " [7,8,9]]) #This is a two dimensional array\n", + " [7,8,9]]) #This is a two dimensional array\n", "print(\"array A ndim: \", A.ndim)\n", "print(\"array A shape:\", A.shape)\n", "print(\"array A size: \", A.size)\n", @@ -73,6 +74,7 @@ } ], "source": [ + "##Indexing in arrays\n", "#In a one-dimensional array, the ith value can be accessed by specifying the desired index in square brackets, just like lists in Python\n", "A[2]" ] @@ -177,6 +179,7 @@ ], "source": [ "#Array Slicing\n", + "\n", "#We use the slice notation, marked by the colon (:) character. This syntax is the same as that of the Python list.\n", "#array[start:stop:step] If these paramaters are unspecified, default will be start=0, stop=size of dimension, step=1.\n", "A[:5] # first five elements" @@ -244,6 +247,7 @@ ], "source": [ "#Reshaping of arrays is done using 'reshape' method.\n", + "\n", "grid = np.arange(1, 13).reshape((4, 3))\n", "print(grid)" ] @@ -270,7 +274,8 @@ } ], "source": [ - "#Also reshaping using the column vector via newaxis, also can be done with row\n", + "#Also reshaping using the column vector via newaxis, also can be done with row vector.\n", + "\n", "A[:, np.newaxis]" ] }, @@ -367,11 +372,11 @@ ], "source": [ "#Concatenation can also be done horizontally or vertically using np.vstack and np.hstack.\n", + "\n", "x = np.array([7, 8, 9])\n", "y=np.array([[0],\n", " [0],\n", " [0]])\n", - "# vertically stack the arrays\n", "print(\"Vertically,\\n\",np.vstack([x, B]))\n", "print(\"Horizontally,\\n\",np.hstack([B,y]))" ] @@ -392,6 +397,7 @@ "source": [ "#Array splitting using 'np.split', 'np.hsplit', and 'np.vsplit'. \n", "#We pass a list of indices giving the split points.\n", + "\n", "arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "x1, x2, x3 = np.split(arr, [3, 5])\n", "print(x1, x2, x3)" @@ -466,6 +472,7 @@ ], "source": [ "#Creating copies of an array. Done using the 'copy()' method.\n", + "\n", "B_copy = B[:2, :2].copy()\n", "print(B_copy)\n", "B_copy[0,0]=99\n", @@ -473,13 +480,6 @@ "#Original array will not be modified.\n", "print(B)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/Python/NumPy arrays/NumPy arrays.md b/Python/NumPy arrays/NumPy arrays.md index b16eeca..c8227ca 100644 --- a/Python/NumPy arrays/NumPy arrays.md +++ b/Python/NumPy arrays/NumPy arrays.md @@ -6,7 +6,7 @@ We will go over these tools in different Python programs. ## NumPy Array Firstly, what is an array?
Where an element is found, how to interpret an element and all this information about the raw data is found in an *array*, which is a grid of values. -This grid can be indexed in different ways. The elements are all of the same type, referred to as the *array dtype*. +This grid can be indexed in different ways. The elements are all of the same type, referred to as the *array dtype*.
An N-dimensional array is simply an array with any number of dimensions. ## Code explanation The Python programs describe the basic functions of the NumPy array through the following sub topics: From 1ea27b2eec9490816c634d9ab333899ee0b0687a Mon Sep 17 00:00:00 2001 From: Javali Date: Fri, 7 Oct 2022 01:02:15 +0530 Subject: [PATCH 4/4] updated --- Python/NumPy arrays/NumPy arrays.ipynb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Python/NumPy arrays/NumPy arrays.ipynb b/Python/NumPy arrays/NumPy arrays.ipynb index d940dca..0f85ec7 100644 --- a/Python/NumPy arrays/NumPy arrays.ipynb +++ b/Python/NumPy arrays/NumPy arrays.ipynb @@ -74,8 +74,9 @@ } ], "source": [ - "##Indexing in arrays\n", + "#Indexing in arrays\n", "#In a one-dimensional array, the ith value can be accessed by specifying the desired index in square brackets, just like lists in Python\n", + "\n", "A[2]" ] }, @@ -182,6 +183,7 @@ "\n", "#We use the slice notation, marked by the colon (:) character. This syntax is the same as that of the Python list.\n", "#array[start:stop:step] If these paramaters are unspecified, default will be start=0, stop=size of dimension, step=1.\n", + "\n", "A[:5] # first five elements" ] },