diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..faa5baf Binary files /dev/null and b/.DS_Store differ diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb new file mode 100644 index 0000000..fc859a6 --- /dev/null +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -0,0 +1,583 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 1.- Import the NUMPY package under the name np." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2.- Print the NUMPY version and the configuration." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.21.0\n" + ] + } + ], + "source": [ + "print(np.version.version)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3.- Generate a 2x3x5 3-dimensional array with random values. Assign the array to variable \"a\". Challenge: there are at least three easy ways that use numpy to generate random arrays. How many ways can you find. Print a" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.17587677, 0.3813641 , 0.6251925 , 0.71451247, 0.71466546],\n", + " [0.397129 , 0.41592668, 0.14682988, 0.56041101, 0.21933926],\n", + " [0.59025395, 0.79489513, 0.35198264, 0.22058347, 0.03169599]],\n", + "\n", + " [[0.33371759, 0.04717761, 0.79779299, 0.3259222 , 0.99089213],\n", + " [0.49865696, 0.23321342, 0.70795892, 0.67157786, 0.47678929],\n", + " [0.97061393, 0.59173887, 0.20081984, 0.74445459, 0.33515286]]])" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 1.-\n", + "a_1 = np.random.random((2, 3, 5))\n", + "a_1" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.12253001, 0.23204466, 0.05318327, 0.75305589, 0.38695835],\n", + " [0.06507331, 0.53373402, 0.56534184, 0.96607451, 0.90444341],\n", + " [0.08427719, 0.06198972, 0.71258041, 0.69541678, 0.09113022]],\n", + "\n", + " [[0.49985424, 0.72953171, 0.82568618, 0.28445211, 0.09408022],\n", + " [0.55287048, 0.16976278, 0.46469315, 0.94171563, 0.02922495],\n", + " [0.14662571, 0.38956247, 0.02650157, 0.14960273, 0.63225123]]])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 3.- \n", + "a_2 = np.random.random(30).reshape(2, 3, 5)\n", + "a_2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4.- Create a 5x2x3 3-dimensional array with all values equaling 1. Assign the array to variable \"b\". Print b." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[1, 1, 1],\n", + " [1, 1, 1]],\n", + "\n", + " [[1, 1, 1],\n", + " [1, 1, 1]],\n", + "\n", + " [[1, 1, 1],\n", + " [1, 1, 1]],\n", + "\n", + " [[1, 1, 1],\n", + " [1, 1, 1]],\n", + "\n", + " [[1, 1, 1],\n", + " [1, 1, 1]]])" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b = np.full((5, 2, 3), 1, dtype = int)\n", + "b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 5.- Do a and b have the same size? How do you prove that in Python code?" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "a and b DO NOT have the same size\n", + "Dimensions of a are: (2, 3, 5)\n", + "Dimensions of b are: (5, 2, 3)\n" + ] + } + ], + "source": [ + "if a_1.shape == b.shape:\n", + " print(True)\n", + " print('a and b have the same size')\n", + "else:\n", + " print(False)\n", + " print('a and b DO NOT have the same size')\n", + " print(f'Dimensions of a are: {a_1.shape}')\n", + " print(f'Dimensions of b are: {b.shape}')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 6.- Are you able to add a and b? Why or why not?" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "operands could not be broadcast together with shapes (2,3,5) (5,2,3) ", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Matrices do not have the same dimensions\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma_1\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,3,5) (5,2,3) " + ] + } + ], + "source": [ + "# Matrices do not have the same dimensions\n", + "c = a_1 + b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 7.- Transpose b so that it has the same structure of a (i.e. become a 2x3x5 array). Assign the transposed array to varialbe \"c\"." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1. 1. 1.]\n", + " [1. 1. 1.]]]\n", + "(1, 2, 3)\n", + "[[[1. 1. 1.]]\n", + "\n", + " [[1. 1. 1.]]]\n", + "(2, 1, 3)\n" + ] + } + ], + "source": [ + "x = np.ones((1, 2, 3))\n", + "print(x)\n", + "print(x.shape)\n", + "\n", + "y = np.transpose(x, (1, 0, 2))\n", + "print(y)\n", + "print(y.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[1, 1, 1, 1, 1],\n", + " [1, 1, 1, 1, 1],\n", + " [1, 1, 1, 1, 1]],\n", + "\n", + " [[1, 1, 1, 1, 1],\n", + " [1, 1, 1, 1, 1],\n", + " [1, 1, 1, 1, 1]]])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c = np.transpose(b, (1, 2, 0))\n", + "c" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 8.- Try to add a and c. Now it should work. Assign the sum to varialbe \"d\". But why does it work now?" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.17587677 1.3813641 1.6251925 1.71451247 1.71466546]\n", + " [1.397129 1.41592668 1.14682988 1.56041101 1.21933926]\n", + " [1.59025395 1.79489513 1.35198264 1.22058347 1.03169599]]\n", + "\n", + " [[1.33371759 1.04717761 1.79779299 1.3259222 1.99089213]\n", + " [1.49865696 1.23321342 1.70795892 1.67157786 1.47678929]\n", + " [1.97061393 1.59173887 1.20081984 1.74445459 1.33515286]]]\n" + ] + } + ], + "source": [ + "d = a_1 + c\n", + "print(d)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 9.- Print a and d. Notice the difference and relation of the two array in terms of the values? Explain." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "c was transposed into a configuration that made possible the addition of a and itself. Thus, the relation between d and a is c, is it is a \"1-only\" matrix, so the only difference is the ones that were added." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 10.- Multiply a and c. Assign the result to e." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.17587677 0.3813641 0.6251925 0.71451247 0.71466546]\n", + " [0.397129 0.41592668 0.14682988 0.56041101 0.21933926]\n", + " [0.59025395 0.79489513 0.35198264 0.22058347 0.03169599]]\n", + "\n", + " [[0.33371759 0.04717761 0.79779299 0.3259222 0.99089213]\n", + " [0.49865696 0.23321342 0.70795892 0.67157786 0.47678929]\n", + " [0.97061393 0.59173887 0.20081984 0.74445459 0.33515286]]]\n" + ] + } + ], + "source": [ + "e = a_1*c\n", + "print(e)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 11.- Does e equal to a? Why or why not?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Of course no. Something times one equals the number itself, which is totally different to adding 1 to a number which increases the value of that number by 1." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 12.- Identify the max, min, and mean values in d. Assign those values to variables \"d_max\", \"d_min\", and \"d_mean\"" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "d_max = np.max(d)\n", + "d_min = np.min(d)\n", + "d_mean = np.mean(d)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 13.-Now we want to label the values in d. First create an empty array \"f\" with the same shape (i.e. 2x3x5) as d using `np.empty`." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.17587677 0.3813641 0.6251925 0.71451247 0.71466546]\n", + " [0.397129 0.41592668 0.14682988 0.56041101 0.21933926]\n", + " [0.59025395 0.79489513 0.35198264 0.22058347 0.03169599]]\n", + "\n", + " [[0.33371759 0.04717761 0.79779299 0.3259222 0.99089213]\n", + " [0.49865696 0.23321342 0.70795892 0.67157786 0.47678929]\n", + " [0.97061393 0.59173887 0.20081984 0.74445459 0.33515286]]]\n" + ] + } + ], + "source": [ + "f = np.empty([2, 3, 5])\n", + "print(f)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 14. Populate the values in f. For each value in d, if it's larger than d_min but smaller than d_mean, assign 25 to the corresponding value in f.If a value in d is larger than d_mean but smaller than d_max, assign 75 to the corresponding value in f. If a value equals to d_mean, assign 50 to the corresponding value in f. Assign 0 to the corresponding value(s) in f for d_min in d. Assign 100 to the corresponding value(s) in f for d_max in d. In the end, f should have only the following values: 0, 25, 50, 75, and 100. Note: you don't have to use Numpy in this question." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "for matrix in range(d.shape[0]):\n", + " for row in range(d.shape[1]):\n", + " for num in range(d.shape[2]):\n", + " x = d[matrix][row][num]\n", + " if x > d_min and x < d_mean:\n", + " f[matrix, row, num] = 25\n", + " elif x > d_mean and x < d_max:\n", + " f[matrix, row, num] = 75\n", + " elif x == d_mean:\n", + " f[matrix, row, num] = 50\n", + " elif x == d_min:\n", + " f[matrix, row, num] = 0\n", + " elif x == d_max:\n", + " f[matrix, row, num] = 100" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[ 25., 25., 75., 75., 75.],\n", + " [ 25., 25., 25., 75., 25.],\n", + " [ 75., 75., 25., 25., 0.]],\n", + "\n", + " [[ 25., 25., 75., 25., 100.],\n", + " [ 75., 25., 75., 75., 75.],\n", + " [ 75., 75., 25., 75., 25.]]])" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 15.- Print d and f. Do you have your expected f?
\n", + "#### For instance, if your d is:
\n", + "array([[[1.85836099, 1.67064465, 1.62576044, 1.40243961, 1.88454931],
\n", + " [1.75354326, 1.69403643, 1.36729252, 1.61415071, 1.12104981],
\n", + " [1.72201435, 1.1862918 , 1.87078449, 1.7726778 , 1.88180042]],
\n", + " [[1.44747908, 1.31673383, 1.02000951, 1.52218947, 1.97066381],
\n", + " [1.79129243, 1.74983003, 1.96028037, 1.85166831, 1.65450881],
\n", + " [1.18068344, 1.9587381 , 1.00656599, 1.93402165, 1.73514584]]])
\n", + " \n", + "#### Your f should be:
\n", + "array([[[ 75., 75., 75., 25., 75.],
\n", + " [ 75., 75., 25., 25., 25.],
\n", + " [ 75., 25., 75., 75., 75.]],
\n", + " [[ 25., 25., 25., 25., 100.],
\n", + " [ 75., 75., 75., 75., 75.],
\n", + " [ 25., 75., 0., 75., 75.]]])
\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 18.- Bonus question: instead of using numbers (i.e. 0, 25, 50, 75, and 100), how to use string values
\n", + "(\"A\", \"B\", \"C\", \"D\", and \"E\") to label the array elements? You are expecting the result to be:
\n", + "array([[[ 'D', 'D', 'D', 'B', 'D'],
\n", + " [ 'D', 'D', 'B', 'B', 'B'],
\n", + " [ 'D', 'B', 'D', 'D', 'D']],
\n", + " [[ 'B', 'B', 'B', 'B', 'E'],
\n", + " [ 'D', 'D', 'D', 'D', 'D'],
\n", + " [ 'B', 'D', 'A', 'D', 'D']]])
\n", + "\n", + "Again, you don't need Numpy in this question.
" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "f = np.empty((2, 3, 5)).tolist()" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "for matrix in range(d.shape[0]):\n", + " for row in range(d.shape[1]):\n", + " for num in range(d.shape[2]):\n", + " x = d[matrix][row][num]\n", + " if x > d_min and x < d_mean:\n", + " f[matrix][row][num] = 'B'\n", + " elif x > d_mean and x < d_max:\n", + " f[matrix][row][num] = 'D'\n", + " elif x == d_mean:\n", + " f[matrix][row][num] = 'C'\n", + " elif x == d_min:\n", + " f[matrix][row][num] = 'A'\n", + " elif x == d_max:\n", + " f[matrix][row][num] = 'E'" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[['B', 'B', 'D', 'D', 'D'],\n", + " ['B', 'B', 'B', 'D', 'B'],\n", + " ['D', 'D', 'B', 'B', 'A']],\n", + " [['B', 'B', 'D', 'B', 'E'],\n", + " ['D', 'B', 'D', 'D', 'D'],\n", + " ['D', 'D', 'B', 'D', 'B']]]" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/main.ipynb b/your-code/main.ipynb new file mode 100644 index 0000000..fc859a6 --- /dev/null +++ b/your-code/main.ipynb @@ -0,0 +1,583 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 1.- Import the NUMPY package under the name np." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2.- Print the NUMPY version and the configuration." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.21.0\n" + ] + } + ], + "source": [ + "print(np.version.version)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3.- Generate a 2x3x5 3-dimensional array with random values. Assign the array to variable \"a\". Challenge: there are at least three easy ways that use numpy to generate random arrays. How many ways can you find. Print a" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.17587677, 0.3813641 , 0.6251925 , 0.71451247, 0.71466546],\n", + " [0.397129 , 0.41592668, 0.14682988, 0.56041101, 0.21933926],\n", + " [0.59025395, 0.79489513, 0.35198264, 0.22058347, 0.03169599]],\n", + "\n", + " [[0.33371759, 0.04717761, 0.79779299, 0.3259222 , 0.99089213],\n", + " [0.49865696, 0.23321342, 0.70795892, 0.67157786, 0.47678929],\n", + " [0.97061393, 0.59173887, 0.20081984, 0.74445459, 0.33515286]]])" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 1.-\n", + "a_1 = np.random.random((2, 3, 5))\n", + "a_1" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.12253001, 0.23204466, 0.05318327, 0.75305589, 0.38695835],\n", + " [0.06507331, 0.53373402, 0.56534184, 0.96607451, 0.90444341],\n", + " [0.08427719, 0.06198972, 0.71258041, 0.69541678, 0.09113022]],\n", + "\n", + " [[0.49985424, 0.72953171, 0.82568618, 0.28445211, 0.09408022],\n", + " [0.55287048, 0.16976278, 0.46469315, 0.94171563, 0.02922495],\n", + " [0.14662571, 0.38956247, 0.02650157, 0.14960273, 0.63225123]]])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 3.- \n", + "a_2 = np.random.random(30).reshape(2, 3, 5)\n", + "a_2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4.- Create a 5x2x3 3-dimensional array with all values equaling 1. Assign the array to variable \"b\". Print b." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[1, 1, 1],\n", + " [1, 1, 1]],\n", + "\n", + " [[1, 1, 1],\n", + " [1, 1, 1]],\n", + "\n", + " [[1, 1, 1],\n", + " [1, 1, 1]],\n", + "\n", + " [[1, 1, 1],\n", + " [1, 1, 1]],\n", + "\n", + " [[1, 1, 1],\n", + " [1, 1, 1]]])" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b = np.full((5, 2, 3), 1, dtype = int)\n", + "b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 5.- Do a and b have the same size? How do you prove that in Python code?" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "a and b DO NOT have the same size\n", + "Dimensions of a are: (2, 3, 5)\n", + "Dimensions of b are: (5, 2, 3)\n" + ] + } + ], + "source": [ + "if a_1.shape == b.shape:\n", + " print(True)\n", + " print('a and b have the same size')\n", + "else:\n", + " print(False)\n", + " print('a and b DO NOT have the same size')\n", + " print(f'Dimensions of a are: {a_1.shape}')\n", + " print(f'Dimensions of b are: {b.shape}')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 6.- Are you able to add a and b? Why or why not?" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "operands could not be broadcast together with shapes (2,3,5) (5,2,3) ", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Matrices do not have the same dimensions\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma_1\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,3,5) (5,2,3) " + ] + } + ], + "source": [ + "# Matrices do not have the same dimensions\n", + "c = a_1 + b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 7.- Transpose b so that it has the same structure of a (i.e. become a 2x3x5 array). Assign the transposed array to varialbe \"c\"." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1. 1. 1.]\n", + " [1. 1. 1.]]]\n", + "(1, 2, 3)\n", + "[[[1. 1. 1.]]\n", + "\n", + " [[1. 1. 1.]]]\n", + "(2, 1, 3)\n" + ] + } + ], + "source": [ + "x = np.ones((1, 2, 3))\n", + "print(x)\n", + "print(x.shape)\n", + "\n", + "y = np.transpose(x, (1, 0, 2))\n", + "print(y)\n", + "print(y.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[1, 1, 1, 1, 1],\n", + " [1, 1, 1, 1, 1],\n", + " [1, 1, 1, 1, 1]],\n", + "\n", + " [[1, 1, 1, 1, 1],\n", + " [1, 1, 1, 1, 1],\n", + " [1, 1, 1, 1, 1]]])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c = np.transpose(b, (1, 2, 0))\n", + "c" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 8.- Try to add a and c. Now it should work. Assign the sum to varialbe \"d\". But why does it work now?" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.17587677 1.3813641 1.6251925 1.71451247 1.71466546]\n", + " [1.397129 1.41592668 1.14682988 1.56041101 1.21933926]\n", + " [1.59025395 1.79489513 1.35198264 1.22058347 1.03169599]]\n", + "\n", + " [[1.33371759 1.04717761 1.79779299 1.3259222 1.99089213]\n", + " [1.49865696 1.23321342 1.70795892 1.67157786 1.47678929]\n", + " [1.97061393 1.59173887 1.20081984 1.74445459 1.33515286]]]\n" + ] + } + ], + "source": [ + "d = a_1 + c\n", + "print(d)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 9.- Print a and d. Notice the difference and relation of the two array in terms of the values? Explain." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "c was transposed into a configuration that made possible the addition of a and itself. Thus, the relation between d and a is c, is it is a \"1-only\" matrix, so the only difference is the ones that were added." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 10.- Multiply a and c. Assign the result to e." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.17587677 0.3813641 0.6251925 0.71451247 0.71466546]\n", + " [0.397129 0.41592668 0.14682988 0.56041101 0.21933926]\n", + " [0.59025395 0.79489513 0.35198264 0.22058347 0.03169599]]\n", + "\n", + " [[0.33371759 0.04717761 0.79779299 0.3259222 0.99089213]\n", + " [0.49865696 0.23321342 0.70795892 0.67157786 0.47678929]\n", + " [0.97061393 0.59173887 0.20081984 0.74445459 0.33515286]]]\n" + ] + } + ], + "source": [ + "e = a_1*c\n", + "print(e)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 11.- Does e equal to a? Why or why not?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Of course no. Something times one equals the number itself, which is totally different to adding 1 to a number which increases the value of that number by 1." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 12.- Identify the max, min, and mean values in d. Assign those values to variables \"d_max\", \"d_min\", and \"d_mean\"" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "d_max = np.max(d)\n", + "d_min = np.min(d)\n", + "d_mean = np.mean(d)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 13.-Now we want to label the values in d. First create an empty array \"f\" with the same shape (i.e. 2x3x5) as d using `np.empty`." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.17587677 0.3813641 0.6251925 0.71451247 0.71466546]\n", + " [0.397129 0.41592668 0.14682988 0.56041101 0.21933926]\n", + " [0.59025395 0.79489513 0.35198264 0.22058347 0.03169599]]\n", + "\n", + " [[0.33371759 0.04717761 0.79779299 0.3259222 0.99089213]\n", + " [0.49865696 0.23321342 0.70795892 0.67157786 0.47678929]\n", + " [0.97061393 0.59173887 0.20081984 0.74445459 0.33515286]]]\n" + ] + } + ], + "source": [ + "f = np.empty([2, 3, 5])\n", + "print(f)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 14. Populate the values in f. For each value in d, if it's larger than d_min but smaller than d_mean, assign 25 to the corresponding value in f.If a value in d is larger than d_mean but smaller than d_max, assign 75 to the corresponding value in f. If a value equals to d_mean, assign 50 to the corresponding value in f. Assign 0 to the corresponding value(s) in f for d_min in d. Assign 100 to the corresponding value(s) in f for d_max in d. In the end, f should have only the following values: 0, 25, 50, 75, and 100. Note: you don't have to use Numpy in this question." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "for matrix in range(d.shape[0]):\n", + " for row in range(d.shape[1]):\n", + " for num in range(d.shape[2]):\n", + " x = d[matrix][row][num]\n", + " if x > d_min and x < d_mean:\n", + " f[matrix, row, num] = 25\n", + " elif x > d_mean and x < d_max:\n", + " f[matrix, row, num] = 75\n", + " elif x == d_mean:\n", + " f[matrix, row, num] = 50\n", + " elif x == d_min:\n", + " f[matrix, row, num] = 0\n", + " elif x == d_max:\n", + " f[matrix, row, num] = 100" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[ 25., 25., 75., 75., 75.],\n", + " [ 25., 25., 25., 75., 25.],\n", + " [ 75., 75., 25., 25., 0.]],\n", + "\n", + " [[ 25., 25., 75., 25., 100.],\n", + " [ 75., 25., 75., 75., 75.],\n", + " [ 75., 75., 25., 75., 25.]]])" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 15.- Print d and f. Do you have your expected f?
\n", + "#### For instance, if your d is:
\n", + "array([[[1.85836099, 1.67064465, 1.62576044, 1.40243961, 1.88454931],
\n", + " [1.75354326, 1.69403643, 1.36729252, 1.61415071, 1.12104981],
\n", + " [1.72201435, 1.1862918 , 1.87078449, 1.7726778 , 1.88180042]],
\n", + " [[1.44747908, 1.31673383, 1.02000951, 1.52218947, 1.97066381],
\n", + " [1.79129243, 1.74983003, 1.96028037, 1.85166831, 1.65450881],
\n", + " [1.18068344, 1.9587381 , 1.00656599, 1.93402165, 1.73514584]]])
\n", + " \n", + "#### Your f should be:
\n", + "array([[[ 75., 75., 75., 25., 75.],
\n", + " [ 75., 75., 25., 25., 25.],
\n", + " [ 75., 25., 75., 75., 75.]],
\n", + " [[ 25., 25., 25., 25., 100.],
\n", + " [ 75., 75., 75., 75., 75.],
\n", + " [ 25., 75., 0., 75., 75.]]])
\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 18.- Bonus question: instead of using numbers (i.e. 0, 25, 50, 75, and 100), how to use string values
\n", + "(\"A\", \"B\", \"C\", \"D\", and \"E\") to label the array elements? You are expecting the result to be:
\n", + "array([[[ 'D', 'D', 'D', 'B', 'D'],
\n", + " [ 'D', 'D', 'B', 'B', 'B'],
\n", + " [ 'D', 'B', 'D', 'D', 'D']],
\n", + " [[ 'B', 'B', 'B', 'B', 'E'],
\n", + " [ 'D', 'D', 'D', 'D', 'D'],
\n", + " [ 'B', 'D', 'A', 'D', 'D']]])
\n", + "\n", + "Again, you don't need Numpy in this question.
" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "f = np.empty((2, 3, 5)).tolist()" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "for matrix in range(d.shape[0]):\n", + " for row in range(d.shape[1]):\n", + " for num in range(d.shape[2]):\n", + " x = d[matrix][row][num]\n", + " if x > d_min and x < d_mean:\n", + " f[matrix][row][num] = 'B'\n", + " elif x > d_mean and x < d_max:\n", + " f[matrix][row][num] = 'D'\n", + " elif x == d_mean:\n", + " f[matrix][row][num] = 'C'\n", + " elif x == d_min:\n", + " f[matrix][row][num] = 'A'\n", + " elif x == d_max:\n", + " f[matrix][row][num] = 'E'" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[['B', 'B', 'D', 'D', 'D'],\n", + " ['B', 'B', 'B', 'D', 'B'],\n", + " ['D', 'D', 'B', 'B', 'A']],\n", + " [['B', 'B', 'D', 'B', 'E'],\n", + " ['D', 'B', 'D', 'D', 'D'],\n", + " ['D', 'D', 'B', 'D', 'B']]]" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/main.py b/your-code/main.py index 78c792b..fe31d97 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -1,7 +1,6 @@ #1. Import the NUMPY package under the name np. - #2. Print the NUMPY version and the configuration.