diff --git a/your-code/.idea/.gitignore b/your-code/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/your-code/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/your-code/.idea/inspectionProfiles/profiles_settings.xml b/your-code/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/your-code/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/your-code/.idea/misc.xml b/your-code/.idea/misc.xml new file mode 100644 index 0000000..d56657a --- /dev/null +++ b/your-code/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/your-code/.idea/modules.xml b/your-code/.idea/modules.xml new file mode 100644 index 0000000..0737990 --- /dev/null +++ b/your-code/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/your-code/.idea/vcs.xml b/your-code/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/your-code/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/your-code/.idea/your-code.iml b/your-code/.idea/your-code.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/your-code/.idea/your-code.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb new file mode 100644 index 0000000..9d46794 --- /dev/null +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -0,0 +1,764 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". Import the NUMPY package under the name np." + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". Print the NUMPY version and the configuration." + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.20.1\n" + ] + } + ], + "source": [ + "print(np.__version__)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". Generate a 2x3x5 3-dimensional array with random values. Assign the array to variable \"a\"
\n", + "Challenge: there are at least three easy ways that use numpy to generate random arrays. How many ways can you find?" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 3, 5)" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = np.random.rand(2,3,5)\n", + "a.shape\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". Print a." + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.29750134 0.56678247 0.28655138 0.92594926 0.09978897]\n", + " [0.34662297 0.85508102 0.88107918 0.75216936 0.84571573]\n", + " [0.05738969 0.15636925 0.19485643 0.28251118 0.73586542]]\n", + "\n", + " [[0.6871298 0.29668834 0.25583283 0.90432925 0.18038284]\n", + " [0.28476782 0.78529327 0.08684501 0.53318491 0.8013668 ]\n", + " [0.80144733 0.71764026 0.65658895 0.28547811 0.38163104]]]\n" + ] + } + ], + "source": [ + "print(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". Create a 5x2x3 3-dimensional array with all values equaling 1.
\n", + "ssign the array to variable \"b\"" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(5, 2, 3)" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b = np.ones([5,2,3])\n", + "b.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". Print b." + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[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.]]]\n" + ] + } + ], + "source": [ + "print(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". Do a and b have the same size? How do you prove that in Python code?" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.size == b.size\n", + "a.size" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". Are you able to add a and b? Why or why not?" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.shape == b.shape # a and b have different dimension\n" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 3, 5)" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(5, 2, 3)" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". 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": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c =np.transpose(b,axes=(1,2,0))\n", + "c.shape == a.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "0. 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": 45, + "metadata": {}, + "outputs": [], + "source": [ + "d = a+c\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain." + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[1.59500268, 2.13356493, 1.57310275, 2.85189853, 1.19957793],\n", + " [1.69324594, 2.71016203, 2.76215835, 2.50433872, 2.69143145],\n", + " [1.11477939, 1.3127385 , 1.38971287, 1.56502237, 2.47173084]],\n", + "\n", + " [[2.3742596 , 1.59337668, 1.51166567, 2.8086585 , 1.36076567],\n", + " [1.56953563, 2.57058654, 1.17369001, 2.06636982, 2.6027336 ],\n", + " [2.60289465, 2.43528053, 2.31317789, 1.57095622, 1.76326209]]])" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a + d " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2. Multiply a and c. Assign the result to e." + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [], + "source": [ + "e = a*c" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "3. Does e equal to a? Why or why not?" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[ True, True, True, True, True],\n", + " [ True, True, True, True, True],\n", + " [ True, True, True, True, True]],\n", + "\n", + " [[ True, True, True, True, True],\n", + " [ True, True, True, True, True],\n", + " [ True, True, True, True, True]]])" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a == e # a*1 = a*c -> e = a*c = a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "4. 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": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.0573896949415125 1.0573896949415125 1.4980946728018971\n" + ] + } + ], + "source": [ + "d_max = np.max(d)\n", + "d_min = np.min(d)\n", + "d_mean= np.mean(d)\n", + "\n", + "print(d_min,d_min,d_mean)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "5. 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": 50, + "metadata": {}, + "outputs": [], + "source": [ + "f = np.empty([2,3,5])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.46994815, 0.136133 , 0.98387684, 0.87601196, 0.16997746],\n", + " [0.62529406, 0.28637488, 0.78453454, 0.38454236, 0.71770555],\n", + " [0.90141005, 0.83143606, 0.50642215, 0.18121103, 0.10300791]],\n", + "\n", + " [[0.83890766, 0.5979778 , 0.37834257, 0.84357368, 0.82553291],\n", + " [0.04567333, 0.96499733, 0.25372636, 0.82008031, 0.30336016],\n", + " [0.79501313, 0.2087781 , 0.1902519 , 0.76796999, 0.64080883]]])" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "#16. 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.\n", + "If a value in d is larger than d_mean but smaller than d_max, assign 75 to the corresponding value in f.
\n", + "If a value equals to d_mean, assign 50 to the corresponding value in f.
\n", + "Assign 0 to the corresponding value(s) in f for d_min in d.
\n", + "Assign 100 to the corresponding value(s) in f for d_max in d.
\n", + "In the end, f should have only the following values: 0, 25, 50, 75, and 100.
\n", + "Note: you don't have to use Numpy in this question.
\n" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [], + "source": [ + "g=d" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "for i_1,i in enumerate(g):\n", + " for i_2,x in enumerate(i):\n", + " for i_3,y in enumerate(x):\n", + " if y> d_min and y < d_mean:\n", + " f[i_1][i_2][i_3]=25\n", + " elif y > d_mean and y < d_max:\n", + " f[i_1][i_2][i_3]=75\n", + " elif y == d_mean:\n", + " f[i_1][i_2][i_3]=50\n", + " elif y == d_min:\n", + " f[i_1][i_2][i_3]=0\n", + " elif y == d_max:\n", + " f[i_1][i_2][i_3]=100\n", + " \n", + " \n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[ 25., 75., 25., 100., 25.],\n", + " [ 25., 75., 75., 75., 75.],\n", + " [ 0., 25., 25., 25., 75.]],\n", + "\n", + " [[ 75., 25., 25., 75., 25.],\n", + " [ 25., 75., 25., 75., 75.],\n", + " [ 75., 75., 75., 25., 25.]]])" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "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": 97, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[ 25., 75., 25., 100., 25.],\n", + " [ 25., 75., 75., 75., 75.],\n", + " [ 0., 25., 25., 25., 75.]],\n", + "\n", + " [[ 75., 25., 25., 75., 25.],\n", + " [ 25., 75., 25., 75., 75.],\n", + " [ 75., 75., 75., 25., 25.]]])" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "
\n", + "#17. 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", + "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": [ + "\n", + "
\n", + "#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", + "Again, you don't need Numpy in this question.
\n" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [], + "source": [ + "h = f.tolist()\n", + "g= f.tolist()" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "for i_1,i in enumerate(d):\n", + " for i_2,x in enumerate(i):\n", + " for i_3,y in enumerate(x):\n", + " if y> d_min and y < d_mean:\n", + " h[i_1][i_2][i_3]=\"B\"\n", + " elif y > d_mean and y < d_max:\n", + " h[i_1][i_2][i_3]=\"D\"\n", + " elif y == d_mean:\n", + " h[i_1][i_2][i_3]=\"C\"\n", + " elif y == d_min:\n", + " h[i_1][i_2][i_3]=\"A\"\n", + " elif y == d_max:\n", + " h[i_1][i_2][i_3]=\"E\"\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[['B', 'D', 'B', 'E', 'B'],\n", + " ['B', 'D', 'D', 'D', 'D'],\n", + " ['A', 'B', 'B', 'B', 'D']],\n", + " [['D', 'B', 'B', 'D', 'B'],\n", + " ['B', 'D', 'B', 'D', 'D'],\n", + " ['D', 'D', 'D', 'B', 'B']]]" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "h" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "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", + " g[matrix][row][num] = 'B'\n", + " elif x > d_mean and x < d_max:\n", + " g[matrix][row][num]= 'D'\n", + " elif x == d_mean:\n", + " g[matrix][row][num] = 'C'\n", + " elif x == d_min:\n", + " g[matrix][row][num] = 'A'\n", + " elif x == d_max:\n", + " g[matrix][row][num] = 'E' \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[['B', 'D', 'B', 'E', 'B'],\n", + " ['B', 'D', 'D', 'D', 'D'],\n", + " ['A', 'B', 'B', 'B', 'D']],\n", + " [['D', 'B', 'B', 'D', 'B'],\n", + " ['B', 'D', 'B', 'D', 'D'],\n", + " ['D', 'D', 'D', 'B', 'B']]]" + ] + }, + "execution_count": 102, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "g" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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..3177238 --- /dev/null +++ b/your-code/main.ipynb @@ -0,0 +1,750 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". Import the NUMPY package under the name np." + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". Print the NUMPY version and the configuration." + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.20.1\n" + ] + } + ], + "source": [ + "print(np.__version__)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". Generate a 2x3x5 3-dimensional array with random values. Assign the array to variable \"a\"
\n", + "Challenge: there are at least three easy ways that use numpy to generate random arrays. How many ways can you find?" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 3, 5)" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = np.random.rand(2,3,5)\n", + "a.shape\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". Print a." + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.29750134 0.56678247 0.28655138 0.92594926 0.09978897]\n", + " [0.34662297 0.85508102 0.88107918 0.75216936 0.84571573]\n", + " [0.05738969 0.15636925 0.19485643 0.28251118 0.73586542]]\n", + "\n", + " [[0.6871298 0.29668834 0.25583283 0.90432925 0.18038284]\n", + " [0.28476782 0.78529327 0.08684501 0.53318491 0.8013668 ]\n", + " [0.80144733 0.71764026 0.65658895 0.28547811 0.38163104]]]\n" + ] + } + ], + "source": [ + "print(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". Create a 5x2x3 3-dimensional array with all values equaling 1.
\n", + "ssign the array to variable \"b\"" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(5, 2, 3)" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b = np.ones([5,2,3])\n", + "b.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". Print b." + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[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.]]]\n" + ] + } + ], + "source": [ + "print(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". Do a and b have the same size? How do you prove that in Python code?" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.size == b.size\n", + "a.size" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". Are you able to add a and b? Why or why not?" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.shape == b.shape # a and b have different dimension\n" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 3, 5)" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(5, 2, 3)" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ". 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": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c =np.transpose(b,axes=(1,2,0))\n", + "c.shape == a.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "0. 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": 45, + "metadata": {}, + "outputs": [], + "source": [ + "d = a+c\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain." + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[1.59500268, 2.13356493, 1.57310275, 2.85189853, 1.19957793],\n", + " [1.69324594, 2.71016203, 2.76215835, 2.50433872, 2.69143145],\n", + " [1.11477939, 1.3127385 , 1.38971287, 1.56502237, 2.47173084]],\n", + "\n", + " [[2.3742596 , 1.59337668, 1.51166567, 2.8086585 , 1.36076567],\n", + " [1.56953563, 2.57058654, 1.17369001, 2.06636982, 2.6027336 ],\n", + " [2.60289465, 2.43528053, 2.31317789, 1.57095622, 1.76326209]]])" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a + d " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2. Multiply a and c. Assign the result to e." + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [], + "source": [ + "e = a*c" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "3. Does e equal to a? Why or why not?" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[ True, True, True, True, True],\n", + " [ True, True, True, True, True],\n", + " [ True, True, True, True, True]],\n", + "\n", + " [[ True, True, True, True, True],\n", + " [ True, True, True, True, True],\n", + " [ True, True, True, True, True]]])" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a == e # a*1 = a*c -> e = a*c = a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "4. 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": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.0573896949415125 1.0573896949415125 1.4980946728018971\n" + ] + } + ], + "source": [ + "d_max = np.max(d)\n", + "d_min = np.min(d)\n", + "d_mean= np.mean(d)\n", + "\n", + "print(d_min,d_min,d_mean)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "5. 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": 50, + "metadata": {}, + "outputs": [], + "source": [ + "f = np.empty([2,3,5])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.46994815, 0.136133 , 0.98387684, 0.87601196, 0.16997746],\n", + " [0.62529406, 0.28637488, 0.78453454, 0.38454236, 0.71770555],\n", + " [0.90141005, 0.83143606, 0.50642215, 0.18121103, 0.10300791]],\n", + "\n", + " [[0.83890766, 0.5979778 , 0.37834257, 0.84357368, 0.82553291],\n", + " [0.04567333, 0.96499733, 0.25372636, 0.82008031, 0.30336016],\n", + " [0.79501313, 0.2087781 , 0.1902519 , 0.76796999, 0.64080883]]])" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "#16. 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.\n", + "If a value in d is larger than d_mean but smaller than d_max, assign 75 to the corresponding value in f.
\n", + "If a value equals to d_mean, assign 50 to the corresponding value in f.
\n", + "Assign 0 to the corresponding value(s) in f for d_min in d.
\n", + "Assign 100 to the corresponding value(s) in f for d_max in d.
\n", + "In the end, f should have only the following values: 0, 25, 50, 75, and 100.
\n", + "Note: you don't have to use Numpy in this question.
\n" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [], + "source": [ + "g=d" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "for i_1,i in enumerate(g):\n", + " for i_2,x in enumerate(i):\n", + " for i_3,y in enumerate(x):\n", + " if y> d_min and y < d_mean:\n", + " f[i_1][i_2][i_3]=25\n", + " elif y > d_mean and y < d_max:\n", + " f[i_1][i_2][i_3]=75\n", + " elif y == d_mean:\n", + " f[i_1][i_2][i_3]=50\n", + " elif y == d_min:\n", + " f[i_1][i_2][i_3]=0\n", + " elif y == d_max:\n", + " f[i_1][i_2][i_3]=100\n", + " \n", + " \n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[ 25., 75., 25., 100., 25.],\n", + " [ 25., 75., 75., 75., 75.],\n", + " [ 0., 25., 25., 25., 75.]],\n", + "\n", + " [[ 75., 25., 25., 75., 25.],\n", + " [ 25., 75., 25., 75., 75.],\n", + " [ 75., 75., 75., 25., 25.]]])" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "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": 97, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[ 25., 75., 25., 100., 25.],\n", + " [ 25., 75., 75., 75., 75.],\n", + " [ 0., 25., 25., 25., 75.]],\n", + "\n", + " [[ 75., 25., 25., 75., 25.],\n", + " [ 25., 75., 25., 75., 75.],\n", + " [ 75., 75., 75., 25., 25.]]])" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "
\n", + "#17. 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", + "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": [ + "\n", + "
\n", + "#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", + "Again, you don't need Numpy in this question.
\n" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [], + "source": [ + "h = f.tolist()\n", + "g= f.tolist()" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "for i_1,i in enumerate(d):\n", + " for i_2,x in enumerate(i):\n", + " for i_3,y in enumerate(x):\n", + " if y> d_min and y < d_mean:\n", + " h[i_1][i_2][i_3]=\"B\"\n", + " elif y > d_mean and y < d_max:\n", + " h[i_1][i_2][i_3]=\"D\"\n", + " elif y == d_mean:\n", + " h[i_1][i_2][i_3]=\"C\"\n", + " elif y == d_min:\n", + " h[i_1][i_2][i_3]=\"A\"\n", + " elif y == d_max:\n", + " h[i_1][i_2][i_3]=\"E\"\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[['B', 'D', 'B', 'E', 'B'],\n", + " ['B', 'D', 'D', 'D', 'D'],\n", + " ['A', 'B', 'B', 'B', 'D']],\n", + " [['D', 'B', 'B', 'D', 'B'],\n", + " ['B', 'D', 'B', 'D', 'D'],\n", + " ['D', 'D', 'D', 'B', 'B']]]" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "h" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "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", + " g[matrix][row][num] = 'B'\n", + " elif x > d_mean and x < d_max:\n", + " g[matrix][row][num]= 'D'\n", + " elif x == d_mean:\n", + " g[matrix][row][num] = 'C'\n", + " elif x == d_min:\n", + " g[matrix][row][num] = 'A'\n", + " elif x == d_max:\n", + " g[matrix][row][num] = 'E' \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[['B', 'D', 'B', 'E', 'B'],\n", + " ['B', 'D', 'D', 'D', 'D'],\n", + " ['A', 'B', 'B', 'B', 'D']],\n", + " [['D', 'B', 'B', 'D', 'B'],\n", + " ['B', 'D', 'B', 'D', 'D'],\n", + " ['D', 'D', 'D', 'B', 'B']]]" + ] + }, + "execution_count": 102, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "g" + ] + } + ], + "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 deleted file mode 100644 index 78c792b..0000000 --- a/your-code/main.py +++ /dev/null @@ -1,114 +0,0 @@ -#1. Import the NUMPY package under the name np. - - - -#2. Print the NUMPY version and the configuration. - - - -#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? - - - -#4. Print a. - - - -#5. Create a 5x2x3 3-dimensional array with all values equaling 1. -#Assign the array to variable "b" - - - -#6. Print b. - - - -#7. Do a and b have the same size? How do you prove that in Python code? - - - - -#8. Are you able to add a and b? Why or why not? - - - -#9. Transpose b so that it has the same structure of a (i.e. become a 2x3x5 array). Assign the transposed array to varialbe "c". - - - -#10. Try to add a and c. Now it should work. Assign the sum to varialbe "d". But why does it work now? - - - -#11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain. - - - - -#12. Multiply a and c. Assign the result to e. - - - -#13. Does e equal to a? Why or why not? - - - - -#14. Identify the max, min, and mean values in d. Assign those values to variables "d_max", "d_min", and "d_mean" - - - - -#15. 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`. - - - - -""" -#16. 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. -""" - - - - -""" -#17. Print d and f. Do you have your expected f? -For instance, if your d is: -array([[[1.85836099, 1.67064465, 1.62576044, 1.40243961, 1.88454931], - [1.75354326, 1.69403643, 1.36729252, 1.61415071, 1.12104981], - [1.72201435, 1.1862918 , 1.87078449, 1.7726778 , 1.88180042]], - - [[1.44747908, 1.31673383, 1.02000951, 1.52218947, 1.97066381], - [1.79129243, 1.74983003, 1.96028037, 1.85166831, 1.65450881], - [1.18068344, 1.9587381 , 1.00656599, 1.93402165, 1.73514584]]]) - -Your f should be: -array([[[ 75., 75., 75., 25., 75.], - [ 75., 75., 25., 25., 25.], - [ 75., 25., 75., 75., 75.]], - - [[ 25., 25., 25., 25., 100.], - [ 75., 75., 75., 75., 75.], - [ 25., 75., 0., 75., 75.]]]) -""" - - -""" -#18. Bonus question: instead of using numbers (i.e. 0, 25, 50, 75, and 100), how to use string values -("A", "B", "C", "D", and "E") to label the array elements? You are expecting the result to be: -array([[[ 'D', 'D', 'D', 'B', 'D'], - [ 'D', 'D', 'B', 'B', 'B'], - [ 'D', 'B', 'D', 'D', 'D']], - - [[ 'B', 'B', 'B', 'B', 'E'], - [ 'D', 'D', 'D', 'D', 'D'], - [ 'B', 'D', 'A', 'D', 'D']]]) -Again, you don't need Numpy in this question. -""" \ No newline at end of file