diff --git a/Python/NumPy arrays/NumPy arrays.ipynb b/Python/NumPy arrays/NumPy arrays.ipynb new file mode 100644 index 0000000..0f85ec7 --- /dev/null +++ b/Python/NumPy arrays/NumPy arrays.ipynb @@ -0,0 +1,514 @@ +{ + "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", + "\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": [ + "#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]" + ] + }, + { + "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", + "\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" + ] + }, + { + "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", + "\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 vector.\n", + "\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", + "\n", + "x = np.array([7, 8, 9])\n", + "y=np.array([[0],\n", + " [0],\n", + " [0]])\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", + "\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", + "\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)" + ] + } + ], + "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..c8227ca --- /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. 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 +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. + + +