1241 lines
43 KiB
Plaintext
1241 lines
43 KiB
Plaintext
{
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"name": "python",
|
||
"display_name": "Python (Pyodide)",
|
||
"language": "python"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "python",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.8"
|
||
},
|
||
"prev_pub_hash": "0184f74c42423d85f7a22c5846d5404e57b47caea53a0577528dc4173a25d13d"
|
||
},
|
||
"nbformat_minor": 4,
|
||
"nbformat": 4,
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<p style=\"text-align:center\">\n <a href=\"https://skills.network\" target=\"_blank\">\n <img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/assets/logos/SN_web_lightmode.png\" width=\"300\" alt=\"Skills Network Logo\">\n </a>\n</p>\n\n# 1D Numpy in Python\n\nEstimated time needed: **40** minutes\n\n## Objectives\n\nAfter completing this lab you will be able to:\n\n* Import and use the `numpy` library\n* Perform operations with `numpy`\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<h2>Table of Contents</h2>\n<div class=\"alert alert-block alert-info\" style=\"margin-thttps://op/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01: 20px\">\n <ul>\n <li>\n <a href=\"#What-is-Numpy?\">What is Numpy?</a>\n <ul>\n <li><a href=\"#Type\">Type</a></li>\n <li><a href=\"#Assign-Value\">Assign Value</a></li>\n <li><a href=\"#Slicing\">Slicing</a></li>\n <li><a href=\"#Assign-Value-with-List\">Assign Value with List</a></li>\n <li><a href=\"#Other-Attributes\">Other Attributes</a></li>\n </ul>\n </li>\n <li>\n <a href=\"#Numpy-Array-Operations\">Numpy Array Operations</a>\n <ul>\n <li><a href=\"#Array-Addition\">Array Addition</a></li>\n <li><a href=\"#Array-Multiplication\">Array Multiplication</a></li>\n <li><a href=\"#Product-of-Two-Numpy-Arrays\">Product of Two Numpy Arrays</a></li>\n <li><a href=\"#Dot-Product\">Dot Product</a></li>\n <li><a href=\"#Adding-Constant-to-a-Numpy-Array\">Adding Constant to a Numpy Array</a></li>\n </ul>\n </li>\n <li><a href=\"#Mathematical-Functions\">Mathematical Functions</a></li>\n <li><a href=\"#Linspace\">Linspace</a></li>\n </ul>\n\n</div>\n\n<hr>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "## What is Numpy?\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "NumPy is a Python library used for working with arrays, linear algebra, fourier transform, and matrices. NumPy stands for Numerical Python and it is an open source project. The array object in NumPy is called **ndarray**, it provides a lot of supporting functions that make working with ndarray very easy.\n\nArrays are very frequently used in data science, where speed and resources are very important.\n\nNumPy is usually imported under the np alias.\n\nIt's usually fixed in size and each element is of the same type. We can cast a list to a numpy array by first importing `numpy`:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# import numpy library\n\nimport numpy as np ",
|
||
"metadata": {
|
||
"trusted": true
|
||
},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We then cast the list as follows:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Create a numpy array\n\na = np.array([0, 1, 2, 3, 4])\na",
|
||
"metadata": {
|
||
"trusted": true
|
||
},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Each element is of the same type, in this case integers:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%205/images/NumOneNp.png\" width=\"500\">\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "As with lists, we can access each element via a square bracket:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Print each element\n\nprint(\"a[0]:\", a[0])\nprint(\"a[1]:\", a[1])\nprint(\"a[2]:\", a[2])\nprint(\"a[3]:\", a[3])\nprint(\"a[4]:\", a[4])",
|
||
"metadata": {
|
||
"trusted": true
|
||
},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "#### Checking NumPy Version\n\nThe version string is stored under __version__ attribute.\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "print(np.__version__)",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Type\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "If we check the type of the array we get <b>numpy.ndarray</b>:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Check the type of the array\n\ntype(a)",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "As numpy arrays contain data of the same type, we can use the attribute \"dtype\" to obtain the data type of the array’s elements. In this case, it's a 64-bit integer:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Check the type of the values stored in numpy array\n\na.dtype",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Try it yourself\n\nCheck the type of the array and Value type for the given array **c**\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "b = np.array([3.1, 11.02, 6.2, 213.2, 5.2])\n\n# Enter your code here\n",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\ntype(b)\n \nb.dtype \n \nIf we examine the attribute dtype we see float 64, as the elements are not integers:\n```\n\n</details>\n\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Assign value\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We can change the value of the array. Consider the array <code>c</code>:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Create numpy array\n\nc = np.array([20, 1, 2, 3, 4])\nc",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We can change the first element of the array to 100 as follows:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Assign the first element to 100\n\nc[0] = 100\nc",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We can change the 5th element of the array to 0 as follows:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Assign the 5th element to 0\n\nc[4] = 0\nc",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Try it yourself\n\nAssign the value 20 for the second element in the given array.\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "a = np.array([10, 2, 30, 40,50])\n\n# Enter your code here\n",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\na[1]=20\na\n \n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Slicing\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Like lists, we can slice the numpy array. Slicing in python means taking the elements from the given index to another given index.\n\nWe pass slice like this: [start:end].The element at end index is not being included in the output.\n\nWe can select the elements from 1 to 3 and assign it to a new numpy array <code>d</code> as follows:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Slicing the numpy array\n\nd = c[1:4]\nd",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We can assign the corresponding indexes to new values as follows:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Set the fourth element and fifth element to 300 and 400\n\nc[3:5] = 300, 400\nc",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We can also define the steps in slicing, like this: [start:end:step].\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "arr = np.array([1, 2, 3, 4, 5, 6, 7])\n\nprint(arr[1:5:2])",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "If we don't pass start its considered 0\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "print(arr[:4])",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "If we don't pass end it considers till the length of array.\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "print(arr[4:])",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "If we don't pass step its considered 1\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "print(arr[1:5:])",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Try it yourself\n\nPrint the even elements in the given array.\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n\n# Enter your code here",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\nprint(arr[1:8:2])\n\n \n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Assign Value with List\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Similarly, we can use a list to select more than one specific index.\nThe list `select` contains several values:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Create the index list\n\nselect = [0, 2, 3, 4]\nselect",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We can use the list as an argument in the brackets. The output is the elements corresponding to the particular indexes:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Use List to select elements\n\nd = c[select]\nd",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We can assign the specified elements to a new value. For example, we can assign the values to 100 000 as follows:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Assign the specified elements to new value\n\nc[select] = 100000\nc",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Other Attributes\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Let's review some basic array attributes using the array <code>a</code>:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Create a numpy array\n\na = np.array([0, 1, 2, 3, 4])\na",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "The attribute <code>size</code> is the number of elements in the array:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Get the size of numpy array\n\na.size",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "The next two attributes will make more sense when we get to higher dimensions but let's review them. The attribute <code>ndim</code> represents the number of array dimensions, or the rank of the array. In this case, one:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Get the number of dimensions of numpy array\n\na.ndim",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "The attribute <code>shape</code> is a tuple of integers indicating the size of the array in each dimension:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Get the shape/size of numpy array\n\na.shape",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Try it yourself\n\nFind the size ,dimension and shape for the given array **b**\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "b = np.array([10, 20, 30, 40, 50, 60, 70])\n\n# Enter your code here",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\nb.size\n\nb.ndim\n\nb.shape\n\n \n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Numpy Statistical Functions\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Create a numpy array\n\na = np.array([1, -1, 1, -1])",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Get the mean of numpy array\n\nmean = a.mean()\nmean",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Get the standard deviation of numpy array\n\nstandard_deviation=a.std()\nstandard_deviation",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Create a numpy array\n\nb = np.array([-1, 2, 3, 4, 5])\nb",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Get the biggest value in the numpy array\n\nmax_b = b.max()\nmax_b",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Get the smallest value in the numpy array\n\nmin_b = b.min()\nmin_b",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Try it yourself\n\nFind the sum of maximum and minimum value in the given numpy array\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<hr>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "c = np.array([-10, 201, 43, 94, 502])\n\n# Enter your code here\n",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\nmax_c = c.max()\nmax_c\n \nmin_c = c.min()\nmin_c\n \n \nSum = (max_c +min_c)\nSum\n\n \n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "## Numpy Array Operations\n\nYou could use arithmetic operators directly between NumPy arrays\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Array Addition\n\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Consider the numpy array <code>u</code>:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "u = np.array([1, 0])\nu",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Consider the numpy array <code>v</code>:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "v = np.array([0, 1])\nv",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We can add the two arrays and assign it to z:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Numpy Array Addition\n\nz = np.add(u, v)\nz",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "The operation is equivalent to vector addition:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Plotting functions\n\n\nimport time \nimport sys\nimport numpy as np \n\nimport matplotlib.pyplot as plt\n\n\ndef Plotvec1(u, z, v):\n \n ax = plt.axes() # to generate the full window axes\n ax.arrow(0, 0, *u, head_width=0.05, color='r', head_length=0.1)# Add an arrow to the U Axes with arrow head width 0.05, color red and arrow head length 0.1\n plt.text(*(u + 0.1), 'u')#Adds the text u to the Axes \n \n ax.arrow(0, 0, *v, head_width=0.05, color='b', head_length=0.1)# Add an arrow to the v Axes with arrow head width 0.05, color red and arrow head length 0.1\n plt.text(*(v + 0.1), 'v')#Adds the text v to the Axes \n \n ax.arrow(0, 0, *z, head_width=0.05, head_length=0.1)\n plt.text(*(z + 0.1), 'z')#Adds the text z to the Axes \n plt.ylim(-2, 2)#set the ylim to bottom(-2), top(2)\n plt.xlim(-2, 2)#set the xlim to left(-2), right(2)\n\n \n",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Plot numpy arrays\n\nPlotvec1(u, z, v)",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Try it yourself\n\nPerform addition operation on the given numpy array arr1 and arr2:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "arr1 = np.array([10, 11, 12, 13, 14, 15])\narr2 = np.array([20, 21, 22, 23, 24, 25])\n\n# Enter your code here",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\narr3 = np.add(arr1, arr2)\narr3\n\n \n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Array Subtraction\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Consider the numpy array a:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "a = np.array([10, 20, 30])\na",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Consider the numpy array b:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "b = np.array([5, 10, 15])\nb",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We can subtract the two arrays and assign it to c:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "c = np.subtract(a, b)\n\nprint(c)",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Try it yourself\n\nPerform subtraction operation on the given numpy array arr1 and arr2:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "arr1 = np.array([10, 20, 30, 40, 50, 60])\narr2 = np.array([20, 21, 22, 23, 24, 25])\n\n# Enter your code here\n",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\narr3 = np.subtract(arr1, arr2)\narr3\n\n \n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Array Multiplication\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Consider the vector numpy array <code>y</code>:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Create a numpy array\n\nx = np.array([1, 2])\nx",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Create a numpy array\n\ny = np.array([2, 1])\ny",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We can multiply every element in the array by 2:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Numpy Array Multiplication\n\nz = np.multiply(x, y)\nz",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "This is equivalent to multiplying a vector by a scaler:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Try it yourself\n\nPerform multiply operation on the given numpy array arr1 and arr2:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "arr1 = np.array([10, 20, 30, 40, 50, 60])\narr2 = np.array([2, 1, 2, 3, 4, 5])\n\n# Enter your code here\n",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\narr3 = np.multiply(arr1, arr2)\narr3\n\n \n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Array Division\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Consider the vector numpy array a:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "a = np.array([10, 20, 30])\na",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Consider the vector numpy array b:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "b = np.array([2, 10, 5])\nb",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We can divide the two arrays and assign it to c:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "c = np.divide(a, b)\nc",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Try it yourself\n\nPerform division operation on the given numpy array arr1 and arr2:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "arr1 = np.array([10, 20, 30, 40, 50, 60])\narr2 = np.array([3, 5, 10, 8, 2, 33])\n\n# Enter your code here",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\narr3 = np.divide(arr1, arr2)\narr3\n\n \n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Dot Product\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "The dot product of the two numpy arrays <code>u</code> and <code>v</code> is given by:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "X = np.array([1, 2])\nY = np.array([3, 2])",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Calculate the dot product\n\nnp.dot(X, Y)",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "#Elements of X\nprint(X[0])\nprint(X[1])",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "#Elements of Y\nprint(Y[0])\nprint(Y[1])",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We are performing the dot product which is shown as below\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%205/images/dot1.png\">\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Try it yourself\n\nPerform dot operation on the given numpy array ar1 and ar2:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "arr1 = np.array([3, 5])\narr2 = np.array([2, 4])\n\n# Enter your code here\n",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\narr3 = np.dot(arr1, arr2)\narr3\n\n \n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Adding Constant to a Numpy Array\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Consider the following array:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Create a constant to numpy array\n\nu = np.array([1, 2, 3, -1]) \nu",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Adding the constant 1 to each element in the array:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Add the constant to array\n\nu + 1",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "The process is summarised in the following animation:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%205/images/NumOneAdd.gif\" width=\"500\">\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Try it yourself\n\nAdd Constant 5 to the given numpy array ar:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "arr = np.array([1, 2, 3, -1]) \n\n# Enter your code here\n",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\narr + 5\n\n \n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<hr>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "## Mathematical Functions\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We can access the value of <code>pi</code> in numpy as follows :\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# The value of pi\n\nnp.pi",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We can create the following numpy array in Radians:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Create the numpy array in radians\n\nx = np.array([0, np.pi/2 , np.pi])",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We can apply the function <code>sin</code> to the array <code>x</code> and assign the values to the array <code>y</code>; this applies the sine function to each element in the array:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Calculate the sin of each elements\n\ny = np.sin(x)\ny",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<hr>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "## Linspace\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "A useful function for plotting mathematical functions is <code>linspace</code>. Linspace returns evenly spaced numbers over a specified interval. \n\n **numpy.linspace(start, stop, num = int value)**\n \nstart : start of interval range\n\nstop : end of interval range\n\nnum : Number of samples to generate.\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Makeup a numpy array within [-2, 2] and 5 elements\n\nnp.linspace(-2, 2, num=5)",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "If we change the parameter <code>num</code> to 9, we get 9 evenly spaced numbers over the interval from -2 to 2:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Make a numpy array within [-2, 2] and 9 elements\n\nnp.linspace(-2, 2, num=9)",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We can use the function <code>linspace</code> to generate 100 evenly spaced samples from the interval 0 to 2π:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Make a numpy array within [0, 2π] and 100 elements \n\nx = np.linspace(0, 2*np.pi, num=100)",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "We can apply the sine function to each element in the array <code>x</code> and assign it to the array <code>y</code>:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Calculate the sine of x list\n\ny = np.sin(x)",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Plot the result\n\nplt.plot(x, y)",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Try it yourself\n\n Make a numpy array within [5, 4] and 6 elements\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Enter your code here\n",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\nnp.linspace(5, 4, num=6)\n\n \n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<hr>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "### Iterating 1-D Arrays\n\nIterating means going through elements one by one.\n\nIf we iterate on a 1-D array it will go through each element one by one.\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "If we execute the numpy array, we get in the array format\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "arr1 = np.array([1, 2, 3])\nprint(arr1)",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "But if you want to result in the form of the list, then you can use for loop:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "for x in arr1:\n print(x)",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "## Quiz on 1D Numpy Array\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Implement the following vector subtraction in numpy: u-v\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Write your code below and press Shift+Enter to execute\n\nu = np.array([1, 0])\nv = np.array([0, 1])",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\nu - v\n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<hr>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Multiply the numpy array z with -2:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Write your code below and press Shift+Enter to execute\n\nz = np.array([2, 4])",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\n-2 * z\n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<hr>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Consider the list <code>\\[1, 2, 3, 4, 5]</code> and <code>\\[1, 0, 1, 0, 1]</code>. Cast both lists to a numpy array then multiply them together:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Write your code below and press Shift+Enter to execute\n",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\na = np.array([1, 2, 3, 4, 5])\nb = np.array([1, 0, 1, 0, 1])\na * b\n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<hr>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Import the libraries\n\nimport time \nimport sys\nimport numpy as np \n\nimport matplotlib.pyplot as plt\n\n\ndef Plotvec2(a,b):\n ax = plt.axes()# to generate the full window axes\n ax.arrow(0, 0, *a, head_width=0.05, color ='r', head_length=0.1)#Add an arrow to the a Axes with arrow head width 0.05, color red and arrow head length 0.1\n plt.text(*(a + 0.1), 'a')\n ax.arrow(0, 0, *b, head_width=0.05, color ='b', head_length=0.1)#Add an arrow to the b Axes with arrow head width 0.05, color blue and arrow head length 0.1\n plt.text(*(b + 0.1), 'b')\n plt.ylim(-2, 2)#set the ylim to bottom(-2), top(2)\n plt.xlim(-2, 2)#set the xlim to left(-2), right(2)\n \n \n",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Convert the list <code>\\[-1, 1]</code> and <code>\\[1, 1]</code> to numpy arrays <code>a</code> and <code>b</code>. Then, plot the arrays as vectors using the fuction <code>Plotvec2</code> and find their dot product:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Write your code below and press Shift+Enter to execute\n",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\n\n\na = np.array([-1, 1])\nb = np.array([1, 1])\nPlotvec2(a, b)\nprint(\"The dot product is\", np.dot(a,b))\n\n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<hr>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Convert the list <code>\\[1, 0]</code> and <code>\\[0, 1]</code> to numpy arrays <code>a</code> and <code>b</code>. Then, plot the arrays as vectors using the function <code>Plotvec2</code> and find their dot product:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Write your code below and press Shift+Enter to execute\n",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\na = np.array([1, 0])\nb = np.array([0, 1])\nPlotvec2(a, b)\nprint(\"The dot product is\", np.dot(a, b))\n\n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<hr>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Convert the list <code>\\[1, 1]</code> and <code>\\[0, 1]</code> to numpy arrays <code>a</code> and <code>b</code>. Then plot the arrays as vectors using the fuction <code>Plotvec2</code> and find their dot product:\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Write your code below and press Shift+Enter to execute\n",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\na = np.array([1, 1])\nb = np.array([0, 1])\nPlotvec2(a, b)\nprint(\"The dot product is\", np.dot(a, b))\n\n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<hr>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Why are the results of the dot product for <code>\\[-1, 1]</code> and <code>\\[1, 1]</code> and the dot product for <code>\\[1, 0]</code> and <code>\\[0, 1]</code> zero, but not zero for the dot product for <code>\\[1, 1]</code> and <code>\\[0, 1]</code>? <p><i>Hint: Study the corresponding figures, pay attention to the direction the arrows are pointing to.</i></p>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Write your code below and press Shift+Enter to execute\n",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\nThe vectors used for question 4 and 5 are perpendicular. As a result, the dot product is zero. \n\n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Convert the list <code>\\[1, 2, 3]</code> and <code>\\[8, 9, 10]</code> to numpy arrays <code>arr1</code> and <code>arr2</code>. Then perform <code>Addition</code> , <code>Subtraction</code> , <code>Multiplication</code> , <code>Division</code> and <code>Dot Operation</code> on the <code>arr1</code> and <code>arr2</code>.\n\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Write your code below and press Shift+Enter to execute\n",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\narr1 = np.array([1, 2, 3])\narr2 = np.array([8, 9, 10])\n\narr3 = np.add(arr1, arr2)\narr3\n\narr4 = np.subtract(arr1, arr2)\narr4\n\narr5 = np.multiply(arr1, arr2)\narr5\n\n\narr6 = np.divide(arr1, arr2)\narr6\n\narr7 = np.dot(arr1, arr2)\narr7\n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "Convert the list <code>\\[1, 2, 3, 4, 5]</code> and <code>\\[6, 7, 8, 9, 10]</code> to numpy arrays <code>arr1</code> and <code>arr2</code>. Then find the even and odd numbers from <code>arr1</code> and <code>arr2</code>.\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "# Write your code below and press Shift+Enter to execute\n\n",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<details><summary>Click here for the solution</summary>\n\n```python\n\n\narr1 = np.array([1, 2, 3, 4, 5])\narr2 = np.array([6, 7, 8, 9, 10])\n\n#Starting index in slice is 1 as first even element(2) in array1 is at index 1\neven_arr1 = arr1[1:5:2]\nprint(\"even for array1\",even_arr1)\n \n#Starting index in slice is 0 as first odd element(1) in array1 is at index 0\nodd_arr1=arr1[0:5:2]\nprint(\"odd for array1\",odd_arr1)\n\n#Starting index in slice is 0 as first even element(6) in array2 is at index 0\neven_arr2 = arr2[0:5:2]\nprint(\"even for array2\",even_arr2)\n \n \n#Starting index in slice is 1 as first odd element(7) in array2 is at index 1\nodd_arr2=arr2[1:5:2]\nprint(\"odd for array2\",odd_arr2)\n\n```\n\n</details>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "<hr>\n<h2>The last exercise!</h2>\n<p>Congratulations, you have completed your first lesson and hands-on lab in Python. \n<hr>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "## Author\n\n<a href=\"https://www.linkedin.com/in/joseph-s-50398b136/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01\" target=\"_blank\">Joseph Santarcangelo</a>\n\n## Other contributors\n\n<a href=\"https://www.linkedin.com/in/jiahui-mavis-zhou-a4537814a?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01\">Mavis Zhou</a>\n\n## Change Log\n\n| Date (YYYY-MM-DD) | Version | Changed By | Change Description |\n| ----------------- | ------- | ---------- | ---------------------------------- |\n| 2023-11-02 | 2.3 | Abhishek Gagneja | Instructions updated |\n| 2022-03-08 | 2.2 | Niveditha | Modified and added practice problem|\n| 2022-01-10 | 2.1 | Malika | Removed the readme for GitShare |\n| 2020-08-26 | 2.0 | Lavanya | Moved lab to course repo in GitLab |\n| | | | |\n| | | | |\n\n<hr/>\n\n## <h3 align=\"center\"> © IBM Corporation 2023. All rights reserved. <h3/>\n",
|
||
"metadata": {}
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"execution_count": null
|
||
}
|
||
]
|
||
} |