Skip to content

Instantly share code, notes, and snippets.

@mwy912
Created October 19, 2020 10:51
Show Gist options
  • Save mwy912/30b2e9b177f646c92193e3462e1e2900 to your computer and use it in GitHub Desktop.
Save mwy912/30b2e9b177f646c92193e3462e1e2900 to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<center>\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/Logos/organization_logo/organization_logo.png\" width=\"300\" alt=\"cognitiveclass.ai logo\" />\n",
"</center>\n",
"\n",
"# 2D Numpy in Python\n",
"\n",
"Estimated time needed: **20** minutes\n",
"\n",
"## Objectives\n",
"\n",
"After completing this lab you will be able to:\n",
"\n",
"- Operate comfortably with `numpy`\n",
"- Perform complex operations with `numpy`\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Table of Contents</h2>\n",
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <ul>\n",
" <li><a href=\"create\">Create a 2D Numpy Array</a></li>\n",
" <li><a href=\"access\">Accessing different elements of a Numpy Array</a></li>\n",
" <li><a href=\"op\">Basic Operations</a></li>\n",
" </ul>\n",
" \n",
"</div>\n",
"\n",
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"create\">Create a 2D Numpy Array</h2>\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Import the libraries\n",
"\n",
"import numpy as np \n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider the list <code>a</code>, the list contains three nested lists **each of equal size**. \n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[11, 12, 13], [21, 22, 23], [31, 32, 33]]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a list\n",
"\n",
"a = [[11, 12, 13], [21, 22, 23], [31, 32, 33]]\n",
"a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can cast the list to a Numpy Array as follow\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[11, 12, 13],\n",
" [21, 22, 23],\n",
" [31, 32, 33]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Convert list to Numpy Array\n",
"# Every element is the same type\n",
"\n",
"A = np.array(a)\n",
"A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can use the attribute <code>ndim</code> to obtain the number of axes or dimensions referred to as the rank. \n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Show the numpy array dimensions\n",
"\n",
"A.ndim"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Attribute <code>shape</code> returns a tuple corresponding to the size or number of each dimension.\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 3)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Show the numpy array shape\n",
"\n",
"A.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The total number of elements in the array is given by the attribute <code>size</code>.\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Show the numpy array size\n",
"\n",
"A.size"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"access\">Accessing different elements of a Numpy Array</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can use rectangular brackets to access the different elements of the array. The correspondence between the rectangular brackets and the list and the rectangular representation is shown in the following figure for a 3x3 array: \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%205/Images/NumTwoEg.png\" width=\"500\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can access the 2nd-row 3rd column as shown in the following figure:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%205/Images/NumTwoFT.png\" width=\"400\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We simply use the square brackets and the indices corresponding to the element we would like:\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"23"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Access the element on the second row and third column\n",
"\n",
"A[1, 2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can also use the following notation to obtain the elements: \n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"23"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Access the element on the second row and third column\n",
"\n",
"A[1][2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Consider the elements shown in the following figure \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%205/Images/NumTwoFF.png\" width=\"400\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can access the element as follows \n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Access the element on the first row and first column\n",
"\n",
"A[0][0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also use slicing in numpy arrays. Consider the following figure. We would like to obtain the first two columns in the first row\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%205/Images/NumTwoFSF.png\" width=\"400\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" This can be done with the following syntax \n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([11, 12])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Access the element on the first row and first and second columns\n",
"\n",
"A[0][0:2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly, we can obtain the first two rows of the 3rd column as follows:\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([13, 23])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Access the element on the first and second rows and third column\n",
"\n",
"A[0:2, 2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Corresponding to the following figure: \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%205/images/2D_numpy.png\" width=\"550\"><br />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"op\">Basic Operations</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also add arrays. The process is identical to matrix addition. Matrix addition of <code>X</code> and <code>Y</code> is shown in the following figure:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%205/Images/NumTwoAdd.png\" width=\"500\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The numpy array is given by <code>X</code> and <code>Y</code>\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 0],\n",
" [0, 1]])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a numpy array X\n",
"\n",
"X = np.array([[1, 0], [0, 1]]) \n",
"X"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 1],\n",
" [1, 2]])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a numpy array Y\n",
"\n",
"Y = np.array([[2, 1], [1, 2]]) \n",
"Y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can add the numpy arrays as follows.\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[3, 1],\n",
" [1, 3]])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Add X and Y\n",
"\n",
"Z = X + Y\n",
"Z"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Multiplying a numpy array by a scaler is identical to multiplying a matrix by a scaler. If we multiply the matrix <code>Y</code> by the scaler 2, we simply multiply every element in the matrix by 2 as shown in the figure.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%205/Images/NumTwoDb.png\" width=\"500\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can perform the same operation in numpy as follows \n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 1],\n",
" [1, 2]])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a numpy array Y\n",
"\n",
"Y = np.array([[2, 1], [1, 2]]) \n",
"Y"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[4, 2],\n",
" [2, 4]])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Multiply Y with 2\n",
"\n",
"Z = 2 * Y\n",
"Z"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Multiplication of two arrays corresponds to an element-wise product or Hadamard product. Consider matrix <code>X</code> and <code>Y</code>. The Hadamard product corresponds to multiplying each of the elements in the same position, i.e. multiplying elements contained in the same color boxes together. The result is a new matrix that is the same size as matrix <code>Y</code> or <code>X</code>, as shown in the following figure.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%205/Images/NumTwoMul.png\" width=\"500\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can perform element-wise product of the array <code>X</code> and <code>Y</code> as follows:\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 1],\n",
" [1, 2]])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a numpy array Y\n",
"\n",
"Y = np.array([[2, 1], [1, 2]]) \n",
"Y"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 0],\n",
" [0, 1]])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a numpy array X\n",
"\n",
"X = np.array([[1, 0], [0, 1]]) \n",
"X"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 0],\n",
" [0, 2]])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Multiply X with Y\n",
"\n",
"Z = X * Y\n",
"Z"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also perform matrix multiplication with the numpy arrays <code>A</code> and <code>B</code> as follows:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, we define matrix <code>A</code> and <code>B</code>:\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 1, 1],\n",
" [1, 0, 1]])"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a matrix A\n",
"\n",
"A = np.array([[0, 1, 1], [1, 0, 1]])\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 1],\n",
" [ 1, 1],\n",
" [-1, 1]])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a matrix B\n",
"\n",
"B = np.array([[1, 1], [1, 1], [-1, 1]])\n",
"B"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We use the numpy function <code>dot</code> to multiply the arrays together.\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 2],\n",
" [0, 2]])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Calculate the dot product\n",
"\n",
"Z = np.dot(A,B)\n",
"Z"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0. , 0.90929743],\n",
" [0. , 0.90929743]])"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Calculate the sine of Z\n",
"\n",
"np.sin(Z)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We use the numpy attribute <code>T</code> to calculate the transposed matrix\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 1],\n",
" [2, 2],\n",
" [3, 3]])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a matrix C\n",
"\n",
"C = np.array([[1,1],[2,2],[3,3]])\n",
"C"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [1, 2, 3]])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Get the transposed of C\n",
"\n",
"C.T"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"<h2>The last exercise!</h2>\n",
"<p>Congratulations, you have completed your first lesson and hands-on lab in Python. However, there is one more thing you need to do. The Data Science community encourages sharing work. The best way to share and showcase your work is to share it on GitHub. By sharing your notebook on GitHub you are not only building your reputation with fellow data scientists, but you can also show it off when applying for a job. Even though this was your first piece of work, it is never too early to start building good habits. So, please read and follow <a href=\"https://cognitiveclass.ai/blog/data-scientists-stand-out-by-sharing-your-notebooks/\" target=\"_blank\">this article</a> to learn how to share your work.\n",
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Author\n",
"\n",
"<a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\" target=\"_blank\">Joseph Santarcangelo</a>\n",
"\n",
"## Other contributors\n",
"\n",
"<a href=\"www.linkedin.com/in/jiahui-mavis-zhou-a4537814a\">Mavis Zhou</a>\n",
"\n",
"## Change Log\n",
"\n",
"| Date (YYYY-MM-DD) | Version | Changed By | Change Description |\n",
"| ----------------- | ------- | ------------- | ----------------------------------------------------------- |\n",
"| 2020-09-09 | 2.1 | Malika Singla | Updated the screenshot for first two rows of the 3rd column |\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 2020. All rights reserved. <h3/>\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"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.6.11"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment