Skip to content

Instantly share code, notes, and snippets.

@matthewchung74
Created November 18, 2019 00:33
Show Gist options
  • Save matthewchung74/5fd65b0ba89a0266d0bb0bfe6450e41a to your computer and use it in GitHub Desktop.
Save matthewchung74/5fd65b0ba89a0266d0bb0bfe6450e41a to your computer and use it in GitHub Desktop.
test
{
nbformat: 4,
nbformat_minor: 0,
metadata: {
colab: {
name: "01-numpy-tutorial.ipynb",
provenance: [ ],
collapsed_sections: [ ],
include_colab_link: true
},
kernelspec: {
name: "python3",
display_name: "Python 3"
}
},
cells: [
{
cell_type: "markdown",
metadata: {
id: "view-in-github",
colab_type: "text"
},
source: [
"<a href="https://colab.research.google.com/github/foobar8675/numpy-tutorials/blob/master/01_numpy_tutorial.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>"
]
},
{
cell_type: "markdown",
metadata: {
id: "LJWL_Bbzs92k",
colab_type: "text"
},
source: [
"# A practical introduction to Numpy, Matplotlib and Pandas - Part 1 ",
" ",
"![alt text](https://miro.medium.com/max/950/1*00pL0zLnfI7y8d5G1aQrHA.jpeg) ",
" ",
"This tutorial is designed to introduce how to do operations in numpy in a practical way. It primarily uses images an an example since they are easy to visualize, but applies to all data in `numpy` as well as your future data engineering and machine learning endeavors! ",
" ",
"A few prerequisites: ",
" ",
" ",
"* An understanding of [RGB](https://en.wikipedia.org/wiki/RGB_color_model). ",
"* Python programming experience. ",
"* [jupyter notebooks](https://jupyter.org/) experience."
]
},
{
cell_type: "markdown",
metadata: {
id: "2afQUDp5s8rL",
colab_type: "text"
},
source: [
"Now that that's out of the way, let's introduce the designer ... ",
" ",
"![alt text](https://www.mediabistro.com/wp-content/uploads/2016/04/what-does-a-graphic-designer-do_860.jpg) ",
" ",
"For this exercise, imagine your designer came to you and said in her menacing voice, "I have a prototyped a photoshop file file where I add a background, border, and color manipulation. Now I have 30,000 images I need to apply this to and it also needs to happen every time a user uploads an image to the website" ",
" ",
"Wow, that's a lot. But we can take it step by step and learn how to use `numpy` operations along the way! ",
" ",
"We're going start by creating a black 2x2 1 byte RGB image, which in other words is a 2x2x3 matrix, keeping in mind - ",
" ",
" ",
"* there will be 2 rows and 2 columns to this matrix (2x2) ",
"* there will be 3 channels RGB for this matrix (3) ",
"* black in RGB has a value of 0"
]
},
{
cell_type: "code",
metadata: {
id: "beXud7vCCrnN",
colab_type: "code",
colab: { }
},
source: [
"import numpy as np"
],
execution_count: 0,
outputs: [ ]
},
{
cell_type: "code",
metadata: {
id: "7P1LOCj4pmDO",
colab_type: "code",
outputId: "52b49a36-33ea-4256-db42-bb70cba073e1",
colab: {
base_uri: "https://localhost:8080/",
height: 102
}
},
source: [
"img = np.zeros([2,2,3],dtype=np.uint8) ",
"img"
],
execution_count: 0,
outputs: [
{
output_type: "execute_result",
data: {
text/plain: [
"array([[[0, 0, 0], ",
" [0, 0, 0]], ",
" ",
" [[0, 0, 0], ",
" [0, 0, 0]]], dtype=uint8)"
]
},
metadata: {
tags: [ ]
},
execution_count: 27
}
]
},
{
cell_type: "markdown",
metadata: {
id: "zSHEwwfq0ntV",
colab_type: "text"
},
source: [
"And let's take a look at this 2x2 black image using `matplotlib`. For now, don't worry about the specifics of `matplotlib`, we'll dive deeper later on. Just know we can use it to view images and graphs."
]
},
{
cell_type: "code",
metadata: {
id: "quMM-7cWzljA",
colab_type: "code",
colab: { }
},
source: [
"%matplotlib inline ",
"import matplotlib.pyplot as plt ",
"import matplotlib.image as mpimg"
],
execution_count: 0,
outputs: [ ]
},
{
cell_type: "code",
metadata: {
id: "63OD4nSYztFQ",
colab_type: "code",
outputId: "b83b4bea-55a3-4c6a-b4f4-02f0e82faa48",
colab: {
base_uri: "https://localhost:8080/",
height: 248
}
},
source: [
"plt.axis('off') ",
"imgplot = plt.imshow(img)"
],
execution_count: 0,
outputs: [
{
output_type: "display_data",
data: {
image/png: "iVBORw0KGgoAAAANSUhEUgAAAOcAAADnCAYAAADl9EEgAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz AAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0 dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAACy0lEQVR4nO3YMQoDMQwAwdOR/39Z+YBJF7zFTCk3 ahaBZ3cfoOe9vQBwJk6IEidEiROixAlRn1+PM+MrF/5sd+c0dzkhSpwQJU6IEidEiROixAlR4oQo cUKUOCFKnBAlTogSJ0SJE6LECVHihChxQpQ4IUqcECVOiBInRIkTosQJUeKEKHFClDghSpwQJU6I EidEiROixAlR4oQocUKUOCFKnBAlTogSJ0SJE6LECVHihChxQpQ4IUqcECVOiBInRIkTosQJUeKE KHFClDghSpwQJU6IEidEiROixAlR4oQocUKUOCFKnBAlTogSJ0SJE6LECVHihChxQpQ4IUqcECVO iBInRIkTosQJUeKEKHFClDghSpwQJU6IEidEiROixAlR4oQocUKUOCFKnBAlTogSJ0SJE6LECVHi hChxQpQ4IUqcECVOiBInRIkTosQJUeKEKHFClDghSpwQJU6IEidEiROixAlR4oQocUKUOCFKnBAl TogSJ0SJE6LECVHihChxQpQ4IUqcECVOiBInRIkTosQJUeKEKHFClDghSpwQJU6IEidEiROixAlR 4oQocUKUOCFKnBAlTogSJ0SJE6LECVHihChxQpQ4IUqcECVOiBInRIkTosQJUeKEKHFClDghSpwQ JU6IEidEiROixAlR4oQocUKUOCFKnBAlTogSJ0SJE6LECVHihChxQpQ4IUqcECVOiBInRIkTosQJ UeKEKHFClDghSpwQJU6IEidEiROixAlR4oQocUKUOCFKnBAlTogSJ0SJE6LECVHihChxQpQ4IUqc ECVOiBInRIkTosQJUeKEKHFClDghSpwQJU6IEidEiROixAlR4oQocUKUOCFKnBAlTogSJ0SJE6LE CVHihChxQpQ4IUqcECVOiBInRIkTosQJUeKEKHFClDghanb39g7AgcsJUeKEKHFClDghSpwQJU6I +gI34gvJPjrjHQAAAABJRU5ErkJggg== ",
text/plain: [
"<Figure size 432x288 with 1 Axes>"
]
},
metadata: {
tags: [ ]
}
}
]
},
{
cell_type: "markdown",
metadata: {
id: "9lk3cB8X0491",
colab_type: "text"
},
source: [
"Now in photoshop, the first thing our designer wants is to start with a solid red color. If we revisit the shape of our image, we see we have 3 2x2 images, where each of the three images represents one channel."
]
},
{
cell_type: "code",
metadata: {
id: "QBapW6CoFF-l",
colab_type: "code",
outputId: "863544da-7931-4967-bb82-e0f701a27e75",
colab: {
base_uri: "https://localhost:8080/",
height: 34
}
},
source: [
"img.shape"
],
execution_count: 0,
outputs: [
{
output_type: "execute_result",
data: {
text/plain: [
"(2, 2, 3)"
]
},
metadata: {
tags: [ ]
},
execution_count: 30
}
]
},
{
cell_type: "markdown",
metadata: {
id: "nAhuwy9XFKCf",
colab_type: "text"
},
source: [
"So if we want a red image, we should be able to change the first channel to `255` while leaving the others `0`. Let's try it out."
]
},
{
cell_type: "code",
metadata: {
id: "OLLKLWNGxkcn",
colab_type: "code",
outputId: "de162fae-6768-47b1-8bfa-1fa9cd8565e5",
colab: {
base_uri: "https://localhost:8080/",
height: 248
}
},
source: [
"img[:,:,0] = 255 ",
"plt.axis('off') ",
"imgplot = plt.imshow(img)"
],
execution_count: 0,
outputs: [
{
output_type: "display_data",
data: {
image/png: "iVBORw0KGgoAAAANSUhEUgAAAOcAAADnCAYAAADl9EEgAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz AAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0 dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAACyklEQVR4nO3YMQoDMQwAwdOR/39Z+YBJF7zFTCk3 ahaBZ3cfoOe9vQBwJk6IEidEiROixAlRn5+vM75y4d925zR2OSFKnBAlTogSJ0SJE6LECVHihChx QpQ4IUqcECVOiBInRIkTosQJUeKEKHFClDghSpwQJU6IEidEiROixAlR4oQocUKUOCFKnBAlTogS J0SJE6LECVHihChxQpQ4IUqcECVOiBInRIkTosQJUeKEKHFClDghSpwQJU6IEidEiROixAlR4oQo cUKUOCFKnBAlTogSJ0SJE6LECVHihChxQpQ4IUqcECVOiBInRIkTosQJUeKEKHFClDghSpwQJU6I EidEiROixAlR4oQocUKUOCFKnBAlTogSJ0SJE6LECVHihChxQpQ4IUqcECVOiBInRIkTosQJUeKE KHFClDghSpwQJU6IEidEiROixAlR4oQocUKUOCFKnBAlTogSJ0SJE6LECVHihChxQpQ4IUqcECVO iBInRIkTosQJUeKEKHFClDghSpwQJU6IEidEiROixAlR4oQocUKUOCFKnBAlTogSJ0SJE6LECVHi hChxQpQ4IUqcECVOiBInRIkTosQJUeKEKHFClDghSpwQJU6IEidEiROixAlR4oQocUKUOCFKnBAl TogSJ0SJE6LECVHihChxQpQ4IUqcECVOiBInRIkTosQJUeKEKHFClDghSpwQJU6IEidEiROixAlR 4oQocUKUOCFKnBAlTogSJ0SJE6LECVHihChxQpQ4IUqcECVOiBInRIkTosQJUeKEKHFClDghSpwQ JU6IEidEiROixAlR4oQocUKUOCFKnBAlTogSJ0SJE6LECVHihChxQpQ4IUqcECVOiBInRIkTosQJ UeKEKHFClDghSpwQJU6IEidEiROixAlR4oQocUKUOCFqdvf2DsCBywlR4oQocUKUOCFKnBAlToj6 Ao0ECskCjd1oAAAAAElFTkSuQmCC ",
text/plain: [
"<Figure size 432x288 with 1 Axes>"
]
},
metadata: {
tags: [ ]
}
}
]
},
{
cell_type: "markdown",
metadata: {
id: "rPyvcjri1ygb",
colab_type: "text"
},
source: [
"Oh, but our designer just came to us and said she wanted to start with a black image instead. Well, no problem, we could make a white image by changing all the channels to `255` "
]
},
{
cell_type: "code",
metadata: {
id: "Q_Pn1BIk1vlz",
colab_type: "code",
outputId: "6829d81c-5f64-4fb6-e199-3d07ea67528b",
colab: {
base_uri: "https://localhost:8080/",
height: 248
}
},
source: [
"img[:,:,:] = 0 ",
"plt.axis('off') ",
"imgplot = plt.imshow(img)"
],
execution_count: 0,
outputs: [
{
output_type: "display_data",
data: {
image/png: "iVBORw0KGgoAAAANSUhEUgAAAOcAAADnCAYAAADl9EEgAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz AAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0 dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAACy0lEQVR4nO3YMQoDMQwAwdOR/39Z+YBJF7zFTCk3 ahaBZ3cfoOe9vQBwJk6IEidEiROixAlRn1+PM+MrF/5sd+c0dzkhSpwQJU6IEidEiROixAlR4oQo cUKUOCFKnBAlTogSJ0SJE6LECVHihChxQpQ4IUqcECVOiBInRIkTosQJUeKEKHFClDghSpwQJU6I EidEiROixAlR4oQocUKUOCFKnBAlTogSJ0SJE6LECVHihChxQpQ4IUqcECVOiBInRIkTosQJUeKE KHFClDghSpwQJU6IEidEiROixAlR4oQocUKUOCFKnBAlTogSJ0SJE6LECVHihChxQpQ4IUqcECVO iBInRIkTosQJUeKEKHFClDghSpwQJU6IEidEiROixAlR4oQocUKUOCFKnBAlTogSJ0SJE6LECVHi hChxQpQ4IUqcECVOiBInRIkTosQJUeKEKHFClDghSpwQJU6IEidEiROixAlR4oQocUKUOCFKnBAl TogSJ0SJE6LECVHihChxQpQ4IUqcECVOiBInRIkTosQJUeKEKHFClDghSpwQJU6IEidEiROixAlR 4oQocUKUOCFKnBAlTogSJ0SJE6LECVHihChxQpQ4IUqcECVOiBInRIkTosQJUeKEKHFClDghSpwQ JU6IEidEiROixAlR4oQocUKUOCFKnBAlTogSJ0SJE6LECVHihChxQpQ4IUqcECVOiBInRIkTosQJ UeKEKHFClDghSpwQJU6IEidEiROixAlR4oQocUKUOCFKnBAlTogSJ0SJE6LECVHihChxQpQ4IUqc ECVOiBInRIkTosQJUeKEKHFClDghSpwQJU6IEidEiROixAlR4oQocUKUOCFKnBAlTogSJ0SJE6LE CVHihChxQpQ4IUqcECVOiBInRIkTosQJUeKEKHFClDghanb39g7AgcsJUeKEKHFClDghSpwQJU6I +gI34gvJPjrjHQAAAABJRU5ErkJggg== ",
text/plain: [
"<Figure size 432x288 with 1 Axes>"
]
},
metadata: {
tags: [ ]
}
}
]
},
{
cell_type: "markdown",
metadata: {
id: "m-XAJQna1nzo",
colab_type: "text"
},
source: [
"But now let's say she changes her mind, as designers often do. She says she wants an addition on the red channel of `100`. How do we do that? ",
" ",
"We can tell `numpy` to subtract a value, `100` on each element of the matrix, or each pixel on the channel of the image with `np.add`"
]
},
{
cell_type: "code",
metadata: {
id: "l8nAWIvWpuPM",
colab_type: "code",
outputId: "7c13c05e-3fbe-4db3-e3fa-5bae8db1e8aa",
colab: {
base_uri: "https://localhost:8080/",
height: 102
}
},
source: [
"red_channel_values = np.add(img[:,:, 0], 100) ",
"img[:,:,0] = red_channel_values ",
"img"
],
execution_count: 0,
outputs: [
{
output_type: "execute_result",
data: {
text/plain: [
"array([[[100, 0, 0], ",
" [100, 0, 0]], ",
" ",
" [[100, 0, 0], ",
" [100, 0, 0]]], dtype=uint8)"
]
},
metadata: {
tags: [ ]
},
execution_count: 33
}
]
},
{
cell_type: "code",
metadata: {
id: "oHhWH-egpyqD",
colab_type: "code",
outputId: "568aea24-9581-4bd0-e456-c843e5b6036a",
colab: {
base_uri: "https://localhost:8080/",
height: 269
}
},
source: [
"imgplot = plt.imshow(img)"
],
execution_count: 0,
outputs: [
{
output_type: "display_data",
data: {
image/png: "iVBORw0KGgoAAAANSUhEUgAAARUAAAD8CAYAAABZ0jAcAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz AAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0 dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAPAklEQVR4nO3df6zddX3H8edrIDQZmxTqoEFXIBIV M1e0wR8YxYmA/FFMJLNmm2XBMN3YkhkXMSS64Jah+4PFTKc36ESzAZNNrZvMVSpxiRatG1CpgxZc JlcURhFHQFzxvT/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S3ATeOeu9F5XQr85bhnXcG5vQp4MfCtBbZfBNwCBHgZcPu4Z250XucC/7jc552KVyoMfnX/+m75 euANY5xlWEv5akL/fG8GXnvwI/kJtmq/clGH/xrKxcAna2AncHyS9aOZbuWWcF4rMi2hclJVPdgt fx84aYH91iTZlWRnkkkNnvm+mnDKQvtU1QHgMeDEkUy3cks5L4A3dm8Rbk7ynHm2T6Olnvs0enmS O5PckuSFSzlglN/9WVSSLwEnz7Ppqv6DqqokC30OvqGqZpOcDuxIsruq7ms9q1bs88ANVfVUkt9h 8Grs18Y8kxa2oq/XTEyoVNV5C21L8oMk66vqwe5l5UMLPMdsd39/ktuAsxi8z58kS/lqwsF9Hkhy NPBMBr+BPMkOe15V1T+H6xhcK1sNVuXXTarqR73lLyT5cJJ1VbXoFyin5e3PNmBrt7wV+NzcHZKs TXJst7wOOAfYM7IJl24pX03on+8lwI7qrpxNsMOe15zrDJuBb49wviNpG/CW7lOglwGP9d6uT61F vl6zuHFfgV7iVeoTgVuBvcCXgBO69ZuA67rlVwC7GXzqsBu4bNxzL3I+FwH3MngVdVW37mpgc7e8 Bvg0sA/4OnD6uGdudF5/Btzd/Rt9GXj+uGde4nndADwI/C+D6yWXAW8D3tZtD4M/NnZf939v07hn bnReV/T+vXYCr1jK8/pr+pKampa3P5KmhKEiqSlDRVJThoqkpgwVSU0ZKpKaMlQkNfV/D6Z05wve MMgAAAAASUVORK5CYII= ",
text/plain: [
"<Figure size 432x288 with 1 Axes>"
]
},
metadata: {
tags: [ ]
}
}
]
},
{
cell_type: "markdown",
metadata: {
id: "R_r9mcE-3pCi",
colab_type: "text"
},
source: [
"Now let's say she now wants the top half of the image to have an addition of 100 to the red channel. We can access the top row of the image by looking at the dimensions from before `[2,2,3]` and thinking of it in terms of `[rows, columns, channels]` So if we want to change the top row of the image, we need to access row `0` (top) which will leave row `1` (bottom) untouched."
]
},
{
cell_type: "code",
metadata: {
id: "Pt41-F8dp4aD",
colab_type: "code",
outputId: "dbcbbc25-65b1-4d32-b64b-470e4f67156f",
colab: {
base_uri: "https://localhost:8080/",
height: 102
}
},
source: [
"img[0,:,0] = np.add(img[0,:,0], 100) ",
"img"
],
execution_count: 0,
outputs: [
{
output_type: "execute_result",
data: {
text/plain: [
"array([[[200, 0, 0], ",
" [200, 0, 0]], ",
" ",
" [[100, 0, 0], ",
" [100, 0, 0]]], dtype=uint8)"
]
},
metadata: {
tags: [ ]
},
execution_count: 39
}
]
},
{
cell_type: "code",
metadata: {
id: "S1eaud_2p84D",
colab_type: "code",
outputId: "d8d239a5-a71e-4946-f085-1489a1be3398",
colab: {
base_uri: "https://localhost:8080/",
height: 269
}
},
source: [
"imgplot = plt.imshow(img)"
],
execution_count: 0,
outputs: [
{
output_type: "display_data",
data: {
image/png: "iVBORw0KGgoAAAANSUhEUgAAARUAAAD8CAYAAABZ0jAcAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz AAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0 dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAPM0lEQVR4nO3df6zddX3H8edrIHSZmxTqoEFXIBIF M1e0wR8YxYmA/FFIJLNmm+2C6XRjSzQuYkh0wS1D9weLmU6biqLZgMmm1k3mKpW4RIvWDajUQQsu k2sVpIAhMFzxvT/Ot8vX23tv773n03PuuXk+kpPzPd8f576/afPKOd9zz32lqpCkVn5h3ANIWl4M FUlNGSqSmjJUJDVlqEhqylCR1NRQoZLkxCTbk+zt7lfOst8zSe7sbtt6609PckeSfUluTnLcMPNI Gr9hX6lcBdxWVWcCt3WPZ/JUVa3tbut76z8IXFdVLwAeBa4Ych5JY5Zhfvktyb3A+VW1P8lq4Paq euEM+z1RVc+eti7Aw8ApVXUwySuBP62qixY9kKSxO3bI40+uqv3d8g+Bk2fZb0WSXcBB4Nqq+jxw EvBYVR3s9nkQOHW2H5RkM7AZ4BfhZWuGHFzS7PYDj1VlMcceMVSS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z8DVVNZXkDGBHkt1VdX/rWbVoXwRurKqnk/w+g1djvznmmTS7 RX29ZsmESlVdMNu2JD9Ksrqq9ncvKx+a5TmmuvsHktwOnMPgff5SMp+vJhza58EkxwLPYfAbyEvZ Ec+rqvrnsJXBtbLlYFl+3aSqftJb/lKSjyZZVVVzfoFyUt7+bAM2dssbgS9M3yHJyiTHd8urgPOA PSObcP7m89WE/vleDuyo7srZEnbE85p2nWE98N0Rznc0bQPe2n0K9Arg8d7b9Yk1x9dr5jbuK9Dz vEp9EnAbsBf4CnBit34dsLVbfhWwm8GnDruBK8Y99xzncwlwH4NXUVd3664B1nfLK4DPAvuAbwJn jHvmRuf1F8A93b/RV4EXjXvmeZ7XjcB+4H8ZXC+5Ang78PZuexj8sbH7u/9768Y9c6PzurL377UT eNV8ntdf05fU1KS8/ZE0IQwVSU0ZKpKaMlQkNWWoSGrKUJHUlKEiqan/A4uYeHHO1OAUAAAAAElF TkSuQmCC ",
text/plain: [
"<Figure size 432x288 with 1 Axes>"
]
},
metadata: {
tags: [ ]
}
}
]
},
{
cell_type: "markdown",
metadata: {
id: "1WWipip65tGb",
colab_type: "text"
},
source: [
"So finally, she says she loves it, but just wants to increase the intensity of all values by 1. There are 2 ways to do this and we'll try both. ",
" ",
" ",
"* The first way is to create a matrix of the same shape and simply add it ",
"* The second way is to add the value `1` ",
" ",
"Then finally, we check the equality using `np.equal`"
]
},
{
cell_type: "code",
metadata: {
id: "ENZyEV0H4HmN",
colab_type: "code",
outputId: "55632633-ce17-4f8d-acad-cd884aa6471b",
colab: {
base_uri: "https://localhost:8080/",
height: 102
}
},
source: [
"one_matrix = np.ones([2,2,3],dtype=np.uint8) ",
"img_one_matrix = img + one_matrix ",
" ",
"img_one_scaler = img + 1 ",
"img_one_scaler ",
" ",
"np.equal(img_one_matrix, img_one_scaler)"
],
execution_count: 0,
outputs: [
{
output_type: "execute_result",
data: {
text/plain: [
"array([[[ True, True, True], ",
" [ True, True, True]], ",
" ",
" [[ True, True, True], ",
" [ True, True, True]]])"
]
},
metadata: {
tags: [ ]
},
execution_count: 46
}
]
},
{
cell_type: "markdown",
metadata: {
id: "8KtDqe93-yS-",
colab_type: "text"
},
source: [
"## Troubleshooting Advice ",
" ",
"Now let's say we have a typo and have a typo and try to add two matrixes of different shapes. When that happens, we have what is called a shape mismatch. If something like that happens, you need to print the `.shape` of each matrix and find out what is different. Try it for yourself. That is the best way to learn. ",
" "
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment