{ "cells": [ { "cell_type": "markdown", "id": "07d04291", "metadata": {}, "source": [ "--- \n", " \n", "\n", "

Department of Data Science

\n", "

Course: Tools and Techniques for Data Science

\n", "\n", "---\n", "

Instructor: Muhammad Arif Butt, Ph.D.

" ] }, { "cell_type": "markdown", "id": "f362a88f", "metadata": {}, "source": [ "

Lecture 3.1 (NumPy-01)

" ] }, { "cell_type": "markdown", "id": "db61dfd7", "metadata": {}, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "fa7486dc", "metadata": {}, "source": [ "# _01-NumPy-ArrayCreation.ipynb_" ] }, { "cell_type": "markdown", "id": "c4837a46", "metadata": {}, "source": [ " " ] }, { "cell_type": "code", "execution_count": null, "id": "0f890596", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "2a5c5e74", "metadata": {}, "source": [ "# Learning agenda of this notebook\n", "1. Introduction to Numpy Library\n", "2. Array Creation using `np.array()` constructor\n", "3. Miscellaneous Array Creation Functions\n", " - `np.zeros()`\n", " - `np.ones()`\n", " - `np.empty()`\n", " - `np.full()`\n", " - `np.eye()`\n", " - `np.fromstring()`\n", " - `np.arange()`\n", " - `np.linspace()`\n", " - `np.random.rand()`\n", " - `np.random.randint()`\n", " - `np.zeros_like()`" ] }, { "cell_type": "markdown", "id": "307bcc79", "metadata": {}, "source": [ "## 1. Introduction to Numpy Library\n", " \n", "\n", "- *A **NumPy array** (https://numpy.org) is a numerically ordered sequence of elements stored contiguously in memory, that can store elements of homogeneous types (usually numbers but can be boolians, strings, or other objects), is iterable, mutable, non-growable/shrinkable and allows duplicate elements.*\n", "- Some attributes of NumPy Array:\n", ">- **ndim:** The number of dimensions, can be 1, 2, 3, ... N\n", ">- **axis:** Each dimension is called an axis: `axis 0` is the vertical axis, goes from top to bottom, represents the number of rows, `axis 1` is the horizontal axis, goes from left to right, represents the number of columns, `axis 2` is the number of columns along z axis\n", ">- **rank:** The number of axes is called the rank.\n", ">- **shape:** It is a tuple whose length is the rank and elements are dimension of each axis.\n", ">- **size:** It is the total number of elements, which is the product of all axis lengths.\n", ">- **itemsize:** It gives you the size in bytes of each element of the array (depends on dtype).\n", ">- **nbytes:** It gives you the total number of bytes occupied by entire array object in memory (size x itemsize)." ] }, { "cell_type": "markdown", "id": "0fcd7b42", "metadata": {}, "source": [ " \n", " " ] }, { "cell_type": "markdown", "id": "28ac3d07", "metadata": {}, "source": [ "To understand the internal memory layout of NumPy ndarray object, consider the four additional attributes:\n", "- A pointer, to block of data in RAM or in a memory-mapped file\n", "- A dtype, that describes fixed-size value cells in the array\n", "- A shape, which is a tuple indicating the array’s shape\n", "- A strides, which is a tuple of integers indicating the number of bytes to “step” in order to advance one element along a dimension (C-Type/Row-Major vs F-Type/Column-Major)\n", " - Row-Major means that if you have a two-dimensional array of data, the values in each row of the array are stored in adjacent memory locations\n", " - Column-Major means that if you have a two-dimensional array of data, the values in each column of the array are stored in adjacent memory locations\n", " \n", " \n", " \n", ">- **data:** A pointer to block of data in RAM or in memory-mapped file. (Returns memory address)\n", ">- **dtype:** It gives you the dtype of the numPy array elements (fixed size value cells in memory)\n", ">- **shape:** It gives you a tuple indicating the array's shape\n", ">- **strides:** The strides attribute is a tuple of the same length as the number of axes (dimensions) of the array. The strides of an array tell us how many bytes we have to skip in memory to move to the next position along a certain axis. For example, in above 2-D array (shown in figure), we have to skip 4 bytes (1 value) to move to the next column, but 12 bytes (3 values) to get to the same position in the next row (for row-major).\n", " - Row-Major means that if you have a two-dimensional array of data, the values in each row of the array are stored in adjacent memory locations\n", " - Column-Major means that if you have a two-dimensional array of data, the values in each column of the array are stored in adjacent memory locations\n", ">- **flags:** It gives you information about the memory layout of the array." ] }, { "cell_type": "code", "execution_count": 5, "id": "b85cdbbb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: pip in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (22.0.3)\n", "Collecting pip\n", " Using cached pip-22.0.4-py3-none-any.whl (2.1 MB)\n", "Installing collected packages: pip\n", " Attempting uninstall: pip\n", " Found existing installation: pip 22.0.3\n", " Uninstalling pip-22.0.3:\n", " Successfully uninstalled pip-22.0.3\n", "Successfully installed pip-22.0.4\n" ] } ], "source": [ "import sys\n", "!{sys.executable} -m pip install --upgrade pip" ] }, { "cell_type": "code", "execution_count": 6, "id": "c2831c56", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: numpy in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (1.19.5)\r\n" ] } ], "source": [ "# Unlike the other modules, we have been working so far, you have to download and install numPy\n", "# To install this library in Jupyter notebook\n", "import sys\n", "!{sys.executable} -m pip install numpy" ] }, { "cell_type": "code", "execution_count": null, "id": "822d275e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 1, "id": "dba905d0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('1.19.5',\n", " ['/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy'])" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "np.__version__ , np.__path__" ] }, { "cell_type": "markdown", "id": "acd0efbb", "metadata": {}, "source": [ "## 2. Array Creation using `np.array()` Constructor\n", " \n", "\n", "```\n", "np.array(seq, dtype)\n", "```\n", "- The only required argument is a sequence like object like a list from which to create an array\n", "- The optional `dtype` argument specifies the data type of the array objects. \n", "- For integer values it is normally int64\n", "- For float values it is normally float64.\n", "- You can mention `dtype` if you want to limit memory usage. " ] }, { "cell_type": "code", "execution_count": null, "id": "8fe6d012", "metadata": {}, "outputs": [], "source": [ "np.sctypes" ] }, { "cell_type": "code", "execution_count": null, "id": "757eb216", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "b8dbea3f", "metadata": {}, "source": [ "### a. Creating an Empty NumPy Array" ] }, { "cell_type": "code", "execution_count": null, "id": "a72abbe2", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "arr = np.array([])\n", "print(arr)\n", "print(type(arr))" ] }, { "cell_type": "code", "execution_count": null, "id": "006754f4", "metadata": {}, "outputs": [], "source": [ "print(\"array: \", arr)\n", "print(\"arr.ndim: \", arr.ndim) # get number of dimensions (an empty array has number of dimensions equals to 1)\n", "print(\"arr.shape: \", arr.shape) # This is an empty array, so have a shape of (0, )\n", "print(\"arr.size: \", arr.size) # get total number of elements\n", "print(\"arr.dtype: \", arr.dtype) # get data type, default for empty array is float64\n", "print(\"arr.itemsize: \", arr.itemsize) # get size in bytes of each element of the array default is 8 for float64\n", "print(\"arr.nbytes: \", arr.nbytes) # get number of bytes occupied by entire array object in memory\n", "\n", "print(\"arr.data: \", arr.data)\n", "print(\"arr.strides: \", arr.strides)\n", "print(\"arr.flags: \\n\", arr.flags)" ] }, { "cell_type": "code", "execution_count": null, "id": "12e3d956", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "38beed8a", "metadata": {}, "source": [ "### b. 0-Dimensional Array\n", "- A 0-D array has a single scalar value, and has zero or no axis" ] }, { "cell_type": "code", "execution_count": 2, "id": "e25df0a4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array: 10\n" ] } ], "source": [ "# Create a 0-D numpy array using array() constructor\n", "import numpy as np\n", "arr = np.array(10)\n", "print(\"array: \", arr)" ] }, { "cell_type": "code", "execution_count": 3, "id": "0e297790", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array: 10\n", "arr.ndim: 0\n", "arr.shape: ()\n", "arr.size: 1\n", "arr.dtype: int64\n", "arr.itemsize: 8\n", "arr.nbytes: 8\n", "arr.data: \n", "arr.strides: ()\n", "arr.flags: \n", " C_CONTIGUOUS : True\n", " F_CONTIGUOUS : True\n", " OWNDATA : True\n", " WRITEABLE : True\n", " ALIGNED : True\n", " WRITEBACKIFCOPY : False\n", " UPDATEIFCOPY : False\n", "\n" ] } ], "source": [ "print(\"array: \", arr)\n", "print(\"arr.ndim: \", arr.ndim) # get number of dimensions (a 0-D array has number of dimensions equals to 0)\n", "print(\"arr.shape: \", arr.shape) # This is a scalar value, so have no shape ( )\n", "print(\"arr.size: \", arr.size) # get total number of elements, in this case it is 1\n", "print(\"arr.dtype: \", arr.dtype) # get data type, default is what is minimum required to hold the only element\n", "print(\"arr.itemsize: \", arr.itemsize) # get size in bytes of each element of the array default is 8 for int64\n", "print(\"arr.nbytes: \", arr.nbytes) # get number of bytes occupied by entire array object in memory\n", "\n", "print(\"arr.data: \", arr.data)\n", "print(\"arr.strides: \", arr.strides)\n", "print(\"arr.flags: \\n\", arr.flags)" ] }, { "cell_type": "code", "execution_count": null, "id": "f4b11685", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "07a08a79", "metadata": {}, "source": [ "### c. 1-Dimensional Arrays\n", " \n", "\n", "- An array that has 0-D arrays or scalar values, as its elements is called a 1-D array. \n", "- Used to represent vectors or 1st order tensors.\n", "- A 1-D array has only one axis:\n", " - **axis 0:** or vertical axis or x-axis or number of rows" ] }, { "cell_type": "code", "execution_count": 10, "id": "72f4134d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 5, 3, 2],\n", " [ 8, 9, -1],\n", " [ 2, -3, 6]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr = np.array([[5,3,2],[8,9,-1],[2,-3,6]])\n", "arr" ] }, { "cell_type": "code", "execution_count": 24, "id": "2e296330", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "3\n", "2\n", "8\n", "9\n", "-1\n", "2\n", "-3\n", "6\n" ] } ], "source": [ "for i in range(3):\n", " for j in range(3):\n", " print(arr[i,j])" ] }, { "cell_type": "markdown", "id": "634f2640", "metadata": {}, "source": [ "# " ] }, { "cell_type": "code", "execution_count": null, "id": "e2f70cb3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "fd03ba47", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "93dab3eb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 30, "id": "3530bb63", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array: [1 4 2 5 3]\n", "arr.ndim: 1\n", "arr.shape: (5,)\n", "arr.size: 5\n", "arr.dtype: uint32\n", "arr.itemsize: 4\n", "arr.nbytes: 20\n", "arr.data: \n", "arr.strides: (4,)\n", "arr.flags: \n", " C_CONTIGUOUS : True\n", " F_CONTIGUOUS : True\n", " OWNDATA : True\n", " WRITEABLE : True\n", " ALIGNED : True\n", " WRITEBACKIFCOPY : False\n", " UPDATEIFCOPY : False\n", "\n" ] } ], "source": [ "# Create a 1-D numpy array using array() constructor of numpy\n", "\n", "mylist = [1, 4, 2, 5, 3]\n", "arr = np.array(mylist, dtype=np.uint8)\n", "arr = np.array([1, 4, 2, 5, 3], dtype=np.uint32)\n", "\n", "print(\"array: \", arr)\n", "print(\"arr.ndim: \", arr.ndim) # get number of dimensions\n", "print(\"arr.shape: \", arr.shape) # This is 1-D array having 5 columns, so return a tuple with one element\n", "print(\"arr.size: \", arr.size) # get total number of elements\n", "print(\"arr.dtype: \", arr.dtype) # get data type, default is what is minimum required to hold the max size element\n", "print(\"arr.itemsize: \", arr.itemsize) # get size in bytes of each element of the array\n", "print(\"arr.nbytes: \", arr.nbytes) # get number of bytes occupied by entire array object in memory (2*5=10)\n", "\n", "print(\"arr.data: \", arr.data)\n", "print(\"arr.strides: \", arr.strides)\n", "print(\"arr.flags: \\n\", arr.flags)" ] }, { "cell_type": "markdown", "id": "f4c9b2bf", "metadata": {}, "source": [ ">Note once you print NumPy array elements they are displayed inside `[ ]` as a Python List, however, the elements are space separated in case of NumPy arrays, while in case of Python Lists the elements are comma separated" ] }, { "cell_type": "code", "execution_count": null, "id": "06a0a65c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "92edb9d5", "metadata": {}, "source": [ "### d. 2-Dimensional Arrays\n", " \n", "\n", "- An array that has 1-D arrays as its elements is called a 2-D array. \n", "- Used to represent matrix or 2nd order tensors.\n", "- A 2-D array has two axis:\n", " - **axis 0:** is the vertical axis, goes from top to bottom, represents the number of rows (5)\n", " - **axis 1:** is the horizontal axis, goes from left to right, represents the number of columns (4)\n", "- If you've taken a linear algebra class in high school, you may recognize this 2-d array as a matrix with five rows and four columns. \n", "- Each row represents a city, and the columns may contain temperature, rainfall, humidity, and smog in that city respectively." ] }, { "cell_type": "code", "execution_count": 31, "id": "3f19c130", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array: \n", " [[1 2 3 4]\n", " [5 6 7 8]\n", " [3 2 4 1]\n", " [7 3 4 9]\n", " [4 0 3 1]]\n", "arr.ndim: 2\n", "arr.shape: (5, 4)\n", "arr.size: 20\n", "arr.dtype: int64\n", "arr.itemsize: 8\n", "arr.nbytes: 160\n", "arr.data: \n", "arr.dtypes: int64\n", "arr.strides: (32, 8)\n", "arr.flags: \n", " C_CONTIGUOUS : True\n", " F_CONTIGUOUS : False\n", " OWNDATA : True\n", " WRITEABLE : True\n", " ALIGNED : True\n", " WRITEBACKIFCOPY : False\n", " UPDATEIFCOPY : False\n", "\n" ] } ], "source": [ "# creating 2-D (5x4) array using array constructor in numpy\n", "\n", "mylist = [\n", " [1, 2, 3, 4], \n", " [5, 6, 7, 8], \n", " [3, 2, 4, 1], \n", " [7, 3, 4, 9], \n", " [4, 0, 3, 1]\n", " ]\n", "arr = np.array(mylist)\n", "print(\"array: \\n\", arr)\n", "print(\"arr.ndim: \", arr.ndim) #get number of dimensions (2-D array)\n", "print(\"arr.shape: \", arr.shape) #get dimension size.For a 2-D array, return a tuple with two elements (rows, cols)\n", "print(\"arr.size: \", arr.size) #get total number of elements (5*4=20 elements)\n", "print(\"arr.dtype: \", arr.dtype) #get data type, default is what is minimum required to hold the max size element\n", "print(\"arr.itemsize: \", arr.itemsize) # get size in bytes of each element of the array\n", "print(\"arr.nbytes: \", arr.nbytes) # get number of bytes occupied by entire array object in memory (8*20=160)\n", "\n", "\n", "print(\"arr.data: \", arr.data)\n", "print(\"arr.dtypes: \", arr.dtype)\n", "print(\"arr.strides: \", arr.strides)\n", "print(\"arr.flags: \\n\", arr.flags)" ] }, { "cell_type": "code", "execution_count": null, "id": "d66656a6", "metadata": {}, "outputs": [], "source": [ "arr[1][1]\n", "arr[1,1]" ] }, { "cell_type": "code", "execution_count": 3, "id": "a447943d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array: \n", " [[1]\n", " [2]\n", " [3]]\n", "arr.ndim: 2\n", "arr.shape: (3, 1)\n", "arr.size: 3\n", "arr.dtype: int64\n", "arr.itemsize: 8\n", "arr.nbytes: 24\n", "arr.data: \n", "arr.strides: (8, 8)\n", "arr.flags: \n", " C_CONTIGUOUS : True\n", " F_CONTIGUOUS : True\n", " OWNDATA : True\n", " WRITEABLE : True\n", " ALIGNED : True\n", " WRITEBACKIFCOPY : False\n", " UPDATEIFCOPY : False\n", "\n" ] } ], "source": [ "# Another example\n", "mylist = [[1], [4], [2], [5], [3]]\n", "arr = np.array(mylist)\n", "arr = np.array([[1], [2], [3]])\n", "print(\"array: \\n\", arr)\n", "print(\"arr.ndim: \", arr.ndim) #get number of dimensions (2-D array)\n", "print(\"arr.shape: \", arr.shape) #get dimension size.For a 2-D array, return a tuple with two elements (rows, cols)\n", "print(\"arr.size: \", arr.size) #get total number of elements (5*4=20 elements)\n", "print(\"arr.dtype: \", arr.dtype) #get data type, default is what is minimum required to hold the max size element\n", "print(\"arr.itemsize: \", arr.itemsize) # get size in bytes of each element of the array\n", "print(\"arr.nbytes: \", arr.nbytes) # get number of bytes occupied by entire array object in memory (8*20=160)\n", "\n", "\n", "print(\"arr.data: \", arr.data)\n", "print(\"arr.strides: \", arr.strides)\n", "print(\"arr.flags: \\n\", arr.flags)" ] }, { "cell_type": "code", "execution_count": null, "id": "8f7a7f9d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "bbd0d916", "metadata": {}, "source": [ "### e. 3-Dimensional Arrays\n", " \n", "\n", "- An array that has 2-D arrays as its elements is called a 3-D array. Used to represent 3rd order tensors.\n", "- A 3-D array has three axis:\n", " - **axis 0:** represent the number of rows along x-axis(axis 0). (5)\n", " - **axis 1:** represents the number of rows along y-axis (axis 1). (4)\n", " - **axis 2:** represents the number of columns along z-axis (axix 2). (3)" ] }, { "cell_type": "code", "execution_count": 32, "id": "94082d91", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array: \n", " [[[1 2 3]\n", " [5 6 7]\n", " [3 2 4]\n", " [7 3 4]]\n", "\n", " [[8 1 6]\n", " [1 9 4]\n", " [5 6 4]\n", " [2 6 2]]\n", "\n", " [[5 3 2]\n", " [2 0 5]\n", " [8 3 0]\n", " [5 6 9]]\n", "\n", " [[6 7 1]\n", " [8 1 6]\n", " [1 4 7]\n", " [2 0 4]]\n", "\n", " [[2 8 9]\n", " [3 0 4]\n", " [5 2 2]\n", " [1 3 8]]]\n", "arr.ndim: 3\n", "arr.shape: (5, 4, 3)\n", "arr.size: 60\n", "arr.dtype: uint8\n", "arr.itemsize: 1\n", "arr.nbytes: 60\n", "arr.data: \n", "arr.strides: (12, 3, 1)\n", "arr.flags: \n", " C_CONTIGUOUS : True\n", " F_CONTIGUOUS : False\n", " OWNDATA : True\n", " WRITEABLE : True\n", " ALIGNED : True\n", " WRITEBACKIFCOPY : False\n", " UPDATEIFCOPY : False\n", "\n" ] } ], "source": [ "# creating 3-D (5x4x3) array using array constructor in numpy\n", "mylist = [\n", " [[1, 2, 3], [5, 6, 7], [3, 2, 4], [7, 3, 4]],\n", " [[8, 1, 6], [1, 9, 4], [5, 6, 4], [2, 6, 2]],\n", " [[5, 3, 2], [2, 0, 5], [8, 3, 0], [5, 6, 9]],\n", " [[6, 7, 1], [8, 1, 6], [1, 4, 7], [2, 0, 4]],\n", " [[2, 8, 9], [3, 0, 4], [5, 2, 2], [1, 3, 8]],\n", " ]\n", "arr = np.array(mylist, dtype=np.uint8)\n", "\n", "\n", "print(\"array: \\n\", arr)\n", "print(\"arr.ndim: \", arr.ndim) # get number of dimensions (3-D array)\n", "print(\"arr.shape: \", arr.shape) # get dimension size.For a 3-D array, return a tuple with three elements\n", "print(\"arr.size: \", arr.size) # get total number of elements (5*4*3=60 elements)\n", "print(\"arr.dtype: \", arr.dtype) # get data type, default is what is minimum required to hold the max size element\n", "print(\"arr.itemsize: \", arr.itemsize) # get size in bytes of each element of the array\n", "print(\"arr.nbytes: \", arr.nbytes) # get number of bytes occupied by entire array object in memory (8*60=4800)\n", "\n", "\n", "print(\"arr.data: \", arr.data)\n", "print(\"arr.strides: \", arr.strides)\n", "print(\"arr.flags: \\n\", arr.flags)" ] }, { "cell_type": "markdown", "id": "5949cabb", "metadata": {}, "source": [ "### Understanding Strides" ] }, { "cell_type": "code", "execution_count": null, "id": "e4304e02", "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Strides of 1-D array\n", "import numpy as np\n", "mylist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]\n", "arr = np.array(mylist, dtype=np.int64)\n", "\n", "print(\"array: \\n\", arr)\n", "print(\"arr.data: \", arr.data)\n", "print(\"arr.shape: \", arr.shape) \n", "print(\"arr.dtype: \", arr.dtype) \n", "print(\"arr.size: \", arr.size) \n", "print(\"arr.itemsize: \", arr.itemsize) \n", "print(\"arr.nbytes: \", arr.nbytes) \n", "print(\"arr.strides: \", arr.strides)\n", "# Use of strides is in Indexing, slicing and reshaping\n", "print(arr[3]) # jump 3*8=24 bytes and then read 8 bytes" ] }, { "cell_type": "code", "execution_count": null, "id": "d89fbe4f", "metadata": {}, "outputs": [], "source": [ "# Strides of 2-D array\n", "import numpy as np\n", "mylist = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]\n", "arr = np.array(mylist, dtype=np.int64)\n", "\n", "print(\"array: \\n\", arr)\n", "print(\"arr.data: \", arr.data)\n", "print(\"arr.shape: \", arr.shape) \n", "print(\"arr.dtype: \", arr.dtype) \n", "print(\"arr.size: \", arr.size) \n", "print(\"arr.itemsize: \", arr.itemsize) \n", "print(\"arr.nbytes: \", arr.nbytes) \n", "print(\"arr.strides: \", arr.strides)\n", "# Use of strides is in Indexing, slicing and reshaping\n", "print(arr[1,2]) # jump 1*32+2*8=48 bytes and then read 8 bytes" ] }, { "cell_type": "code", "execution_count": null, "id": "d7f544e3", "metadata": {}, "outputs": [], "source": [ "# Strides of 3-D array\n", "import numpy as np\n", "mylist = [\n", " [[0, 1], [2, 3]],\n", " [[4, 5], [6, 7]],\n", " [[8, 9], [10, 11]]\n", " ]\n", "arr = np.array(mylist, dtype=np.int64)\n", "\n", "print(\"array: \\n\", arr)\n", "print(\"arr.data: \", arr.data)\n", "print(\"arr.shape: \", arr.shape) \n", "print(\"arr.dtype: \", arr.dtype) \n", "print(\"arr.size: \", arr.size) \n", "print(\"arr.itemsize: \", arr.itemsize) \n", "print(\"arr.nbytes: \", arr.nbytes) \n", "print(\"arr.strides: \", arr.strides)\n", "# Use of strides is in Indexing, slicing and reshaping\n", "print(arr[1,1,1]) # jump 1*32 + 0*16 + 1*8 =40 bytes and then read 8 bytes" ] }, { "cell_type": "code", "execution_count": null, "id": "b65d9e8d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "2ad94463", "metadata": {}, "source": [ "## 3. Miscellaneous Array Creation Function\n", "- Numpy also provides some handy methods to create arrays of desired shapes with fixed or random values. Check out the [official documentation](https://numpy.org/doc/stable/reference/routines.array-creation.html) or use the `help` function to learn more." ] }, { "cell_type": "markdown", "id": "cc2a77b5", "metadata": {}, "source": [ " \n", "\n", "### a. Using `np.zeros()` Method\n", "This method returns an array of given shape and type, filled with zeros.\n", "\n", "```\n", "numpy.zeros(shape, dtype=float)\n", "```\n", "\n", "- shape : The shape is an int or tuple of ints to define the size/shape of the array.\n", "- dtype : The dtype is an optional parameter with default value as float." ] }, { "cell_type": "code", "execution_count": null, "id": "8db932d3", "metadata": {}, "outputs": [], "source": [ "# Creating 1-Dimensional array of all zeros\n", "arr = np.zeros(3)\n", "arr\n", "# Notice that the elements are having the default data type as the float. That’s why the zeros are 0." ] }, { "cell_type": "code", "execution_count": null, "id": "57ec3ff3", "metadata": {}, "outputs": [], "source": [ "# Creating 2-Dimensional array filled with zeros\n", "arr = np.zeros((2,3))\n", "arr\n" ] }, { "cell_type": "code", "execution_count": null, "id": "c1823941", "metadata": {}, "outputs": [], "source": [ "# Creating 2-Dimensional array filled with zeros of integer types\n", "arr = np.zeros((2,3), dtype=np.int16)\n", "arr, arr.nbytes" ] }, { "cell_type": "code", "execution_count": null, "id": "0dc5e89e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "ddc10a77", "metadata": {}, "source": [ " \n", "\n", "### b. Using `np.ones()` Method\n", "This method returns an array of given shape and type, filled with ones.\n", "\n", "```\n", "numpy.ones(shape, dtype=float)\n", "```\n", "\n", "- shape : The shape is an int or tuple of ints to define the size/shape of the array.\n", "- dtype : The dtype is an optional parameter with default value as float." ] }, { "cell_type": "code", "execution_count": null, "id": "418071eb", "metadata": {}, "outputs": [], "source": [ "# Creating one-dimensional array with ones\n", "arr = np.ones(3)\n", "arr\n" ] }, { "cell_type": "code", "execution_count": null, "id": "a8825242", "metadata": {}, "outputs": [], "source": [ "# Creating 2-Dimensional array having 3 columns filled with ones\n", "arr = np.ones((2,3))\n", "arr" ] }, { "cell_type": "code", "execution_count": null, "id": "3365218c", "metadata": {}, "outputs": [], "source": [ "# Creating 2-Dimensional array having 3 columns filled with ones of integer types\n", "arr = np.ones((2,3), dtype=np.int16)\n", "arr" ] }, { "cell_type": "code", "execution_count": null, "id": "1504856f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "092ed147", "metadata": {}, "source": [ " \n", "\n", "### c. Using `np.empty()` Method\n", "This method returns an array of given shape and type, filled with junk values.\n", "\n", "```\n", "numpy.empty(shape, dtype=float)\n", "```\n", "\n", "- shape : The shape is an int or tuple of ints to define the size/shape of the array.\n", "- dtype : The dtype is an optional parameter with default value as float.\n", "\n", "\n", "Un-like numpy.zeros(), and numpy.ones(), the numpy.empty() function doesn't initialize the entries, so they contain junk values." ] }, { "cell_type": "code", "execution_count": null, "id": "b0626a6f", "metadata": {}, "outputs": [], "source": [ "arr = np.empty(3)\n", "arr" ] }, { "cell_type": "code", "execution_count": null, "id": "32cd666e", "metadata": {}, "outputs": [], "source": [ "arr = np.empty(3, dtype=np.int16)\n", "arr" ] }, { "cell_type": "code", "execution_count": null, "id": "ccc1b0f1", "metadata": {}, "outputs": [], "source": [ "# Creating 2-Dimensional array having 3 columns filled with junk values\n", "arr = np.empty((2,3), dtype=np.int16)\n", "arr" ] }, { "cell_type": "code", "execution_count": null, "id": "66c813bf", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "cef960cb", "metadata": {}, "source": [ " \n", "\n", "### d. Using `np.full()` Method\n", "This method return a new array of given shape and type, filled with fill_value.\n", "```\n", "np.full(shape, fill_value, dtype=None)\n", "```\n", "- shape: Shape of the new array\n", "- fill_value: Fill value.\n", "- dtype\tThe desired data-type for the array" ] }, { "cell_type": "code", "execution_count": null, "id": "ffbef9b8", "metadata": {}, "outputs": [], "source": [ "arr = np.full(7, 54)\n", "arr" ] }, { "cell_type": "code", "execution_count": null, "id": "3650c4c8", "metadata": {}, "outputs": [], "source": [ "arr = np.full(7, 21.5)\n", "arr" ] }, { "cell_type": "code", "execution_count": null, "id": "0c012b63", "metadata": {}, "outputs": [], "source": [ "arr = np.full((2,3), 21.5)\n", "arr" ] }, { "cell_type": "code", "execution_count": null, "id": "2cf1688d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "0476bba5", "metadata": {}, "source": [ "\n", "\n", "### e. Using `np.eye()` Method\n", "This method is used to create a 2-D array with ones on the diagonal and zeros elsewhere.\n", "\n", "```\n", "eye(N, M=None, k=0 , dtype=float)\n", "```\n", "- N: Number of rows in the output\n", "- M: Number of columns in the output. If None, defaults to N\n", "- k: Index of the diagonal: (Default 0) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal\n", "- dtype: Data-type of the returned array" ] }, { "cell_type": "code", "execution_count": null, "id": "0e0d26d2", "metadata": {}, "outputs": [], "source": [ "# creating 2-D array using eye function\n", "# with 2 rows and 2 columns having 1's at on the main diagonal\n", "eye_arr = np.eye(3,3)\n", "eye_arr\n" ] }, { "cell_type": "code", "execution_count": null, "id": "a9a00e2b", "metadata": {}, "outputs": [], "source": [ "# creating 2-D array of int type using eye function with 4 rows and 3 columns having 1's at on the main diagonal\n", "eye_arr = np.eye(4, 3, dtype=np.int8)\n", "eye_arr\n" ] }, { "cell_type": "code", "execution_count": null, "id": "d1d38f70", "metadata": {}, "outputs": [], "source": [ "# creating 2-D array of int type using eye function with 4 rows and 3 columns having 1's at one place higher\n", "eye_arr = np.eye(4, 3, k = 1, dtype=int)\n", "eye_arr" ] }, { "cell_type": "code", "execution_count": null, "id": "df6a499a", "metadata": {}, "outputs": [], "source": [ "# creating 2-D array of int type using eye function with 4 rows and 3 columns having 1's at one place lower\n", "eye_arr = np.eye(4, 3, k = -1, dtype=int)\n", "eye_arr" ] }, { "cell_type": "code", "execution_count": null, "id": "d3ba626d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "649f820a", "metadata": {}, "source": [ " \n", "\n", "### f. Using `np.fromstring()` Method\n", "This function is used to create a new 1-D array initialized from raw binary or text data in a string.\n", "```\n", "np.fromstring(string, dtype, sep)\n", "```\n", "- string: A string containing the data\n", "- dtype: The data type of the array (by default float)\n", "- sep: The string separating numbers in the data" ] }, { "cell_type": "code", "execution_count": null, "id": "147af890", "metadata": {}, "outputs": [], "source": [ "# convert the space separated string '25 30 35 40' into an floats array\n", "arr = np.fromstring('25 30 35 40', sep=' ')\n", "arr" ] }, { "cell_type": "code", "execution_count": null, "id": "b6c3dc8b", "metadata": {}, "outputs": [], "source": [ "# convert the space separated string '25 30 35 40' into an integer array\n", "arr = np.fromstring('25 30 35 40', sep=' ', dtype=np.uint8)\n", "arr" ] }, { "cell_type": "code", "execution_count": null, "id": "72a344df", "metadata": {}, "outputs": [], "source": [ "# converting comma separated string into float type array\n", "arr = np.fromstring('0.7, 10.4, 50.5', sep=\",\")\n", "print(arr)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "a8ca92d9", "metadata": {}, "outputs": [], "source": [ "# It will raise error\n", "arr = np.fromstring('0.57, str, 50.555', dtype=float, sep=',')" ] }, { "cell_type": "code", "execution_count": null, "id": "0641540e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "0d429a59", "metadata": {}, "source": [ "### g. Using `np.arange()` Method\n", " \n", "This function is used to get evenly spaced values within a given interval.\n", "\n", "```\n", "numpy.arange([start,] stop[, step])\n", "```\n", "- If only one argument is given will generate an int64 array from zero to that value (not inclusive)\n", "- If two arguments are given then start value in inclusive, stop value is not inclusive and the default step is 1\n", "- If three arguments are given then the third argument is the distance between two adjacent values. (default step size is 1)\n", "- All the three arguments can be integers or floats\n", "\n", "The image shows the array created by np.arange(5,9,1)\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "c567ce50", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3 4]\n", "\n", "int64\n" ] } ], "source": [ "import numpy as np\n", "arr = np.arange(5)\n", "print(arr)\n", "print(type(arr))\n", "print(arr.dtype)" ] }, { "cell_type": "code", "execution_count": 2, "id": "32a8c7a1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 1. 2. 3. 4.]\n", "float64\n" ] } ], "source": [ "arr = np.arange(5, dtype=float)\n", "print(arr)\n", "print(arr.dtype)" ] }, { "cell_type": "code", "execution_count": 3, "id": "f84c7cb4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([5, 6, 7, 8, 9])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr = np.arange(5,10)\n", "arr" ] }, { "cell_type": "code", "execution_count": 4, "id": "41f6e3c6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([5, 7, 9])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr = np.arange(5, 10, 2)\n", "arr" ] }, { "cell_type": "code", "execution_count": 5, "id": "8b48cd16", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([], dtype=int64)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr = np.arange(5, 15, -1)\n", "arr" ] }, { "cell_type": "code", "execution_count": 6, "id": "591ba433", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-1. , -1.5, -2. , -2.5, -3. , -3.5])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr = np.arange(-1, -4, -.5)\n", "arr" ] }, { "cell_type": "code", "execution_count": null, "id": "d0a3a7df", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "c88a4fb1", "metadata": {}, "source": [ "### h. Using `np.linspace()` Method\n", " \n", "\n", "This method by default returns an array of 50 evenly spaced elements starting from the first argument (inclusive) to the second argument (inclusive). The third argument, if given, is the count of number of elements of the array, default is 50.\n", "\n", "```\n", "numpy.linspace(start, stop, num=50)\n", "```" ] }, { "cell_type": "code", "execution_count": 7, "id": "415725c9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2. 2.33333333 2.66666667 3. ]\n", "float64\n" ] } ], "source": [ "arr = np.linspace(2,3,4)\n", "print(arr)\n", "print(arr.dtype)" ] }, { "cell_type": "code", "execution_count": 8, "id": "4a98e38d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1. 1.02040816 1.04081633 1.06122449 1.08163265 1.10204082\n", " 1.12244898 1.14285714 1.16326531 1.18367347 1.20408163 1.2244898\n", " 1.24489796 1.26530612 1.28571429 1.30612245 1.32653061 1.34693878\n", " 1.36734694 1.3877551 1.40816327 1.42857143 1.44897959 1.46938776\n", " 1.48979592 1.51020408 1.53061224 1.55102041 1.57142857 1.59183673\n", " 1.6122449 1.63265306 1.65306122 1.67346939 1.69387755 1.71428571\n", " 1.73469388 1.75510204 1.7755102 1.79591837 1.81632653 1.83673469\n", " 1.85714286 1.87755102 1.89795918 1.91836735 1.93877551 1.95918367\n", " 1.97959184 2. ]\n", "float64\n" ] } ], "source": [ "arr = np.linspace(1,2)\n", "print(arr)\n", "print(arr.dtype)" ] }, { "cell_type": "code", "execution_count": 9, "id": "fa61f7a4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-1. -1.08163265 -1.16326531 -1.24489796 -1.32653061 -1.40816327\n", " -1.48979592 -1.57142857 -1.65306122 -1.73469388 -1.81632653 -1.89795918\n", " -1.97959184 -2.06122449 -2.14285714 -2.2244898 -2.30612245 -2.3877551\n", " -2.46938776 -2.55102041 -2.63265306 -2.71428571 -2.79591837 -2.87755102\n", " -2.95918367 -3.04081633 -3.12244898 -3.20408163 -3.28571429 -3.36734694\n", " -3.44897959 -3.53061224 -3.6122449 -3.69387755 -3.7755102 -3.85714286\n", " -3.93877551 -4.02040816 -4.10204082 -4.18367347 -4.26530612 -4.34693878\n", " -4.42857143 -4.51020408 -4.59183673 -4.67346939 -4.75510204 -4.83673469\n", " -4.91836735 -5. ]\n" ] } ], "source": [ "arr = np.linspace(-1,-5)\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": null, "id": "9f699b0f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "6ae200be", "metadata": {}, "source": [ "### i. Using `np.random.rand()` Method\n", "- This method generates an array of random floats (between 0 and 1) of as many dimensions passed as argument.\n", "- If no argument is passed, generates a scalar value between 0 and 1\n", "\n", "```\n", "numpy.random.rand(d0 [,d1] [,d2]....)\n", "```\n" ] }, { "cell_type": "code", "execution_count": null, "id": "30215cdf", "metadata": {}, "outputs": [], "source": [ "#Python's built-in random module has random() function that returns a single float between 0 and 1\n", "import numpy as np\n", "value = np.random.rand()\n", "value" ] }, { "cell_type": "code", "execution_count": 13, "id": "0b52089a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.20053717, 0.67323379, 0.28275728, 0.9770833 , 0.20820593])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Creating 1-D array of 5 elements of random floats between 0 and 1\n", "arr = np.random.rand(5)\n", "arr\n" ] }, { "cell_type": "code", "execution_count": null, "id": "0cc0885d", "metadata": {}, "outputs": [], "source": [ "# Creating 1-D array of 5 elements of random floats between 0 and 10\n", "arr = np.random.rand(5)*10\n", "arr\n" ] }, { "cell_type": "code", "execution_count": 10, "id": "cbde7fc6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2.52589781, 7.45726117, 2.01008613],\n", " [8.13257141, 5.63025205, 5.33048513],\n", " [9.96415767, 4.77972696, 9.99791874],\n", " [8.8997235 , 6.39801751, 1.25707233]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Creating 2-D array (4x3) having random floats between 0 and 1\n", "arr = np.random.rand(4,3)*10\n", "arr" ] }, { "cell_type": "code", "execution_count": 11, "id": "908d56a7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[9.3194226 , 9.02291444, 9.55027305],\n", " [4.38278072, 6.02580758, 7.23071008],\n", " [1.53933886, 6.05247583, 1.21925365],\n", " [9.66975926, 6.15310811, 4.76285839]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Creating 2-D array (4x3) having random floats between 0 and 10\n", "arr = np.random.rand(4,3)*10\n", "arr" ] }, { "cell_type": "code", "execution_count": 12, "id": "f1713e5a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[0.30197889, 0.49797858, 0.38725267],\n", " [0.70668659, 0.36086929, 0.73627458],\n", " [0.65957854, 0.29473759, 0.29208301],\n", " [0.11654146, 0.92557503, 0.67449756]],\n", "\n", " [[0.50938416, 0.70830489, 0.22475061],\n", " [0.67923685, 0.84583092, 0.20966569],\n", " [0.68397828, 0.43892349, 0.21607785],\n", " [0.92754154, 0.90540658, 0.88277135]],\n", "\n", " [[0.41907063, 0.90539579, 0.63679767],\n", " [0.31575133, 0.22486231, 0.76004992],\n", " [0.43865241, 0.14497764, 0.1188367 ],\n", " [0.50535223, 0.9428481 , 0.35010095]],\n", "\n", " [[0.48230864, 0.43042317, 0.53339408],\n", " [0.06012913, 0.52535111, 0.47999186],\n", " [0.31028082, 0.93291025, 0.80171092],\n", " [0.30478443, 0.39595128, 0.37472118]],\n", "\n", " [[0.92363078, 0.48968596, 0.6758323 ],\n", " [0.61803612, 0.33532638, 0.5253386 ],\n", " [0.26828096, 0.8244032 , 0.81776823],\n", " [0.7014707 , 0.70560875, 0.47130194]]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Creating 3-D array of with random values using np.random.rand()\n", "arr = np.random.rand(5,4,3)\n", "arr" ] }, { "cell_type": "code", "execution_count": null, "id": "a4a2fd10", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "f97ace7a", "metadata": {}, "source": [ "### i. Using `np.random.randint()` Method\n", "This method returns an array of specified shape and fills it with random integers.\n", "```\n", "numpy.random.randint(low, high=None, size)\n", "```\n", "- low: Lowest (signed) integer to be drawn from the distribution. But, it works as a highest integer in the sample if high=None.\n", "- high: Largest (signed) integer to be drawn from the distribution (not inclusive)\n", "- size: number of samples to be generated (default is 1 for scalar and >1 for 1-D array and a tupple for ndarray)" ] }, { "cell_type": "code", "execution_count": null, "id": "967ec0b5", "metadata": {}, "outputs": [], "source": [ "# Generating a random integer scalar b/w interval (0,9) \n", "value = np.random.randint(10)\n", "value" ] }, { "cell_type": "code", "execution_count": 14, "id": "a773e1bf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Generating a random integer scalar b/w interval (5,19) \n", "value = np.random.randint(low=5, high=20)\n", "value" ] }, { "cell_type": "code", "execution_count": 15, "id": "8d8c685b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Generating a random integer scalar from -5 to 4 \n", "value = np.random.randint(low=-5, high=5)\n", "value" ] }, { "cell_type": "code", "execution_count": 16, "id": "830eee4b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 99, 99, 100, 13, 76, 17])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# creating 1-D array of size 6 of int type b/w interval (1,100) \n", "arr = np.random.randint(low=1, high=101, size=6)\n", "arr" ] }, { "cell_type": "code", "execution_count": 17, "id": "ad60a9b1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 4, -3, -5, -4])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# creating 1-D array of size 6 of int type b/w interval (-5,4) \n", "arr = np.random.randint(low = -5, high = 5, size = 4)\n", "arr" ] }, { "cell_type": "code", "execution_count": null, "id": "5b72f8a5", "metadata": {}, "outputs": [], "source": [ "# By passing a tuple to size means rows and columns\n", "arr = np.random.randint(low = 1, high = 10, size = (3,3))\n", "arr" ] }, { "cell_type": "code", "execution_count": null, "id": "f5720914", "metadata": {}, "outputs": [], "source": [ "arr = np.random.randint(low = 1, high = 10, size = (2,3,4))\n", "arr" ] }, { "cell_type": "code", "execution_count": null, "id": "d97eabf5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "3a053da9", "metadata": {}, "source": [ "\n", "\n", "### k. Using `np.zeros_like()` Method\n", "This method is used to get an array of zeros with the same shape and type as a given array.\n", "\n", "```\n", "zeros_like(arr, dtype=None) \n", "```\n", "\n", "- arr: array like input\n", "- dtype: Overrides the data type of the result" ] }, { "cell_type": "code", "execution_count": null, "id": "373857fe", "metadata": {}, "outputs": [], "source": [ "# creating a 2-D array\n", "mylist = [[0, 1],[2, 3]]\n", "arr1 = np.array(mylist)\n", "print(\"A 2-D array \\n\", arr1)\n", "\n", "# creating the same array as the shape of 'arr' filled with zeros\n", "arr2 = np.zeros_like(arr1)\n", "print(\"\\n Converted Array \\n\", arr2)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "b97a4eed", "metadata": {}, "outputs": [], "source": [ "# creating 1-D array\n", "arr1 = np.arange(10)\n", "print(\"A 1-D array \\n\", arr1)\n", "\n", "# creating the same array as the shape of 'arr' filled with zeros\n", "arr2 = np.zeros_like(arr1)\n", "print(\"\\n Converted Array \\n\", arr2)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "e3733992", "metadata": {}, "outputs": [], "source": [ "np.fromstring" ] }, { "cell_type": "code", "execution_count": null, "id": "e1c720b1", "metadata": {}, "outputs": [], "source": [ "np.eye(4)" ] }, { "cell_type": "code", "execution_count": null, "id": "549b3fac", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.7" } }, "nbformat": 4, "nbformat_minor": 5 }